我有一个从用户接收整数的赋值,并将其输出为字符串(在MIPS中)。
我正在使用MARS,并收到错误:
程序计数器值无效:0x00000000。
我见过其他人提出这个问题,但答案通常与我正在使用的jal
有关。由于这是我的第一个汇编语言课程,我对编写MIPS代码并不是很有经验,所以我假设有一些我忽略的简单错误。
这是学校的作业,所以我没有要求答案 - 我只是在寻找一些方向,所以我可以正确地完成这个问题。另外,请不要使用比我在这里使用的更复杂的命令。 (我已经对代码进行了大量评论,所以我希望它能够轻松查看。) 谢谢;我很感激任何意见!
.data
InputPrompt: .asciiz "Enter an integer:"
Array: .space 16 #15 max space, at one byte each, plus a null terminator
.text
main:
la $a0, InputPrompt #prompt user to enter an integer
li $v0, 4 #print out the prompt
syscall
li $v0, 5 #load the given input into register v0
syscall
move $a1, $v0 #move the input to register a1 to save it there(param for sub-routine)
li $t1, 10 #load 10 into register t1 for division purposes
la $a0, Array #store first spot of array
jal getDigit
jal printString #after all digits are converted and stored in the array, call the print sub-routine
getDigit:
addi $sp, $sp, -4 #make room on stack
sw $ra, 0($sp) #save $ra
loop: beq $a1, $zero, return #if quotient has become 0, we are done
div $a1, $t1 #divide number by 10
mfhi $a2 #the modulus(remainder) of number/10 is here
mflo $a3 #the quotient is here
move $a1, $a3 #the new number is the quotient
jal convertDigit #call the ConvertDigit sub-routine, with the number in the a2 parameter register
j loop
lw $ra, 0($sp) #restore $ra
addi $sp, $sp, 4 #restore stack pointer
return: jr $ra
convertDigit:
addi $sp, $sp, -8 #make room on stack
sw $ra, 4($sp) #save $ra
addi $v1, $a2, 48 #convert to ascii
move $t3, $a0
sb $v1, 0($t3) #$t3 = Array[0] the first time, then gets incremented
addi $t3, $t, 1 #increment index of array
lw $ra, 0($sp) #restore $ra
addi $sp, $sp, 4 #restore stack pointer
jr $ra #return to getDigit sub-routine
printString:
lb $t1, Array($t2)
add $t2, $t2, 1
bne $t1, $zero, printString
sub $t2, $t2, 1
loop2: lb $t0, 0($a0) # load a single character from the array into $t0
li $v0, 11
syscall # print
addi $t0, $t0, 1 # add 1 to the loop index
blt $t0, $t1, loop2 # continue if not at string length
exit: li $v0, 10
syscall