Archive for the ‘Programming in Linux’ 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
4 _start:
6 ;display msg
7 mov eax,4
8 mov ebx,1
9 mov ecx,msg
10 mov edx,lenmsg
11 int 0x80
14 ;print 1 to 9
15 ;set counter
16 mov ecx,9
17 mov eax,'1'
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
30 ;increase num
31 mov eax,[num]
32 sub eax,'0'
33 inc eax
34 add eax,'0'
35 pop ecx
36 loop repeat
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


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

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.
If I visualize into a table, it will become like this.
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.

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

Open another linux terminal, then type:

$ ps -aux | grep keypress


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.

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
3 global _start
4 _start:
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
13 ;exit
14 mov eax,1
15 int 0x80
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




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
3 global _start
4 _start:
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


Read Full Post »

What is method?
In programming language, method is something that you do to an object.

For example, the object is ‘door‘ and you want to apply an action ‘open‘.

In python you can write as: door.open().

door = object

open = method

( ) = argument

Argument is the place for parameters that you apply to the method.

For example, you want to open your door half only or full open then changing the color painting to blue or red.

The syntax will be:




In python, you can use built-in methods or create it manually.

Let me explain more details.

Read Full Post »

Older Posts »