Subroutines and Stacks 1. Subroutines Separate, independent module of program, performs a specific task shortens code, provide reusable “tools” High-level.

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

1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Load and store instruction.
UEE072HM Linking HLL and ALP An example on ARM. Embedded and Real-Time Systems We will mainly look at embedded systems –Systems which have the computer.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Multiple data transfer instructions ARM also supports multiple loads and stores: ldm/ldmia/ldmfd: load multiple registers starting from [base register],
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
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.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Chapter 7 Interupts DMA Channels Context Switching.
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
Topics covered: ARM Instruction Set Architecture CSE 243: Introduction to Computer Architecture and Hardware/Software Interface.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
System Calls 1.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
MIPS coding. SPIM Some links can be found such as:
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Objective At the conclusion of this chapter you will be able to:
The Stack This is a special data structure: –The first item to be placed into the stack will be the last item taken out. Two basic operations: –Push: Places.
Execution of an instruction
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
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.
Instruction Sets: Addressing modes and Formats Group #4  Eloy Reyes  Rafael Arevalo  Julio Hernandez  Humood Aljassar Computer Design EEL 4709c Prof:
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Interrupt I/O (again!) Arithmetic using a stack.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Assembly Language Co-Routines
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
The Stack. ARMSim memory space: 0x Unused 0x x11400 Stack 0x x09400 Heap 0x?????-0x01400 Data 0x x????? Text 0x x01000.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Lecture 10: Load/Store cont. and Integer Arithmetic CS 2011 Fall 2014, Dr. Rozier.
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Multiple data transfer instructions ARM also supports multiple loads and stores: When the data to be copied to the stack is known to be a multiple of 4.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Writing Functions in Assembly
Displacement (Indexed) Stack
Computer Science 210 Computer Organization
Instruction set Architecture
ECE 3430 – Intro to Microcomputer Systems
The Stack.
Edexcel GCSE Computer Science Topic 15 - The Processor (CPU)
Introduction to the ARM Instruction Set
© Craig Zilles (adapted from slides by Howard Huang)
The Stack.
Architecture Review Instruction Set Architecture
Chapter 4 Addressing modes
Chapter 10 And, Finally... The Stack
Microcomputer Programming
William Stallings Computer Organization and Architecture 8th Edition
Writing Functions in Assembly
Chapter 10 The Stack.
Arithmetic using a stack
Chapter 8 Central Processing Unit
Processor Organization and Architecture
ARM Load/Store Instructions
Chapter 10 And, Finally... The Stack
ECE 3430 – Intro to Microcomputer Systems
ARM ORGANISATION.
Process.
Some Assembly (Part 2) set.html.
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Presentation transcript:

Subroutines and Stacks 1

Subroutines Separate, independent module of program, performs a specific task shortens code, provide reusable “tools” High-level languages typically have libraries of subroutines. The objective is to avoid “reinventing the wheel.” 2

Subroutine Modularity 3 Main Program Subroutine 2 Subroutine 1 Subroutine 3 Subroutine 2.1 Subroutine 3.1 Subroutine Sub. 3.2

Subroutine Techniques Techniques Keep them short Make them reusable (more on this coming) Main will frequently do nothing but call subroutines. Good subroutine Practice Independence: does not depend on other code, can be used in many programs. Registers: stores/restores key registers upon entrance/exit using Push and Pull commands (where?) Data and code location independent Use local variables, do not use hardcoded addresses. 4

Nothing But Subroutines Well written code will often consist of nothing but subroutines at the top: mainBLGetPosition BLCalcOffset BLDisplayData Using BL presumes the subroutine code is within 32MB of the calling routine. This is almost always a fair assumption. 5

The Stack Calls to subroutines require use of the stack. The stack is a piece of memory dedicated to temporary storage of run time variables What type of memory? RAM, ROM, DRAM, SRAM, register file…? The stack is always organized as a LIFO queue. Last In, First Out PUSH an element onto the stack. POP one off. Mostly used for saving/restoring machine register. 6

An example of stack operations 7

Load/Store Architecture Being a RISC machine, ARM processor does not have dedicated PUSH/POP instructions. Uses LDR/STR instead. Assembler may translate PUSH/POP mnemonics to load/store opcodes R13 is normally designated as the stack pointer (sp). Points to top of stack. Top can be either the next empty location or the last filled one. More flexibility than other architectures. 8

