簡體   English   中英

c ++和匯編程序之間的Scanf函數

[英]Scanf function between c++ and assembler

我有這個代碼:

void scan ()
{
    char scanf_fmt[] = "%c";
    char printf_fmt[] = "%c\n";
    char character[30];

    _asm
    {
        push 1 // Buffer size, you can also write `push size character`

        lea eax, character
            push eax     // Pointer to character

            lea eax, scanf_fmt
            push eax                // Pointer to format string
            call scanf_s


            add esp, 12             // Clean up three pushes

            movzx eax, byte ptr[character] // MOVZX: Load one unsigned byte into a 32-bit-register
            push eax// Character as value


            lea eax, printf_fmt
            push eax // Pointer to format string

            call printf 

            add esp, 8               // Clean up two pushes era 8
    }

    //return 0;
}

此函數打印單詞中的第一個字符,但我復制了此函數並更改了名稱,例如: scan() to scan2();

當我使用新功能時,程序會獲取功能scan的先前單詞。

我該怎么做才能給我讀一個不同的詞?

運用

lea eax, character
push eax     // Pointer to character

你告訴scanf將你掃描的數據放在哪里

char character[30];

如果你有第二個功能,但使用相同的緩沖區,你將覆蓋彼此的輸入

如果要保存內容,請創建第二個緩沖區,例如

char character[30];
char character2[30];

並使用此代替:

        push 1 
        lea eax, character2
        push eax                // Pointer to buffer
        lea eax, scanf_fmt
        push eax                // Pointer to format string
        call scanf_s
        add esp, 12             // Clean up three pushes

        movzx eax, byte ptr[character2] 
        push eax                // Character as value
        lea eax, printf_fmt
        push eax                // Pointer to format string
        call printf 
        add esp, 8               // Clean up two pushes era 8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM