Assembly x86程序集等效于常量字符[]
因此,我试图找出x86汇编中的Assembly x86程序集等效于常量字符[],assembly,gcc,Assembly,Gcc,因此,我试图找出x86汇编中的constcharabc[]=“hello”的等价物是什么 为了解决这个问题,我用C编写了以下代码: int count(const char* str) { int i = 0; for (; str[i] != '\0'; i++); return i; } int main(int argc, char* argv[]) { const char abc[] = "hello"; return count(abc
constcharabc[]=“hello”
的等价物是什么
为了解决这个问题,我用C编写了以下代码:
int count(const char* str)
{
int i = 0;
for (; str[i] != '\0'; i++);
return i;
}
int main(int argc, char* argv[])
{
const char abc[] = "hello";
return count(abc);
}
然后我让gcc
使用以下命令将其转换为x86程序集:
gcc-m32-O0-S main.c
这就给了我:
.file "main.c"
.text
.globl count
.type count, @function
count:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $16, %esp
call __x86.get_pc_thunk.ax
addl $_GLOBAL_OFFSET_TABLE_, %eax
movl $0, -4(%ebp)
jmp .L2
.L3:
addl $1, -4(%ebp)
.L2:
movl -4(%ebp), %edx
movl 8(%ebp), %eax
addl %edx, %eax
movzbl (%eax), %eax
testb %al, %al
jne .L3
movl -4(%ebp), %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size count, .-count
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $16, %esp
call __x86.get_pc_thunk.ax
addl $_GLOBAL_OFFSET_TABLE_, %eax
movl $1819043176, -6(%ebp)
movw $111, -2(%ebp)
leal -6(%ebp), %eax
pushl %eax
call count
addl $4, %esp
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE1:
.size main, .-main
.section .text.__x86.get_pc_thunk.ax,"axG",@progbits,__x86.get_pc_thunk.ax,comdat
.globl __x86.get_pc_thunk.ax
.hidden __x86.get_pc_thunk.ax
.type __x86.get_pc_thunk.ax, @function
__x86.get_pc_thunk.ax:
.LFB2:
.cfi_startproc
movl (%esp), %eax
ret
.cfi_endproc
.LFE2:
.ident "GCC: (Debian 10.2.0-13) 10.2.0"
.section .note.GNU-stack,"",@progbits
现在,我真的不明白我的“你好”文字在哪里?
有人能帮我理解吗?试试
gcc-Wall-fverbose asm-m32-O2-S main.c
然后检查main.S
;同时将main
和count
放在两个不同的C文件中(因此有一个count.C
文件,其中包含count
的C代码和一些extern int count(const char*);
在main.C
中声明),它被伪装成movl$1819043176,-6(%ebp)
和movw$111,-2(%ebp)
1819043176=0x6C6568=lleh
和111=0x6F=o
。由于endianness提供了hello
。如果您希望编译器将其粘贴到中,请将其设置为static
。rodata
而不是立即在堆栈上构建它。@Jester感谢您的回答,现在我明白了!