c++ 中的内联汇编在 vs __asm

发布于 2024-11-08 19:48:08 字数 871 浏览 0 评论 0原文

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,name给了我这个编译错误:C2443:操作数大小冲突

更新2: lea 工作正常。

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;

I don't know how to get address of my chay array in asm block.
When I try to use "&" e.g. &name i get error while compiling:

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

UPDATE:

mov esi, name gives me this compile error: C2443: operand size conflict

UPDATE 2:
lea is working fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十级心震 2024-11-15 19:48:08

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

lea esi, name

You seem to be looking for the lea instruction, which loads the effective address of some symbol into a register. The following instruction will store the address of name in esi.

lea esi, name
再可℃爱ぅ一点好了 2024-11-15 19:48:08

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

name is already (or rather decays to) a pointer. Just use mov esi, name.

别理我 2024-11-15 19:48:08
move esi, name

已经是名字的地址了。如果您想要内容(名称[0]),您可以使用

move esi, [name]
move esi, name

already is the address of name. If you wanted the content (name[0]) you would use

move esi, [name]
兔小萌 2024-11-15 19:48:08

lea 是您正在寻找的:

#include <stdio.h>

int main()
{
    char name[25];
    char* fmt = "%p\n";

    __asm {
        lea eax,name
        push eax
        mov eax,fmt
        push fmt
        call printf
    }
    return 0;
}

lea is what you're looking for:

#include <stdio.h>

int main()
{
    char name[25];
    char* fmt = "%p\n";

    __asm {
        lea eax,name
        push eax
        mov eax,fmt
        push fmt
        call printf
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文