Monday, March 17, 2014

Hello World Assembly language program

Since our last post described how we could use Unix system calls in unistd.h header file and then putting the system call and its options into the registers.

Suppose we want to write "Hello World" to the screen using assembly language.
  1. We will use the write() syscall to print the “Hello World”
  2. System call for write is #define __NR_write 4
  3. We need to store 4 into EAX register
  4. We would need to store the  file descriptor (FD) which is STDOUT =1
  5. Remember in Linux FD STDIN = 0, FD STDERR = 2
When we do a man 2 write in Linux we get the format of the write call which is

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

int fd – is the stdout FD which is 1

Const void *buf – pointer to the memory location which holds the “hello world” string

size_t count – Size of the string which is 12