为什么我的程序无法正常工作?

时间:2013-08-29 21:45:24

标签: macos assembly nasm sse

我正在Mac OS X上的NASM中编写汇编语言程序。我使循环正常工作,但SSE寄存器中的求和在打印之前没有保持其值。是因为它失去了rsp注册的焦点吗?这是汇编代码:

;Assemble: nasm -f macho64 -l Assignment1.lis -o Assignment1.o Assignment1.asm

extern _printf
extern _scanf
extern _getchar
global _findsum

segment .data

    welcome db "Welcome to Computing the Sum", 10, 0   
    askforfirst db "Enter the first number: ", 0
    askfornumber db "Please enter the next number: ", 0  
    askformore db "Do you have numbers to be entered (Y or N)? ", 0
    outputsum db "The sum of these inputs is %.18lf ", 10, 0
    outputaverage db "The average of these inputs is %.18lf ", 10, 0 
    goodbye db "Goodbye.", 10, 0

    floatformat db "%lf", 0
    stringformat db "%s", 0
    characterformat db "%c", 0

segment .bss

                                                                  ;Currently not in use

segment .text

_findsum:

    push rbp
    push rdi
    push rsi
    push r14
    push r15
    pushf
    pushf
    push rax
    push rax

    mov qword rax, 0
    mov rdi, stringformat
    mov rsi, welcome
    call _printf

    mov qword rax, 0
    mov rdi, stringformat
    mov rsi, askforfirst
    call _printf

    mov qword rax, 0
    mov rdi, floatformat
    mov rsi, rsp
    call _scanf

    movsd xmm0, [rsp]
    addsd xmm1, xmm0
    movsd xmm0, xmm1

    continue:

        push rax
        inc r14
        pop rax

        mov qword rax, 0
        mov rdi, stringformat
        mov rsi, askformore
        call _printf

        call _getchar

        mov qword [rsp], 0
        mov qword rax, 0
        mov rdi, characterformat
        mov rsi, rsp
        call _scanf

        pop r15
        cmp r15, 0x59

        je adding
        jne foundsum

    adding:

        mov qword rax, 0
        mov rdi, stringformat
        mov rsi, askfornumber
        call _printf

        mov qword rax, 0
        mov rdi, floatformat
        mov rsi, rsp
        call _scanf

        movsd xmm0, [rsp]
        addsd xmm1, xmm0
        movsd xmm0, xmm1

        push r15

        jmp continue

    foundsum:

        push qword 0
        mov qword rax, 1
        mov rdi, outputsum
        call _printf
        pop rax

        push qword 0
        mov qword rax, 1
        mov rdi, outputaverage
        call _printf
        pop rax

        mov qword rax, 0
        mov rdi, stringformat
        mov rsi, goodbye
        call _printf

    pop rax
    popf
    popf
    pop r15
    pop r14
    pop rsi
    pop rdi
    pop rbp

    mov qword rax, 2
    ret

这是终端中的输出:

Welcome to Computing the Sum
Enter the first number: 2
Do you have numbers to be entered (Y or N)? Y
Please enter the next number: 3
Do you have numbers to be entered (Y or N)? N
The sum of these inputs is 0.000000000000000000 
The average of these inputs is 0.000000000000000000 
Goodbye.
The result code is 2. Have a nice day.

我知道平均值尚未奏效。我只想先得到这笔钱。如果有人想帮助平均部分,请成为我的客人。任何事都可能有所帮助提前谢谢。

0 个答案:

没有答案