1 / 27

Branches and Procedure Calls

Branches and Procedure Calls. Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007. Review: Instructions, Operands. Instructions add, sub, addi, and, lw, sw, etc. Operands Registers Memory Immediates (constants). Review: MIPS Register Set.

amie
Download Presentation

Branches and Procedure Calls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007

  2. Review: Instructions, Operands • Instructions • add, sub, addi, and, lw, sw, etc. • Operands • Registers • Memory • Immediates (constants)

  3. Review: MIPS Register Set

  4. Review: MIPS Memory

  5. MIPS Memory: Big- and Little-Endian Little-Endian: Big-Endian:

  6. Review: Instruction Formats

  7. Shift Instructions # shift left sll $t0, $s1, 2 # $t0 <= $s1 << 2 # shift right logical srl $t0, $s1, 2 # $t0 <= $s1 >> 2 # shift right arithmetic sra $t0, $s1, 2 # $t0 <= $s1 >>> 2

  8. Shift Instructions

  9. Review: The Stored Program

  10. Branching Conditional Branching: • branch if equal (beq) • branch if not equal (bne) Unconditional Branching: • jump (j) • jump register (jr) • jump and link (jal)

  11. Conditional Branching (beq) # MIPS assembly addi $s0, $0, 4 # $s0 = 0 + 4 = 4 addi $s1, $0, 1 # $s1 = 0 + 1 = 1 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4 beq $s0, $s1, target addi $s1, $s1, 1 sub $s1, $s1, $s0 target: add $s1, $s1, $s0 # $s1 = 4 + 4 = 8

  12. Conditional Branching (bne) # MIPS assembly addi $s0, $0, 4 # $s0 = 0 + 4 = 4 addi $s1, $0, 1 # $s1 = 0 + 1 = 1 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4 bne $s0, $s1, target addi $s1, $s1, 1 sub $s1, $s1, $s0 target: add $s1, $s1, $s0 # $s1 = 1 + 4 = 5

  13. Unconditional Branching (j) # MIPS assembly addi $s0, $0, 4 # $s0 = 4 addi $s1, $0, 1 # $s1 = 1 j target # jump to target sra $s1, $s1, 2 # not executed addi $s1, $s1, 1 # not executed sub $s1, $s1, $s0 # not executed target: add $s1, $s1, $s0 # $s1 = 1 + 4 = 5

  14. Unconditional Branching (jr) # MIPS assembly 0x00002000 addi $s0, $0, 0x2010 0x00002004 jr $s0 0x00002008 addi $s1, $0, 1 0x0000200C sra $s1, $s1, 2 0x00002010 lw $s3, 44($s1)

  15. High-Level Code Constructs • If/Else Statements • While Loops • For Loops

  16. If Statement // high-level code if (i == j) f = g + h; f = f - i; // MIPS assembly # $s0 = f, $s1 = g, $s2 = h, $s3 = i, $s4 = j

  17. If / Else Statement // high-level code if (i == j) f = g + h; else f = f - i; // MIPS assembly # $s0 = f, $s1 = g, $s2 = h, $s3 = i, $s4 = j

  18. While Loops // high-level code int result = 5; while (result > 0) { result = result - 1; } // MIPS assembly # $s0 = result

  19. For Loops // high-level code int sum = 0; for (i=0; i != 10; i = i+1) sum = sum + i; // MIPS assembly # $s0 = i, $s1 = sum

  20. For Loops: Using slt // high-level code int sum = 0; for (i = 1; i < 101; i = i * 2) sum = sum + i;

  21. For Loops: Using slt # MIPS assembly code # $s0 = i, $s1 = sum addi $s1, $0, 0 # sum = 0 addi $s0, $0, 1 # i = 1 addi $t0, $0, 101 # $t0 = 101 loop: slt $t1, $s0, $t0 # if (i < 101) $t1 = 1, else $t1 = 0 beq $t1, $0, done # if $t1 == 0 (i >= 101), branch to done add $s1, $s1, $s0 # sum = sum + i sll $s0, $s0, 1 # i = i * 2 j loop done:

  22. Procedure Calls Definitions • Caller: calling procedure • Callee: called procedure How it works • Caller needs to call a procedure without any unintended side effects • Callee returns to point of call (place it was called from) when done • Caller passes arguments to callee • Callee returns result to caller

  23. Procedure Calls // high-level code int main() { simple(); ... } // void means the function returns no value void simple(){ return; }

  24. Procedure Calls # MIPS assembly code 0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2 ... 0x00401020 simple: jr $ra

  25. Input Arguments, Return Values // high-level code int main() { int y; ... y = diffofsums(2, 3, 4, 5); ... } int diffofsums(int f, int g, int h, int i) { int result; result = (f + g) - (h + i); return result; }

  26. Input Arguments, Return Values # MIPS assembly # $s0 = y main: ... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call procedure add $s0, $v0, $0 # y = returned value ... # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

  27. Next Time • More on Procedures • Addressing Modes • Linking and Launching Applications

More Related