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

Similar documents
What are the differences between these three ARM instructions: (3 marks) ADD R1, R2, R3 ADDS R1, R2, R3 and ADDEQ R1, R2, R3

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Systems Architecture The Stack and Subroutines

Bonus Lecture: Fibonacci

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

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

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

CA Compiler Construction

Functions in MIPS. Functions in MIPS 1

Run-time Environment

Concepts Introduced in Chapter 7

CSE Lecture In Class Example Handout

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

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

Implementing Procedure Calls

Support for high-level languages

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

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

ECE251: Tuesday September 11

Run-time Environments - 2

ECE232: Hardware Organization and Design

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

Today. Putting it all together

SPIM Procedure Calls

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

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

ECE251: Tuesday September 12

Procedures and Stacks

ARM Assembly Programming

ECE260: Fundamentals of Computer Engineering

button.c The little button that wouldn t

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

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.

Compiling Code, Procedures and Stacks

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

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

ARM Assembly Programming

Procedure Call and Return Procedure call

Run-Time Data Structures

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

Implementing Functions at the Machine Level

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

MIPS Functions and Instruction Formats

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

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

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

Storage in Programs. largest. address. address

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

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Lecture 5: Procedure Calls

CSE Lecture In Class Example Handout

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

Architecture. Digital Computer Design

System Software Assignment 1 Runtime Support for Procedures

Compilers and computer architecture: A realistic compiler to MIPS

Run Time Environment

CMPSCI 201 Fall 2006 Midterm #2 November 20, 2006 SOLUTION KEY

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

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

CIT Week13 Lecture

CS 314 Principles of Programming Languages. Lecture 13

Functions and Procedures

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

EE 109 Unit 15 Subroutines and Stacks

CS429: Computer Organization and Architecture

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

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

ECE331: Hardware Organization and Design

Assembly Programming (III)

Lecture 4: Instruction Set Design/Pipelining

ECE251: Tuesday September 18

Course Administration

G Programming Languages - Fall 2012

Chapter 9 :: Subroutines and Control Abstraction

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

CMPSCI 201 Fall 2005 Midterm #2 Solution

High Performance Computing Lecture 7. Matthew Jacob Indian Institute of Science

Embedded Operating Systems

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

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

CS64 Week 5 Lecture 1. Kyle Dewey

Lecture 6: Assembly Programs

Instruction Set Architectures (4)

EE319K (Gerstlauer), Spring 2013, Midterm 1 1. Midterm 1. Date: February 21, 2013

Scope, Functions, and Storage Management

ECE 471 Embedded Systems Lecture 8

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

ECE 3210 Lab 4: Calculator

Homework #3: Functions Calling Functions, and Recursion

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

ARM Assembly Programming II

Lecture 5: Procedure Calls

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

Systems I. Machine-Level Programming V: Procedures

Summer 2003 Lecture 14 07/02/03

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

Transcription:

OMP15111 Lecture 9 1/26 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 RSB R0, R0, R0,LSL #2 Write down an ADD instruction that has the same effect as the RSB instruction above. (4 marks)

OMP15111 Lecture 9 2/26 From last time ctd. Explain, with the aid of an example, how pre-indexed or post-indexed addressing can be used to improve the efficiency of a program which uses offset addressing to access the characters of a string in a loop. (4 marks)

OMP15111 Lecture 9 3/26 COMP15111: Introduction to Architecture Lecture 9: Methods Dr. Javier Navaridas School of Computer Science, University of Manchester Autumn 2016

OMP15111 Lecture 9 4/26 Overview & Learning Outcomes What happens during a Java method call A simple but naïve solution A better solution: use a Stack Some more ARM instructions and registers

OMP15111 Lecture 9 What happens during a Java method call 6/26 Java Method declaration and calls Declare method in one place: int age (int day, int month, int year) { } Use it from several places (possibly with different parameters): myage = age (myday, mymonth, myyear); agenextyear = age (myday, mymonth, myyear-1); if (age (myday, mymonth, myyear) == age (1, 1, myyear-1)) Animation: method calls & returns

OMP15111 Lecture 9 What happens during a Java method call 7/26 Sequence of events during a method call/return Call: (1) process method arguments (2) remember where to restart (3) branch to method Inside method: (4) use method parameters (5) perform body of method (6) set result (7) branch back to restart point Back at restart point, just after (3): (8) use result

OMP15111 Lecture 9 What happens during a Java method call 8/26 Call and method need to agree: (1) + (4) : where to put method arguments/parameters (2) + (7) : where to put restart point (5) : how to use registers safely (6) + (8) : where to put result

OMP15111 Lecture 9 A simple but naïve solution 10/26 Use registers as much as possible Result: e.g. R0 Parameters: e.g. R0 = day, R1 = month, R2 = year Restart point: R14 (LR Link Register) e.g. last part of call to method: ADR LR, restart ; (2) restart point B age ; (3) branch to method restart STR R0, myage ; (8) use result method ends with: MOV PC, LR ; (7) return to restart point Special BL Branch-and-Link instruction e.g. BL age does same as (2) + (3) above