Push/Pull Multiple Going to a subroutine may require saving/restoring a lot of registers. ARM has LDM/STM instruction to push/pull multiple registers with one opcode. Actual reads/writes are still done sequentially. Can’t push/pull more than one register on any given clock cycle. Saves code space, saves fetching/decoding multiple load/store opcodes. 9

LDM/STM Load/Store multiple ops can transfer between 1 and 16 registers to/from memory. Only 32 bit words, not valid for bytes or half words Order of registers to be transferred can not be specified: reordering in transfer list will be ignored Lowest register number will be transferred to/from lowest address Contents of base register will be used to determine lowest address 10

LDM/STM Addressing Modes LDM and STM have special operating modes: IA: increment after IB: increment before DA: decrement after DB: decrement before Not for use with other instructions PUSH/POP mnemonics are the same as LDMIA and STMDB. 11

LDMIA Example Load r0,r2,r4 with memory data starting at address 0xBEEF0000. LDR r1,= 0xBEEF0000 LDMIA r1, {r0, r2, r4} ;mem 32 [r1]  r0 ;mem 32 [r1 + 4]  r2 ;mem 32 [r1 + 8]  r4 LDMIA r1, {r4, r0, r2}; EXACTLY the same r1 will remain unchanged. Effective addresses are generated on the fly. 12

STMDB Example Save values of r0,r2,r4 to the stack with top byte address 0xBEEF0000. LDR r1,= 0xBEEF0000 STMDB r1, {r0, r2, r4} ; r0  mem 32 [r1-4] ; r2  mem 32 [r1 - 8] ; r4  mem 32 [r1 - 12] STMDB r1, {r4, r0, r2}; EXACTLY the same r1 will remain unchanged. Effective addresses are generated on the fly. 13

What’s the Point? Can be used for block data transfers. Mostly used for context switches. Most common context switch is going to a subroutine. Save the current processor state so it can be restored after the subroutine. Can also be interrupt (to be covered shortly) or switching users/tasks in multi-threaded applications 14

What If You Want Base Register to Change? Previous examples all leave base address pointer alone. Effective address can be saved in it. Just append ! to base register. LDMIA r10!, r0 ; r10 will be incremented by 4 (one word) LDMIA r10! {r0-r3} ; r10 will be incremented by 16 15

Up/Down, Empty/Full Most processors have rigid definitions for stack ops. e.g., stacks always decrement on push, increment on pop. e.g., stack pointer always points to next address to be pushed. ARM leaves that up to the developer. Stack can grow up or down (i.e., address could increase or decrease) Stack pointer can point to next address to be filled or last address that was filled. 16

What kind of stack it is? 17

Up/Down, Empty/Full 18 r5 r1 r0 STMIA r9!, {r0, r1, r5} r9 final value: 1018 STMIA r9!, {r0, r1, r5} r9 final value: 1018 r5 r1 r C 1010 STMDA r9!, {r0, r1, r5} r9 final value: 1000 STMDA r9!, {r0, r1, r5} r9 final value: r1 r0 r5 r1 r0 STMIB r9!, {r0, r1, r5} r9 final value: 1018 STMIB r9!, {r0, r1, r5} r9 final value: 1018 STMDB r9!, {r0, r1, r5} r9 final value: 1000 STMDB r9!, {r0, r1, r5} r9 final value: 1000 In all cases, r9 starts out with 100C. If the ! was left out, r9 would always be 100C. Increasing

Up/Down, Empty/Full Stack can be full or empty Full – stack pointer (sp) points to last address written to Empty - stack pointer points to next available address Stack can be ascending or descending Ascending – memory address goes up as data push in Descending – memory address goes down as data push in How to use the stack? Normally, LDM/STM work together Store register values before executing subroutine tasks Load back original register values after subroutine tasks 19

How to use the stack operations? A stack can be Full descending (FD) Full ascending (FA) Empty descending (ED) Empty ascending (EA) Select a type, and be consistent. 20 Stack TypePUSHPULL Full descendingSTMFD (STMDB)LDMFD (LDMIA) Full ascendingSTMFA (STMIB)LDMFA (LDMDA) Empty descendingSTMED (STMDA)LDMED (LDMIB) Empty ascendingSTMEA (STMIA)LDMEA (LDMDB)

Subroutines The general structure of a subroutine in a program is: 21

