Reputation: 253
I'm trying to make a simple x86 program that reverses a string, in this case: "ciao".
section .data
msg db "ciao"
len equ $ - msg
section .text
global _start
_start:
lea eax, [msg]
mov ebx, len
add eax, ebx-1
reverseloop:
push [eax]
sub eax, 1
cmp eax, [msg]
jl reverseloop
When I try to assemble it, I get the error:
main.asm:14: error: operation size not specified
So, I tried to specify the size, by modifying line 14 to:
push byte [eax]
And now I get the error:
main.asm:14: error: invalid combination of opcode and operands
I don't really understand what is the problem here.
The command I use to compile is:
nasm -f elf32 main.asm -o main.o
Upvotes: 1
Views: 192
Reputation: 39166
push [eax]
NASM does not know the size of the memory operand that you would like to push. You need to specify it as push dword [eax]
.
It is not an option to use push byte [eax]
because the stack operations don't do single bytes.
add eax, ebx-1
This is your next error! The source operand ebx-1
is invalid.
You could first decrement EBX and then add that to EAX, but as I will show below you don't need this decrement.
cmp eax, [msg] jl reverseloop
This is not doing what you want either. The cmp
compares EAX to a dword from memory, but you want this to be a comparison between addresses. Use cmp eax, msg
.
And in order to continu the loop for all characters of the string, you need to conditionally jump for as long as the address in EAX is GreaterOrEqual msg.
mov eax, msg
add eax, len
reverseloop:
dec eax
movzx ebx, byte [eax]
push ebx
cmp eax, msg
ja reverseloop
Now I don't see how this code will reverse anything. The characters land on the stack in the same order as they were in the memory (interspersed with some zero bytes of course).
mov eax, msg
mov ecx, len
reverseloop1:
movzx ebx, byte [eax]
push ebx
inc eax
dec ecx
jnz reverseloop1
mov eax, msg
mov ecx, len
reverseloop2:
pop ebx
mov [eax], bl
inc eax
dec ecx
jnz reverseloop2
Solutions that don't use the stack exist and generally would be better.
Upvotes: 4