OMP15111 Lecture 9 A simple but naïve solution 11/26 Complete (simple) example myage = age (myday, mymonth, myyear); int age (int day, int month, int year) { } LDR R0, myday ; (1) pass arguments LDR R1, mymonth ; LDR R2, myyear ; BL age ; (2,3) call method, save return point STR R0, myage ; (8) use result age ; use R0, R1, R2 (4) parameters ; to calculate (5) body ; result in R0 (6) result MOV PC, LR ; (7) branch back

OMP15111 Lecture 9 A simple but naïve solution 12/26 Question: ARM code for this a= add(c, d); int add (int x, int y) { return x+y; }

OMP15111 Lecture 9 A simple but naïve solution 13/26 How to use registers safely (5) Save any other registers used by method (e.g. just after start), Restore at end (e.g. just before return) callee saved (v. caller saved ) e.g. age STR R4, temp method body can use R4 LDR R4, temp MOV PC, LR temp DEFW 0

What if one method calls another? int age (int day, int month, int year) { System.out.println("trace " + n); } Need to save LR and parameter registers age STR LR, temp1 STR R0, temp2 (put println s argument in R0) BL println LDR R0, temp2 LDR PC, temp1 temp1 DEFW 0 temp2 DEFW 0 OMP15111 Lecture 9 A simple but naïve solution 14/26

OMP15111 Lecture 9 A simple but naïve solution 15/26 More problems Recursion a method can call any method, including itself maybe one temp per register won t be enough Efficiency space occupied by all those temp s mostly unused at any given moment during run-time We need another way to save and restore registers Method calls and returns are nested (like brackets)...so register saves and restores are also nested...so what we need is something that works the same way

OMP15111 Lecture 9 A better solution use a Stack 17/26 Stack A stack is like a very heavy pile of books: you can take anything off the top of the pile ( pop ) you can add anything to the top of the pile ( push ) you can t move anything into/out of the pile except at the top! This is simple, and exactly what we need for method calls LIFO = last-in, first-out

OMP15111 Lecture 9 A better solution use a Stack 18/26 Using a Stack on ARM On most computers: stacks start at a large address, which decreases as the stack grows ARM has: stack-pointer register (SP, R13) 4 different ways of using a stack! We will use commonest version: SP = address of top word on stack (stack-top) decrease SP to push, increase to pop Push: e.g. STR R0, [SP,#-4]! (pre-indexed) Pop: e.g. LDR R0, [SP],#4 (post-indexed)

OMP15111 Lecture 9 A better solution use a Stack 19/26 What if one method calls another? (again) int age (int day, int month, int year) { System.out.println("trace " + n); } (same example, better answer) age STR LR, [SP,#-4]! STR R0, [SP,#-4]! (put println s argument in R0) BL println LDR R0, [SP],#4 LDR PC, [SP],#4 We only use extra stack locations when needed, whereas temp1 and temp2 always allocated

OMP15111 Lecture 9 A better solution use a Stack 20/26 Question: What happens what is output? main ADR SP,stack BL print SVC 2 print STR LR, [SP,#-4]! MOV R0, #? BL print2 SVC 0 LDR PC, [SP],#4 print2 SVC 0 MOV R0, # @ SVC 0 MOV PC, LR stack DEFS 400 Terminal

OMP15111 Lecture 9 A better solution use a Stack 21/26 Summary of key points What happens during a Java method call A simple but naïve solution A better solution: use a Stack Some more ARM instructions (BL) and registers (LR, SP)

Your Questions OMP15111 Lecture 9 A better solution use a Stack 22/26

OMP15111 Lecture 9 A better solution use a Stack 23/26 Glossary Method declaration Method call Method entry Method exit/return Method result Method parameter Method argument LR register BL instruction Recursion Stack (LIFO) Pop Push SP register

OMP15111 Lecture 9 A better solution use a Stack 24/26 For next time Explain what the ARM instruction BL does, and how control is returned to the calling code when the method finishes. If the method itself calls another method, how does this affect the return? (4 marks) Why is extra memory (i.e. a stack) useful in the implementation of method calls? Why do we use a stack rather than fixed memory locations? (4 marks) A stack is implemented using SP and pre-/post-indexing instructions. Give code for the push and pop operations and explain the value in SP after each operation. (4 marks)

OMP15111 Lecture 9 A better solution use a Stack 25/26 Exam Questions A leaf method is a method that doesn t call any other method. Explain why ARM code for a leaf method can be simpler than the code for a non-leaf method (i.e. a method that does call another method). Give example instruction sequences for both kinds of methods as part of your explanation. (6? marks) A method main calls a second method work. Method work needs to use registers R0, R1 & R2, which are also used by main. What code is necessary at entry to and exit from work to preserve the values in the registers for use by main? (4 marks) During method calls, arguments can be passed either in registers or on the stack. Explain both mechanisms, giving example code. Explain the advantages and disadvantages of each mechanism. (8? marks)

OMP15111 Lecture 9 A better solution use a Stack 26/26 Exam Questions ctd. There are two alternative mechanisms for preserving the contents of a register across a method call (and return): caller saved is when a register is saved and restored by the method making the call, in case the method being called uses it. callee saved is when a register is saved and restored by the method being called, in case the method making the call uses it. Give example code for both mechanisms. Can you think of any circumstances where one mechanism or the other would be more efficient? (8? marks)