linuxassemblyx86system-calls32-bit

Write system call won't print characters from a register


I want to print AAAA with the following:

BITS 32;

;write;
 push 0x41414141;
 pop ecx        ;
 mov eax, 4     ; write is syscall 4 for Ubuntu 32-bit
 mov ebx, 1     ; stdout
 mov edx, 4     ;
 int 0x80       ;

;exit;
 mov eax, 1     ;
 mov ebx, 0     ;
 int 0x80       ;

Yet, once assembled and linked this code only exits, no errors, what is wrong ?


Solution

  • A quick fix of your code:

    push 0x41414141 ; put 'AAAA' into stack memory
    mov ecx,esp     ; pointer to the 'AAAA'
    mov eax, 4      ; write is syscall 4 for 32-bit Linux
    mov ebx, 1      ; stdout
    mov edx, 4
    int 0x80
    add esp,4      ; restore stack
    

    No explanation, as you should first check what I did ask in comment, then the fix will be either obvious, or you will have to ask about something particular you don't understand...

    If you run your original code with strace ./my_program, you'd see write() return -EFAULT because you passed a bad address. Always use strace to debug programs that make syscalls and don't behave the way you expected.