Feeds:
Posts
Comments

Archive for December, 2018

In this tutorial I will show you how to generate assembly language code from c using gcc.

First, type the c code below, save it to ‘hello.c’.

1 #include
2 main()
3 {
4 printf("Hello World!\n");
5 return 0;
6 }

To generate assembly code use option -S.

$ gcc -S hello.c -o hello.s

Check the result

$ ls -l hello.s
-rwxrwxr-x 1 darklinux darklinux 491 2018-12-29 18:10 hello.s
$

Open with text editor.

1 .file "hello.c"
2 .section .rodata
3 .LC0:
4 .string "Hello World!"
5 .text
6 .globl main
7 .type main, @function
8 main:
9 .LFB0:
10 .cfi_startproc
11 pushl %ebp
12 .cfi_def_cfa_offset 8
13 .cfi_offset 5, -8
14 movl %esp, %ebp
15 .cfi_def_cfa_register 5
16 andl $-16, %esp
17 subl $16, %esp
18 movl $.LC0, (%esp)
19 call puts
20 movl $0, %eax
21 leave
22 .cfi_restore 5
23 .cfi_def_cfa 4, 4
24 ret
25 .cfi_endproc
26 .LFE0:
27 .size main, .-main
28 .ident "GCC: (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1"
29 .section .note.GNU-stack,"",@progbits
30

(more…)

Read Full Post »

In assembly, you can execute a program with or without parameter. In order to do that, you need to call syscall no 11.

Execute without arguments.
This program (execute) will call a program (hello) without pass any argument.
Let’s try.
Type the codes below and save it to ‘execute.asm’.

1 section .text
2
3 global _start
4 _start:
5
6 mov eax,11 ;sys_execve
7 mov ebx,command
8 mov ecx,0h
9 int 0x80 ;syscall
10
11 mov eax,1 ;exit
12 int 0x80
13
14 section .data
15 command db './hello'

Type the codes below and save it to ‘hello.asm’.

1 section .text
2 global _start
3 _start:
4 mov eax,sys_write
5 mov ebx,stdout
6 mov ecx,msg
7 mov edx,lenmsg
8 int 0x80
9
10 mov eax,sys_exit
11 int 0x80
12
13 section .data
14 sys_write equ 4
15 sys_exit equ 1
16 stdout equ 1
17 msg db 'Hello word!',0xA
18 lenmsg equ $ - msg

Compile and run the program.

$ nasm -f elf32 hello.asm -o hello.o && ld hello.o -o hello
$ ls -l hello*
-rwxrwxr-x 1 darklinux darklinux 739 2018-12-23 10:04 hello
-rw-rw-r-- 1 darklinux darklinux 253 2018-12-23 09:58 hello.asm
-rw-rw-r-- 1 darklinux darklinux 704 2018-12-23 10:04 hello.o
$ nasm -f elf32 execute.asm -o execute.o && ld execute.o -o execute
$ ls -l execute*
-rwxrwxr-x 1 darklinux darklinux 636 2018-12-23 10:04 execute
-rw-rw-r-- 1 darklinux darklinux 183 2018-12-23 09:56 execute.asm
-rw-rw-r-- 1 darklinux darklinux 608 2018-12-23 10:04 execute.o
$ ./execute
Hello word!
$

(more…)

Read Full Post »

The ‘error: operation size not specified’ is a common error that occur when you try to copy a value into a location of a memory without specify the size of data that you want to copy.

I will show what it is.
Type the codes below and save it to ‘test.asm’.

1 section .text
2 global _start
3 _start:
4
5 mov ebx,33 ;copy '33' to ebx register
6 mov [x],5 ;copy '5' to location of address
7 ;pointer x
8
9 mov eax,1
10 int 0x80
11
12 section .data
13 x db 0

Compile it.

$ nasm -f elf32 test.asm -o test.o && ld test.o -o testtest.asm:5: error: operation size not specified
$

As you see the this error occur.

Now change the code:

1 section .text
2 global _start
3 _start:
4
5 mov ebx,33 ;copy '33' to ebx register
6 mov byte [x],5 ;copy '5' to location of address
7 ;pointer x
8
9 mov eax,1
10 int 0x80
11
12 section .data
13 x db 0

Compile it again.

$ nasm -f elf32 test.asm -o test.o && ld test.o -o test
$

When I specify the size of data (mov byte [x],5), there is no error now.

Read Full Post »

