Unit8-Subroutines&Stacks-EE357-Nazarian-Fall09

Unit8-Subroutines&Stacks-EE357-Nazarian-Fall09 -...

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

View Full Document Right Arrow Icon
University of Southern California Viterbi School of Engineering EE357 asic Organization of Computer Systems Basic Organization of Computer Systems ubroutines Subroutines Stacks References: 1) Textbook 2) Mark Redekopp’s slide series Shahin Nazarian Fall 2009 3) Freescale documents on CF
Background image of page 1

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

View Full DocumentRight Arrow Icon
Subroutines Subroutines (or functions) are portions of code that we can call from anywhere in our code, execute that subroutine, and then return to where we left off oid main() { C code: void main() { ... x = 8; We call the subroutine to calculate the average res = avg(x,4); ... } subroutine to int avg(int a, int b){ return (a+b)/2; calculate the average of 2 numbers Shahin Nazarian/EE357/Fall 2009 2 }
Background image of page 2
Subroutines Subroutines are similar to branches where we jump to a new location in the code void main() { C code: ... x = 8; es = avg(x ; res = avg(x,4); ... } 1 Call “avg” sub-routine will require us to branch that code int avg(int a, int b){ return (a+b)/2; } to that code Shahin Nazarian/EE357/Fall 2009 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Normal Branches vs. Subroutines Difference between normal branches and subroutines is that subroutines automatically return to location after the subroutine call void main() { C code: ... x = 8; es = avg(x ; res = avg(x,4); ... } 1 Call “avg” sub-routine to calculate the average fter subroutine int avg(int a, int b){ return (a+b)/2; } completes, return to the statement in the main code 2 Shahin Nazarian/EE357/Fall 2009 4 where we left off
Background image of page 4
Implementing Subroutines To implement subroutines in assembly we need to be able to: ranch to the subroutine code (BSR/JSR instruc ) Branch to the subroutine code (BSR/JSR instruc.) Return to the instruction after BSR when we finish the subroutine (RTS instruc.) ... C code: Assembly: ... res = avg(x,4); ... BSR.W AVG ... org 800 int avg(int a, int b) { . .. } .org 0x0800 AVG: ... RTS Shahin Nazarian/EE357/Fall 2009 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Branching to a Subroutine Use BSR instruction (B ranch S ubR outine) Format: BSR.{S,W,L} Addr Similar to branches we still add a displacement lue to the PC value to the PC [e.g. PC + disp PC] S,W,L refers to 8-, 16-, or 32-bit displacement lues similar to normal branches values similar to normal branches Addr is the address you want to branch to Usually specified as a label Automatically stores the return address ( RA ) for use by the RTS instruction Shahin Nazarian/EE357/Fall 2009 6
Background image of page 6
BSR & RTS Branch Formula: disp. = Addr of Label – (Addr. of Branch + 2) = 0x0800 – (0x0010+2) = 0x07EE Use RTS instruction to indicate that the subroutine is complete and we should return to where the routine was alled ... called Assembly: (.text+0x10) BSR.W AVG (.text+0x14) ... RTS loads the 3 1 BSR will add displacement 0x07EE to PC to get the new PC = .text+0x0800 .org 0x0800 AVG: ... TS PC with the return address stored by the BSR 2 BSR will also store the return address: .text+0x14 for use by the RTS instruction Shahin Nazarian/EE357/Fall 2009 7 RTS
Background image of page 7

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

View Full DocumentRight Arrow Icon
Jumping to a Subroutine Format: JSR Addr Rather than storing a displacement to add to the PC like BSR does, JSR simply stores the start address of the subroutine [e.g. PC = Addr.] Addr is the address you want to branch to Usually specified as a label yp f
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

Unit8-Subroutines&Stacks-EE357-Nazarian-Fall09 -...

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

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