【问题标题】:assembly nasm input float values into array将 nasm 输入浮点值组装到数组中
【发布时间】:2011-03-14 22:57:52
【问题描述】:

将浮点值输入到数组中,然后将它们相加得到一个和

到目前为止,我有此代码,但我的标志检查输入是否为 0 以退出循环以输入浮点数,继续进行(无限循环)

目前我可以读取输入并立即输出,但不知道如何存储到数组中

    %include "asm_io.asm"

segment .bss
array1      resd    20
segment .data
done        db  "That was ridiculously incredible! Bye!", 0
segment .text
    extern puts, _printf, scanf, dump_line, stack_dump, geomean
    global asm_main

asm_main:
    enter 0,0
    pusha
;Declare Array:
    push 5
    push 0
    push array1

    call    read_sarray32

    ;call getfloat
    ;call putfloat

    xor ebx, ebx
    mov eax, 1
    int 80h

    mov eax, done
    call    print_string

    popa
    leave
    ret

;beginning of get float
;*******************************getFLoat:*****************************
segment .bss
;
segment .data
    fmt1        db "%lf", 0
    enterNumber     db "Enter Your Float Number: ", 0
segment .text
getfloat:
    push ebp
    mov eax, enterNumber
    call    print_string
    mov ebp, esp
    sub esp, 8
    lea eax, [ebp-8]
    push eax
    push fmt1
    call scanf
    add esp, 8

    fld qword [ebp -8]
    mov esp, ebp
    pop ebp
    ret
;Beginning of putfloat
;************************putfloat:*****************************************
segment .bss
;
segment .data
    fmt2 db 10, "The number is: %f", 10, 10
segment .text
putfloat:
    push ebp
    mov ebp, esp
    sub esp, 8

    fst qword [ebp - 8]
    push fmt2
    call printf
    add esp, 12

    mov esp, ebp
    pop ebp
    ret
;*********************READ array*************************************************
segment .bss
;
segment .data
prompt2     db  "Do you have more inputs? (-1 = yes,0 = no)?: ", 0

segment .text
read_sarray32:
    pusha
    mov ebx, [esp+36]   ;move starting position into ebx
    mov edx, [esp+40]   ;move max size into edx
    mov ecx, 0
read_loop:      
    ;mov    eax, prompt2
    ;call   print_string
    ;call   read_int
    call    getfloat
    inc     ecx;        increment counter

    call    print_nl
    call    putfloat
    call    print_nl
    cmp eax, 0  
    jz  Done
    jmp continue_loop
continue_loop:
    mov     [ebx], eax  ;move value into memory slot ebx
    add ebx, 4      ;move to next location for db word

    cmp ecx, edx    ; did i reach maximum values of array size?
    jz  Done
    jmp read_loop
Done:
    call    putfloat
    sub ecx, 1      ;to offset the 0 that was entered to exit loop
    mov [esp+44], ecx   ;moves counter back to stack
    popa
    ret

【问题讨论】:

    标签: assembly nasm


    【解决方案1】:

    我不确定你的问题到底是什么(提示获得好的答案:总是清楚你在问什么问题)但是...

    1. 您似乎希望您的浮点数位于eax 中。为什么会在那里?那是一个整数寄存器。您的getfloat 例程将读取的数字放入浮点堆栈。

    2. 当输入 0 以响应 prompt2 时,很难判断终止循环的代码可能出了什么问题,因为该代码不再存在 :-)。

    3. 显然这不是你的问题,但你有jmp continue_loop 紧跟continue_loop 标签。这是早期版本的代码遗留下来的吗?

    4. 我认为您在read_sarray32 开头附近加载ebxedx 的堆栈偏移量是错误的;你忘了call 将返回地址压入堆栈吗?具体来说,我认为你得到ebx = 随机废话和edx = 0;因为您的代码正在执行相当于 do...while 循环的操作,所以它将永远运行——或者至少运行 2^32 次迭代。

    【讨论】:

    • 您好,感谢您的回复,我能够弄清楚如何读取带有浮点数的数组并将它们打印出来。
    • 现在我正在研究如何计算当前数组中浮点值的总和...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多