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

Similar documents
Support for high-level languages

ARM Assembly Programming

ARM Assembly Programming

ARM Assembly Programming II

ARM Assembly Language. Programming

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

ECE 471 Embedded Systems Lecture 8

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

Course Administration

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

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

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

C-Style Strings. CS2253 Owen Kaser, UNBSJ

Systems Architecture The ARM Processor

Lecture 5: Procedure Calls

CSCE 5610: Computer Architecture

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

Lecture 5: Procedure Calls

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

CS A331 Programming Language Concepts

ARM Assembly Programming

Run-time Environment

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

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

ECE232: Hardware Organization and Design

Lecture 6: Assembly Programs

Chapter 2A Instructions: Language of the Computer

EE 361 University of Hawaii Fall

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

Instructions: Assembly Language

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

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

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

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

ECE251: Tuesday September 18

Assembly Language Programming

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Problem Sheet 1: ANSWERS (NOT ) (NOT ) 3. 37, ,

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

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

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

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

Lecture XXIV: Review Xuan Guo CSC 3210 Computer Organization and Programming Georgia State University April 23, 2015 Xuan Guo Lecture XXIV: Review

Writing ARM Assembly. Steven R. Bagley

Advanced Assembly, Branching, and Monitor Utilities

Compiling Code, Procedures and Stacks

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

ECE232: Hardware Organization and Design

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

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

ARM PROGRAMMING. When use assembly

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

ECE251: Tuesday September 11

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

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

button.c The little button that wouldn t

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

NET3001. Advanced Assembly

Architecture. Digital Computer Design

Computer Organization and Components

Computer Systems Lecture 9

G Programming Languages - Fall 2012

ECE251: Tuesday September 12

Systems Architecture The Stack and Subroutines

Compiling Techniques

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

Control Instructions

Stacks and Subroutines

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

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.

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

MIPS Functions and Instruction Formats

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

ECE 571 Advanced Microprocessor-Based Design Lecture 4

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

ARM Memory Addressing and Function Calls

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Instruction Set Architectures Part I: From C to MIPS. Readings:

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Compilers and computer architecture: A realistic compiler to MIPS

ECE251: Thursday September 13

F28HS2 Hardware-Software Interfaces. Lecture 6: ARM Assembly Language 1

Code Generation. Lecture 19

CSE Lecture In Class Example Handout

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Chapter 2: Instructions:

Systems Architecture I

Branch Instructions. R type: Cond

Instruction Set Architecture

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

COMPUTER ORGANIZATION AND DESIGN

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

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Developing StrongARM/Linux shellcode

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

COSC 6385 Computer Architecture. Instruction Set Architectures

Chapter 15. ARM Architecture, Programming and Development Tools

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

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

Transcription:

Programming the ARM Computer Design 2002, Lecture 4 Robert Mullins

2 Quick Recap The Control Flow Model Ordered list of instructions, fetch/execute, PC Instruction Set Architectures Types of internal storage Accumulator, stack and general purpose register machines Addressing modes Register indirect, displacement, memory indirect etc. Branch conditions Condition codes, condition registers, branch and compare Encoding Instructions Fixed length or variable length encodings Representing immediates, opcode and operands

Quick Recap Making the common case fast and Amdahl s Law - See H&P (chapters 1 and 2) The Big Picture - Interaction between the compiler, architecture and instruction set E.g. a poorly designed instruction set may make highperformance implementation difficult and restrict the effectiveness of an optimizing compiler. 3

Lecture 4 This Lecture Implementing functions and procedures The ARM Procedure Call Standard (APCS) Development tools Practical 4 of the ECAD labs 4

Functions and Procedures Structure program (abstraction/hierarchy) Package useful code so it can be reused Must use a well defined interface Questions How do we pass arguments to the function? How do we obtain the result? 5

Procedure Calls Register values are preserved by saving and restoring them from a stack in memory Agree on a standard, define how registers are used (part of ABI Application Binary Interface) Which registers hold arguments Which register holds the result (or pointer to) Some registers may need to be saved (preserved) by caller (caller-saved) Some may need to be saved by callee (callee-saved) 6

7 ARM Procedure Call Standard There are in fact many variants, lets look at base standard Four argument registers (r0-r3) Not preserved by routine/function (callee) May be saved by caller if necessary May be used to return results to caller Registers r4-r11 are typically used to hold the routine s local variables The value of these registers remains unchanged after a subroutine call Registers may need to be saved and restored by callee Registers r12-r15 have special dedicated roles e.g. the link register holds the return address

