I am learning assembly. I am working on a simple program which should do the following:
- read a 2 digits number from STDIN
- decrement the read number
- send the result of the decrement to STDOUT
Here the program I have so far:
section .data
max_number_size: equ 2 ; max length of read input
section .bss
input_buffer: resb max_number_size ; used to store input number
section .text
global _start
_start:
mov eax, 3 ; sys call read
mov ebx, 0 ; from FD 0
mov ecx, input_buffer ; indicate the adress of the memory buffer where the bytes will be stored
mov edx, max_number_size ; read this quantity of character
int 80H ; store max_number_size to input_buffer from STDIN
atoi:
mov eax, 0 ; Set initial total to 0
mov ebx, 0 ; keep track of nbr of char processed
atoi_convert:
mov esi, [ecx] ; Get the current character
test ebx, max_number_size ; break the loop
je _stdout
cmp esi, 48 ; Anything less than char 0 is invalid (check ASCII table)
jl _exit_1
cmp esi, 57 ; Anything greater than char 9 is invalid (check ASCII table)
jg _exit_1
sub esi, 48 ; Convert from ASCII to decimal (0 starts at 48d in ASCII)
imul eax, 10 ; Multiply total by 10d
add eax, esi ; Add current digit to total
inc ecx ; Get the address of the next character
inc ebx ; keep track of nbr of char processed
jmp atoi_convert
_stdout:
mov ecx, eax
mov eax, 4
mov ebx, 1
mov edx, 32
int 80h
_exit_0:
mov eax, 1
mov ebx, 0
int 80H
_exit_1:
mov eax, 1
mov ebx, 1
int 80H
Notice that, in the code above, there is no decrement yet.
What I am habing trouble to understand is what is actually sent to STDOUT in the _stdout
label here.
What I understand is that, using the atoi
, the ASCII chars read from stdin are transformed into an actual decimal value (if input is 12, then the value in eax after atoi would be 0000 1100 (binary)).
So, when reaching _stdout
, we have 12d (0000 1100b) in eax
. In order to send it to STDOUT, I move the value into ecx
, configure eax
, ebx
, edx
for the syscall and then, boom, syscall.
However, there is no output at all.
See, for example:
/app # make
nasm -f elf -g -F stabs main.asm
ld -o main.exe main.o -m elf_i386
/app # ./main.exe > stdout
49
/app #
/app # ls -l | grep stdout
-rwxr-xr-x 1 root root 0 Feb 7 12:05 stdout
The stdout
file is empty. Not even a single byte.
What I am doing wrong in this program and what, consequently, am I not understanding correctly ?