ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560.

Slides:



Advertisements
Similar presentations
Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
Advertisements

Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
Code Composer Department of Electrical and Computer Engineering
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Lecture 6 Machine Code: How the CPU is programmed.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 1: Bits, bytes and a simple processor dr.ir. A.C. Verschueren.
TK 2633 Microprocessor & Interfacing
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Memory - Registers Instruction Sets
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Lecture 18 Last Lecture Today’s Topic Instruction formats
The M68HC11 Basic Instruction Set Basic Arithmetic Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
L12-Computing Factorial
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
ECE Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
ECE Lecture 1 1 L11-HLL to Assembler Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
ECE Lecture 1 1 L8:Flowcharting a program Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Ass Prof Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 6: Control Instructions.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
ECE Lecture 1 1 L13a – Integer add Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
ECE Lecture 1 1 The Hardware Multiplier Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer structure: Procedure Calls
Figure 8.1 of course package
ECE 3430 – Intro to Microcomputer Systems
Subroutines and the Stack
Chapter 7 Subroutines Dr. A.P. Preethy
Chapter 10 The Stack.
ECE 3430 – Intro to Microcomputer Systems
Final Exam Review Department of Electrical and Computer Engineering
Subroutines and the Stack
The University of Adelaide, School of Computer Science
L13b – 32 bit multiply Department of Electrical and
by Richard P. Paul, 2nd edition, 2000.
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
ECE 3430 – Intro to Microcomputer Systems
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
8051 ASSEMBLY LANGUAGE PROGRAMMING
Subroutines and the Stack
Some Assembly (Part 2) set.html.
Computer Organization and Assembly Language
(The Stack and Procedures)
Presentation transcript:

ECE Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Subroutines What are they How they can simplify a program An example Stacks Passing data on the stack How subroutine calls use the stack ECE Lecture 1 2

Stacks The stack – an area of memory for storage of data accessed through the PUSH and POP instructions ECE Lecture 1 3

The 430 stack After initialization – where is the stack? After initialization the value in the SP register (R2) is 0x0300 (can see this in Code Composer) Placing data on the stack Done using a PUSH(.B) instruction PUSH(.B) src Push the source onto stack Action is SP-2-> SP Removing data from the stack Done using a POP(.B) instruction POP(.B) dst Pop item from stack to destination Action dst SP+2 -> SP ECE Lecture 1 4

Also in code composer If you view memory it will show you data variables names at their location To see action of stack will do some push and pop actions while watching memory Will push values of 10 copies of $FFFF on the stack Then 10 pops to get the SP back to no data – pop into R4 Then push $0000,$0110,$0220,etc up to $0990 on the stack Again 10 pops back into R4 Also show what occurs when one is.B ECE Lecture 1 5

In Demo After initialization, the SP is set to value 0x0300. Remember that the stack grows down in memory and the SP register points to where the last value pushed on the stack is. So when a value is pushed on the stack it, the SP register is decremented by 2 and then the data is stored in memory. ECE Lecture 1 6

Binary Multiplication Need to perform integer multiplication of A*B. Will do this by simply adding up the value B, A times in a loop. multmovA,R5;A is in R5 movB,R6;B is in R6 clrR7;R7 to accumulate sum mlpaddR6,R7;add R6 to R7 decR5;A=A-1 jnemlp;repeat ;end R7=B*A As register is 16 bits and treated as 2’s complement, can do up to a 7-bit x 7-bit multiply for a 14 bit result. Number of times through the loop? 128 times max ECE Lecture 1 7

Now turn this into a subroutine Could code up the previous instructions each time you need to do a multiply OR create a routine to do it. The routine needs the data to multiply passed to it. Where to pass the data? Could do it in registers Could do it in memory Could do it on the stack Set up before call move A and B to stack use push instruction ECE Lecture 1 8

Why one an not the other? Why pass the data on the stack? Pass it in registers Works fine but program may be using registers for other tasks. Pass in variables in memory Works fine Pass on the stack Also works fine For a simple routine like this no real advantage of any method. Just shows how to do pass variables on the stack. Will look at subroutine for all three. ECE Lecture 1 9

