$ra寄存器中存入的是pc的值(程序运行处的地址),调用函数时,在跳转前,必须保存当前地址(pc的值),以便后来返回。jal $ra 保存后跳转,jr $ra,返回到跳转前,通过$ra保存进入上层栈地址。
jal 直接跳转指令,并带有链接功能,指令的跳转地址在指令中,跳转发生时要把返回地址存放到R31寄存器(ra)中。jr 使用寄存器的跳转指令,跳转地址在寄存器中。所以根据ra定位core位置时,可以直接在ra位置前找jal便可以找到当前正在执行的函数。
转自
• As in high level languages , when programming in assembly language you should split up your program into smaller functions, that you can reuse.
• One of the key ideas with functions is that you can call themfrom any where and return back to where you called the function from.• The MIPS processor has two instructions that enable you to call functions, jr and jal.• Jump and link. jal label Copies the address of the next instruction into the register $ra(register 31) and then jumps to the address label.• jr $register jumps to the address in $register most common use jr $ra.datastr: .asciiz "Hello mum!.\n" .text .globl main #necessary for the assembler main: jal message jal message li $v0,10 syscall #exit the program gracefully message: la $a0,str li $v0,4 syscall #Magic to printhings on the screen. jr $ra
• There are many way of passing values to functions, but there is a convention that most programs on the MIPS follow.
• $a0-$a3 (registers 4 to 7) arguments 1-4 of a function.• $v0-$v1 (registers 2 and 3) results of a function.
li $a0,10 li $a1,21 li $a3,31 jal silly #Now the result of the function is is $v0. li $v0,10 syscallsilly: add $t0,$a0,$a1 sub $v0,$a3,$t0 jr $ra