装配在x86_64 I386 code [英] Assembling i386 code on x86_64
问题描述
预期下code不工作:
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_len, hello_world_end - hello_world
.text
.global _start
_start:
mov ebx, 1
mov ecx, hello_world
mov edx, hello_world_len
mov eax, 4
int 0x80
mov ebx, 0
mov eax, 1
int 0x80
在通过跑:
as test.s -o test.o
ld test.o -o test
./test
它输出什么。当我改变行:
It outputs nothing. When I change the line:
mov ecx, offset hello_world ; added offset
它工作正常。我试图编译原来的code。与 - 32 -march = i386的
和 -m elf_i386
链接,但它仍然没有输出
It works fine. I tried compiling the original code with --32 -march=i386
and linking with -m elf_i386
but it still outputs nothing.
$ uname -a
Linux ubuntu 3.2.0-38-generic #60-Ubuntu SMP Wed Feb 13 13:22:43 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
我的猜测是这种情况,因为内存模型不平坦像I386。我能以某种方式效仿呢?
My guess is this happens because the memory model isn't flat like in i386. Can I emulate this somehow?
推荐答案
这不是内存模式。
在气体语法 MOV ECX,参考hello world
用于从内存地址读 参考hello world
如可以通过检查与ndisasm完成拆卸予以确认:
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
您需要的是为存储内存地址 参考hello world
的。在气体来完成,该方法是 MOV ECX,偏移参考hello world
,如从拆卸予以确认:
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
顺便说一句,另一种方法做的内存地址加载到寄存器的 LEA
ECX,参考hello world
。
一些其他的汇编(如NASM和YASM)有不同的语法,这种差异可能会造成混淆,因为可以用一个小桌子来说明:
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
这篇关于装配在x86_64 I386 code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!