Macos 为什么我的程序不能正常工作?
我正在Mac OS X上用NASM编写一个汇编语言程序。我使循环正常工作,但SSE寄存器中的求和在打印之前无法保持其值。是因为它失去了Macos 为什么我的程序不能正常工作?,macos,assembly,nasm,sse,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 we
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.
我知道平均值还不起作用。我只是想先把总数计算出来。如果有人想帮忙做一般的部分,请便。什么都可以。提前感谢。我怀疑是否有人会为您调试此程序-您只需启动gdb并开始单步执行代码,同时检查寄存器和变量的内容。这样你就可以修复你的bug,作为奖励,你在这个过程中也学到了很多东西。在我看来,你好像把一个零推到了堆栈上(
0
和rax
,按照前面的逻辑,它包含零),然后让printf
用格式字符串打印出来,“这些输入的总和是%.18lf”
。因此,输出是预期的。