I'm trying to write an assembly program that takes in a string of length 5 and prints the reversed string. I tried to print the characters one by one in reverse order by using a loop with ecx = 5. To print the characters I calculate the reversed address and then use a subroutine to print that character.
But there seems to be a mistake in my code, it only outputs the first character.
For example for a given input 12345 I'm getting 1 as output but I expected it to print 54331
segment .bss
INPT: resb 5
segment .text
global asm_main
asm_main:
mov eax, 3 ; system call no (sys_read = 3)
mov ebx, 1 ; std_in = 1
mov ecx, INPT ; ptr to location to store input (INPT)
mov edx, 5 ; no of bytes to read
int 0x80 ; issue a system call
mov ecx, 5
swap:
mov edx, -8
imul edx, ecx
add edx, 40
add edx, INPT ; address of next byte to print = [INPT + 5 * 8 - ecx * 8]
call print
loop swap
jmp exit
print:
push ecx
mov eax, 4
mov ebx, 1
mov ecx, edx
mov edx, 1
int 0x80
pop ecx
ret
exit:
mov eax, 1
xor ebx, ebx
int 0x80