Hello World!
Last updated
Last updated
Lets follow the tradition and write Hello world code in Assembly.
So, basically what we will do is, use system call(write) to display ‘Hello World’ and system call(exit) to exit successfully from program.
→ What are system calls?
→ It is a request from User to kernel to perform a specific activity. (Actually it is request from User Space to Kernel space. User/Kernel space is explained in OS internal section in detail). Each system call have a number assigned to it and to invoke them we can call them using that number.
EAX must contain syscall number
Argument needs to be passed via registers.
Lets look into the syscalls we are gonna use.
Here, we can see the System call list, along with their number.
Write is having 4
Exit is having 1
Lets look into them specifically to see how or what data it need for proper execution.
Here, we need to provide 3 argument to write syscall.
Exit status
Buffer- Message to be printed.
Count- Length of Message.
How do we provide the argument to syscall?
Answer is Register.
Since EAX, will have syscall number. We can use other registers EBX, ECX, EDX to pass the argument.
But you can ask, ECX is used for Counter. Then?
Right now, there is nothing related to counter. No instruction for it. Therefore, we can use them separately to pass arguments.
For exit syscall, we need to provide 1 argument. Which is exit status.
With the above information, we can start writing the code.
Semi-Colon is used for comment.
We have multiple sections. How will program know, from where to start the execution. We specify that using global _start. (This is what we call entrypoint of executable, from where the execution will start. Just like main function of c.)
.text section is for executable code. Writing code in it
For printing, moving 0x4 in EAX register. And arguments(0x1, message, mlen) in other register.
Invoking the system call with 0x80
Same goes for Exit syscall
.data is used for Initialized data. We have to specify our strings over here. ‘message’ is the variable. db means ‘define byte’
“Hello World!”, 0xA. 0xA is used for \n (new line).
mlen is used for message length.
How it is calculated. Using one of the Special Token → $ . It points to current line.
message: db “Hello World!”, 0xA [$ Points here] mlen equ $-message
Here, $ is pointing to starting of mlen, which can also be seen as ending of first line.
And message is pointing to our ‘Hello World!’ String starting.
$ -(minus) message ⇒ Message starting subtracted from Ending of message line. This will result in our string length (end - start)
Now, we need to compile and link the code. Using NASM assembler and ld linker we can do the same.
Assembler will generate an object file (*.o) and that will be used in linker to get final executable.
And Kaboom. We ran our Hello World program using Assembly Language.
💡 There are multiple ways of invoking system call. 0x80, syscall, sysenter etc etc. System call number might differ in each ways. For example, write system call is having the number 4 in 0x80 and 1 in syscall.