maxflow
maxflow

Reputation: 939

Different between jump and jump and link

Could someone provide an example of the usefulness of the jal instruction can how it can be used to return to the main body after completing a subroutine?

regards

Upvotes: 25

Views: 62615

Answers (3)

tree
tree

Reputation: 239

Jump places a certain address in the program counter and continues execution from there, however, JAL(jump and link) does the same but it saves the return address in ra so that you can continue execution after your subroutine finishes ie:

int main() {
    dosomething();
    //code here
}


void dosomething() {
//.....code here
}

dosomething would be a subroutine that returns to the main block and continues executing

Upvotes: 8

Rocky Zhang
Rocky Zhang

Reputation: 91

jump and link will do the jump to the routine and store the program counter into GPR r31, and when the routine finishes MIPS uses 'jr r31' to return from that routine.

Upvotes: 2

paulsm4
paulsm4

Reputation: 121849

"jmp" is a "goto".

"jal" is a "gosub"

jal saves the return address in $ra, should you wish to return from whence you came ;)

http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm

Upvotes: 25

Related Questions