2017-06-12 76 views
1

這是我第一次寫這裏...我嘗試解釋我的問題! 我在MASM32printf error在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犯規打印字符( 'Y'),其_scanf讀過。 .. 請幫幫我!

回答

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 stringres 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