I'm simply trying to read a number from 0 up to 9 and subtract 4 from it, store the value in a table, then print it to the terminal. It kinda works, but for some reason when I print "bye!" at the end of the program it prints "eye!". Also, for some reason I need to set the pointer 2 bytes backwards to "work". The code is the fallowing:
segment .data
val: ; defines a new array of 2 bytes long
db 0
db 0
num db 0
msg db 'bye!', 0ah
segment .bss
segment .text
global _start ; defines the entry point
_start:
mov ecx , num
mov edx , 01h ; sets the reading length
mov ebx , 00h ; tells that it's an input call
mov eax , 03h ; system call (read)
int 80h ; calls it
mov eax , [num] ; moves number to register
sub eax , 04h ; subtracts 4 from it
mov ecx , val ; sets ecx to point to val
mov [ecx], eax ; adds the result to val, pointed by ecx
inc ecx ; moves the pointer to the next address
mov [ecx], byte 0ah ; appends \n to the end of it
sub ecx , 02h ; retreats pointer (is over here that I need to retreat 2 for some reason
mov edx , 03h ; sets the string length
mov ebx , 01h ; tells that it's an output call
mov eax , 04h ; system call (write)
int 80h ; calls it
mov ecx , msg ; passes the string to the function
mov edx , 05h ; sets the string length
mov ebx , 01h ; tells that it's an output call
mov eax , 04h ; system call (write)
int 80h ; calls it
mov eax , 01h ; system call (exit)
int 80h ; calls it
output:
[mmd@pxdev asm]$ ./main
9
5
eye!
[mmd@pxdev asm]$
[mmd@pxdev asm]$
for some reason it's exiting twice (?) too. If I set the line 29 to retreat only one byte, the output is yeye!
in the last line.
I'm at the very beginning of assembly and, honestly, I don't feel that tutorials out there are explained enough. So sorry if I'm missing something obvious or I'm doing something really stupid.
Edit: I found one of my errors at line 31, because I was telling the system that I'm printing 3 bytes, when actually I was above to print only 2. So no need anymore to retreat two bytes from ecx
. But the main error keeps happening.