the functions (procedures) in MIPS

后端 未结 3 492
眼角桃花
眼角桃花 2020-12-28 08:21

I\'m new in MIPS language and I don\'t understand how the functions (procedures) in the MIPS assembly language work. Here are but I will specify my problem :

  1. <
3条回答
  •  借酒劲吻你
    2020-12-28 09:20

    1.the first two are instructions,the third it's kind of special register

    • jal=jump and link (Address of following instruction put in $ra,and jump to target address)
    • jr=jump to specify register
    • $ra=return address

    we often use the instruction like this ...

    • jr $ra (Copy $ra to program counter)

    it means return(jump) to the address saved in $ra .

    2.

    Here's an example function (procedure) in C

    int main(){
       x=addthem(a,b);
    }
    int addthem(int a, int b){
       return a+b;
    }
    

    function in MIPS

    .text
    main:    #assume value a is already in $t0, b in $t1
        add $a0,$0,$t0   # it's the same function as move the value
        add $a1,$0,$t1 
        jal addthem      # call procedure
        add $t3,$0,$v0   # move the return value from $v0 to where we want
        syscall
    
    addthem:
        addi $sp,$sp,-4     # Moving Stack pointer
        sw $t0, 0($sp)      # Store previous value
    
        add $t0,$a0,$a1     # Procedure Body
        add $v0,$0,$t0      # Result
    
        lw $t0, 0($sp)      # Load previous value
        addi $sp,$sp,4      # Moving Stack pointer 
        jr $ra              # return (Copy $ra to PC)
    

提交回复
热议问题