2015-05-17 31 views
0

我在使用裝配中的fgets讀取緩衝區時遇到了問題。 我知道數字等於ASCII值ATM,而不是問題。我無法獲得用戶輸入的實際價值。 在使用fgets之前,我需要輸出字符串「calc:」,我認爲它與我的真實輸入相混淆。在程序集中使用fgets接收輸入

section .rodata 

     CALC: DB "calc:" , 10 , 0   ;format string 

section .bss 
     buffer:   resb 80    ;store my input 

; ......更多的數據,不相關

push CALC      ;push string to stuck 
call printf    
add esp, 4      ;remove pushed argument 

push dword [stdin]    ;fgets need 3 param 
push dword 80     ;max lenght 
push dword buffer    ;input buffer 
call fgets 
add esp, 12      ;remove 3 push from stuck 

mov ecx, [buffer]    ;THIS IS FOR GDB 
stop_here: 

現在的問題是,在緩衝值不把輸入什麼用戶。當我用GDB調試器爲更好地瞭解我得到了以下結果:

input 0 --> ecx value is: 2608 (should be 48) 
input 1 --> ecx value is: 2609 (should be 49) 
input 10 --> ecx value is: 667697 (should be 48+49) 
input 34 --> ecx value is: 668723 (should be 51+52) 

編輯: 我用得到,而不是tryed,而現在它的作品! 可以有人請向我解釋爲什麼?!

回答

2

fgets也將終止換行存儲到緩衝區中,並且由於您正在將4個字節加載到ecx中,因此您也會看到這一點。

2608 = 0A30 hex = '0' LF 
667697 = 0A3031 hex = '1' '0' LF 

(請記住,86是小端)。

+0

喜,對答案的坦克,它可以幫助,但 – lolu

+0

可以解釋爲什麼它沒有與得到工作? – lolu

+0

是的,因爲'gets'不會將換行符放到緩衝區中:)請參閱'man gets':_gets()從stdin中讀取一行到s指向的緩衝區中,直到終止的換行符或EOF取代與一個空字節_ – Jester