Reputation: 313
I am writing an assembly code to get a number and print some text as many times as that number.
for example when the input is 4, I want to write "Hello!" 4 times.
my code:
section .data
msg db 'Hello!',0xA
len equ $-msg
section .bss
n resb 1
section .text
global _start
_start:
mov edx, 1
mov ecx, n
mov ebx, 0
mov eax, 3
int 0x80
mov ecx, n
loop1:
push ecx
mov edx, len
mov ecx, msg
mov ebx, 1
mov eax, 4
int 0x80
pop ecx
loop loop1
mov eax, 1
int 0x80
I run it with this codes in terminal
nasm -f elf32 test.asm
ld -m elf_i386 -o test test.o
./test
But i get an infinity "Hello!"
Upvotes: 2
Views: 689
Reputation: 9899
mov ecx, n
In NASM an instruction like this loads the address of the variable in ECX
.
(MASM would have complained about the variable not being a dword!)
You knew this since you used it correctly for the input of the single character.
However the instruction that initializes the loop counter should have dereferenced to obtain the actual input. You need to use square brackets for this.
Now by itself this is not enough! The input that you got represents a digit character where you need the actual value of that digit.
e.g. If you input the character "4", the variable n will hold 52.
movzx ecx, byte [n] ; Load 1 byte and store in dword register
sub cl, '0' ; Convert from character "4" to value 4 (e.g.)
loop1:
Upvotes: 3