printf 在 masm32 中打印一个字符时出错

printf error printing a char in masm32

这是我第一次在这里写...我试图解释我的问题! 我在 masm32

中写了这段代码
.586
.model flat
.data

mess db "digita un carattere    ",0
res db "x = %c",10,0

.data?
salva db ?

.code
extern _printf:proc
extern _scanf:proc

_funzione proc
;pre
push ebp
mov ebp,esp
push ebx
push edi
push esi

mov eax, offset mess
push eax
call _printf ;puting out the message
mov eax, offset salva
push eax
mov eax, offset res
push eax
call _scanf ;taking the char and saving it in "salva"
add esp,12
xor eax,eax
mov eax,offset salva
push eax
mov eax, offset res
push eax
call _printf ;printing the char
add esp,8

;post
pop esi
pop edi
pop ebx
pop ebp

ret
_funzione endp
end

当我编译它时,输出是:

我不明白为什么 _printf 不打印 _scanf 读取的字符 ('y')... 请帮助我!

printfscanf 的格式字符串非常 不同。 scanf format string 控制 only 输入,scanf not 输出一些东西(只是回显输入的字符)。 scanf 只会产生 res db "x = %c",10,0 的错误。添加一行 scanf_res db "%c",0 并将 mov eax, offset res 更改为 mov eax, offset scanf_res.

printf format string res db "x = %c",10,0 期望推送一个直接值,而不是字符串的偏移量。将 mov eax,offset salva 更改为 mov al, salva.

.586
.model flat
.data

mess db "digita un carattere    ",0
scanf_res db "%c",0
res db "x = %c",10,0

.data?
salva db ?

.code
extern _printf:proc
extern _scanf:proc
extern _fflush:proc

_funzione proc
;pre
push ebp
mov ebp,esp
push ebx
push edi
push esi

mov eax, offset mess
push eax
call _printf ;puting out the message
mov eax, offset salva
push eax
mov eax, offset scanf_res
push eax
call _scanf ;taking the char and saving it in "salva"
add esp,12

xor eax,eax
mov al, salva
push eax
mov eax, offset res
push eax
call _printf ;printing the char
add esp,8

;post
pop esi
pop edi
pop ebx
pop ebp

ret
_funzione endp
end