Feeds:
Posts
Comments

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
$


Continue Reading »

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.

In this article, I will show you how to:
-Creating a file
-Opening a file
-Reading a file
-Writing a file
-Closing a file

Creating a File

The registers used to create new file:
eax =8 (sys_create)
ebx = file name
ecx = file permission (4=read, 2=write, 1=executable). Set for UOG (user,owner,group).

Writing a file

The register used to write on file:
eax=4 (sys_write)
ebx=file descriptor
ecx=pointed to data buffer
edx=length of data

Opening a file

eax=5 (sys_open)
ebx=file name
ecx=access mode (0=read only, 1=write only, 2=read and write)
edx=file permission

Reading a file

eax=3 (sys_read)
ebx=file descriptor
ecx=pointer to data buffer
edx=buffer size

Closing a file

The registers used to close the file:
eax=6
ebx=file descriptor

Continue Reading »

In assembly, register only know ASCII (American Standard Code for Information Interchange) characters. It mean, If you are working with number and need to display the result by calling syscall number 4 (32 bit x86), you can’t just put the result address into ecx register. Because, the ecx register will assume that your result in Hex number.
For example, your result is 7. ecx will assume that it’s 7hexadesimal not 7decimal meanwhile 7 is 37hexadesimal in ASCII table.

Let’s try in coding to see how it works.
It’s very simple program. I just do simple math, 4+2=6. I add 4 into eax register and 2 into ebx register. Then add ebx register to eax register, so the result will be:
eax = eax + ebx.
= 4 + 2
= 6

1 section .text
2 global _start
3 _start:
4
5 mov eax,4
6 mov ebx,2
7
8 add eax,ebx
9 mov [result],eax
10
11 mov eax,4
12 mov ebx,1
13 mov ecx,result
14 mov edx,1
15 int 0x80
16
17 mov eax,4
18 mov ebx,1
19 mov ecx,newline
20 mov edx,1
21 int 0x80
22
23 mov eax,1
24 int 0x80
25
26 section .data
27 newline db 0xA
28
29 section .bss
30 result resb 1

$ nasm -f elf32 add.asm -o add.o
$ ld add.o -o add
$ ./add
?
$

Continue Reading »

What is constant in assembly? it’s assigned value that is not change. In Nasm there are few directives that make constant. I will explain few of it.

EQU directive
The syntax is:

constant_name equ expression

for example

length equ 10
It mean set ‘length’ with value 10.

Let’s try.
Type the code below, save to equ.asm, assemble, link and run.
1 section .text
2 global _start
3 _start:
4
5 %macro printmsg 2
6 mov eax,SYS_WRITE
7 mov ebx,STDOUT
8 mov ecx,%1
9 mov edx,%2
10 int 0x80
11 %endmacro
12
13 printmsg msg,lenmsg ;print msg
14
15 ;copy AREA to memory
16 mov ebx,AREA
17 add ebx,'0' ;set value from number to character
18 mov [result],ebx
19
20 printmsg result,1 ;print AREA
21 printmsg newline,1 ;add line feed
22
23 mov eax,SYS_EXIT ;exit
24 int 0x80
25
26 section .data
27 STDOUT equ 1
28 SYS_EXIT equ 1
29 SYS_WRITE equ 4
30 WIDTH equ 4
31 LENGTH equ 2
32 AREA equ WIDTH*LENGTH
33 msg db 'Math (area=lenghtxwidth): 2x4='
34 lenmsg equ $ - msg
35 newline db 0xA
36
37 section .bss
38 result resb 1

Compile and run the program.

$ nasm -f elf32 equ.asm -o equ.o
$ ld equ.o -o equ
$ ./equ
Math (area=lenghtxwidth): 2x4=8
$

Macro is set of instruction codes that assigned by name. This is another option that you can use to create modular programming in Assembly Language.

The syntax is:

%macro macro_name macro_parameter
   macro_body
   …
   …
%endmacro

Let’s try.

1 section .text
2 global _start
3 _start:
4
5 ;set macro with 2 parameter
6 %macro printmsg 2
7 mov eax,4
8 mov ebx,1
9 mov ecx,%1
10 mov edx,%2
11 int 0x80
12 %endmacro
13
14 ;calling macro
15 printmsg msg1,lenmsg1
16 printmsg msg2,lenmsg2
17 printmsg msg3,lenmsg3
18
19 ;exit
20 mov eax,1
21 int 0x80
22
23 section .data
24 msg1 db 'Welcome to assembly language programming',0xA
25 lenmsg1 equ $ - msg1
26 msg2 db 'Trial with MACRO',0xA
27 lenmsg2 equ $ - msg2
28 msg3 db 'Happy trying',0xA
29 lenmsg3 equ $ - msg3
$ nasm -f elf32 macro.asm -o macro.o
$ ld macro.o -o macro
$ ./macro
Welcome to assembly language programming
Trial with MACRO
Happy trying
$

