Lecture 4 (part 2): Data Transfer Instructions

Similar documents
Overview COMP Microprocessors and Embedded Systems. Lecture 11: Memory Access - I. August, 2003

Bitwise Instructions

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

Control Flow Instructions

ECE 471 Embedded Systems Lecture 5

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

ARM Cortex-M4 Programming Model Memory Addressing Instructions

ECE 498 Linux Assembly Language Lecture 5

Topic 10: Instruction Representation

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

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

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

EE251: Tuesday September 5

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

Lab5 2-Nov-18, due 16-Nov-18 (2 weeks duration) Lab6 16-Nov-19, due 30-Nov-18 (2 weeks duration)

ARM Instruction Set. Introduction. Memory system. ARM programmer model. The ARM processor is easy to program at the

LAB 1 Using Visual Emulator. Download the emulator Answer to questions (Q)

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

The ARM Instruction Set

ARM Cortex-A9 ARM v7-a. A programmer s perspective Part 2

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

Comparison InstruCtions

ECE 571 Advanced Microprocessor-Based Design Lecture 3

Chapter 15. ARM Architecture, Programming and Development Tools

Exam 1. Date: Oct 4, 2018

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

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

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

18-349: Introduction to Embedded Real- Time Systems Lecture 3: ARM ASM

ARM Assembly Programming

CS 310 Embedded Computer Systems CPUS. Seungryoul Maeng

Chapter 15. ARM Architecture, Programming and Development Tools

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

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

Chapter 3: ARM Instruction Set [Architecture Version: ARMv4T]

ECE 471 Embedded Systems Lecture 6

UCB CS61C : Machine Structures

ARM Assembly Language. Programming

Chapters 5. Load & Store. Embedded Systems with ARM Cortex-M. Updated: Thursday, March 1, 2018

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

ECE 471 Embedded Systems Lecture 9

ARM-7 ADDRESSING MODES INSTRUCTION SET

Instruction Set. ARM810 Data Sheet. Open Access - Preliminary

Writing ARM Assembly. Steven R. Bagley

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

Lecture #6 Intro MIPS; Load & Store

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

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

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

Processor Status Register(PSR)

Architecture. Digital Computer Design

The ARM Cortex-M0 Processor Architecture Part-2

Overview COMP 3221 Bitwise Logical Operations

Introduction to the ARM Processor Using Intel FPGA Toolchain. 1 Introduction. For Quartus Prime 16.1

ARM Assembly Language

STEVEN R. BAGLEY ARM: PROCESSING DATA

CprE 488 Embedded Systems Design. Lecture 3 Processors and Memory

The Original Instruction Pipeline

Chapter 2. Instructions: Language of the Computer

ARM7TDMI Instruction Set Reference

Cortex M3 Programming

Introduction to Embedded Systems EE319K (Gerstlauer), Spring 2013

ECE 471 Embedded Systems Lecture 7

Introduction to the ARM Processor Using Altera Toolchain. 1 Introduction. For Quartus II 14.0

ARM Cortex-M4 Programming Model Logical and Shift Instructions

The PAW Architecture Reference Manual

Basic ARM InstructionS

The ARM Instruction Set Architecture

Systems Architecture The ARM Processor

Assembler: Basics. Alberto Bosio October 20, Univeristé de Montpellier

Chapter 2 Instructions: Language of the Computer

ARM Instruction Set 1

The ARM Architecture T H E A R C H I T E C T U R E F O R TM T H E D I G I T A L W O R L D

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

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Lecture 3: Instruction Set Architecture

ECE251: Intro to Microprocessors Name: Solutions Mid Term Exam October 4, 2018

Assembly Language. CS2253 Owen Kaser, UNBSJ

Instruction Set Architecture (ISA)

Assembly Language Programming

The ARM processor. Morgan Kaufman ed Overheads for Computers as Components

Embedded assembly is more useful. Embedded assembly places an assembly function inside a C program and can be used with the ARM Cortex M0 processor.

ARM Instruction Set Dr. N. Mathivanan,

Chapter 5 The LC-3. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

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

Developing StrongARM/Linux shellcode

EEL 5722C Field-Programmable Gate Array Design

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

3. The Instruction Set

CSE /003, Fall 2014, Homework 4 Due October 7, 2014 in Class (at 2:00pm for 002, 3:30pm for 003)

Solution to the Problems and Questions

A Bit of History. Program Mem Data Memory. CPU (Central Processing Unit) I/O (Input/Output) Von Neumann Architecture. CPU (Central Processing Unit)

The ARM Architecture

Exam 1. EE319K Spring 2013 Exam 1 (Practice 1) Page 1. Date: February 21, 2013; 9:30-10:45am. Printed Name:

Lecture 2: Number Representa2on

CS61C : Machine Structures

A block of memory (FlashROM) starts at address 0x and it is 256 KB long. What is the last address in the block?

