Tuesday, March 2, 2010

Hello World - Assembly Language (x86)

This is a "Hello World" program written in pure assembly language on Linux/X86 platform.

1 /* as helloworld.s -o helloworld.o; ld helloworld.o -o helloworld */
2 /* gcc helloworld.s -nostartfiles -nostdlib -static -o helloworld */
3
4 .text
5 .globl _start
6
7 _start:
8 mov $13, %edx
9 mov $msg, %ecx
10 mov $1, %ebx
11 mov $4, %eax
12 int $0x80
13
14 xorl %ebx, %ebx /* exit code */
15 movl $1, %eax /* exit function */
16 int $0x80
17
18 msg:
19 .string "Hello world\n"
20

The result is...

$ gcc helloworld.s -nostartfiles -nostdlib -static -o helloworld
$
$ uname -a
Linux lungswu-mobile 2.6.28-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux
$
$ ./helloworld
Hello world
$
$

Of Linux on X86 CPU...
  • The Linux kernel entry, from user mode application into kernel mode, is triggled by INT 0x80 instrusion.
  • The system call number is got from the CPU register EAX.
  • The system call parameter is placed in the sequence of EBX, ECX, EDX...

    In the example of the WRITE() system call

    ssize_t write (int __fd, __const void *__buf, size_t __n);
  • The system call index (EAX) is 0x04
  • __fd is EBX
  • __buf is ECX
  • __n is EDX
  • No comments:

    Post a Comment