So, what is the different between MACRO and PROCEDURE?
We can check the object code and compare it.
If you see below, when we use macro, during the creating the object file, nasm will replace the macro_name with set of code instructions inside it. Only we don’t have to type it again.
If we compare to ‘procedure’, procedure will use less of memory because program only call the procedure at the time it need it then procedure will return the control back to the program.
(see my article about procedure).

$ objdump -d -M,intel macro.o

macro.o: file format elf32-i386
Disassembly of section .text:

00000000 <_start>:
0: b8 04 00 00 00 mov eax,0x4
5: bb 01 00 00 00 mov ebx,0x1
a: b9 00 00 00 00 mov ecx,0x0
f: ba 29 00 00 00 mov edx,0x29
14: cd 80 int 0x80
16: b8 04 00 00 00 mov eax,0x4
1b: bb 01 00 00 00 mov ebx,0x1
20: b9 29 00 00 00 mov ecx,0x29
25: ba 11 00 00 00 mov edx,0x11
2a: cd 80 int 0x80
2c: b8 04 00 00 00 mov eax,0x4
31: bb 01 00 00 00 mov ebx,0x1
36: b9 3a 00 00 00 mov ecx,0x3a
3b: ba 0d 00 00 00 mov edx,0xd
40: cd 80 int 0x80
42: b8 01 00 00 00 mov eax,0x1
47: cd 80 int 0x80
$

macro1macro2

 

Procedure or sub routine is set of codes that we can use repeatedly in a program. When you write a large program codes, procedure is very important to simplify your code.

The syntax is:

procedure_name:
   procedure body
   …
   …
ret

The procedure is called from another function by using CALL function.
The syntax is:

call procedure_name

RET is used to return the control to the calling procedure.

Let’s try.

1 section .text
2 global _start
3
4 _start:
5
6 mov ecx,msg1
7 mov edx,lenmsg1
8 call printmsg ;call print procedure
9
10 mov ecx,msg2
11 mov edx,lenmsg2
12 call printmsg
13
14 call exit ;call exit procedure
15
16 printmsg: ;print procedure
17 push ecx
18 push edx
19 mov eax,4
20 mov ebx,1
21 int 0x80
22 pop ecx
23 pop edx
24 ret ;return the control back
25
26 exit: ;exit procedure
27 mov eax,1
28 int 0x80
29 ret ;return the control back
30
31 section .data
32 msg1 db 'This is mesagge 1',0xA
33 lenmsg1 equ $ - msg1
34 msg2 db 'This is message 2',0xA
35 lenmsg2 equ $ - msg2
36
$ nasm -f elf32 proc.asm -o proc.o
ld proc.o -o proc
$ ./proc
This is mesagge 1
This is message 2
$

Let’s see from it’s object file how it will be after we assemble the codes. In here I use objdump program. The option -d is to display the assembler mnemonics for the machine instructions from objfile. This option only disassembles those sections which are expected to contain instructions.
Since the default of objdump format is AT&T, I have to use option -M,intel, to tell objdump to display intel format.

$ objdump -d -M,intel proc.o

proc.o: file format elf32-i386
Disassembly of section .text:

00000000 <_start>:
0: b9 00 00 00 00 mov ecx,0x0
5: ba 12 00 00 00 mov edx,0x12
a: e8 14 00 00 00 call 23 
f: b9 12 00 00 00 mov ecx,0x12
14: ba 12 00 00 00 mov edx,0x12
19: e8 05 00 00 00 call 23 
1e: e8 11 00 00 00 call 34 

00000023 :
23: 51 push ecx
24: 52 push edx
25: b8 04 00 00 00 mov eax,0x4
2a: bb 01 00 00 00 mov ebx,0x1
2f: cd 80 int 0x80
31: 59 pop ecx
32: 5a pop edx
33: c3 ret

00000034 :
34: b8 01 00 00 00 mov eax,0x1
39: cd 80 int 0x80
3b: c3 ret
$