Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ 如何在程序集中调用scanf_以输入字符?_Visual C++_Assembly - Fatal编程技术网

Visual c++ 如何在程序集中调用scanf_以输入字符?

Visual c++ 如何在程序集中调用scanf_以输入字符?,visual-c++,assembly,Visual C++,Assembly,这是代码的一部分(“调用scanf_s”输入int) 但是如何调用scanf_s来输入字符呢 char format[]="%d"; //format string for the scanf function int first; _asm{ lea eax,first push eax lea eax,format; 读取第一个number push eax call scanf_s add esp,8

这是代码的一部分(“调用scanf_s”输入int) 但是如何调用scanf_s来输入字符呢

char format[]="%d"; //format string for the scanf function 
int first;
_asm{ 

      lea eax,first
      push eax 
      lea eax,format; 读取第一个number 
      push eax 
      call scanf_s
      add esp,8

        mov eax,dword ptr [first]
        push eax
        lea eax,format
        push eax
        call printf
        add esp,8

}

scanf_s
需要字符的附加参数。从:

scanf_s和wscanf_s要求为所有缓存指定缓冲区大小 类型为c、c、s、s或字符串控制集的输入参数 附于[]内。缓冲区大小(以字符为单位)作为 紧跟在指向缓冲区的指针之后的附加参数 或变量

此函数的C形式类似于
scanf_s(“%C”,&char,1)。在程序集中从右向左推送参数:

#include <stdio.h>

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

    _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
    }

    return 0;
}
#包括
内部主(空)
{
char scanf_fmt[]=%c;
char printf_fmt[]=%c\n;
字符;
_asm
{
push 1//缓冲区大小,也可以写入` push size字符`
leaeax,字符
将eax//指针推送到字符
lea eax,scanf_fmt
将eax//指针推送到格式字符串
打电话给scanf_s
添加esp,12//清理三次推压
movzx eax,字节ptr[character]//movzx:将一个无符号字节加载到32位寄存器中
按eax//字符作为值
lea eax,printf_fmt
将eax//指针推送到格式字符串
调用printf
添加esp,8//清理两次推压
}
返回0;
}

中列出了所有格式说明符。您是对的,谢谢您的建议。☺读了你的代码后,我知道我的错是我没有注意到“scanf_s需要一个字符的附加参数”。这个问题折磨了我两天。非常感谢^_^