我有来自 CMU 架构实验室的以下 y86-64 程序,它应该总结链表的值。
# Adam Cooper ac251190
init:
.pos 0x0
irmovq Stack, %rsp # set up stack pointer
irmovq Stack, %rbp # set up base pointer
call Main
halt
# Sample linked list
.align 8
ele1:
.quad 0x00a
.quad ele2
ele2:
.quad 0x0b0
.quad ele3
ele3:
.quad 0xc00
.quad 0
Main:
irmovq ele1, %rax
pushq %rax # Pointer to list pushed to stack
call Sum
ret
Sum:
pushq %rbp # Push %rbp onto the stack
rrmovq %rsp, %rbp
mrmovq 8(%rbp), %rdx # Move ele1 into %rdx
irmovq $0, %rax # Set up a base to add eles to
andq %rdx, %rdx # Is this the end of the list?
je End # If it is, jump to the end
irmovq $8, %rcx # Turn %rcx into a index mover
Loop:
mrmovq (%rdx), %rbx # Move ls into %rbx
addq %rbx, %rax # val += ele
addq %rcx, %rdx # Move to next value in the list
mrmovq (%rdx), %rdx
andq %rdx, %rdx # Are we at the last ele?
jne Loop # If not, go again
End:
popq %rbp # TEAR! DOWN! THE STACK!
ret # Return the original call to Main
.pos 0x400
Stack:
"ADR"
程序在 line 处停止,状态为0x093
line
Loop:
mrmovq (%rdx), %rbx
现在,我被文档引导相信"ADR"
错误意味着程序试图访问高于 的地址0xFFF
,但事实并非如此。堆栈似乎也已正确初始化和设置。我使用了与我编写的其他几个运行良好的程序相同的方法。不太确定这里出了什么问题。