MIPS jal指令:跳转目标与指令pc 0x400014的高位4位不同

时间:2015-02-03 07:32:00

标签: assembly mips

我知道我必须在某个地方跳得太大,但我无法确定问题所在。我怀疑我在代码中使用了太多寄存器。但程序规范说某些值必须存储在$ s0,$ s1和#s2中。我还必须使用参数寄存器和结果寄存器。使用的唯一临时寄存器是t0和t1。

伪代码:

Main:
int z = 37;
input x;
input y;
sum = procedure1(x,y);
output x, y, sum;

int procedure1 (int a, int b)
a = a-b;
output a;
total = a+3b;
return total;

以下是我的完整代码:

.data
zVar: .word 37
outputnewline: .asciiz "\n"
getInput: .asciiz "Enter an integer"
outproc1: .asciiz "procedure 1: "
outmain: .asciiz "Main: "
space: .asciiz " "

main:    
li $v0, 4            #prompt for x
la $a0, getInput
syscall

li $v0, 5            #read in x
syscall
move $s0, $v0        #store x in $s0
move $a0, $v0        #pass x using $a0

li $v0, 4            #start a new line
la $a0, outputnewline
syscall

li $v0, 4            #prompt for y
la $a0, getInput
syscall

li $v0, 5            #read in y
syscall
move $s1, $v0        #store y in $s1
move $a1, $v0        #pass y using $a1

jal procedure1

move $s2, $v0        #store sum in $s2

li $v0, 4            #print out "Main: "
la $a0, outmain
syscall

li $v0, 1            #print out x
move $a0, $s0
syscall

li $v0, 4            #print out " "
la $a0, space
syscall

li $v0, 1            #print out y
move $a0, $s1
syscall

li $v0, 4            #print out " "
la $a0, space
syscall

li $v0, 1
move $a0, $s2
syscall

li $v0, 4            #start a new line
la $a0, outputnewline
syscall

li $v0, 10
syscall              #exits the program

procedure1:
sub $t0, $a0, $a1    #a=a-b, store a in $t0
addi $sp, $sp, -12
sw $a0, 0($sp)       #put a on stack
sw $a1, 4($sp)       #put b on stack
sw $s0, 8($sp)       #put $s0 on stack

li $v0, 4            #print out "procdure 1: "
la $a0, outproc1
syscall

li $v0, 1            #print out a
move $a0, $t0
syscall

li $v0, 4            #start a new line
la $a0, outputnewline
syscall

lw $a0, 0($sp)       #restore a
lw $a1, 4($sp)       #restore b

add $t1, $zero, 3   #operand 3
mult $a1, $t1        # 3*b
mflo $s0             #store 3b in $s0
add $s0, $s0, $a0    #store a+3b in $s0
move $v0, $s0        #store total in $v0

lw $s0, 8($sp)
addi $sp, $sp, 8
jr $ra

0 个答案:

没有答案