lecture 11 mips assembly language 4langer/273/11-slides.pdf · stack pointer stack pointer...

34
lecture 11 MIPS assembly language 4 - functions - MIPS stack February 15, 2016

Upload: others

Post on 17-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

lecture 11

MIPS assembly language 4

- functions

- MIPS stack

February 15, 2016

Page 2: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

MIPS registers already mentioned

new today

Page 3: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 4: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 5: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 6: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 7: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 8: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 9: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 10: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 11: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 12: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 13: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 14: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 15: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 16: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 17: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 18: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 19: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

main stores temporary registers before calling myfunction.(Here we assume variables m and n use $t0 and $t1.)

myfunction stores previous values of any save registers thatit will use. (Here we assume variable k uses $s0.)

Page 20: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

Stack pointerStack pointer (register $sp = $29)contains the lowest address of the stack.

Page 21: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 22: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 23: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 24: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 25: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 26: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 27: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 28: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 29: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 30: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

sumton: # if n == 0 then branch to base case # else push the two items onto the stack: # - return address ($ra) # - argument n ($a0) # # compute (n-1 ) argument for next call# jump and link to sumton (recursive)

# load the return address (pop) # load argument from the stack (pop) # change the stack pointer

# register $v0 contains result of sumton(n-1) # add argument n to $v0 # return to parent

basecase:# assign 0 as the value in $v0# return to parent

Page 31: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

sumton:beq $a0,$zero, basecase # if n == 0 then branch to base caseaddi $sp,$sp,-8 # else , make space for 2 items on the stacksw $ra, 4($sp) # store return address on stacksw $a0, 0($sp) # store argument n on stack

# (will need it to calculate returned value)addi $a0, $a0, -1 # compute argument for next call: n = n-1jal sumton # jump and link to sumton (recursive)

lw $ra, 4($sp) # load the return addresslw $a0, 0($sp) # load n from the stackaddi $sp, $sp,8 # change the stack pointer

# register $v0 contains result of sumton(n-1)add $v0, $a0, $v0 # add n to $v0jr $ra # return to parent

basecase:addi $v0,$zero,0 # assign 0 as the value in $v0jr $ra # return to parent

Page 32: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 33: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack
Page 34: lecture 11 MIPS assembly language 4langer/273/11-slides.pdf · Stack pointer Stack pointer (register $sp = $29) contains the lowest address of the stack

Announcements

A2 is due next Sunday at midnight.

A3 will be posted soon and will be due on the last day ofReading Week (~3 weeks from now)

A4 (last one) will be posted in mid-March and due at endof March.

Last lecture is April 13. Final exam is April 26 (tent.)