简体   繁体   English

汇编nasm,计算实整数数组的和(浮点数)

[英]assembly nasm, compute sum of an array of real integers(floats)

the logic of this make sense to me, however it's still giving a segfault any ideas are appreciated... 这种逻辑对我来说很有意义,但是任何想法都会受到赞赏……

addarray:
    push ebx
    push ebp
    push edi
    push ecx
    push esi
    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

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

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

    add ebx,8
    inc edi

    cmp esi, edi
    jz  add_done
    jmp add_loop
add_done:
    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 [ebx]                    ;Copy contents of st0 to space currently on top of the system stack
    pop     ecx                            ;Copy 4 MSBs to ecx
    pop     edx                            ;Copy 4 LSBs to ecx
    call    writedouble                    ;Show the 8-byte value
    call    print_nl                       ;Newline

    pop esi
    pop ecx
    pop edi
    pop ebp
    pop ebx
    ret

For starters, you are comparing edi to 0: 首先,您将edi与0进行比较:

mov esi, 0      ;initialize accum to 0
...
inc edi
cmp esi, edi

The basic idea would be: 基本思想是:

    fldz                       ;Load zero into ST0
    mov esi,<address_of_array>
    mov ecx,<number_of_entries>
.next:
    fadd dword [esi]           ;Add float (not double!) to ST0
    add esi,4                  ;esi = address of next float in array
    loop .next

For improved accuracy, you might want to sort the array and/or do the additions in order of smallest to largest. 为了提高准确性,您可能希望对数组进行排序和/或按最小到最大的顺序进行添加。

  • Brendan 布伦丹

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 addarray:pusha mov edi,0;将计数器初始化为0 mov esi,0;将accuming初始化为0 mov ecx,0;将ecx和edx归零mov edx,0

mov ebx, [ebp]  ;moves starting location of array1 into ebx
mov edi, [ebp+12]   ;move quantity into edi 
fld qword [ebx]

add_loop: add ebx,8 fld qword [ebx] ;The second input is now in a floating point register, specifically st0. add_loop:添加ebx,8 fld qword [ebx];第二个输入现在在浮点寄存器中,特别是st0。

fadd                 ;The first input is added to the second input and the sum
                     ;replaces the second input in st0
inc esi     ;increment counter

cmp edi, esi    ;compare to see if all values have been added
jz  add_done
jmp add_loop

add_done: call print_nl mov eax, summessage ;Setup to display a message call print_string ;Dr. add_done:调用print_nl mov eax,summessage;设置显示一条消息,调用print_string;博士。 Carter's library 卡特图书馆

add ebx, 8        ;increment to not overwrite any values
    fstp    qword [ebx]       ;Copy contents of st0 to space currently on top of the system stack
mov ecx, [ebx]
mov edx, [ebx+4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM