所以我试图使用MIPS将用户输入字符串的每个字符推入堆栈。我将字符串加载到内存中并尝试迭代它,将每个字符逐个保存到寄存器中,然后将该寄存器压入堆栈。我的问题是:
我设置为将每个字符保存到寄存器中的循环将加载第一个字符没问题,但随后它将开始加载不在字符串中的字符。如果用户输入" lol"作为字符串,程序将在第一个循环上加载108(小写l的ascii代码),但在下一个循环它将加载105(小写i),然后它将加载79(大写O)等。这是我一直在使用的循环:
.text
main:
li $v0, 4 # Prints str1
la $a0, str1
syscall
jal Init # Sets $s0 equal to $sp to compare if the stack is empty later
li $v0, 8 # Read String
la $a0, buffer # Loads memory buffer (100)
li $a1, 100 # Defines length of buffer
syscall
la $t0, buffer # Moves base register to $t0
PushLoop:
lb $a2, 0($t0) # Loads current character into $a2
beq $a2, 0, fin # if $a2 is equal to ascii code 10, the loop is terminated
jal Push # Pushes what is stored in $a0 to the stack
add $t0, $t0, -8 # Subtracts from buffer
j PushLoop
fin:
la $t0, buffer # Resets the memory buffer
Push:
addi $sp, $sp, -1 # Move stack pointer
sb $a2, ($sp) # Store contents of $a2 at ($sp)
jr $ra
我还应该提一下这是一个跟进问题:Push Each Character of a String Into a Stack MIPS