Demo of routine – use registers Pick two register to pass data Pick R5, and R6 for numbers to be multiplied Pick R7 for the result Code it up – calling routine movA,R5;A is in R5 movB,R6;B is in R6 call#mult endjmpend ;the subroutine multclrR7;R7 to accumulate sum mlpaddR6,R7;add R6 to R7 decR5;A=A-1 jnemlp;repeat ret;end R7=B*A.data A.word3 B.word5 RES.word0 ECE Lecture 1 10

Subroutine call/return At end of subroutine code need a ret instruction. (see manual) RET Note action of  PC SP + 2  SP The CALL instruction action – again see manual. dst  tmp SP-2  SP PC tmp  PC (tmp is not a register you can see) ECE Lecture 1 11

Demo of routine – use memory Pick memory locations to pass data A and B are the data to be multiplied RES is the result Code it up – calling routine mov#3,&A mov#4,&B call#mult endjmpend ;the subroutine multclr&RES;RES to accumulate sum mlpadd&B,RES;add B to RES dec&A;A=A-1 jnemlp;repeat ret;end RES=B*A.data A.word3 B.word5 RES.word0 ECE Lecture 1 12

A third way – the stack First two methods work, BUT Limitations REGISTER USE is fixed Subroutine restricted to specific registers For subroutine to have no side effects, must protect any register it uses. ECE Lecture 1 13

The main and subroutine In Main program ;Setup for call movA,R5 movB,R6 pushR5 push R6 call#srmult ;after return have to cleanup SP will be pointing to B – result is A pop stack twice – 2 nd time is result position ECE Lecture 1 14

Subroutine Now in subroutine srmultmov2(SP),R6;B to R6 mov 4(SP),R5;A to R5 clrR7 mlpaddR6,R7 decR5 jnemlp;at end R7 is A*B movR7,4(SP);R7 for rtn ret;return from sr ECE Lecture 1 15

But the call can be a problem Have to make sure that call works correctly The first time this was done, call was transferring control all over the place or so it seemed. ;in main;subroutine movA,R5smultmov2(SP),R6 movB,R6mov4(SP),R5 pushR5ret pushR6 mov #smult,R4 callR4 popR7 pushR5 pushR6 call#smult;this code worked popR7 movR7,res ECE Lecture 1 16

Get code working When having problems get some code working and then build on it In this case will build the subroutine Demo ECE Lecture 1 17

Multiply routine The multiply routine is important as there is no multiply instruction Note that the routine is for positive integer multiplication only. With the algorithm implemented cannot multiply a negative number Both A and B and the result have to be between 0 and , positive integers ECE Lecture 1 18

Clean up the stack After returning from subroutine A ret simply pops the return address from the stack into the PC register So execution continues after the subroutine call If data was passed on the stack it needs to be cleaned up to retrieve the result and set the SP to before the call. ECE Lecture 1 19

The pop instruction The pop instruction removes the top item of the stack (the item currently pointed to by the SP register) and moves it to dst. POP(.B) dst Pop item from stack to destination Action dst SP+2 -> SP ECE Lecture 1 20

Cleanup on Return Same code – note the pop instructions that remove data and result from the stack ;in main;subroutine movA,R5smultmov2(SP),R6 movB,R6mov4(SP),R5 pushR5ret pushR6 mov #smult,R4 callR4 call#smult;this code worked popR7 movR7,res ECE Lecture 1 21

Subroutine side effects Subroutine should have no side effects!,i.e., change the values of registers or data the main program is using If subroutine should have no side effects then the state of machine (registers, memory, etc.) should be the same after the RTS as before the call. What does that mean? That means that subroutine needs to preserve any register that it will affect during execution of the subroutine. What are they? (refer to slide 11) CALL and RET do not affect SR Action in subroutine will alter sr bits, and here contents of R5, R6, and R7 ECE Lecture 1 22

Items to preserve The first item to preserve is the SR How to do it? Push it onto the stack. What else to save? Any register used. ECE Lecture 1 23

Saving of the SR (R(2)) Save the register you will use and SR. Becomes > State of SR upon return is same as it was before the call. ECE Lecture 1 24

Save other registers Subroutine used R5,R6,R7 – push on Stack ECE Lecture 1 25

What is the code? Note the change to the offsets of the passed arguments. Now subroutine has no effect on calling program. ECE Lecture 1 26

Assignment Code up a positive integer multiply subroutine as shown in this lecture Description of this is on the webpage and is assignment HW6 ECE Lecture 1 27