Systems Architecture The Stack and Subroutines

Similar documents
ARM Instruction Set Architecture. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

ECE251: Tuesday September 11

Multiple data transfer instructions

ECE 471 Embedded Systems Lecture 5

ARM Assembly Programming

ECE251: Tuesday September 12

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

F28HS Hardware-Software Interface. Lecture 10: ARM Assembly Language 5

Stacks and Subroutines

ARM Architecture and Instruction Set

NET3001. Advanced Assembly

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

ARM Assembly Language

C-Style Strings. CS2253 Owen Kaser, UNBSJ

Subroutines & Traps. Announcements. New due date for assignment 2 5pm Wednesday, 5May

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

From last time. What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3

Introduction to the ARM Processor Using Altera Toolchain. 1 Introduction. For Quartus II 14.0

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Support for high-level languages

ARM Assembly Language. Programming

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Introduction to the ARM Processor Using Intel FPGA Toolchain. 1 Introduction. For Quartus Prime 16.1

Bonus Lecture: Fibonacci

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

ECE251: Tuesday September 18

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

ARM Assembler Workbook. CS160 Computer Organization Version 1.1 October 27 th, 2002 Revised Fall 2005

ARM Instruction Set. Computer Organization and Assembly Languages Yung-Yu Chuang. with slides by Peng-Sheng Chen

Tutorial 1: Programming Model 1

Computer Systems Lecture 9

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

ARM Instruction Set. Introduction. Memory system. ARM programmer model. The ARM processor is easy to program at the

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Chapters 5. Load & Store. Embedded Systems with ARM Cortex-M. Updated: Thursday, March 1, 2018

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

Subroutines and the Stack

Lab 4 : MIPS Function Calls

ECE 471 Embedded Systems Lecture 8

EE 361 University of Hawaii Fall

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Chapter 10 And, Finally... The Stack. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

ARM Assembly Programming II

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Subroutine. Chapter 8

Interrupt-Driven Input/Output

Chapter 15. ARM Architecture, Programming and Development Tools

Developing StrongARM/Linux shellcode

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Assembly Language Programming

Chapter 15. ARM Architecture, Programming and Development Tools

ARM Cortex-A9 ARM v7-a. A programmer s perspective Part 2

ECE 498 Linux Assembly Language Lecture 5

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

Architecture. Digital Computer Design

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

MNEMONIC OPERATION ADDRESS / OPERAND MODES FLAGS SET WITH S suffix ADC

Programming Model 2 A. Introduction

button.c The little button that wouldn t

Instruction Set Architectures (4)

F28HS2 Hardware-Software Interface. Lecture 7: ARM Assembly Language 2

Registers. Registers

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Lab 8 The Stack (LIFO) Structure

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack

ECE232: Hardware Organization and Design

Embedded Operating Systems

ARM PROGRAMMING. When use assembly

ARM-7 ADDRESSING MODES INSTRUCTION SET

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Systems Architecture The ARM Processor

The ARM instruction set

The PAW Architecture Reference Manual

Central Processing Unit

Processor Status Register(PSR)

Midterm 2 Review Chapters 4-16 LC-3

EE445M/EE380L.6 Quiz 2 Spring 2017 Solution Page 1 of 5

Course Administration

(2) Part a) Registers (e.g., R0, R1, themselves). other Registers do not exists at any address in the memory map

Implementing Functions at the Machine Level

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

STEVEN R. BAGLEY ARM: PROCESSING DATA

ECE 375 Computer Organization and Assembly Language Programming Winter 2018 Solution Set #2

Chapter 10 And, Finally... The Stack

Outline. ARM Introduction & Instruction Set Architecture. ARM History. ARM s visible registers

Lecture Embedded System Security A. R. Darmstadt, Runtime Attacks

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

CSCE 5610: Computer Architecture

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

The ARM Instruction Set

Optimizing ARM Assembly

EE319K Exam 1 Summer 2014 Page 1. Exam 1. Date: July 9, Printed Name:

The ARM Cortex-M0 Processor Architecture Part-2

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Systems I. Machine-Level Programming V: Procedures

