2011-03-17 6 views
0

이것은 이전 게시물의 확장입니다. 업데이트 된 코드는 입니다. 지금까지 배열에 마지막으로 입력 한 번호가 출력되므로 실제로 값을 합산하지 않았습니다.실제 배열 배열의 nasm 합계

;*************************************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 
+0

이 게시물에는 질문이 없습니다. – Mat

+1

확장 프로그램 인 경우 원본 질문에 게시해야합니다. 누군가는 내가 – jtpereyda

+0

질문입니다 , 나는 포스트를 편집했다, 지금 그것은 올바른 코드 다. – John

답변

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