Subroutines. we jump to a new location in the code

Similar documents
Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Subroutine. Chapter 8

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

EE 109 Unit 15 Subroutines and Stacks

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

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

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

CSE Lecture In Class Example Handout

Today s objective: introduction to really simple subroutines to simplify program structure for I/O

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Subprograms, Subroutines, and Functions

Course Administration

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

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

stack frame where register An is used as the argument pointer.

CSE Lecture In Class Example Handout

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

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

Recursive Subroutine Calls Example

Chapter 2A Instructions: Language of the Computer

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

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

Control Instructions

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

EE 361 University of Hawaii Fall

Systems I. Machine-Level Programming V: Procedures

Module 27 Switch-case statements and Run-time storage management

ECE260: Fundamentals of Computer Engineering

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

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

Functions in MIPS. Functions in MIPS 1

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

CSCE 5610: Computer Architecture

CSC 2400: Computing Systems. X86 Assembly: Function Calls

EE 3170 Microcontroller Applications

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Lab 2 Use Traps. Lab 2 Input and Output 2 nd Semester. Lab 2 English. Lab 2 Pseudocode

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

MIPS Procedure Calls. Lecture 6 CS301

Lecture 6: Assembly Programs

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

68000 Architecture, Data Types and Addressing Modes. 9/20/6 Lecture 2 - Prog Model 1

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

CS240: Programming in C

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

Mark Redekopp, All rights reserved. EE 357 Unit 4b. CF Assembly Basics

Outline. 2.8 Stack. 2.9 Subroutines

MIPS Functions and Instruction Formats

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

Stacks and Subroutines

Implementing Procedure Calls

Programming. A. Assembly Language Programming. A.1 Machine Code. Machine Code Example: Motorola ADD

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Today. Putting it all together

Lecture 5: Procedure Calls

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

Computer Systems Lecture 9

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

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

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.

CS64 Week 5 Lecture 1. Kyle Dewey

Run-time Environment

Run-Time Data Structures

1 Number Representation(10 points)

CPE/EE 421 Microcomputers

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

Implementing Subroutines. Outline [1]

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.

Wednesday, October 15, 14. Functions

SRC Assembly Language Programming - III

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

X86 Stack Calling Function POV

Implementing Functions at the Machine Level

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

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

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

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

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Assembly III: Procedures. Jo, Heeseung

CS61C : Machine Structures

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

Tutorial 1 Microcomputer Fundamentals

Compiling Code, Procedures and Stacks

Alex Milenkovich 1. CPE/EE 421 Microcomputers: Motorola 68000: Assembly Language and C. Outline

CS153: Compilers Lecture 8: Compiling Calls

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

Systems Architecture The Stack and Subroutines

Computer Architecture Prof. Mainak Chaudhuri Department of Computer Science & Engineering Indian Institute of Technology, Kanpur

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Lecture 7: Procedures and Program Execution Preview

ECE232: Hardware Organization and Design

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

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

Lab 4 : MIPS Function Calls

Transcription:

