Feeds:
Posts
Comments

Archive for the ‘Programming in Linux’ Category

To get input from user, we can use sys_call no.3 with file descriptor in ebx which is 0 (stdin).
Type the code below, save to input.asm, assemble, link and run.

1 section .text
2 global _start
3 _start:
4 %macro printmsg 2
5 mov eax,4
6 mov ebx,1
7 mov ecx,%1
8 mov edx,%2
9 int 0x80
10 %endmacro
11 %macro getinput 1
12 mov eax,3
13 mov ebx,0
14 mov ecx,name
15 mov edx,10
16 int 0x80
17 %endmacro
18
19 printmsg msg1,lengmsg1 ;enter name
20 getinput name ;get input
21 printmsg msg2,lenmsg2 ;print hello
22 printmsg name,6 ;print name
23 printmsg newline,1 ;new line
24 printmsg msg3,lenmsg3 ;print welcome
25
26 mov eax,1 ;exit
27 int 0x80
28
29 section .data
30 msg1 db 'Please, enter your name (6 chars)?',0xA
31 lengmsg1 equ $ - msg1
32 msg2 db 'Hello '
33 lenmsg2 equ $ - msg2
34 msg3 db 'Welcome to assembly programming',0xA
35 lenmsg3 equ $ - msg3
36 newline db 0xA
37 section .bss
38 name resb 0

Compile and run the program

$ nasm -f elf32 input.asm -o input.o && ld input.o -o input
$ ./input
Please, enter your name (6 chars)?
Taufan
Hello Taufan
Welcome to assembly programming
$

Read Full Post »

In this tutorial, I will show you how to count the string length.
Type the codes below and save it to ‘stringlen.asm’.
1 section .text
2 global _start
3 _start:
4
5 mov eax,msg ;copy msg address pointer to eax register
6 mov ebx,eax ;copy to ebx register
7
8 nextchar: ;LOOP
9 cmp byte [eax],0 ;check if reach 0h character
10 jz done ;if reach go to done:
11 inc eax ;if not reach, increase eax pointer by 1
12 jmp nextchar ;jump back to next character:
13 done:
14 sub eax,ebx ;after all done. eax will has the last address of msg memory
15 ;by subtitute eax with ebx (eax=eax-ebx)
16 ;from the beginning msg memory you will get
17 ;total number of characters.
18 mov ebx,eax ;copy the total number to ebx register so you can display it
19 ;using ‘$echo $?’ command.
20 mov eax,1 ;exit
21 int 0x80
22
23 section .data
24 msg db ‘12345678901234567890’,0h

Compile and run it.
$ nasm -f elf32 stringlen.asm -o stringlen.o && ld stringlen.o -o stringlen
$ ./stringlen
To display the return value of the program, you can type: echo $?
$ echo $?
20
$

 

Read Full Post »

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 »

Older Posts »