L21-AssemblyProg2

L21-AssemblyProg2 - Fall 2009 ECE 18-240 Structure and...

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

View Full Document Right Arrow Icon
Fall 2009 ECE 18-240 Structure and Design of Digital Systems LEC 21: Assembly Programming, Part 3 Bill Nace & Don Thomas Electrical & Computer Engineering Carnegie Mellon University LEC 21 F09 18-240 LEC21 — 18-240: Where are we. ..? ! 1 Handout: Lec21 " Lab 4 and HW6 starting up " Recitation: No quizzes 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
F09 18-240 LEC21 — Assembly Programming, part 3 ! What you know so far… " The P18240 complete architecture specification " Programmer’s view ! lots of assembly — both straight-line code and control flow ! simple stack stuff ! What you don’t know: Linkage " How to handle a real subroutine " Stack discipline ! Programmer rules for getting into and back from subroutines ! Supported, but not enforced by architecture " Stack frame ! what stack looks like after proper subroutine call Lec 21 3 Function Linkage call site fxn 4 ! A function (aka subroutine, procedure) is a reusable piece of code that you can branch to (aka call) " the caller stores the arguments at agreed-upon locations " the caller branches to the start of the function " the function computes the answer and stores the return value at some an agreed-upon location " the function branches back to where the caller left off (aka call site)
Background image of page 2
Function Linkage: Problems call site fxn call site ? 5 ! It’s just control flow, what is the big deal? ! Can’t you just branch to the function and branch back when the function is complete? ! NO! A function needs to be able to return to different call sites " How does the function know where to go back to? " the branch instruction we’ve seen can only goto a statically specified address ! Program State Issues " Which registers are safe from change by the function, which are safe to change by the function ! Recursion: what happens when a function calls itself? F09 18-240 LEC21 — The Stack Pointer (SP) 6 R0 R1 R2 R3 R4 R5 R6 R7 PC FFFB SP general purpose registers special purpose registers ALU Z N C V condition codes Data [15:0] Address[15:0] (16 bit words) b15 . .. b0 0000 0001 0002 0003 0004 2nd Free 1st Free Top Element 2nd Element FFFC 3rd Element FFFD 4th Element FFFE Oldest Element FFFF Memory P18240 Processor FFFB FFFA FFF9 FFF8 ! A 16-bit register " Contains the address of the newest element in the stack (the top) " Stack starts at $FFFF and grows down toward low memory " Special instructions manipulate the stack ! To initialize the stack " LDSP Rs — Load the stack pointer from register Rs, e.g., - LDI R0, $0 - LDSP R0
Background image of page 3

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

View Full DocumentRight Arrow Icon
Stack Instructions ! Push Rs " Register SP is decremented, and then used as the address to which register Rs is written " Suppose we “PUSH R3” and R3 = $1987, and SP = $FFFB Before After 0000 0001 2nd Free 1st Free Top Element 2nd Element FFFC 3rd Element FFFD 4th Element FFFE Oldest Element FFFF Memory FFFB FFFA FFF9 FFF8 FFFB SP 0000 0001 Next Free $1987 FFFC FFFD FFFE Oldest Element FFFF Memory FFFB FFFA FFF9 FFF8 FFFA SP Stack Instructions ! Pop Rd " Register SP is used as the address from which register Rd is loaded, SP is then incremented " Suppose we “POP R4” and R4 = $0000, and SP = $FFFA !
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

L21-AssemblyProg2 - Fall 2009 ECE 18-240 Structure and...

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

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