ECE251: Tuesday September 12

Similar documents
ECE251: Tuesday September 11

Stacks and Subroutines

ECE251: Thursday September 13

ECE251: Tuesday September 18

Flow Control In Assembly

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

ECE251: Thursday September 27

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

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

Systems Architecture The Stack and Subroutines

Control Flow. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 21

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

ARM Assembly Language. Programming

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

Control Instructions

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

ECE 471 Embedded Systems Lecture 5

Subroutines and the Stack

Processor Status Register(PSR)

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

EE251: Tuesday September 5

NET3001. Advanced Assembly

ARM Architecture and Instruction Set

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

Practical 1 Review. ECE Rick

EE319K Spring 2015 Exam 1 Page 1. Exam 1. Date: Feb 26, 2015

Introduction to C. Write a main() function that swaps the contents of two integer variables x and y.

Exam 1. Date: Oct 4, 2018

CSE Lecture In Class Example Handout

ECE 372 Microcontroller Design Assembly Programming. ECE 372 Microcontroller Design Assembly Programming

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

ARM Cortex M3 Instruction Set Architecture. Gary J. Minden March 29, 2016

Systems I. Machine-Level Programming V: Procedures

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

Exam 1 Fun Times. EE319K Fall 2012 Exam 1A Modified Page 1. Date: October 5, Printed Name:

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Exam 1. Date: February 23, 2016

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.

AND SOLUTION FIRST INTERNAL TEST

Compiling Code, Procedures and Stacks

Chapter 2 Instructions Sets. Hsung-Pin Chang Department of Computer Science National ChungHsing University

Branch Instructions. R type: Cond

ARM Assembly Programming

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

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

ECE 3610 MICROPROCESSING SYSTEMS

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CENG3420 Lecture 03 Review

MIPS Functions and the Runtime Stack

EE319K Fall 2013 Exam 1B Modified Page 1. Exam 1. Date: October 3, 2013

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

Summer 2003 Lecture 14 07/02/03

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

ARM Cortex-M4 Programming Model Flow Control Instructions

Chapters 3. ARM Assembly. Embedded Systems with ARM Cortext-M. Updated: Wednesday, February 7, 2018

ARM Cortex-M4 Architecture and Instruction Set 2: General Data Processing Instructions

ARM Cortex-M4 Architecture and Instruction Set 3: Branching; Data definition and memory access instructions

ECE 3210 Lab 4: Calculator

ARM Architecture and Assembly Programming Intro

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

Support for high-level languages

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

Course Administration

Chapter 2A Instructions: Language of the Computer

ECE 498 Linux Assembly Language Lecture 5

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

Computer Organization Laboratory. Class Notes. Instructor: Ken Q. Yang Dept. of ECE, URI

Computer Architecture and System Software Lecture 07: Assembly Language Programming

Comparison InstruCtions

Architecture. Digital Computer Design

Computer Architecture and System Software Lecture 06: Assembly Language Programming

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

Cortex M3 Programming

EE319K Spring 2016 Exam 1 Solution Page 1. Exam 1. Date: Feb 25, UT EID: Solution Professor (circle): Janapa Reddi, Tiwari, Valvano, Yerraballi

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

ARM Assembly Programming

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

EE4144: ARM Cortex-M Processor

STEVEN R. BAGLEY ARM: PROCESSING DATA

Practical Malware Analysis

Writing ARM Assembly. Steven R. Bagley

MIPS Functions and Instruction Formats

Lecture 8: Control Structures. Comparing Values. Flags Set by CMP. Example. What can we compare? CMP Examples

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CSE Lecture In Class Example Handout

Compiler Design. Homework 1. Due Date: Thursday, January 19, 2006, 2:00

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

ARM Assembly Programming

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

Computer System Architecture

Procedures and Stacks

Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

Unsigned and signed integer numbers

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Chapter 15. ARM Architecture, Programming and Development Tools

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

ARM Cortex-M4 Programming Model

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Transcription:

ECE251: Tuesday September 12 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab #3 this week and next. Note: Lab 3 has prework! Homework: #2 Due Thursday, 4:30 pm Mid-Term Exam: October 5 Review on October 3 Lecture #7 1

