Feeds:
Posts
Comments

Archive for the ‘Programming-Assembly’ Category

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 »

In 1991, Linus Torvalds release Linux. There is no GUI (graphic user interface), just command line. But, it can handle multitasking and the internal structure is very powerful.
Core of Linux is block code called Kernel.
Memory system is signed as Kernel Space and User Space. Kernel space is used for handling hardware and devices and user space for the application that run on Linux.
Communication between Kernel space and User space is handled by System Call.
If you did programming in the old DOS before, it’s almost the same like “Interupt with service no”.

Access to hardware is limited in software that run in Kernel Space and only can be done via Kernel Mode Device Drivers.

How to use System Call?
-Put the system call number in EAX register.
-Put arguments in EBX, ECX, EDX, ESI, EDI or EBP registers.
-call interrupt 80h (0x80).
-the result is usually returned into EAX register.
(more…)

Read Full Post »

In this tutorial, you will use 2 syscall in linux, write and exit.
Type the code below then compile to assembly.

1 section .text
2
3 global _start
4 _start:
5
6 ;display msg
7 mov eax,4 ;syscall for write
8 mov ebx,1 ;file descriptor 0=stdin, 1=stdout
9 mov ecx,msg ;point to msg
10 mov edx,lenmsg
11 int 0x80
12
13 ;exit
14 mov eax,1
15 int 0x80
16
17 section .data
18 msg db 'Hello World',0xA,0xD
19 lenmsg equ $ - msg ;lenght of msg = end of memory - msg characters

Convert to assembly

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

Run the gnu linker.

$ ld hello.o -o hello

Run the program

$ ./hello
Hello World

hello1

hello2

 

Read Full Post »

Assembly Language is a low level programming that work based on architecture of computer or device that can be programmed. We need assembler to convert into machine language. In this tutorial I will use NASM (netwide assembler) for Intel x80-86 (8086, 8088, 80286, 80386, 80486, Pentium, Pentium Pro, Pentium MMX, Pentium II, Pentium III, pentium 4, Pentium Xeon, Pentium II Xeon, Pentium Core, Celeron etc) or it’s compatible.

Assembly code then will be converted into machine language using Assembler. In this tutorial I use Nasm (netwide assembler). It’s most use assembler in Linux. Created by Simon Tatham who is also creator of Putty.

If you use Ubuntu, just type in your Linux Terminal:

$sudo apt-get install nasm

Nasm is a assembler for Intel x80-86 (32 bit) and x80-64 (64 bit) which designed for portability and modularity. It supports various .obj format.

The nasm command line:

$nasm -f   -o []

for example:

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

Since we are using Linux, the format executable file will be ELF (executable and linkable format object). It’s format that is used in Linux, Unix system V, Solaris X86, Unixware and ScoUnix. Elf32 means Elf format for 32 bits.

You can produce also a machine code

$ nasm -f elf32 exit.asm -l exit.lst
$ cat exit.lst
1 section .text
2
3 global _start
4 _start:
5
6 00000000 B801000000 mov eax,1 ;linux syscall 1=exit
7 00000005 BB02000000 mov ebx,2 ;return value after program close
8 0000000A CD80 int 0x80 ;call the syscall
9

(more…)

Read Full Post »

What is stack register?
Stack is a group of memory which is use for storage informations during the execution of program. It’s temporary area in CPU.
How it work?
The First data that goes into the stack is the Last data that come out from the stack.

In this article I will show you how it works using gdb (gnu debugger).
First, I create a program named stack in assembly language. I name the file stack.asm.

section .text
 global _start
 _start:

push 0xAAAAAAAA
 push 0xBBBBBBBB
 push 0xCCCCCCCC
 push 0xDDDDDDDD

pop eax
 pop ebx
 pop ecx
 pop edx

exit:
 mov eax,1
 mov ebx,0
 int 0x80

stack01
Then I compile with “nasm” and link with “ld”. I add option -gstabs+ to tell nasm to save debugging information that I will use in gdb to print the line of assembler code that correspond to each assembler instruction. Since my processor is 32 bit, the format will be elf32.

$ nasm -f elf32 -gstabs+ stack.asm -o stack.o
$ ld stack.o - stack

stack02
Now, let start using gdb.
Type “gdb” followed by the program “stack”.

$ gdb stack
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08 
Copyright (C) 2011 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "i686-linux-gnu". 
For bug reporting instructions, please see: 
... 
Reading symbols from /home/darklinux/assembly/stack...done. 
(gdb)

(more…)

Read Full Post »