c++ 中的内联 asm 与 __asm 的对比

Inline asm in c++ in vs __asm

本文关键字:asm c++      更新时间:2023-10-16
char name[25];
int generated_int;
for(int i = 0; i<sizeof(name); i++)
{
    name[i] = (char)0;
}
cout << "Name: ";
cin >> name;
int nameLen = strlen(name);
__asm
{
    pusha;
    mov esi, &name //I got error here, I cant use "&". How to move name address to esi?
    mov ecx, nameLen
    mov ebx, 45
start:
    mov al, [esi]
    and eax, 0xFF
    mul ebx
    inc esi
    add edi, eax
    inc ebx
    dec ecx
    jnz start
    mov generated_serial, edi
    popa
}

cout << endl << "Serial: " << generated_serial << endl << endl;

我不知道如何在 asm 块中获取我的 chay 数组的地址。当我尝试使用"&"(例如 &name)时,我在编译时出现错误:

error C2400: inline assembler syntax error in 'second operand'; found 'AND'

更新:

mov esi,名称给了我这个编译错误:C2443:操作数大小冲突

更新 2:莉亚工作正常。

您似乎正在寻找lea指令,它将某些交易品种的有效地址加载到寄存器中。以下指令将以esi存储name的地址。

lea esi, name

name已经(或者更确切地说是衰减到)一个指针。只需使用mov esi, name.

move esi, name

已经是名称的地址。如果你想要内容(名称[0]),你会使用

move esi, [name]
lea

你要找的:

#include <stdio.h>
int main()
{
    char name[25];
    char* fmt = "%pn";
    __asm {
        lea eax,name
        push eax
        mov eax,fmt
        push fmt
        call printf
    }
    return 0;
}