L04_Functions in MIPS

L04_Functions in MIPS - Translate into MIPS /* a = array of...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Translate into MIPS /* a = array of int */ for(i = 0; i < 10; i++) { if(a[i]==a[i+1]) break; } Try this on your own: /* a = array of int */ for(i = 9; i >= 0; i--) { a[i+1]=a[i]; } a[0] = 0; la $t0, a addi $t1, $t0, 40 loop: beq $t0, $t1, done lw $t2, 0($t0) lw $t3, 4($t0) beq $t2, $t3, done addi $t0, $t0, 4 j loop done: la $t0, a addi $t1, $t0, 36 loop: beq $t1, $t0, done lw $t2, -4($t1) sw $t2, 0($t1) addi $t1, $t1, -4 j loop done: sw $0, 0($t1)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Control flow in MIPS MIPS uses the jump-and-link instruction jal to call functions jal saves the return address (the address of the next instruction) in the dedicated register $ra , before jumping to the function jal is the only MIPS instruction that can access the value of the program counter, so it can store the return address PC+4 in $ra To transfer control back to the caller, the function uses a jump-register instruction to the address that was stored in $ra : jr $ra Arguments to functions can be “passed” by placing them, by convention , in registers $a0 to $a3 Values are returned, by convention , in registers $v0 and $v1
Background image of page 2
3 Assembly language is untyped — there is no distinction between integers, characters, pointers or other kinds of values — You must “type check” your programs — Make sure your function arguments/return values are consistent
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

L04_Functions in MIPS - Translate into MIPS /* a = array of...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online