如何在汇编中调用scanf_s输入一个字符?
How to call scanf_s in assembly to input a char?
这是代码的一部分("call 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
需要一个额外的字符参数。来自 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.
此函数的 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;
}
这是代码的一部分("call 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
需要一个额外的字符参数。来自 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.
此函数的 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;
}