Archive for April, 2010

Hello world!

April 11, 2010

Assembly programming is something which we don’t do often. Since, this is my hello world blog, I wanted to start off with something simple and geeky. So, I decided to write a assembly Hello world code which will communicate with the Linux system calls directly.


section    .text
global _start            ;linker looks for this
_start:                    ;linker entry point
mov    edx,length    ;length of string
mov    ecx,string   ;string to print
mov    ebx,1    ;file descriptor stdout
mov    eax,4    ;system call number for sys_write
int    0x80    ;interrupts the kernel
mov    eax,1    ;sys_exit system call
int    0x80
section    .data
string    db    'Hello, world!',0xa    ;the string
length    equ    $ - string           ;length of the string

Now, let me explain the code.
The system call number is moved into register eax. The arguments are passed through registers ebx, ecx, edx, esi, edi and ebp
To find out the system call number look at the asm/unistd.h in the user space header files.(/usr/include/asm/unistd.h).


#define __NR_restart_syscall      0
#define __NR_exit          1
#define __NR_fork          2
#define __NR_read          3
#define __NR_write          4
#define __NR_open          5
#define __NR_close          6

The system call numebr for sys_write is 4. Now, look at the sys_write man page.

man 2 write

ssize_t write(int fd, const void *buf, size_t count);

The arguments are fd-file descriptor, buf-pointer to the string and count-size of the string. The arguments are passed to the respective registers. System calls in linux are done using int 0x80.

To make an executable issue the following commands.


anirudh@powerbox:~/code/assembly$ nasm -f elf hello.asm
anirudh@powerbox:~/code/assembly$ ld -o hello hello.o
anirudh@powerbox:~/code/assembly$ ./hello
Hello, world!

To reduce the binary size, the use -s switch. This will omit all the symbol information.

anirudh@powerbox:~/code/assembly$ ld -s -o hello1 hello.o
anirudh@powerbox:~/code/assembly$ ls -lh hello hello1
-rwxr-xr-x 1 anirudh anirudh 7622010-04-21 09:19 hello
-rwxr-xr-x 1 anirudh anirudh 440 2010-04-21 09:20 hello1

Now, let us find out the size of an Hello world executable written in C.


anirudh@powerbox:~/code/c_cpp$ gcc -o hello hello.c
anirudh@powerbox:~/code/c_cpp$ ls -lh hello
-rwxr-xr-x 1 anirudh anirudh 6.3K 2010-04-21 09:31 hello

Now, what if the system call has  more than six arguments ? I’m yet to figure this out.

Advertisements