2011-03-17 81 views
0

的這個道理給我的邏輯,但它仍然給段錯誤的任何想法都是讚賞...裝配NASM,真正的整數數組的計算總和(浮點)

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 

回答

0

對於初學者來說,在比較EDI以0:

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

感謝您的答覆延, – John 2011-03-17 14:44:36

+0

現在我得到0的總和,什麼都沒有添加 – John 2011-03-17 14:44:54

+0

也許你應該張貼在這裏您將參數傳遞一個完整的例子。 – 2011-03-17 20:55:14

0

的基本思路是:

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 

爲了提高精度,你可能婉t對數組進行排序和/或按照從最小到最大的順序進行添加。

  • 布倫丹
0

addarray: PUSHA MOV EDI,0;初始化計數器爲0 MOV ESI,0;初始化ACCUM至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: 添加EBX,8 FLD qword的[EBX];該秒ond輸入現在在浮點寄存器中,具體爲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: 呼叫print_nl MOV EAX,summessage;安裝程序來顯示消息 呼叫print_string;博士。卡特圖書館

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]