Comparison Instructions Instruction Operands Operation Flags CMP Rn, Op2 Compare N,Z,C,V CMN Rn, Op2 Compare Negative N,Z,C,V TEQ Rn, Op2 Test Equivalence N,Z,C TST Rn, Op2 Test N,Z,C The only effect of the comparisons is to update the N, Z, C and V flags. There is no destination register Rd. Operations are: - CMP operand1 - operand2, but result not written - CMN operand1 OR operand2, but result not written - TST operand1 AND operand2, but result not written - TEQ operand1 EXOR operand2, but result not written Examples: CMP r0, r1 ;Performs r0 - r1 and updates N,Z,C,V appropriately TST r2, #5 ;Performs r2 AND 5 and updates N,Z,C appropriately Lecture #7 2

Number Interpretation Which is greater? 0xFFFFFFFF or 0x00000001 If they represent signed numbers, the latter is greater. (1 > -1) If they represent unsigned numbers, the former is greater. (4billion+ > 1) Lecture #7 3

So What? It s software s responsibility to tell computer how to interpret data. Use signed vs unsigned branch instructions: MOVS r6, #0xFFFFFFFF MOVS r5, #0x00000001 CMP r5, r6 BLE Then_Clause... BLE: Branch if less than or equal, signed MOVS r6, #0xFFFFFFFF MOVS r5, #0x00000001 CMP r5, r6 BLS Then_Clause... BLS: Branch if lower or same, unsigned Lecture #7 4

If-then Statement If a<0 then a = -a x = x + 1 Implementation 1: ; r1 = a, r2 = x CMP r1, #0 ; Compare a with 0 BGE endif ; Go to endif if a 0 then RSB r1, r1, #0 ; a = - a endif ADD r2, r2, #1 ; x = x + 1 Lecture #7 5

Compound Boolean Expression x > 20 AND x < 25 x 20 OR x 25 x == 20 OR x == 25 NOT(x == 20 OR x == 25) What condition above does the program below test? What happens if condition is met (i.e. then )? Assembly Program ; r0 = x CMP r0, #20 ; compare x and 20 BLE then ; go to then if x 20 CMP r0, #25 ; compare x and 25 BLT endif ; go to endif if x < 25 then MOV r1, #1 ; a = 1 endif Lecture #7 6

If a=1 then b = 3 else b=4 If-then-else ; r1 = a, r2 = b CMP r1, #1 ; compare a and 1 BNE else ; go to else if a 1 then MOV r2, #3 ; b = 3 B endif ; go to endif else MOV r2, #4 ; b = 4 endif What are then, else and endif in the program above? Lecture #7 7

Big New Topic: The Stack Breaking large programming problems down into smaller subcomponents (subroutines) is an excellent practice. The stack is useful to store parameters passed between these subcomponents and temporary data used only by the subroutine. In addition, storage space is required for the return address of each (possibly nested) subroutine call. These storage requirements are even more obvious if a recursive algorithm is used (subroutine calls itself, like a factorial computation might be done). So, we need to allocate this temporary storage space as we go. This space is called a stack because the last stored data is the first accessed, like a stack of plates. Lecture #7 8

Stack A Last-In-First-Out (LIFO) data structure Allows easy access to the most recently added item(s) Also called the top of the stack Key operations: push (add item(s) to stack) pop (remove top item(s) from stack) Lecture #7 9

Stack Growth Convention: Ascending vs Descending Descending stack: Stack grows towards low memory address Ascending stack: Stack grows towards high memory address Lecture #7 10

Stack Growth Convention: Full vs Empty Full stack: SP points to the last item pushed onto the stack Empty stack: SP points to the next free space on the stack Lecture #7 11

ARM Cortex-M4 Stack: Full Descending Cortex-M4 uses full descending stack! Memory Address 0xFFFFFFFF Stack always operates on 32-bit data Stack base Stack Pointer (SP) = R13 Stack Pointer is Pre-decremented on PUSH Stack Pointer (SP) Stack top Post-incremented on POP This must be, given Full Descending definition PUSH stack grow downwards POP 0x00000000 Lecture #7 12

