printf错误在masm32中打印一个char

时间:2017-06-12 16:27:59

标签: assembly printf scanf masm 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

当我编译它时,输出是:

output

我不明白为什么_printf不打印_scanf读过的char('y')... 请帮帮我!

1 个答案:

答案 0 :(得分:0)

printfscanf的格式字符串非常不同。 scanf format string控件输入,scanf 输出内容(只是回显输入的字符)。 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