2013-12-09 10 views
0

首先我要感謝任何回覆此消息的人,非常感謝您的幫助!Sscanf不能從命令行參數字符串轉換爲IEEE浮點數Nasm x86程序集

我想獲得命令行參數(在下面的代碼中,只有一個參數),並使用sscanf()將它們轉換爲浮點數。我將要存儲浮點數的位置地址(temp)傳遞給我希望轉換的字符串(dword [ecx + 4])的指針和格式字符串。但是,在調用sscanf()之後,我發現我初始化temp的值不變。顯然,我正在使用sscanf()。我究竟做錯了什麼?

EXTERN printf 
EXTERN sscanf 
GLOBAL main 

SEGMENT .data 
formd:  DB "%d", 10, 0 
formf:  DB "%f", 10, 0 
format:  DB "%f", 0 
temp:  DD 1.1235     ; where I want the number to go, initialized with some arbitrary non zero number 

SEGMENT .text 
main: 
     push ebp 
     mov  ebp, esp 
     mov  ebx, [ebp + 8]   ; number of params 
     mov  ecx, [ebp + 12]   ; &(parameter table) 

     pushad       ; print out the number of parameters 
     push ebx     
     push formd 
     call printf 
     add  esp, 8 
     popad 

     pushad       ; get number 
     push temp     ; pass the address of the destination 
     push dword [ecx + 4]   ; pass the first command line parameter 
     push format     ; pass the format string 
     call sscanf     ; convert it to floating point 
     add  esp, 12 
     popad 

     pushad       ; print out temp with FPU 
     finit 
     fld  dword [temp] 
     sub  esp, 8 
     fstp qword [esp] 
     push formf 
     call printf 
     add  esp, 12 
     popad 

     pop  ebp 
     ret 

再次感謝!

回答

0

那麼,你有參數sscanf切換。

您有:

 push temp     ; pass the address of the destination 
     push dword [ecx + 4]   ; pass the first command line parameter 
     push format     ; pass the format string 
     call sscanf     ; convert it to floating point 

formatdword [ecx + 4]應該被交換!另外,你把參數表的指針放入ecx,但是ecx是一個易失性寄存器,所以printf會把它打掉。所以,要麼把[ebp + 12]到非易失性寄存器這樣的廣告esi或只是MOV該指針爲ecx該呼叫之前sscanf但你仍然有問題,第一個參數是exe文件的名稱,而不是你通過數...

拳頭參數將+ 4 ..試試這個:

mov  ecx, [ebp + 12] 
    push temp     ; pass the address of the destination 
    push format   ;pass the format string 
    push dword [ecx + 4] ;; pass the first command line parameter     ; 
    call sscanf     ; convert it to floating point 
    add  esp, 12 
+0

我改變了我推送參數到你說的,並且它的工作的順序。謝謝! – Tom