【发布时间】:2021-04-05 18:53:00
【问题描述】:
该程序旨在允许用户将破折号('-' 和 '|')放入框,连接 3x3 网格的顶点。
因此,如果您愿意,可以将其称为初学者汇编程序员问题,但我在堆栈指针上存储三个或更多值时遇到问题。我想通过从堆栈指针中弹出返回地址从loop1返回到main;但是,在 checkMove 中,堆栈指针被添加了两次。
如何解决我的问题,以便我可以从 loop1 返回到 main?
.data #Data declaration segment
board: .ascii "\n\n . . . . 0 . 1 ."
.ascii "\n 2 3 4"
.ascii "\n . . . . 5 . 6 ."
.ascii "\n 7 8 9"
.asciiz "\n . . . . a . b .\n"
offset: .byte 6, 8, 33, 35, 37, 62, 64, 89, 91, 93, 118, 120
marker: .byte '-', '-', '|', '|', '|', '-', '-', '|', '|', '|', '-', '-'
.text #Text declaration statement
main:
li $t0, 6 #Number of loops/moves to make
jal loop1
#Print game board
li $v0, 4
la $a0, board
syscall
#Exit Program
li $v0, 10
syscall
loop1:
#Load return address onto stack pointer
subu $sp, $sp, 4
sw $ra, ($sp) #Store $ra into $sp
#Read integer for marking excluding a and b
li $v0, 5
syscall
move $a0, $v0 #Set $v0 to $a0
jal markMove #Jump to markMove; return address is set in $ra
sub $t0, $t0, 1 #Decrement counter by 1
bnez $t0, loop1 #Return to L1 if counter not 0
#Once counter is 0, pop return address off stack pointer
lw $ra, ($sp)
addu $sp, $sp, 4
jr $ra
#Mark a move on the game board
#Input : $a0 (Integer move 0-9)
markMove:
##Prepare to pass necessary variables ($v0, $ra) into function.
#Push return address ($ra) onto stack pointer
subu $sp, $sp, 4
sw $ra, ($sp) #Store $ra into $sp
#Push $t0 onto stack pointer
subu $sp, $sp, 4
sw $t0, ($sp) #Store $t0 into $sp
lb $t0, offset($a0)
#Actually mark now
lb $t1, marker($a0) #Find the marker
sb $t1, board($t0) #Place the marker at spot in board
#Clear stack pointer ($sp) and return to $ra
loop2:
##Prepare to take the variables ($v0, $ra) from above.
lw $t0, ($sp) #Pop $t0 off of stack pointer
addu $sp, $sp, 4
lw $ra, ($sp) #Pop $t0 off of stack pointer
addu $sp, $sp, 4
jr $ra #Return to return address; jump register
【问题讨论】:
标签: assembly return mips cpu-registers mars-simulator