Subroutines EE 57 Unit 7 Subroutines Stacks Subroutines (or functions) are portions of code that we can call from anywhere in our code, execute that t subroutine, and then A subroutine to calculate the average of numbers C code: void main() { x = 8; res = avg(x,4); int avg(int a, int b){ return (a+b)/; We call the subroutine to calculate the average Subroutines Subroutines are similar to branches where we jump to a new location in the code Normal Branches vs. Subroutines Difference between normal branches and subroutines is that subroutines automatically return to location after the subroutine call C code: void main() { C code: void main() { x = 8; x = 8; res = avg(x,4); res = avg(x,4); 1 Call avg sub-routine int avg(int a, int b){ will require us to branch to that code return (a+b)/; After subroutine completes, return to the statement in the main code where we left off 1 Call avg sub-routine to calculate the average int avg(int a, int b){ return (a+b)/;

Implementing Subroutines To implement subroutines in assembly we need to be able to: Branch to the subroutine code (BSR/JSR instruc.) Return to the instruction after BSR when we finish the subroutine ( instruc.) C code: res = avg(x,4); int avg(int a, int b) { BSR.W AVG AVG: Branching to a Subroutine Use BSR instruction (Branch SubRoutine) Format: BSR.{S,W,L Addr Similar to branches we still add a displacement value to the PC [e.g. PC + disp PC] S,W,L refers to 8-, 16-, or -bit displacement values similar to normal branches Addr is the address you want to branch to Usually specified as a label the return address () for use by the instruction Branching to a Subroutine Branch Formula: disp. = Addr of Label (Addr. of Branch + ) = 0x0800 (0x0010+) = 0x07EE Branch Formula: BSR & disp. = Addr of Label (Addr. of Branch + ) = 0x0800 (0x0010+) = 0x07EE Use instruction to indicate that the subroutine is complete and we should return to where the routine was called (.text+0x10) BSR.W AVG (.text+0x14) AVG: 1 BSR will add displacement 0x07EE to PC to get the new PC =.text+0x0800 BSR will also store the return address: for use by the instruction loads the with the return address stored by the BSR (.text+0x10) BSR.W AVG (.text+0x14) AVG: 1 BSR will add displacement 0x07EE to PC to get the new PC =.text+0x0800 BSR will also store the return address: for use by the instruction

Jumping to a Subroutine Format: JSR Addr Rather than storing a displacement to add to the PC like BSR does, JSR simply stores the start address of the subroutine [e.g. PC = Addr.] Addr is the address you want to branch to Usually specified as a label Automatically stores the return address () for use by the instruction just like BSR Return Addresses: Fact 1 AVG may be called many times from many places in the code which means a different return address each time 0x0014 is the return address for this BSR (.text+0x10) BSR.W AVG (.text+0x14) 0x0050 is the return address for this BSR (.text+0x4c) t BSR.W AVG (.text+0x50) AVG: Return Addresses: Fact Subroutines may call (i.e. BSR.W SUB1 arbitrary number of 1 0x0014 subroutines calls) Example: main calls SUB1: BSR.W SUB sub1 which calls sub 0x0804 and so on Need to.org 0x0900 SUB: 4 Dealing with Return Addresses Q: Can we store return address in a particular processor register BSR.W SUB1 (i.e. select A6 as the return 1 0x0014 address register )? A: Q: Can we use more registers to store s? A: SUB1: 0x0804 BSR.W SUB.org 0x0900 SUB: 4

Storing Return Addresses Stacks To store arbitrary number of return addresses, need to store them in (which has enough storage to 1 allow for arbitrary depth of subroutine calls) SUB1: Return addresses will be 0x0804 accessed in as they are stored 0x0804 is the to be stored but should be the one used to return Implies we should use a data structure to store s BSR.W SUB1 0x0014 4 BSR.W SUB.org 0x0900 SUB: Use a stack to store the return addresses Stack is a data structure where data is accessed in reverse order as it is stored System stack will use a specific area in memory and growing towards addresses Stack is accessed using as a pointer is renamed SP (Stack pointer) (SP/ )= 0x57c Stack Pointer Convention: Stack Main memory 0000 0000 564 568 0000 56c 0000 570 0000 0000 0000 57c 0x057c is the base of the stack, but it will always be due to our convention Stacks Push Operation Operations on stack Push: Put new data on top of stack (SP)= 57c Empty stack Write value to where SP points Pop: Retrieves and removes data from top of (SP)= stack from where SP points to effectively delete top value (SP)= 57c Push 57c Push will add a value to the top of the stack Pop 57c Pop will remove the top value from the stack Push: Put new data on top of stack Decrement SP Write value to where SP points Canbeaccomplished accomplished w/ predecrement mode Push value 0x0000804 570 0000804 (SP)= 57c Decrement SP by 4 (since pushing a longword), then write value to where the SP is now pointing

Pop Operation User-Defined Stack Example Pop: Retrieves and removes data from top of stack Read value from where SP points Increment SP to effectively delete top value Can be accomplished w/ predecrement mode Pop value (SP)= 57c 570 0000804 Read value that SP points at then increment SP (this effectively deletes the value because the next push will overwrite it) Warning: Because the stack grows towards lower addresses, when you push something on the stack you subtract 4 from the SP and when you pop, you add 4 to the SP. Users can create their own stack data structures using M68000 Instructions Predecrement mode is perfect for push operations while postincrement mode is perfect for pop operations Due to the assumption that top of stack is 1 st occupied location and that the stack grows upwards (towards lower addresses) * Setup stack pointer MOVEA.L #$000,A5 1 * Use pre-decrement to push MOVE.L #1,-(A5) MOVE.L #,-(A5) * Use post-increment to pop MOVE.L (A5)+,D0 4 MOVE.L (A5)+,D1 5 (A5)= 1FF8 1FFC 4 1 000 5 1FF8 1FFC 000 004 0000000 00000001 M68000 System Stack s are used in reverse (LIFO) order Processor automatically maintains system stack A7 is used as system stack pointer Points to top occupied element of stack Aliased with name SP [i.e. use (SP)+, -(SP), etc.] Coldfire 511 initializes SP/A7 to 0x0004000 In this class we will start our stack at 0x57C (just to use save writing). BSR/JSR will automatically PUSH the will automatically POP the Subroutines and Stacks BSR instruction automatically pushes the SP = SP - 4 M[SP] = Return Address PC = PC + displacement instruction ti automatically ti pops the PC = M[SP] // i.e. return address put back in PC SP = SP + 4

An Example Subroutine Example 1 5 4 0510 BSR.W PRINTF 0514.org 0x100 PRINTF: MOVE.B (A0)+,D1 180 BSR.W PCHAR 1806 181C.org 0x1400 PCHAR: PC= SP= 0000510 00057C 570 57c DAT:.long 8 RES:.space 4.text BEG: MOVE.L DAT,D0 MOVE.L #4,D1 BSR.W AVG MOVE.L D1,RES.org 0x100 AVG: ADD.L D0,D1 LSR.L #1,D1 PC= 0500 D1= SP= 57c.text section starts at 0x500 section starts at 0x004 Stack starts at 0x57c and grows upward Main Code AVG Code Data Stack MOVE 0500 = BEG BSR 050C disp. 050E MOVE 0510 ADD 0600 = AVG LSR 060 00000008 004 = DAT 008 = RES 570 Subroutine Example Subroutine Arguments & Return Values DAT:.long 8 RES:.space 4.text BEG: MOVE.L DAT,D0 MOVE.L #4,D1 BSR.W AVG MOVE.L D1,RES.org 0x100 AVG: ADD.L D0,D1 LSR.L #1,D1 PC= 0500 D1= SP= 57c MOVE 0500 = BEG BSR 050C disp. 050E MOVE 0510 ADD 0600 = AVG LSR 060 00000008 004 = DAT 008 = RES 570 Hand-coded assembly Parameters and return values can be passed/returned in specific registers If too many arguments, use the stack (i.e. caller can push arguments on and have callee retrieve them Passing values in registers does not work for recursive of re-entrant routines where multiple instances of the routine can be running at the same time => Need separate storage for each instance Compilers Almost always use the stack Create a structure on the stack known as a frame

Return Values Subroutines often need to return a value HLL s like C only allow return value Usually returned in a specific register For Coldfire, is usually the return value int ans; void main() { ans = avg(1,5); int avg(int a, int b) { int temp = 1; return a + b >> temp; temp Return Value ans Saving Registers One routine may ygenerate values in registers, call a subroutine, and then expect to use those values Meanwhile the subroutine may Solution:.text main: move.l var,d5 movea.l #dat,a0 bsr.w sub1 add.l (a0)+,d5.org 0x000 sub1: move.l #1,d5 rts.text t main: move.l var,d5 movea.l #dat,a0 bsr.w sub1 add.l (a0)+,d5.org 0x000 sub1: move.l d5,-(sp) move.l #1,d5 move.l (sp)+,d5 rts Recursive Factorial Routine Recursive Call Timeline C Code: int ans; void fact(int n) { if(n == 1) ans = 1; else { // calculate (n-1)! fact(n-1); // now ans = (n-1)! // so calculate n! ans = n * ans;.text BSR.W FACT MOVE.L D1,ANS BEQ.S NEQ1 MOVE.L (SP)+,D0 MULU D0,D1 B.L DONE DONE: Time Fact() {Save n= Restore n= ans = ( * ans) = * Fact() {Save n= Restore n= ans = ( * ans) = *1 Fact(1) ans=1 Implementation ti Detail: Make sure each call of Fact is working with its own value of n Before calling yourself, you need to save copies of all your locally declared variables/parameters (e.g. n)

004 = ANS 004 = ANS MOVE.L D1,ANS MOVE.L (SP)+,D0 B.L DONE DONE: D1= SP= 57c 560 564 568 56c 570 57c MOVE.L (SP)+,D0 B.L DONE DONE: D1= 560 564 568 56c 570 SP= ANS is where we will place the final n! answer when finished calculating it. During calculation we will keep it in D1 Initialize N (D0) = Call Fact() => BSR pushes return address and goes to FACT 004 = ANS 0000000 004 = ANS MOVE.L (SP)+,D0 B.L DONE DONE: D1= 560 564 568 56c 570 SP= B.L DONE DONE: D1= 560 564 568 56c SP= 570 n= Start by checking if N==1 If not we continue sequentially We want to call Fact() but that means changing g n (i.e. D0) to. We don t want to lose our current value of so we push it on the stack first. Then we decrement n and call Fact() pushing the

00000001 004 = ANS 00000001 004 = ANS B.L DONE DONE: D1= 560 564 SP= 568 n= n= B.L DONE DONE: D1= 00000001 560 564 SP= 568 n= n= We now perform the check of n again, find that it is not equal to 1, save n= and decrement n=1 in order to call Fact(1) In Fact(1) our comparison will find that n is equal to 1, branch to NEQ1 and store the value 1 in D1 (i.e. ans) 00000001 004 = ANS 0000000 004 = ANS B.L DONE DONE: D1= 00000001 560 564 SP= 56c n= n= B.L DONE DONE: D1= 0000000 560 564 SP= 570 n= will pop poff the of $880E and go back to that instruction, effectively returning us into the context of Fact() We will first restore our value of n = by ypopping pp it off the stack back into D0 We then calculate the factorial by taking ans = n * ans

0000000 004 = ANS 004 = ANS B.L DONE DONE: D1= 0000000 560 564 SP= n= B.L DONE DONE: D1= 00000006 560 564 SP= is executed again,,popping pp the off the stack and returning us into the context of Fact() We again restore our value of n = by ypopping pp it off the stack back into D0 We then calculate the factorial by taking ans = n * ans B.L DONE DONE: 00000006 004 = ANS D1= 00000006 560 564 SP= 57c Executing the this time pops p the last from the stack and returns us to the original calling function We then store the result to memory