You can call C function from assembly but you can’t compile the object file with ‘ld’ command. You have to use ‘GCC’. If you want to use ‘GCC’ then you have to change the ‘_start’ with ‘main’.
Mostly, you will use:

section .text
global _start
_start:

But, if you use ‘gcc’ then you have to change it to become:

section .text
global _main
main:

Let’s try.
Type the codes below and save it to ‘hello.asm’.

1 extern printf
2
3 section .text
4 global main
5
6 main:
7 push ebp
8 mov ebp,esp
9
10 push msg ;move msg to stack
11 call printf ;call printf
12
13 mov esp,ebp
14 pop ebp
15 ret
16
17 section .data
18 msg db 'Hello, world.',0xA
19

Assemble it with ‘nasm’ to get the object file.

$ nasm -f elf32 hello.asm -o hello.o

Then compile it with gcc.

$ gcc hello.o -o hello
$ ./hello
Hello, world.
$

Type the code below and save it to ‘hello2.asm’.

1 extern printf
2
3 section .text
4 global main
5
6 main:
7 sub esp,4 ;allocate 4 byte in stack
8
9 lea eax,[mystring]
10 mov [esp],eax ; pointer to string
11
12 call printf ; call printf
13
14 add esp,4
15
16 ret
17
18 section .data
19 mystring db 'Hello World',0xA
20

Assemble the code to get the object code.

$ nasm -f elf32 hello2.asm -o hello2.o

Compile the object code with gcc

$ gcc hello2.o -o hello2
$ ./hello2
Hello World
$

 

Read Full Post »

Assembly Language is the most difficult programming language to learn, to code and to maintain. It’s not portable because it depends on the type processors and operating system. Instruction code in Intel will be different with PowerPC, Motorola any other processors. Although AMD processor is Intel Compatible but not all codes in Intel can run on AMD.
Programming will take longer time because it will take more codes for just simple print ‘Hello word’. More codes mean more difficult to debug when there is a problem. I will show you later.

So, if it’s

-difficult to develop
-difficult to maintain
-not portable

Why do we still need programming in Assembly?

There are 2 main reasons:

-Assembly Language is Very efficient in term of space in Hard disk, space in Memory and Speed.
-Direct access to hardware

Assembly is very efficient in time consumption when related with speed. It take less memory space and hard disk space compare to other programs which is written in high level language that perform the same task.

Then people will say, now, hard disk and memory are getting cheaper, so space of hard disk and memory are not the issue. And compilers are getting “smarter”. It can generate codes that much more efficient in space and time efficiency.
So, why still assembly.
(more…)

Read Full Post »

In this article, I will show you assembly command for arithmetic function.

INC
It’s increase ‘operand’ by 1 (one).
Syntax

INC destination

Type the codes below and save it to ‘inc.asm’. It’s a very simple program. It will increase the value in variable num by 1 from 2 to 3.

1 section .text
2 global _start
3 _start:
4
5 ;put number in num
6 mov eax,2
7 mov [num],eax
8 inc byte [num]
9
10 ;exit
11 mov eax,1
12 mov ebx,[num]
13 int 0x80
14
15 section .data
16 num db 0
17

Compile and run the program.

$ nasm -f elf32 inc.asm -o inc.o
$ ld inc.o -o inc
$ echo $?
0
$ ./inc
$ echo $?
3
$


(more…)

Read Full Post »

You can combining Assembly Language and C language for flexible programming. In this tutorial I will show a simple code to display a message that is written in assembly then it will be called from c.

Let’s start.
Type the code below and save it to ‘hello.asm’.

1 section .text
2
3 global printmsg
4
5 printmsg:
6 mov eax,4 ;sys_call write
7 mov ebx,1 ;file descriptor write
8 mov ecx,msg ;pointer to msg
9 mov edx,lenmsg ;len msg
10 int 0x80 ;call interrupt
11 ret ;return back to main program
12
13 section .data
14 msg db 'Mixing assembly with c.',0xA
15 lenmsg equ $ - msg
16

Compile it to be object file.

$ nasm -f elf32 hello.asm -o hello.o
$ ls -l hello.o
-rw-rw-r-- 1 darklinux darklinux 640 2018-12-01 11:09 hello.o
$

Type the codes below and save it to ‘hi.c’.

1 #include
2 main()
3 {
4 extern hello();
5 printmsg();
6 }

Compile the code with the assembly object file, then run the program.

$ gcc hi.c hello.o -o hello
$ ./hello
Mixing assembly with c.
$

It works.

Read Full Post »

Older Posts »