the functions (procedures) in MIPS

后端 未结 3 490
眼角桃花
眼角桃花 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:13

    Firstly, you might want to check this quick MIPS reference. It really helped me.

    Secondly, to explain jal, jr and $ra. What jal does is jump to the label label and store the program counter (think of it as the address of the current instruction) in the $ra register. Now, when you want to return from label to where you initially were, you just use jr $ra.

    Here's an example:

    .text
    main:
    li $t0, 1
    jal procedure # call procedure
    li $v0, 10
    syscall
    
    procedure:
    li $t0, 3
    jr $ra # return
    

    You will notice when running this in a SPIM emulator that the value left in $t0 is 3, the one loaded in the so-called procedure.

    Hope this helps.

提交回复
热议问题