下面的程序在 32 位和 64 位机器上给出不同的输出。当我检查编译器生成的汇编代码时,我发现 32 位机器上的变量引用是 esp,而在 64 位操作系统上它是 rbp。
在我看来,ebp 必须是变量的参考,因为它在一个函数中保持不变。
不使用 ebp 作为变量的参考是否有任何具体原因?
#include "stdio.h"
void main()
{
int i=5;
while(i)
{
printf("Hello world..\t");
asm("push %rax"); // for 64bit OS
//asm("push %eax"); // for 32bit OS
printf("Have a nice day..\n");
i--;
asm("pop %rax"); // for 64bit OS
//asm("pop %eax"); // for 32bit OS
}
}
在 64 位操作系统上,这两行打印了 6 次
在 32 位操作系统上无限次(直到它幸运地得到 0)打印两行,因为 eax 是对变量的引用。