I want to write a simple assembly program to receive user input 'iterations', then it prints the numbers from zero till 'iterations'. example, of the expected output:
enter the number of iterations
4
performing the loop:
0123
exiting.
But the actual output is an infinite loop with dummy characters ! here is my code, I have commented every operation:
section .data
user_ask: db "enter the number of iterations", 0xA
str_len: equ $ - user_ask
out_msg: db "performing the loop: ", 0xA
out_len: equ $ - out_msg
exiting: db 0xA, "exiting.", 0xA
ext_len: equ $ - exiting
global _start
section .bss
out_buff resb 4
iterations resb 2
section .text
_start:
;; Prompt the user to enter a number
mov eax, 4
mov ebx, 1
mov ecx, user_ask
mov edx, str_len
int 0x80
;; Read user input and stores that ot 'iterations'
mov eax, 3
mov ebx, 2
mov ecx, iterations
mov edx, 5
int 0x80
;; Message: "performing the loop: ... "
mov eax, 4
mov ebx, 1
mov ecx, out_msg
mov edx, out_len
int 0x80
;; Setting edi to zero
xor edi, edi
.loop:
mov ecx, edi
add ecx, 0x30
mov [out_buff], ecx
mov ecx, out_buff
;; Writing the numbers
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
inc edi
;; Compare current edi with user input 'iterations'
cmp edi, DWORD[iterations]
jl .loop
.exit:
; Message: "exiting."
mov eax, 4
mov ebx, 1
mov ecx, exiting
mov edx, ext_len
int 0x80
;exit
mov eax, 1
mov ebx, 0
int 0x80
the problem is happening in the compare line, I think I am comparing an ASCII value with an integer or so...
BTW: I am running this on ubuntu 20.04 with nasm, as follow: nasm -f elf64 loop.asm && ld -o loop loop.o && ./loop