8 APCS Register Usage Convention Register 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 APCS Name a1 a2 a3 a4 v1 v2 v3 v4 v5 sb or v6 sl or v7 fp ip sp lr pc APCS Role argument 1 / result / scratch register for function argument 2 / result / scratch register for function argument 3 / result / scratch register for function argument 4 / result / scratch register for function variable register 1 variable register 2 variable register 3 variable register 4 variable register 5 static base or variable register 6 stack limit or variable register 7 frame pointer (pointer to start of current stack frame) or variable register 8 the intra-procedure-call scratch register lower end of current stack frame (stack pointer) the link register (return address) the program counter

Parameter Passing and result return a1-a4 (r0-r3) may be used to hold parameters Additional parameters may be passed on the stack Results may be returned in a1-a4 as a value or indirectly as an address 9

Simple function call MOV r0, #10 MOV r1, #5 BL max ;.. max CMP r0, r1 MOVLT r0, r1 ; if r0<r1 r0=r1 MOV pc, lr For simple leaf functions that only use r0-r3 (a1-a4) the function overhead is small as no registers need to be saved. In real programs around half the functions may be simple leaf functions like this. 10

Saving and restoring registers BL myfunction.. myfunction STMFD sp!, {r4-r10, lr} ; save registers.... LDMFD sp!, {r4-r10, pc} ; restore and ; return 11

12 The Stack Full-descending stack - ARM stack grows down, stack pointer points to last entry (not next free entry)

ARM Assembler Examples 13

Assembler Directives Information for the assembler Common uses Define and name new section, code or data Constants, aliases Allocate bytes of memory as data and initialize contents 14

Allocating memory for data Allocate bytes of memory (DCB) C_String DCB MyString, 0 Allocate words of memory (DCD) Data DCD 1234, 1, 5, 20 Reserve a zeroed block of memory (SPACE or %) table % 1024 ; 1024 bytes of zeroed ; memory 15

Writing Assembler, Hello World Example ; create new code area called hello AREA hello, CODE, READONLY SWI_WriteC EQU 0x3 ; symbolic name for constant SWI_Exit EQU 0x18 ENTRY ; entry point into program START ADR r1, TEXT ; ADD r1, pc, offset of TEXT LOOP LDRB r2, [r1] ; load character CMP r2, #0 ; check we are not at end of string MOV r0, #SWI_WriteC ; call putchar (r1 points to char) SWINE 0x123456 ; do system call ADD r1, r1, #1 ; increase our pointer BNE LOOP ; loop if we are not finished MOV r0, #SWI_Exit ; standard exit system call MOV r1, #0x20000 ADD r1,r1,#0x26 SWI 0x123456 TEXT DCB "Hello World\n", 0 END 16 ; end of source file!

17

The program in memory. 18

19 int fib (int a) { if (a<=1) return a; else return fib (a-1)+fib(a-2); } fib: stmfd r13!, {r4, r5, r14} ; preserve registers mov r4, r0 ; r4=a cmp r0, #1 ; compare a with 1 movle r0, r4 ; (if a<=1) result=a ldmlefd r13!, {r4, r5, pc} ; (if a<=1) return sub r0, r4, #1 bl fib ; fib (a-1) mov r5, r0 ; r5=fib(a-1) sub r0, r4, #2 bl fib ; fib (a-2) add r0, r5, r0 ; result=fib(a-1)+fib(a-2) ldmfd r13!, {r4, r5, pc} ; return

20

21

22 Fib(5) R5 R4 Return Address to Main() R4=5 Fib(R4-1) R5 R4 (a=5) Return address Fib(3).. etc. Fib(R4-2) Return sum

23 ECAD Workshop Four Sieve of Eratosthenes 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 1 0 1 0 1 0 1 0 1 0 1 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 1 0 1 0 1 1 1 0 1 0 1 1

Later Workshops Make sure you use procedure call standard correctly Use branch-and-link (BL) instruction to call procedure Pass parameters in correct registers Save registers when necessary Restore registers on exit 24

Next Lecture OS Support and Memory Management Virtual Memory Interrupts/Exceptions ARM specifics (operating modes, page table organisation etc.) 25

26 Self Study/Supervision Work Write a simple C program to sum the numbers 1 to n, compile it and examine the assembler produced You might want to try executing the program in the debugging environment, this will allow you to single step through the program. You can look at the contents of the registers and memory as you go. Challenge Write a program (in ARM assembly language) to reverse the bytes of a 32-bit register It s possible to do this using only one additional register to hold temporary results and 4 ARM instructions!