I am just starting with x86 assembly and was trying some basics with the MOV instruction. (code below)
BITS 32
SECTION .data
somedata: db "Hello world",10
SECTION .text
global _start
_start:
mov eax, somedata
mov al, [eax]
mov edx, [somedata]
I seem to not understand why nasm is using RIP relative addressing, when it is specified BITS 32 in the assembly (I thought the relative addressing in only in the 64 bit mode). Moreover it is using RAX in 32 bit mode. If I do not specify anything, it seems to not use relative addressing and uses EAX.
Code with BITS 32
Disassembly of section .text:
00000000004000b0 <_start>:
4000b0: b8 c0 00 60 00 mov eax,0x6000c0
4000b5: 8a 00 mov al,BYTE PTR [rax]
4000b7: 8b 15 c0 00 60 00 mov edx,DWORD PTR [rip+0x6000c0] # a0017d <_end+0x4000ad>
Code without BITS 32
Disassembly of section .text:
00000000004000b0 <_start>:
4000b0: b8 c0 00 60 00 mov eax,0x6000c0
4000b5: 67 8a 00 mov al,BYTE PTR [eax]
4000b8: 8b 14 25 c0 00 60 00 mov edx,DWORD PTR ds:0x6000c0
I know it is not the assembler, it is me. What is it that I am doing wrong?
PS:
Using nasm, and 64 bit computer with linux.
Assembling using
nasm -f elf64 -F stabs -g sandbox.asm -o sandbox.o
Disassembling using
objdump -M intel -d sandbox
I also tried the following assembler and linker flags:
nasm -f elf32 -F stabs -g sandbox.asm -o sandbox.o
ld -oformat=elf32-i386 -o sandbox sandbox.o
but it is not working saying ld: i386 architecture of input file `sandbox.o' is incompatible with i386:x86-64 output
BITS
directive is almost certainly wrong. As a rule of thumb, it's either superfluous or wrong in almost all situations. – fuz