[英]nasm sum of real number array, assembly
this is an extension to my previous post, here is the updated code, so far it will ouput the last number I inputed into the array, so it's not actually summing values yet 这是我以前的帖子的扩展,这是更新的代码,到目前为止,它将输出我输入到数组中的最后一个数字,因此它实际上还不是求和值
;*************************************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
I'm going to guess that having fldz
inside the loop is probably at least part of your problem. 我猜想在循环中
fldz
可能至少是您问题的一部分。
Your loop to add the numbers looks a bit...strange to me. 您添加数字的循环对我来说有点...奇怪。 I think I'd use something more like this [warning: I mostly use MASM, so I used its syntax here...]:
我想我会使用更多类似的内容[警告:我主要使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.