Search code examples
assemblyx86gnu-assembleratt

Read & write and integer in assembly ATT


I have been trying to to read an integer from stdin and after write it in stdout, but the only thing that I accomplish is writing a simply hello world. This is the code that I have:

.global _start
_start:
push %ebp
movl %esp,%ebp
subl $4,%esp 


#Read
mov $3,%eax #READ
mov $0,%ebx #STDIN
mov -4(%ebp),%ecx 
mov $1,%edx
int $0x80  

#Write call
mov $4,%eax #WRITE
mov $1,%ebx #STDOUT
mov -4(%ebp),%ecx 
mov $1,%edx 
int $0x80  

mov $1,%eax #exit
mov $0,%ebx # exit code
int $0x80

Thanks in advance and don't be too hard on me because these are my first steps in assembly :)


Solution

  • mov -4(%ebp),%ecx 
    

    When making the read system call, %ecx is supposed to contain the address where you want the data to be stored. In other words, you want %ecx to equal %ebp minus 4. But this mov instruction will load %ecx with the contents of the memory at %ebp - 4.

    I think what you want (in both places) is

    lea -4(%ebp), %ecx