Reputation: 3
I am a newbie to assembly language. I am trying to add two numbers using this code; the values are hardcoded. I am using "nasm", and I am successfully building the executable. When I run it, it gives no output. I came to know that if the value is an integer like in this case sum of two numbers, we need to convert it to string to write it using sys_write. If it's the only thing that needs to be taken care of, how should I do it? If not, why am I not able to see the output on stdout even when I can see the output in the registers using gdb?
global _start
section .text
_start:
mov eax,0x3
mov ebx,0x5
add eax,ebx
mov ecx,eax
mov eax,0x4
mov ebx,0x1
mov edx,0x1
int 0x80
mov eax,0x1
mov ebx,0x0
int 0x80
segment .data
sum db 0x00
Upvotes: 0
Views: 329
Reputation: 58762
You are invoking the write
system call which looks like this:
ssize_t write(int fd, const void *buf, size_t count);
Instead of a pointer to a buffer containing what you want printed, you pass the resulting number. That won't work. Also, your number is in binary, not text. As a quick fix for single digit numbers you can add the ascii code of 0
, and place the resulting character into memory for printing. You can use the stack for this.
global _start
section .text
_start:
mov eax,0x3
mov ebx,0x5
add eax,ebx
add al, '0' ; convert to text
push eax ; store on stack
mov ecx,esp ; pass its address for printing
mov eax,0x4
mov ebx,0x1
mov edx,0x1
int 0x80
add esp, 4 ; free stack space
mov eax,0x1
mov ebx,0x0
int 0x80
segment .data
sum db 0x00
Upvotes: 2