我正在研究c ++如何通过汇编语言调用正确的成员函数.我带来的简单程序如下:
class A
{
public:
virtual void show() {}
};
class B : public A
{
public:
void show() {}
};
int main()
{
A* pA = new B;
pA->show();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其装配如下:
main:
.LFB2:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
pushq %rbx
subq $24, %rsp
movl $8, %edi
.cfi_offset 3, -24
call _Znwm <<<================ 1
movq %rax, %rbx
movq %rbx, %rax
movq %rax, %rdi
call _ZN1BC1Ev
.L11:
movq …
Run Code Online (Sandbox Code Playgroud) 看看输出objdump -d ELFfile
,我无法区分直接和间接的跳转/调用.有什么建议?
有了这个简单的组装:
.text
.globl main
str:
.asciz "%i\n"
add:
push %rbp
mov %rsp, %rbp
movl %esi, %eax
addl %edi, %eax
pop %rbp
ret
main:
mov $1, %esi
mov $2, %edi
lea add(%rip), %rax
call %rax #what is wrong here? the rax should contain first instruction of add
mov %eax, %esi
xor %eax, %eax
lea str(%rip), %rdi
call printf
xor %eax, %eax
ret
Run Code Online (Sandbox Code Playgroud)
我收到错误:
foo.s:17: Warning: indirect call without `*'
Run Code Online (Sandbox Code Playgroud)
为什么?应该%rax
包含函数的地址(如注释中所示),这不是c
,其中有带有 的指针*
,而是包含地址的寄存器。那么这里出了什么问题呢?