-2

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 :)

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Miguel
  • 1
  • 5
  • Although Nate's answer deals with one issue,the other really comes down to what you intend to do.You must be aware that `sys_read` syscall via `int 0x80` **does not convert** ASCII character representing numbers and store the integer in the destination. Literally what is being stored at -4(%ebp) will be the ASCII characters that make up the number, not the integer value.If you need it converted into an integer so you can do some operation and calculations on it and then write it out then you'll need code to convert ASCII to integer when reading and integer to ASCII before writing – Michael Petch Apr 10 '16 at 19:26
  • Effectively `sys_read` only reads ASCII characters into a buffer. and `sys_write` only writes a buffer containing ASCII characters. – Michael Petch Apr 10 '16 at 19:29
  • Thanks for the additional info. – Miguel Apr 11 '16 at 09:41

1 Answers1

0
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
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82