Suppose we want to write "Hello World" to the screen using assembly language.
- We will use the write() syscall to print the “Hello World”
- System call for write is #define __NR_write 4
- We need to store 4 into EAX register
- We would need to store the file descriptor (FD) which is STDOUT =1
- 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
Now we are finally ready to write out our program. Given below is our hello world program.
# Hello World assembly program
.section .data
message: .ascii
"Hello World\n"
.section .text
.global _start
_start:
#Load all arguments for write () system
call
movl
$4, %eax
movl
$1, %ebx
movl
$message,%ecx
movl
$12, %edx
int
$0x80
# We now need to exit the program
movl
$1, %eax
movl
$0, %ebx
int $0x80
We will now compile and link this program in Ubuntu Linux by using the following commands.
% as -o helloworld.o helloworld.s # Compile the object file
% ld -o helloworld helloworld.o # Link the Hello World program
No comments:
Post a Comment