Using stack in subroutines 22 … BL SUB1 … SUB1STMFDsp!,{r0-r2,r5} …;r0-r2, r5 are used in here …;may be altered by SUB1 LDMFDsp!,{r0-r2,r5} MOVpc, lr … BL SUB1 … SUB1STMFDsp!,{r0-r2,r5} …;r0-r2, r5 are used in here …;may be altered by SUB1 LDMFDsp!,{r0-r2,r5} MOVpc, lr … BL SUB1 … SUB1STMFDsp!,{r0-r2,r5} … BL SUB2;calls SUB2 in SUB1 …; LDMFDsp!,{r0-r2,r5} MOVpc, lr … BL SUB1 … SUB1STMFDsp!,{r0-r2,r5} … BL SUB2;calls SUB2 in SUB1 …; LDMFDsp!,{r0-r2,r5} MOVpc, lr anything wrong?

Subroutines Nesting Subroutine may call another subroutine or itself A routine that can call itself is “recursive.” Example: factorial Factorial(N) = N*factorial(N-1) Return address is stored in r14, which is transferred to pc when routine terminates. But if a subroutine is called recursively (or calls other subroutines), what happens? 23

Subroutine Nesting Main calls subroutine A. Return address to main is stored in r14 Subroutine A calls subroutine B. r14 is over written with new return address to A. Subroutine B finishes, r14 is transferred to PC. PC now points where? To the subroutine A. Start executing subroutine A again. Solution: Use the stack. Push link register before the next BL. Pop it upon return. Always push/pop link register value in subroutine 24

Previous Example 25 … BL SUB1 … SUB1STMFDsp!,{r0-r2,r5, lr} … BL SUB2;calls SUB2 in SUB1 …; LDMFDsp!,{r0-r2,r5, pc} MOVpc, lr;no longer necessary, why? … BL SUB1 … SUB1STMFDsp!,{r0-r2,r5, lr} … BL SUB2;calls SUB2 in SUB1 …; LDMFDsp!,{r0-r2,r5, pc} MOVpc, lr;no longer necessary, why? : BL SUB1 : BL SUB1 : STMFD sp!,{regs,lr} : BL SUB2 : LDMFD sp!,{regs,pc} STMFD sp!,{regs,lr} : BL SUB2 : LDMFD sp!,{regs,pc} SUB1 SUB2 : MOV pc, lr : MOV pc, lr

Parameter Passing Usually a subroutine needs to operate on some data that has been set by the calling program. For flexibility, it would be poor practice to ‘hardwire” the passed parameters. For example, square root function that only calculates the square root of 8 These data can be passed to the subroutine by “Value” or by “Reference.” 26

Call by Value Uses CPU registers: subroutine operates on the ‘values’ of the associated registers. Values used by the routine are “parameters.” Call by value means the parameters are held in certain registers. Any/all registers r0 – r12 may be used. ARM Application Procedure standard calls for them to be held in r0 – r3. If that’s not enough, use the stack, too. Calling program sets the registers, subroutine uses values found there. 27

Saving register values If the subroutine is going to operate on a register should that register be pushed before starting the subroutine? A subroutine may need registers for temporary storage, intermediate variables. Solution is to push any registers the routine will use, pop them when it’s done. Use SDM to save machine state, including link register. Use LDM to restore machine state, including link register (to program counter). 28

Call by Reference If the parameters are in large amount, stored in memory, use Call-by-reference. For example: get average of 100 grades listed in a table. Used to manipulate data in memory. Data are stored in memory consecutively The start address of the data block is passed to subroutine How are memory addresses passed? 29 By putting them in registers!

Call by Reference Example 30 startaddrEQU0x5000;start address of block to be scrambled stopaddrEQU0x6000;stop scrambling here LDRsp, =SRAM_BASE LDRr0, = startaddr LDRr1, =stopaddr BLscramble startaddrEQU0x5000;start address of block to be scrambled stopaddrEQU0x6000;stop scrambling here LDRsp, =SRAM_BASE LDRr0, = startaddr LDRr1, =stopaddr BLscramble What will happen if sp is not initialized?

ARM APC Application Procedure Call Standard sets guidelines for subroutine behavior. Allows multiple programmers to write routines that won’t corrupt other segments Says some registers may be changed, others must be preserved. Requires stack to be eight-byte aligned (top of stack address ends in 000) and full descending. Stack may be used for parameters and to preserve other register values that the routine would otherwise corrupt. 31

APC Registers 32 r0 r1 r2 r3 Pass parameters via r0 – r3. r4 r5 r6 r7 r8 r9 r10 r11 Register r4 –r 11 values must be preserved. r12r13 r12 can be changed by the routine and does not need to be restored. r14 r15 Registers 13, 14 and 15 are the stack pointer, link register and program counter. Their functioning does not change in subroutines.