L4 Instructions_3_ - Topic 4 MIPS Instruction Set -...

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

View Full Document Right Arrow Icon
Topic 4 MIPS Instruction Set - Assembly Program
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter 2 — Instructions: Language of the Computer — 2 Procedure (Function) Calling ± Used to improve reusability and manageability ± Steps required 1. Place parameters in registers 2. Transfer control to procedure 3. Acquire storage for procedure 4. Perform procedure’s operations 5. Place result in register for caller 6. Return to place of call
Background image of page 2
Chapter 2 — Instructions: Language of the Computer — 3 Procedure Call Instructions ± Procedure call: jump and link jal ProcedureLabel (J-type) ± $ra = PC+4; Address of following instruction put in $ra ± PC=ProcedureLable; Jumps to target address ± Procedure return: jump register jr $ra (R-type) ± PC = $ra; Copies $ra to program counter ± Can also be used for computed jumps (to any other register) ± e.g., for case/switch statements
Background image of page 3

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

View Full DocumentRight Arrow Icon
Chapter 2 — Instructions: Language of the Computer — 4 Leaf Procedure ± Procedures that don’t call other procedures ± C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } ± Arguments g, …, j in $a0, …, $a3 ± f in $s0 (hence, need to save $s0 on stack) ± Result in $v0
Background image of page 4
Chapter 2 — Instructions: Language of the Computer — 5 Register Usage ± $zero: constant 0 (reg 0) ± $at: Assembler Temporary (reg 1) ± $v0, $v1: result values (reg’s 2 and 3) ± $a0 – $a3: arguments (reg’s 4 – 7) ± $t0 – $t9: temporaries (reg’s 8 – 15) ± Can be overwritten by callee ± $s0 – $s7: saved (reg’s 16 – 23) ± Must be saved/restored by callee ± $t8, $t9: temporaries (reg’s 24 and 25) ± $k0, $k1: reserved for OS kernel (reg’s 26 and 27) ± $gp: global pointer for static data (reg 28) ± $sp: stack pointer (reg 29) ± $fp: frame pointer (reg 30) ± $ra: return address (reg 31)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Chapter 2 — Instructions: Language of the Computer — 6 Leaf Procedure Example ± MIPS code: leaf_example: addi $sp, $sp, -12 sw $t1, 8($sp) $t0, 4($sp) sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) lw addi $sp, $sp, 12 jr $ra Unnecessary
Background image of page 6
Chapter 2 — Instructions: Language of the Computer — 7 Leaf Procedure Example Before During -Push After -Pop
Background image of page 7

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

View Full DocumentRight Arrow Icon
Chapter 2 — Instructions: Language of the Computer — 8 String Copy Example ± C code: ± Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') i += 1; } ± Addresses of x, y in $a0, $a1 ± i in $s0
Background image of page 8
Chapter 2 — Instructions: Language of the Computer — 9 String Copy Example ± MIPS code: strcpy: addi $sp, $sp, -4 # adjust stack for 1 item sw $s0, 0($sp) # save $s0 add $s0, $zero, $zero # i = 0 L1: add $t1, $s0, $a1 # addr of y[i] in $t1 lbu $t2, 0($t1) # $t2 = y[i] add $t3, $s0, $a0 # addr of x[i] in $t3 sb $t2, 0($t3) # x[i] = y[i] beq $t2, $zero, L2 # exit loop if y[i] == 0 addi $s0, $s0, 1 # i = i + 1 j L1 # next iteration of loop L2: lw $s0, 0($sp) # restore saved $s0 addi $sp, $sp, 4 # pop 1 item from stack jr $ra # and return
Background image of page 9

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

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

Page1 / 36

L4 Instructions_3_ - Topic 4 MIPS Instruction Set -...

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

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