我有一个声明全局变量char file[MAX]
的C代码。此变量直接用于各种函数以将文件名复制到它。我可以将这个c文件编译成汇编代码,但我不知道如何找到这个变量的地址?
在x86堆栈中,我如何找到地址全局变量?你能举个例子说明如何在汇编代码中引用全局变量吗?
左编辑:我在汇编代码中看不到.Data段。
答案 0 :(得分:2)
要存储file
的地址以注册EAX:
AT& T语法:movl $_file, %eax
intel语法:mov eax, OFFSET _file
如何检查:
首先,编写一个简单的代码(test.c
)。
#define MAX 256
char file[MAX];
int main(void) {
volatile char *address = file;
return 0;
}
然后,将其编译为asssembly代码:gcc -S -O0 -o test.s test.c
.file "test.c"
.comm _file, 256, 5
.def ___main; .scl 2; .type 32; .endef
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $16, %esp
call ___main
movl $_file, 12(%esp)
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE0:
.ident "GCC: (GNU) 4.8.1"
或者如果你想要英特尔语法:gcc -S -O0 -masm=intel -o test_intel.s test.c
.file "test.c"
.intel_syntax noprefix
.comm _file, 256, 5
.def ___main; .scl 2; .type 32; .endef
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB0:
.cfi_startproc
push ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
mov ebp, esp
.cfi_def_cfa_register 5
and esp, -16
sub esp, 16
call ___main
mov DWORD PTR [esp+12], OFFSET FLAT:_file
mov eax, 0
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE0:
.ident "GCC: (GNU) 4.8.1"
通过更多的实验和检查,我得到了结果。