Lecture 15 ARM Processor A RISC Architecture

Transcription:

Lecture 4 (part 2): Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University of California, San Diego

Assembly Operands: Memory v Memory: Think of as single one-dimensional array where each cell v Stores a byte size value v Is referred to by a 32 bit address e.g. value at 0x4000 is 0x0a 0x0a 0x0b 0x0c 0x0d 0x4000 0x4001 0x4002 0x4003 v Data is stored in memory as: variables, arrays, structures v But ARM arithmetic instructions only operate on registers, never directly on memory. v Data transfer instructions transfer data between registers and memory: v Memory to register or LOAD from memory to register v Register to memory or STORE from register to memory

Load/Store Instructions v The ARM is a Load/Store Architecture: v Does not support memory to memory data processing operations. v Must move data values into registers before using them. v This might sound inefficient, but in practice isn t: v Load data values from memory into registers. v Process data in registers using a number of data processing instructions which are not slowed down by memory access. v Store results from registers out to memory.

Load/Store Instructions v The ARM has three sets of instructions which interact with main memory. These are: v Single register data transfer (LDR/STR) v Block data transfer (LDM/STM) v Single Data Swap (SWP) v The basic load and store instructions are: v Load and Store Word or Byte or Halfword v LDR / STR / LDRB / STRB / LDRH / STRH

Single register data transfer LDR STR Word LDRB STRB Byte LDRH STRH Halfword LDRSB LDRSH Signed byte load Signed halfword load v Memory system must support all access sizes v Syntax: v LDR{<cond>}{<size>} Rd, <address> v STR{<cond>}{<size>} Rd, <address> e.g. LDREQB

Data Transfer: Memory to Register v To transfer a word of data, we need to specify two things: v Register: r0-r15 v Memory address: more difficult v How do we specify the memory address of data to operate on? v We will look at different ways of how this is done in ARM Remember: Load value/data FROM memory

Addressing Modes v There are many ways in ARM to specify the address; these are called addressing modes. v Two basic classification 1. Base register Addressing Register holds the 32 bit memory address Also called the base address 2. Base Displacement Addressing mode An effective address is calculated : Effective address = < Base address +offset> Base address in a register as before Offset can be specified in different ways

Base Register Addressing Modes v Specify a register which contains the memory address v In case of the load instruction (LDR) this is the memory address of the data that we want to retrieve from memory v In case of the store instruction (STR), this is the memory address where we want to write the value which is currently in a register v Example: [r0] v specifies the memory address pointed to by the value in r0

Data Transfer: Memory to Register v Load Instruction Syntax: 1 2, [3] v where 1) operation name 2) register that will receive value 3) register containing pointer to memory v ARM Instruction Name: v LDR (meaning Load Register, so 32 bits or one word are loaded at a time)

Data Transfer: Memory to Register v LDR r2,[r1] This instruction will take the address in r1, and then load a 4 byte value from the memory pointed to by it into register r2 v Note: r1 is called the base register r1 0x200 Base Register 0x200 0x201 0x202 0x203 Memory 0xaa 0xbb 0xcc 0xdd r2 0xddccbbaa Destination Register for LDR

Data Transfer: Register to Memory v STR r2,[r1] This instruction will take the address in r1, and then store a 4 byte value from the register r2 to the memory pointed to by r1. v Note: r1 is called the base register r1 0x200 Base Register 0x200 0x201 0x202 0x203 Memory 0xaa 0xbb 0xcc 0xdd r2 0xddccbbaa Source Register for STR

