【问题标题】:Sscanf not converting from command line parameter string to IEEE float Nasm x86 AssemblySscanf 未从命令行参数字符串转换为 IEEE 浮点 Nasm x86 程序集
【发布时间】:2013-12-09 05:45:54
【问题描述】:

首先我要感谢任何回复此问题的人,非常感谢您的帮助!

我正在尝试获取命令行参数(在下面的代码中,只有一个参数)并使用 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

再次感谢!

【问题讨论】:

    标签: assembly x86 command-line-arguments nasm scanf


    【解决方案1】:

    嗯,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,要么在调用sscanf 之前将该指针移动到ecx 但您仍然有问题,第一个参数是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
    

    【讨论】:

    • 我将推送参数的顺序更改为您所说的,并且有效。谢谢!
    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 2017-09-16
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多