Transcription:

Systems Architecture The Stack and Subroutines The Stack p. 1/9

The Subroutine Allow re-use of code Write (and debug) code once, use it many times A subroutine is called Subroutine will return on completion Defer writing actual code until later Helps with readability and maintenance We can nest subroutines: subroutine calls other subroutines May be a procedure: does not return a value May be a function: does return a value The Stack p. 2/9

Operation of The Subroutine Branch to Subroutine (Branch and Link) BL cc label Link Register: LR (R14) Holds address of instruction after the BL instruction Return from Subroutine MOV cc PC, LR The Stack p. 3/9

Operation of The Subroutine Branch to Subroutine (Branch and Link) BL cc label cc : LR PC + 4 cc : PC PC + IR(offset) Link Register: LR (R14) Holds address of instruction after the BL instruction Return from Subroutine MOV cc PC, LR cc : PC LR The Stack p. 3/9

Example Subroutines ; Read line from keyboard into string at R10, uses R0 and R13 GetStr MOV R13, LR ; Save Return Address BLAL GetChar ; Read keyboard into R0 CMP R0, #13 ; Is it return key? BEQ GetStr1 ; Yes Exit Subroutine STRB R0, [R10], #1 ; No Save char in string BAL GetStr ; Get next character GetStr1 EOR R0, R0, R0 ; Clear R0 STRB R0, [R10] ; Add terminating zero byte MOV PC, R13 ; Return from GetStr ; Read char from keyboard into R0 GetChar SWI &4 ; Reach char into R0 MOV PC, LR ; Return from GetChar The Stack p. 4/9

The Stack Stack used to remember values Provides temporary (local) storage Allows for subroutines (functions) R13 points to current Top Of Stack (TOS) Also known as the Stack Pointer (SP) Stack is a LIFO (last-in-first-out) device Most recent value pushed on is first popped off Stack grows downwards in memory Each mode has it s own stack pointer: R13_fiq, R13_svc, R13_abt, R13_undef,... R13 or SP refer to current mode The Stack p. 5/9

2 Push A A

2 Push A 3 Push B A B

2 Push A 3 Push B 4 Push C A B C

2 Push A 3 Push B 4 Push C 5 Push D A B C D

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop A B C

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E A B C E

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop A B C

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop A B

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop A

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop 11 Pop

2 Push A 3 Push B 4 Push C 5 Push D 6 Pop 7 Push E 8 Pop 9 Pop 10 Pop 11 Pop Stack Empty

Stack as Temporary Storage Push: Save register on the stack STR cc Rs, [SP], #-4 Pop: Recover register from stack LDR cc Rd, [SP, #4]! The Stack p. 7/9

Stack as Temporary Storage Push: Save register on the stack STR cc Rs, [SP], #-4 cc : MBR Rs cc : MAR cc : SP - 4 cc : M(MAR) MBR Pop: Recover register from stack LDR cc Rd, [SP, #4]! The Stack p. 7/9

Stack as Temporary Storage Push: Save register on the stack STR cc Rs, [SP], #-4 cc : MBR Rs cc : MAR cc : SP - 4 cc : M(MAR) MBR Pop: Recover register from stack LDR cc Rd, [SP, #4]! cc : SP + 4 cc : MAR cc : MBR M(MAR) cc : Rd MBR The Stack p. 7/9

Push/Pop a Set of Register Push: Save a set of registers on the stack STM cc mode SP!, { Register List } Pop: Recover the set of registers LDM cc mode SP!, { Register List } mode can be one of: IB: Increment Before IA: Increment After DB: Decrement Before DA: Decrement After Register List A list of the registers to load/store E.g., R0-R7, R10 The Stack p. 8/9

Nested Subroutines Use Stack to store Return Address (Link Register) Save all register used in the subroutine just in case the caller is using them Must pop off all values pushed onto stack! Pass parameters (arguments) into a subroutine Three types of variable passing by value / reference / name Three methods of passing variables In registers / on Stack / in parameter block Return a value from the subroutine The Stack p. 9/9