将 nasm 输入浮点值组装到数组中

发布于 2024-10-22 05:38:56 字数 2474 浏览 1 评论 0原文

致力于将浮点值输入到数组中,然后将它们相加并得到总和,

到目前为止我有这个代码,但是我的标志检查输入是否为 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

working on inputing floating point values into an array, then adding them up and getting a sum

so far i have this code for it, but my flag that checks to see if an input is 0 to exit the loop for entering floats, keeps on going(endless loop)

so far I can read an input and output it immediately, but dont know how to store into array

    %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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2024-10-29 05:38:56

我不确定您的问题到底是什么(获得良好答案的提示:始终明确您要问的问题)但是...

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

  2. 来响应 prompt2 时终止循环的代码可能出了什么问题,因为该代码已不存在:-)。

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

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

I'm not sure exactly what your question is (hint for getting good answers: always make it clear what question you're asking) but ...

  1. You appear to be expecting your floating-point number to be in eax. Why would it be there? That's an integer register. Your getfloat routine puts the number it's read onto the floating-point stack.

  2. It's hard to tell what might have been wrong with your code for terminating the loop when 0 is entered in response to prompt2 since that code is no longer there :-).

  3. Obviously this isn't your problem, but you have jmp continue_loop immediately followed by the continue_loop label. Is this left over from an earlier version of the code?

  4. I think the stack offsets from which you're loading ebx and edx near the start of read_sarray32 are wrong; have you forgotten that call pushes the return address onto the stack? Specifically, I think that you're getting ebx = random nonsense and edx = 0; since your code is doing the equivalent of a do...while loop it'll run for ever -- or at least for 2^32 iterations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文