内联程序集printf将整数解释为地址

Inline assembly printf interpreting integers as an address

本文关键字:解释 地址 整数 程序集 printf      更新时间:2023-10-16

我正在尝试通过内联程序集打印数组。Printf函数一直将堆栈上的值解释为需要打印的地址,并导致错误(屏幕截图:https://prnt.sc/r692d3)。如果我将地址传递给printf,它就会打印出如下垃圾值:(屏幕截图:https://prnt.sc/r691de)。此外,如果有人知道如何使用内联ASM将'n'放入字符串中?谢谢:(

int main()
{
int mas[5] = { 1,2,3,4,5 };
int32_t diff = sizeof(int);
__asm
{
mov esi, 0x0
lea ecx, [mas]
mov eax, [ecx]
push ecx
call printf; Here it tries to read value '1' as an address
pop eax
loop_t:
xor ebx, ebx; Clear the registers
xor ecx, ecx;
lea ecx, [mas]; ECX = &mas
mov ebx, diff;
add ebx, ecx; &mas + diff
mov eax, [ebx]; Transfer the value
push eax; Push it on stack
call printf; Same thing here, interprets it as an address
pop eax;
add diff, 0x4;
inc esi; Cleanup process and looping back on
cmp esi, 0x5;
jne loop_t;
}
}

printf函数的第一个参数是格式字符串,即指向以null结尾的字符数组的第一个字符的指针。因此,第一个参数将始终被视为一个地址。

如果将值1作为第一个参数传递给printf(最后将其推到堆栈上(,则它将尝试从地址1读取格式字符串(这将失败(。