Assembling i386 code on x86_64

后端 未结 1 948
北海茫月
北海茫月 2021-01-22 04:05

The following code does not work as expected:

.intel_syntax noprefix
.arch i386
.data
hello_world:
.ascii \"Hello world!\\n\"
hello_world_end:
.equ hello_world_l         


        
相关标签:
1条回答
  • 2021-01-22 04:41

    It's not about the memory model.

    In gas syntax mov ecx, hello_world means a read from memory address hello_world, as can be confirmed by checking the disassembly done with ndisasm:

    00000000  BB01000000        mov ebx,0x1
    00000005  8B0C25D4104000    mov ecx,[0x4010d4]
    0000000C  BA0D000000        mov edx,0xd
    00000011  B804000000        mov eax,0x4
    00000016  CD80              int 0x80
    

    What you want is to store the memory address of hello_world. In gas the way to accomplish that is mov ecx, offset hello_world, as can be confirmed from the disassembly:

    00000000  BB01000000        mov ebx,0x1
    00000005  B9D4104000        mov ecx,0x4010d4
    0000000A  BA0D000000        mov edx,0xd
    0000000F  B804000000        mov eax,0x4
    00000014  CD80              int 0x80
    

    By the way, another way to do load the memory address into a register is leaecx, hello_world.

    Some other assemblers (such as NASM and YASM) have different syntax, and this difference may cause confusion, as can be illustrated with a small table:

    gas                           NASM/YASM                ndisasm disassembly
    mov ecx,hello_world           mov ecx,[hello_world]    mov ecx,[0x4010d4]
    mov ecx,[hello_world]         mov ecx,[hello_world]    mov ecx,[0x4010d4]
    mov ecx,offset hello_world    mov ecx,hello_world      mov ecx,0x4010d4
    
    0 讨论(0)
提交回复
热议问题