【问题标题】:nasm sum of real number array, assembly实数数组的 nasm 和,程序集
【发布时间】:2011-03-18 09:58:19
【问题描述】:

这是我之前帖子的扩展,这里是更新后的代码, 到目前为止,它会输出我输入到数组中的最后一个数字,所以它实际上还没有对值求和

;*************************************ADD ARRAY**********************************************
segment .bss
sum     resq    1
segment .data
summessage  db  "The Sum is: ", 0
segment .text
extern readdouble,print_string, read_int, writedouble, print_nl, print_int
    global addarray
addarray:
    pusha
    mov edi, 0      ;initialize counter to 0
    mov esi, 0      ;initialize accum to 0
    ;mov    ecx, 0      ;zero out ecx and edx
    ;mov    edx, 0

    mov ebx, [ebp]  ;moves starting location of array1 into ebx
    mov edi, [ebp+12]   ;moves array size
add_loop:
    mov ecx, [ebx]  ;mov higher order
    mov edx, [ebx+4]    ;mov lower order

    push ecx
    push edx

    fldz
        fld     qword [esp]                    ;The second input is now in a floating point register, specifically st0.
        pop     dword ecx 
        pop     dword edx                      ;The first input is now on top of the system stack (the stack addressed by bytes)

    fadd    qword [esp]                    ;The first input is added to the second input and the sum
                                       ;replaces the second input in st0

    add ebx,8
    inc esi

    cmp edi, esi
    jz  add_done
    jmp add_loop
add_done:
    call    print_nl
    mov     eax, summessage                ;Setup to display a message
        call    print_string                   ;Dr. Carter's library

        push    dword 0                      ;Make space on sytem stack for the sum value
        push    dword 0                      ;Ditto
        fst     qword [esp]                    ;Copy contents of st0 to space currently on top of the system stack
        pop     edx                            ;Copy 4 MSBs to ecx
        pop     ecx                            ;Copy 4 LSBs to ecx

        call    writedouble                    ;Show the 8-byte value
        call    print_nl                       ;Newline

    popa
    ret

【问题讨论】:

  • 这篇文章没有问题。
  • 如果这是一个扩展,你应该在原始问题中发布这个。
  • 问题是,如果有人可以添加一些关于它为什么不求和并打印出正确的总和值的见解,截至目前它只打印出我输入到数组中的最后一个值跨度>
  • 对不起,我以为我发布了 addarray 程序,我编辑了帖子,现在它是正确的代码

标签: assembly nasm


【解决方案1】:

我猜想在循环中包含fldz 可能至少是您的问题的一部分。

您添加数字的循环看起来有点……对我来说很奇怪。我想我会使用更像这样的东西[警告:我主要使用 MASM,所以我在这里使用它的语法...]:

add_array proc 
    mov  esi, [ebp]
    mov  ecx, [ebp+12] ; I'm assuming these are the right offsets
    fldz
add_loop:
    fadd  qword ptr [esi]
    add   esi, 8
    dec   ecx
    jnz   add_loop
    fstp  result
    ret
add_array endp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2020-03-21
    相关资源
    最近更新 更多