找到你要的答案

Q:How to call scanf_s in assembly to input a char?

Q:如何调用scanf_s汇编输入字符?

this is part of code ("call scanf_s" to input a int) but how to call scanf_s to input a char?

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

}

this is part of code ("call scanf_s" to input a int) but how to call scanf_s to input a char?

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

}
answer1: 回答1:

scanf_s needs an additional parameter for characters. From the Microsoft manual:

scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

The C form of this function looks like scanf_s ("%c",&char,1);. Push the arguments from right to left in assembly:

#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;
}

scanf_s需要一个额外的字符参数。从微软手册:

scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

这个函数的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;
}
visual-c++  assembly