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

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

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

CSE Lecture In Class Example Handout

ECE232: Hardware Organization and Design

Lab3: Machine Language and Assembly Programming

Instructions: Assembly Language

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.

Course Administration

Systems Architecture The Stack and Subroutines

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

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

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

CSE Lecture In Class Example Handout

Instruction Set Architectures (4)

Functions in MIPS. Functions in MIPS 1

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Lab 4 : MIPS Function Calls

Implementing Functions at the Machine Level

Lecture 5. Announcements: Today: Finish up functions in MIPS

ECE260: Fundamentals of Computer Engineering

Computer Architecture. The Language of the Machine

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Today. Putting it all together

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

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

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Compiling Code, Procedures and Stacks

Review of Activation Frames. FP of caller Y X Return value A B C

MIPS Functions and the Runtime Stack

Subprograms, Subroutines, and Functions

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

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Run-time Environment

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

Control Instructions

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Subroutine. Chapter 8

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

CS429: Computer Organization and Architecture

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Procedures and Stacks

MIPS Assembly Programming

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

CS 61c: Great Ideas in Computer Architecture

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

CA Compiler Construction

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

Lecture 5: Procedure Calls

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

CSC 2400: Computer Systems. Using the Stack for Function Calls

Lectures 3-4: MIPS instructions

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

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

SPIM Procedure Calls

EE 109 Unit 15 Subroutines and Stacks

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

MIPS Procedure Calls. Lecture 6 CS301

EE 361 University of Hawaii Fall

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Anne Bracy CS 3410 Computer Science Cornell University

Thomas Polzer Institut für Technische Informatik

Storage in Programs. largest. address. address

Bonus Lecture: Fibonacci

Computer Organization: A Programmer's Perspective

Chapter 2A Instructions: Language of the Computer

6.001 Recitation 23: Register Machines and Stack Frames

MIPS Functions and Instruction Formats

Lecture 2: RISC V Instruction Set Architecture. James C. Hoe Department of ECE Carnegie Mellon University

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

Lecture 5: Procedure Calls

Problem: ! bell! help!! 1! 2!! Bal help!!!! Bal bell! 3 Jr $31!!!! ! Jr $31! What happend! What will happen here?!

CENG 450: Instruction Set (16-bit)

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Code Generation. Lecture 12

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

Implementing Procedure Calls

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Anne Bracy CS 3410 Computer Science Cornell University

Topic 7: Activation Records

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

ECE331: Hardware Organization and Design

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

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Lecture 2: RISC V Instruction Set Architecture. Housekeeping

System Software Assignment 1 Runtime Support for Procedures

Anne Bracy CS 3410 Computer Science Cornell University

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

SRC Assembly Language Programming - III

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

Common Problems on Homework

CENG3420 Lecture 03 Review

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Using SRAM in AVR assembler language

Transcription:

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

MicroBlaze Subroutines Same idea as a C/C++ function There s no call instruction Instead, branch and link Example: MAIN_PROG:... brlid nop... r15, MY_SUB MY_SUB: <sub. code goes here> rtsd r15, 8 nop 2

MAIN_PROG:... brlid nop... r15, MY_SUB MY_SUB: <sub. code goes here> rtsd r15, 8 nop This puts the address of the brlid instruction into r15. Execution transfers to MY_SUB. This transfers execution to r15 + 8, = the 2 nd instruction after the brlid. A delay instruction allows the instruction following it to execute before the branch is taken 3

Other Branch-Link Flavors To invoke a subroutine: brld rd, rb brald rd, rb brlid rd, IMM braild rd, IMM link : rd PC if (absolute) PC offset else PC PC + offset 4

Issues with Subroutine Linkage The convention is to use R15 as the link register (but this is not dictated by the MB hardware) Now, suppose one subroutine needs to call another... Both subroutines can t use r15, can they? Also, we need a way to pass parameters to subroutines, and to receive values returned from subroutines Suppose we want to support MANY levels of nested subroutine calls... 5

Examples of Stacks Top of stack Bottom of stack 6

