Feeds:
Posts
Comments

Archive for November, 2018

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
?
$

(more…)

Read Full Post »

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
$

Read Full Post »

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

 

Read Full Post »

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
$

Read Full Post »

What is Loop? It’s repeat cycle of instruction in a program.
The loop instruction in assembly is below syntax:

LOOP label

Label is the target of memory that is set for the starting pointer of the first instruction in loop cycle.
Loop will use ECX register as counter number. When the loop starts, the ECX register is decremented. It will continue until the counter reaches the value zero.

Let’s try with code.

1 section .text
2 global _start
3
4 _start:
5
6 ;display msg
7 mov eax,4
8 mov ebx,1
9 mov ecx,msg
10 mov edx,lenmsg
11 int 0x80
12
13
14 ;print 1 to 9
15 ;set counter
16 mov ecx,9
17 mov eax,'1'
18
19
20 ;display number 1 and repeat until 4.
21 repeat:
22 push ecx
23 mov [num],eax
24 mov eax,4
25 mov ebx,1
26 mov ecx,num
27 mov edx,1
28 int 0x80
29
30 ;increase num
31 mov eax,[num]
32 sub eax,'0'
33 inc eax
34 add eax,'0'
35 pop ecx
36 loop repeat
37
38 ;new line
39 mov eax,4
40 mov ebx,1
41 mov ecx,newline

Save, assemble and link it.

$ nasm -f elf32 loop.asm -o loop.o
$ ld loop.o -o loop
$ ./loop
Print 1-9
123456789
$

loop1
loop2
loop3

Read Full Post »

CPU (central processing unit) can be divided into 3 parts:
control unit (it’s for retrieve/decode instructions, retrieve/store data into memory)
execution unit (The actual execution of instruction happen here)
registers (Internal memory location used as ‘variables’).

Since reading data from memory and storing back into memory slows down the processor because it will involve complicated processes of sending the data request via control bus and into the memory storage unit. And same processes when getting back the data from the memory.

To speed up the process, the processor need internal memory storage location. This internal memory is called registers. Instead of storing the data elements into memory, processor store it into registers.

The registers are grouped into three categories:
General Registers
Control Registers
Segment Registers

General registers are divided again into:
Data registers
Pointer registers
Index registers
(more…)

Read Full Post »

The way how memory works in Linux is much complicated. If you want know more details about it, you can read the book “Understanding The Linux Virtual Memory Manager by Mel Gorman”. He explains details about Physical Memory, page table management, process address space, boot memory allocator, Frame, swap, shared memory etc.

In this article, I just explain the simple one.
Below is section/segment standard when you write codes in assembly.
memory
If I visualize into a table, it will become like this.
memory2
All program instruction code will start on address 0x8048000. Then followed by .data section, .bss section heap and stack.
.data section is used for static variables.
.bss section is used for variables that are not initialized yet to any value.
Fyi, BSS (Block started by Symbo), historically is pseudo-operation in UA-SAP (United aircraft Symbolic Assembly Program), the assembler developed in the mid 1950s for the IBM 704 by Roy nutt.
The end address of Linux is 0xbFFFFFF.

At the time program loaded into memory, each ‘section’ is loaded into certain area in the memory.
All codes and data which are declared are brought together even if the source code are separated.

Let’s try if the statement is correct.
Open Linux terminal, type the code below, save into keypress.c, compile then run it.
keypress1

$ gcc keypress.c -o keypress
$ ./keypress
Press anykey to continue.

Open another linux terminal, then type:

$ ps -aux | grep keypress

keypress2
(more…)

Read Full Post »

Older Posts »