Figure 8.1 of course package

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

Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
There are two types of addressing schemes:
9/20/6Lecture 3 - Instruction Set - Al Instruction Set.
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.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
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.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
9/20/6Lecture 3 - Instruction Set - Al Instruction Set (2)
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
9/20/6Lecture 2 - Prog Model Architecture, Data Types and Addressing Modes.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Topics covered: Instruction Set Architecture CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
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.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
MicroProcessors Lec. 3 Dr. Tamer Samy Gaafar. Course Web Page
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
1 Contents: 3.1 Instruction format and Addressing Modes 3.2 Instruction Introduction Chapter 3 Instruction system.
ELE22MIC Lecture 6 Continuation of Lecture 5 Instruction Set Overview, Part 4 –HC-COM - Lab notes –Stack Pointer, Push, Pull Call/return Data –Conditional.
Computer Architecture & Operations I
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.
Displacement (Indexed) Stack
Computer Science 210 Computer Organization
Machine dependent Assembler Features
Instruction set Architecture
Overview of Instruction Set Architectures
ECE 3430 – Intro to Microcomputer Systems
© Craig Zilles (adapted from slides by Howard Huang)
Subroutines and the Stack
William Stallings Computer Organization and Architecture 8th Edition
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Chapter 3 Addressing Modes
Arithmetic using a stack
Lecture 3 - Instruction Set - Al
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
MIPS Instructions.
Subroutines and the Stack
Passing Parameters Data passed to a subroutine is called a parameter.
Chapter 8 Central Processing Unit
The University of Adelaide, School of Computer Science
by Richard P. Paul, 2nd edition, 2000.
68000 Architecture, Data Types and Addressing Modes
; main program ; main move Param2, -(sp) move Param1, -(sp) Call Sub1
ECEG-3202 Computer Architecture and Organization
Figure 8.1 of course package
Introduction to Micro Controllers & Embedded System Design
ECEG-3202 Computer Architecture and Organization
Subroutines … passing data
Subroutines and the Stack
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
© Craig Zilles (adapted from slides by Howard Huang)
Lecture 3 - Instruction Set - Al
Computer Organization
ECE511: Digital System & Microprocessor
(The Stack and Procedures)
Lecture 3 - Instruction Set - Al
Presentation transcript:

Figure 8.1 of course package Subroutines Subroutine suba Call suba Only one copy of the code is placed in memory Whenever we wish to use the code, a jump is made to it Jump to address of the first instruction of the subroutine Next instruction address should be saved before jump to subroutine is made Start of subroutine Next instruction Call suba Next instruction Jump back Call suba Next instruction Figure 8.1 of course package

Subroutine calls and returns main equ * ; ; first call move.l #next1, save_return ; save return address jmp suba ; jump to subroutine next1 … …….. ; this is where we continue … …….. ; second call move.l #next2, save_return ; save return address next2 … …….. ; this is where we continue ; third call move.l #next3, save_return ; save return address next3 … …….. ; this is where we continue ; Subroutine suba ; suba knows the symbols x and save_return ; suba equ * move.w x,d0 muls d0,d0 move.w d0,x lea save_return,a0 ;put the correct ;return address ;into a0 jmp (a0) ;return ;end of subroutine save_return ds.l 4 ;storage for return address Four extra instructions to implement subroutine. Programmer must explicitly save the return address before jumping to subroutine

Figure 2.24 [Hamacher] Subroutine linkage using a link register Memory location Calling program Memory location Subroutine SUB --- 200 Call SUB 1000 First instruction 204 next instruction ---- ---- ---- ---- Return Here, address of next instruction must be saved by the Call instruction to enable returning to Calling program 1000 PC Link Register Call Return 204 Note: Link Register is dedicated to save return address 204 204

Nested subroutines One subroutine calling another - if link register is used, its previous contents will be destroyed - it is therefore important to save it in some other location Stack should be used - list of similar items arranged in a structure, such that last item added is the first item removed – Last-in-First-out - Push an element onto stack - Pop an element from stack to remove - elements are either word or longwords Call instruction – push address of next instruction Return – pop return address Stack Pointer originally points to the beginning of the block of memory (Fig 8.2)

How to Call Subroutine Two instructions – jsr, bsr Jump to subroutine – jsr address (ex. jsr suba) operand is the Effective Address (specified as absolute address) Longword address of the next instruction is pushed on to the stack Stack is implicitly used when calling subroutines The EA specified is then used to jump to the subroutine Equivalent machine instruction is (see Fig 8.3): 4EB9 0040 0100

How to Call Subroutine Two instructions – jsr, bsr Branch to subroutine – bsr.b address bsr.w address (ex. bsr suba) Same as jsr, except signed displacement is added to PC Equivalent machine instruction is (see Fig 8.3): (bsr.b) 617E or (bsr.w) 6100 007E Machine instruction contains displacement, calculated using: Target address = PC + 2 + displacement

Return from Subroutine Two ways – rts, rtr Return from subroutine – rts - top of stack is popped off and loaded into PC Return and Restore – rtr - first pops a word from stack placing its low byte into CCR (condition code register) - PC is loaded with next two words popped If “rtr” is used to return, the subroutine should do the following immediately upon entry to subroutine: move.w SR, -(SP)

Ex: Calling and Returning from suba main equ * ; ; code to make call jsr suba ; first call next1 …. ……. ; this is where we continue after return …. ……. jsr suba ; second call next2 …. ……. ; this is where we continue after return jsr suba ; third call next3 …. ……. ; this is where we continue after return ; code of subroutine suba, notice that ; suba knows the symbol x ; suba equ * ;entry point move.w x,d0 muls d0,d0 move.w d0, x rts ; end of subroutine