Stacks LOW MEM Very common data structure Also called a LIFO (Last-In, First- Out) Terminology: push: place a value on the top of a stack pop: remove a value from the top of a stack SP-> <E M P T Y > Bottom of stack-> HIGH MEM 7

Stacks LOW MEM No dedicated stack mechanism in MicroBlaze Free to invent your own rules My rules: Stack grows down in memory Devote (arbitrarily) R1 as the SP SP points to the top item on the stack SP-> Bottom of stack-> <E M P T Y > HIGH MEM 8

Stacks LOW MEM Conventions / cautions: Access only the top value on a stack, otherwise, be careful Refer to Stack Frames Bottom Beware overflow and underflow of stack-> It is VERY common to use a system stack to support subroutine linkage (it s exactly what is needed to support nested calls/ returns) SP-> <E M P T Y > HIGH MEM 9

Physical Stacks

Stacks on the MicroBlaze No dedicated stack mechanism in MicroBlaze Typical for most RISC machines Just use load/store instructions Need to dedicate a particular register as the Stack Pointer Conventions: Use R1 as the Stack Pointer R1 will normally contain the address of the value at the top of the stack We will cause the stack to grow toward memory address 0 11

Stack Operations in Memory R1 R1 R1 R1

Call / Return brlid rl,imm rtsd rl,imm <------ CALL <------- RETURN

Example Stack Operations.equ TOP_OF_MEMORY, 0x00003ffc # Last addr of on-chip mem INITIALIZE:... addi r1,r0,top_of_memory # Initialize r1 as SP... MY_CODE:... addi r1,r1,-4 # Decrement SP sw r2,r0,r1 # Push r2 onto stack...... lw r2,r0,r1 # Pop value from the stack addi r1,r1,4 # Increment SP... PUSHING: PREDECREMET POPPING: POST- INCREMENT 14

Efficiency When Pushing Lots of Registers MY_CODE:... addi r1,r1,-28 # Adjust stack pointer swi r2,r1,24 # Push r2 onto the stack swi r3,r1,20 # Push r3 onto the stack swi r4,r1,16 # Push r4 onto the stack swi r5,r1,12 # Push r5 onto the stack swi r6,r1,8 # Push r6 onto the stack swi r7,r1,4 # Push r7 onto the stack swi r8,r1,0 # Push r8 onto the stack... 15

Register Usage With Stacks and Subroutines Particular register must be dedicated as the Stack Pointer (convention: R1) MicroBlaze requires a link register for calls/returns (convention: R15) Pass a small number of parameters via registers (R5, etc.) It s common for a subroutine to return a value via a register (R4) Subroutine etiquette: scratchpad registers should be restored to their original values before returning With nested calls, we could quickly run out of registers Solution: temporarily store register values on the system stack 16

Summary Subroutines Modular design Divide and conquer Code re-use A good subroutine is one that does only one thing --- and does it very well. Stacks Fast, simple means of dynamic memory allocation No size limit (in theory) Ideally suited for subroutine linkage 17

Stack Frame High Memory Func 1 Func 1 Func 1 Func 1 SP Func 2 Func 2 Func 2 SP Func 3 SP Low Memory SP X9584 Figure 3-2: Stack Frame Func1 calls Func2, which in turn calls Func3. After the call from Func1 to Func 2, the value of the stack pointer (SP) is decremented. This value of SP is again decremented to accommodate the stack frame for Func3. On return from Func3 the value of the stack pointer is increased to its original value in the function, Func 2.

Stack Frame Figure 3-1: Stack Convention High Address Function Parameters for called sub-routine (Arg n..arg1) (Optional: Maximum number of arguments required for any called procedure from the current procedure.) Old Stack Pointer Link Register (R15) Callee Saved Register (R31...R19) (Optional: Only those registers which are used by the current procedure are saved) Local Variables for Current Procedure (Optional: Present only if Locals defined in the Figure procedure) 3-1: Stack Convention Functional Parameters (Arg n.. Arg 1) (Optional: Maximum number of arguments required for any called procedure from the current procedure) New Stack Pointer Link Register Low Address