Stack Manipulation PUSH {Rd} ;n registers in list SP = SP-4*n descending stack [[SP]] = Rd full stack Push multiple registers PUSH {r6, r7, r8} Examples below are all equivalent. PUSH {r8, r7, r6} PUSH {r8} PUSH {r7} PUSH {r6} The order in which registers listed in the register list does not matter. When pushing multiple registers, these registers are automatically sorted by name and the lowest-numbered register is stored to the lowest memory address (stored last). Lecture #7 13

Stack Manipulation POP {Rd} Rd = [[SP]] SP = SP + 4*n Stack shrinks. So why +? Pop multiple registers Examples below are all equivalent. POP {r6, r7, r8} POP {r8, r7, r6} POP {r6} POP {r7} POP {r8} The order in which registers listed in the register list does not matter. When popping multiple registers, these registers are automatically sorted by name and the lowest-numbered register is loaded from the lowest memory address, (loaded first). Lecture #7 14

Stack Examples PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2} Lecture #7 15

Stack Examples PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2} Lecture #7 16

Stack Examples PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2} Lecture #7 17

Example: Swap R1 & R2 R1 0x11111111 R2 0x22222222 PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x20000200 xxxxxxxx xxxxxxxx xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 18

Example: Swap R1 & R2 R1 0x11111111 R2 0x22222222 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x200001FC xxxxxxxx 0x11111111 xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 19

Example: Swap R1 & R2 R1 0x11111111 R2 0x22222222 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x200001F8 xxxxxxxx 0x11111111 0x22222222 Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 20

Example: Swap R1 & R2 R1 0x22222222 R2 0x22222222 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x200001FC xxxxxxxx 0x11111111 xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 21

Example: Swap R1 & R2 R1 0x22222222 R2 0x11111111 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x20000200 xxxxxxxx xxxxxxxx xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 Could you do this with one PUSH and One POP instruction? memory Lecture #7 22

Subroutine A subroutine, also called a function or a procedure, single-entry, single-exit Return to caller after it exits When a subroutine is called, the Link Register (LR), R14, holds the memory address of the next instruction to be executed after the subroutine exits (the instruction physically beyond the instruction that calls the subroutine). That is how it knows to get back to where it came from. Lecture #7 23

Subroutines Separate, independent module of program, performs a specific task shortens code modules (not necessarily total code) often provide reusable tools Good subroutine: independence: does not depend on other code, and calling code isn t affected by it (e.g. register values) registers: stores/restores key registers upon entrance/exit using PUSH and POP instructions as needed data and code independent: local variables, limited use of direct memory addressing, unless it accesses global (program-wide) data structures. Lecture #7 24

Link Register 32 bits Low Registers R0 R1 R2 R3 R4 R5 R6 R7 General Purpose Register Link Register (LR) holds the return address of the current subroutine call I.e. how to get back to where you were (the following instruction) when the subroutine was called R8 High Registers R9 R10 R11 32 bits xpsr R12 R13 (SP) R14 (LR) R13 (MSP) R13 (PSP) BASEPRI PRIMASK FAULTMASK Special Purpose Register R15 (PC) CONTROL Lecture #7 25

Call a Subroutine BL <Subroutine Name> to go to subroutine BX LR to return from subroutine Subroutine itself enclosed with PROC and ENDP Directives Caller Program MOV r4, #100... BL foo;call subroutine foo... ADD r4, r4, #1 ; r4 =? Subroutine/Callee foo PROC... MOV r4, #10 ; foo changes r4... BX LR ; return to caller ENDP Lecture #7 26

Preserve Runtime Environment using the Stack Subroutine must not change caller s r4 value Save it on the stack! Can this be idea be generalized? Yes! Caller Program MOV r4, #100... BL foo... ADD r4, r4, #1 ; r4 = 101, not 11 Subroutine/Callee foo PROC PUSH {r4} ; preserve r4... MOV r4, #10 ; foo changes r4... POP {r4} ; Recover r4 BX LR ENDP Lecture #7 27

Summary The Stack Subroutines Next Lecture: Subroutines and passing info Read Chapter 8 of text. Lab #3, starting this week is about Subroutines. Two week lab not a trivial lab Prework on this lab! 3 deliverables to TA Pre-read the entire lab to be better prepared Lecture #7 28