Base Displacement Addressing Mode v To specify a memory address to copy from, specify two things: v A register which contains a pointer to memory v A numerical offset (in bytes) v The effective memory address is the sum of these two values. v Example: [r0,#8] v specifies the memory address pointed to by the value in r0, plus 8 bytes

Base Displacement Addressing Mode 1. Pre-indexed addressing syntax: I. Base register is not updated LDR/STR <dest_reg>[<base_reg>,offset] Examples: LDR/STR r1 [r2, #4]; offset: immediate 4 ;The effective memory address is calculated as r2+4 LDR/STR r1 [r2, r3]; offset: value in register r3 ;The effective memory address is calculated as r2+r3 LDR/STR r1 [r2, r3, LSL #3]; offset: register value *2 3 ;The effective memory address is calculated as r2+r3*2 3

Base Displacement Addressing Mode 1. Pre-indexed addressing: I. Base register is not updated: LDR/STR <dest_reg>[<base_reg>,offset] II. Base register is first updated, the updated address is used LDR/STR <dest_reg>[<base_reg>,offset]! Examples: LDR/STR r1 [r2, #4]!; offset: immediate 4 ;r2=r2+4 LDR/STR r1 [r2, r3]!; offset: value in register r3 ;r2=r2+r3 LDR r1 [r2, r3, LSL #3]!; offset: register value *2 3 ;r2=r2+r3*2 3

Base Displacement, Pre-Indexed v Example: LDR r0,[r1,#12] This instruction will take the pointer in r1, add 12 bytes to it, and then load the value from the memory pointed to by this calculated sum into register r0 v Example: STR r0,[r1,#-8] v Notes: This instruction will take the pointer in r0, subtract 8 bytes from it, and then store the value from register r0 into the memory address pointed to by the calculated sum v r1 is called the base register v #constant is called the offset v offset is generally used in accessing elements of array or structure: base reg points to beginning of array or structure

Pre indexed addressing What is the value in r1 after the following instruction is executed? STR r2,[r1, #-4]! A. 0x200 B. 0x1fc C. 0x196 D. None of the above Memory r1 0x200 Base Register 0x20_ 0x20_ 0x20_ 0x20_ 0xaa 0xbb 0xcc 0xdd r2 0xddccbbaa Destination Register for LDR

Base Displacement Addressing Mode 1. Post-indexed addressing:base register is updated after load/ store LDR/STR <dest_reg>[<base_reg>],offset Examples: LDR/STR r1 [r2], #4; offset: immediate 4 ;Load/Store to/from memory address in r2, update r2=r2+4 LDR/STR r1 [r2], r3; offset: value in register r3 ;Load/Store to/from memory address in r2, update r2=r2+r3 LDR r1 [r2] r3, LSL #3; offset: register value left shifted ;Load/Store to/from memory address in r2, update r2=r2+r3*2 3

Post-indexed Addressing Mode * Example: STR r0, [r1], #12 Memory Updated Base Register r1 0x20c Offset 12 0x20c r0 0x5 Source Register for STR 0x200 0x5 r1 Original Base 0x200 Register * If r2 contains 3, auto-increment base register to 0x20c by multiplying this by 4: STR r0, [r1], r2, LSL #2 * To auto-increment the base register to location 0x1f4 instead use: STR r0, [r1], #-12

Using Addressing Modes Efficiently * Imagine an array, the first element of which is pointed to by the contents of r0. * If we want to access a particular element, then we can use pre-indexed addressing: r1 is element we want. LDR r2, [r0, r1, LSL #2] Pointer to start of array * If we want to step through every 1 element of the array, for instance r0 0 to produce sum of elements in the array, then we can use post-indexed addressing within a loop: r1 is address of current element (initially equal to r0). LDR r2, [r1], #4 element 3 2 Memory Offset 12 8 4 0 Use a further register to store the address of final element, so that the loop can be correctly terminated.

Pointers vs. Values v Key Concept: A register can hold any 32-bit value. That value can be a (signed) int, an unsigned int, a pointer (memory address), and so on v If you write ADD r2,r1,r0 then r0 and r1 better contain values v If you write LDR r2,[r0] then [r0] better contain a pointer v Don t mix these up!

Compilation with Memory v What offset in LDR to select A[8] in C? v 4x8=32 to select A[8]: byte vs word v Compile by hand using registers: g = h + A[8];! v g: r1, h: r2, r3:base address of A v 1st transfer from memory to register:!ldr r0,[r3, #32] ; r0 gets A[8] v Add 32 to r3 to select A[8], put into r0! v Next add it to h and place in g ADD r1,r2,r0 ; r1 = h+a[8]

Notes about Memory v Pitfall: Forgetting that sequential word addresses in machines with byte addressing do not differ by 1. v Many assembly language programmers have toiled over errors made by assuming that the address of the next word can be found by incrementing the address in a register by 1 instead of by the word size in bytes. v So remember that for both LDR and STR, the sum of the base address and the offset must be a multiple of 4 (to be word aligned)

More Notes about Memory: Alignment v ARM typically requires that all words start at byte addresses that are multiples of 4 bytes Aligned! Not! Aligned! 0 1 2 3! Last hex digit of address is:! 0, 4, 8, or C hex! 1, 5, 9, or D hex! 2, 6, A, or E hex! 3, 7, B, or F hex! v Called Alignment: objects must fall on address that is multiple of their size.

Role of Registers vs. Memory v What if more variables than registers? v Compiler tries to keep most frequently used variables in registers v Why not keep all variables in memory? v Smaller is faster: registers are faster than memory v Registers more versatile: v ARM arithmetic instructions can read 2, operate on them, and write 1 per instruction v ARM data transfer only read or write 1 operand per instruction, and no operation

Conclusion v Memory is byte-addressable, but LDR and STR access one word at a time. v A pointer (used by LDR and STR) is just a memory address, so we can add to it or subtract from it (using offset).

v Instructions so far: v Previously: Conclusion ADD, SUB, MUL, MULA, [U S]MULL, [U S]MLAL, RSB AND, ORR, EOR, BIC MOV, MVN LSL, LSR, ASR, ROR New: LDR, LDR, STR, LDRB, STRB, LDRH, STRH