EE357 Lecture 8

EE357 Lecture 8 - Recap:Branch Instructions Operation: (PC)...

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

View Full Document Right Arrow Icon
EE 357 Lecture 8 Subroutine Calling Conventions Recursive Routines Stack Frames Recap:Branch Instructions • Operation: (PC) + disp. ĺ PC • Branches allow us to jump backward or forward • Don’t forget the extended word for forward branches! – Extended word must be added if displacement does not fit in the byte inside the instruction word. label ---- ---- ---- ---- branch extended word ---- branch extended word ---- ---- label ---- Recap: Normal Branches vs. Subroutines • Difference between normal branches and subroutines branches is that with subroutines we have to return to where we left off void main() { ... x = 8; res = avg(x,4); ... } int avg(int a, int b){ return (a+b)/2; } C code: 1 Call “avg” sub-routine to calculate the average After subroutine completes, return to the statement in the main code where we left off 2 Recap: RTS instruction • Use RTS instruction to indicate that the subroutine is complete and we should return to where the routine was called • How do we know where to return to? Assembly: ... 8010 BSR.L AVG 8014 ... ORG $8800 AVG ... RTS 1 BSR will add a displacement to PC so new (PC) = 8800 How do we get back here? 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
Recap: Dealing with Return Addresses • We need to be able to store multiple return addresses • Return addresses will be accessed in reverse order as they are stored – 8804 is the second RA to be stored but should be the first one used to return Assembly: ... BSR.L SUB1 8014 ... ORG $8800 SUB1 BSR.L SUB2 8804 RTS ORG $8900 SUB2 ... RTS 1 2 3 4 Stacks Stack is a variable sized memory with a fixed origin •A stack pointer ( register), points to the most recently referenced location on the stack Two stack operations : push : – Place item at stack pointer location – Stack pointer adjusted by the size of the item Pop : – Remove item at stack pointer location – Stack pointer adjusted by the size of the data item. TOP vs BOTTOM of stack Stacks in MC68000 • Use a stack to store the return addresses • Stack is a data structure where data is accessed in reverse order as it is stored • System stack will use a specific area in memory and growing towards smaller addresses • Stack is accessed using A7 as a pointer – A7 is renamed SP (Stack pointer) 0000 0000 0000 3FEC 3FEE 3FF0 3FF2 3FF4 0000 3FF6 0000 3FF8 3FF8 (SP)= (A7)= Stack 0000 0000 Main memory Stack Pointer Always points to top occupied element of the stack 3FF8 is the bottom of the stack, but it will always be empty Operations on Stacks • 2 Operations on stack – Push: Put new data on top of stack • Decrement SP • Write value to where SP points – Pop: Retrieves and “removes” data from top of stack • Read value from where SP points • Increment SP to effectively “delete” top value – Always long word (address) new 3FF4 value 3FF6 0000 3FF8 3FF4 (SP)= Push will add a value to the top of the stack new 3FF4 value 3FF6 0000 3FF8
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

EE357 Lecture 8 - Recap:Branch Instructions Operation: (PC)...

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

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