NASM x87浮点算术计算结果为0

时间:2016-03-29 04:05:04

标签: c assembly floating-point nasm x86-64

我是NASM大会的新成员,我不知道为什么我的汇编代码会返回0.有人可以提供帮助或建议吗?该代码基于 C 函数模拟点积:

double dot(long int n, double x[], double y[])
{
  double sum = 0.0;
  long int i;
  for(i=0; i<n; i++) sum = sum + x[i]*y[i];
  return sum;
} // end dot

带值

long int n = 4;
double x[4] = {2.0,  3.0, 4.0, 5.0};
double y[4] = {5.0, -3.0, 2.0, 4.0};
double sum;

链接到nasm x86 64位汇编代码:

    extern printf

    section .bss
fltstorage:      resq 1
tempflt:      resq 1

    section .data

dotMsg:         db "dot n= ", 0
fmt_float:      db "%f",10,0
fmt_dig:        db "%ld",10, 0 ; print just one digit, e.g. 0 or 1
fmt_end:        db 10, 0    ; just end line

sumOffset equ 32

    section .text
    global dot              ;for dot function

dot:
    push rbp                                ;push the stack
    mov rbp, rsp                            ;callers stack
    sub rsp, 8                              ;make space for local variables (one quadword)
    push rdi                                ;save registers
    push rsi
;calculations go here
                                            ;RBX is off-limits
    mov rsi, qword [rbp + 28]               ;RSI now contains third argument
    mov rdi, qword [rbp + 20]               ;RDI now contains second argument
    mov rcx, [rbp + 12]                     ;RCX should now contain first argument  (this is a failure point- check it later)

    mov r8, 0                               ;clear results storage

dot_loop:

    mov [fltstorage], rdi                   ;rdi is a pointer to element in x
    fld qword [fltstorage]                  ;nth index of x array to st0

    mov [fltstorage], rsi                   ;rsi is a pointer to element in y
    fmul qword [fltstorage]                 ;multiply ST0 by element from y
    add rdi, 8                              ;increment rdi, go to next element in x
    add rsi, 8                              ;increment rsi, go to next element in y

    mov [fltstorage], r8
    fadd qword [fltstorage]                 ;add cumulative sum to fresh value
    fstp qword [tempflt]                    ;move sum to storage
    mov r8, tempflt                         ;reduce rcx
    cmp rcx, 0                              ;check if it's at the end of the loop
    jg dot_loop                             ;if it is isn't,loop again

    push rdi
    push rsi
    push rax

    ;;;   print r8 to check output
    mov rdi, fmt_float
    mov rsi, r8
    mov rax, 0
    call printf

    pop rax
    pop rsi
    pop rdi

    mov rax, r8                             ;prep result for return
    add rsp, 8                              ;balance out rsp from subtraction up top


    pop rsi                                 ;restore registers
    pop rdi
    mov rsp, rbp                            ;restore initial stack
    pop rbp                                 ;restore B register
    ret

0 个答案:

没有答案