1

Assembly language is quite straight forward but I am confused on how to use registers for example:

section .data    

msg db "Hello, world!",0xa  
len equ $ - msg 

section .text    


global _start   

_start:

;write our string to stdout

mov edx,len 
mov ecx,msg 
mov eax,4 
int 0x80        

;and exit

mov ebx,0   
mov eax,1 
int 0x80

Its working fine but when i change the code to:

section .data    

msg db "Hello, world!",0xa  
len equ $ - msg 

section .text    


global _start   

_start:

;write our string to stdout

mov ebx,len 
mov edx,msg 
mov eax,4 
int 0x80    

;and exit

mov ebx,0   
mov eax,1 
int 0x80    

It will compile but it will not display 'Hello, World'. All I did was change the register for len and msg. Is there a convention on what register must contain?

1 Answer 1

4

Of course it matters what registers are used when executing a syscall. eax holds the number of the syscall (in this case 4), and ebx, ecx, edx, esi, edi and ebp hold the argumens of the syscall in that order. The first argument has to be stored in ebx, the second in ecx, and so on, it doesn't make sense otherwise.

Look here for a list of Linux syscalls. As you can see, syscall number 4 is sys_write:

ssize_t sys_write(unsigned int fd, const char * buf, size_t count)

Therefore your registers must be set up as so:

  • eax - syscall number = 4 for sys_write
  • ebx - fd = file descriptor, 1 for stdout
  • ecx - buf = pointer to your string (msg in your code)
  • edx - count = amount of characters to print (len in your code)

EDIT: Here's another table with what each register should be filled with for specific syscalls. These two links also contain more information about syscalls that might be worth a read:

2
  • So the setup of register must be in order eax, ebx, ecx, edx, esi, edi, ebp. What if i use ebp to hold the number of syscall, Therefore the order of the register is ebp, edi, esi, edx, ecx, ebx, eax to make sense? Mar 3, 2012 at 6:18
  • 1
    @the_transltr: eax is for holding the syscall number, ebx for the first argument, ecx for the second argument, edx for the third argument, etc. This is how the syscall expects the arguments to be passed. There's no "order", that's just how it is.
    – AusCBloke
    Mar 3, 2012 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.