Midterm #2 Review February 25, 2013 1 / 11 I will provide . . . a - - PowerPoint PPT Presentation

midterm 2 review
SMART_READER_LITE
LIVE PREVIEW

Midterm #2 Review February 25, 2013 1 / 11 I will provide . . . a - - PowerPoint PPT Presentation

Midterm #2 Review February 25, 2013 1 / 11 I will provide . . . a list of relevant instructions for assembly problems just the keywords and the names! you should know the difference between (and how to use) add , addi , sub ,


slide-1
SLIDE 1

Midterm #2 Review

February 25, 2013

1 / 11

slide-2
SLIDE 2

I will provide . . .

  • a list of relevant instructions for assembly problems
  • just the keywords and the names!
  • you should know the difference between (and how to use)
  • add,

addi, sub, subi

  • move,

la, li, lw, sw

  • blt,

ble, bgt, bge

  • j,

jr, jal

  • a diagram of the stack frame layout
  • same as in slides, but black & white

2 / 11

slide-3
SLIDE 3

You should bring . . .

  • a pencil and eraser
  • one page of notes (optional)
  • a calculator (optional and probably not needed)

3 / 11

slide-4
SLIDE 4

Structure of test

Part 1: Procedure call vocabulary

  • procedure, procedure call, jump-and-link, return address,
  • caller, callee, calling conventions,
  • the stack, stack pointer, stack frame, argument section
  • saved register section, local variable section

Part 2: Array addressing

  • For an array initialized in the data segment . . .
  • determine value given base address and offset
  • determine offset from the base address of a given value
  • Also, register values as pointers into an array

4 / 11

slide-5
SLIDE 5

Structure of test

Part 3: Implementing control structures

  • if-then, if-then-else
  • while loop, do-while loop, for loop

Part 4: Procedure calls

  • How to call a procedure, and return from it
  • How to push/pop a stack frame
  • How to pass and retrieve arguments
  • using registers and the stack
  • How to save and restore the return address on the stack

5 / 11

slide-6
SLIDE 6

Array addressing

Answer the questions in comments below

.data # the first ten powers of two (remember words are four bytes!) array: .word 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 .text la $t0, array # put base address into a register lw $t1, 4($t0) # value of $t1? 2 lw $t2, 20($t0) # value of $t2? 32 lw $t3, X($t0) # value of X so $t3=8? 12 lw $t4, Y($t0) # value of Y so $t4=64? 24 addi $t0, $t0, 8 # change base address pointer lw $t5, 0($t0) # value of $t5? 4 lw $t6, 12($t0) # value of $t6? 32

6 / 11

slide-7
SLIDE 7

Control structures – if-then statement

# Pseudocode: # if (3 + a >= b) { # a = a + b # } # Register mappings: # a => $t0 b => $t1

Strategy 1: Complement condition, branch on false

addi $t2, $t0, 3 # tmp = a + 3 blt $t2, $t1, end # if (tmp < b) goto end add $t0, $t0, $t1 # (then) a = a + b end: addi $t2, $t0, 3 # tmp = a + 3 bge $t2, $t1, end # if (tmp >= b) goto then j end # jump to end on condition true then: add $t0, $t0, $t1 # (then) a = a + b end:

7 / 11

slide-8
SLIDE 8

Control structures – for loop

# Pseudocode: # sum = 0 # for (i = 0; i < n; i++) { # sum = sum + i # } # Register mappings: # sum => $t0 i => $t1 n => $t2 loop: li $t0, 0 # sum = 0 li $t1, 0 # i = 0 bge $t1, $t2, end # while (i >= n) goto end add $t0, $t0, $t1 # (body) sum = sum + i addi $t1, $t1, 1 # (update) i++ j loop # (end of loop) end:

8 / 11

slide-9
SLIDE 9

Anatomy of a stack frame (for reference)

. . .          previous stack frame (arg 3) (arg 2) (arg 1) sp+fsize → (arg 0) sp+fsize-4 → local var m    local variable section . . . local var 0 (empty) ← padding (if needed) return address ← return address saved reg k    saved register section . . . saved reg 0 arg n                  argument section . . . 16+sp → arg 4 12+sp → (arg 3) 8+sp → (arg 2) 4+sp → (arg 1) sp → (arg 0) top of stack

9 / 11

slide-10
SLIDE 10

Procedures and argument passing

# Pseudocode: Register mappings: # int myProc(a,b,c,d,e,f,g) { a => $a0 b => $a1 # ... c => $a2 d => $a3 # x = subroutine() e => $t0 f => $t1 # return x g => $t2 # } myProc:

Procedure prologue

# retrieve args e, f, g from previous stack frame lw $t0, 16($sp) # retrieve e from prev stack frame lw $t1, 20($sp) # retrieve f from prev stack frame lw $t2, 24($sp) # retrieve g from prev stack frame # new stack frame, 6 words: # 4 word arg section, return address, padding addiu $sp, $sp, -24 # push new stack frame # save return address in previous stack frame sw $ra, 16($sp) # save return address

10 / 11

slide-11
SLIDE 11

Procedures and argument passing

# Pseudocode: Register mappings: # int myProc(a,b,c,d,e,f,g) { a => $a0 b => $a1 # ... c => $a2 d => $a3 # x = subroutine() e => $t0 f => $t1 # return x g => $t2 # } myProc:

Procedure epilogue

# restore return address in previous stack frame lw $ra, 16($sp) # save return address # pop stack frame addiu $sp, $sp, 24 # return jr $ra

11 / 11