COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II

Slides:



Advertisements
Similar presentations
ARM versions ARM architecture has been extended over several versions.
Advertisements

Wat gaan we doen? harhaling data types
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Goal: Write Programs in Assembly
The University of Adelaide, School of Computer Science
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
COMP3221 lec23-decode.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 23: Instruction Representation; Assembly and Decoding.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Load and store instruction.
Introduction to Embedded Systems Intel Xscale® Assembly Language and C Lecture #3.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
COMP3221 lec10-logical-II&mul.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 10: C/Assembler Logical and Shift – II & Multiplication.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
The University of Adelaide, School of Computer Science
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
Chap.2: Instructions: Language of the computer Jen-Chang Liu, Spring 2006 Adapted from
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Memory Access March,
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
MIPS assembly. Computer What’s in a computer? Processor, memory, I/O devices (keyboard, mouse, LCD, video camera, speaker), disk, CD drive, …
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE Lecture 15 & 16 Stacks, Endianness Addressing Modes Course Instructor: Engr. Aisha Danish.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
1 Computer Architecture COSC 3430 Lecture 3: Instructions.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Arrays and Strings in Assembly
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
Address alignment When a word (4-bytes) is loaded or stored the memory address must be a multiple of four. This is called an alignment restriction. Addresses.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Architecture & Operations I
Computer Architecture & Operations I
Chapter 4 Copying Data.
ECE 3430 – Intro to Microcomputer Systems
Chapter 4 Addressing modes
April 2006 Saeid Nooshabadi
COMP3221: Microprocessors and Embedded Systems
EECE-276 Fall 2003 Microprocessors & Microcontrollers II
CS170 Computer Organization and Architecture I
ARM Memory.
The University of Adelaide, School of Computer Science
C second, objective-c, Go up!
ARM Load/Store Instructions
3.
Optimizing ARM Assembly
COMS 361 Computer Organization
March 2006 Saeid Nooshabadi
Overheads for Computers as Components 2nd ed.
ARM Memory.
March 2006 Saeid Nooshabadi
9/27: Lecture Topics Memory Data transfer instructions
ARM Load/Store Instructions
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II August, 2003 Saeid Nooshabadi

COMP3221 lec-12-mem-II.2 Saeid Nooshabadi Overview °Word/ Halfword/ Byte Addressing °Byte ordering °Signed Load Instructions °Instruction Support for Characters

COMP3221 lec-12-mem-II.3 Saeid Nooshabadi Review: Assembly Operands: Memory °C variables map onto registers; what about large data structures like arrays? °1 of 5 components of a computer: memory contains such data structures °But ARM arithmetic instructions only operate on registers, never directly on memory. °Data transfer instructions transfer data between registers and memory: Memory to register Register to memory

COMP3221 lec-12-mem-II.4 Saeid Nooshabadi Review: Data Transfer: Memory  Reg °Example: ldr a1, [v1, #8] °Example: ldr a1, [v1, v2] °Example: ldr a1, [v1,#12]! Pre Indexed Load: Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v ). °Example: ldr a1, [v1, v2]! Pre Indexed Load: Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v1 + v2 ). °Example: ldr a1, [v1],#12 Post Indexed Load: Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v ). °Example: ldr a1, [v1], v2 Post Indexed Load: Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v1 + v2 ). Similar instructions For STR

COMP3221 lec-12-mem-II.5 Saeid Nooshabadi Review: Memory Alignment Aligned Not Aligned °ARM requires that all words start at addresses that are multiples of 4 bytes °Called Alignment: objects must fall on address that is multiple of their size. °Some machines like Intel allow non-aligned accesses

COMP3221 lec-12-mem-II.6 Saeid Nooshabadi Data Transfer: More Mem to Reg Variants (#1/2) °Load Byte Example: ldrb a1, [v1,#12] This instruction will take the pointer in v1, add 12 bytes to it, and then load the byte value from the memory pointed to by this calculated sum into register a1. °Load Byte Example: ldrb a1, [v1, v2] This instruction will take the pointer in v1, add an index offset in register v2 to it, and then load the byte value from the memory pointed to by this calculated sum into register a1. 1 word = 4 Bytes v1 a

COMP3221 lec-12-mem-II.7 Saeid Nooshabadi Data Transfer: More Mem to Reg Variants (#2/2) °Load Half Word Example: ldrh a1, [v1,#12] This instruction will take the pointer in v1, add 12 bytes to it, and then load the half word value from the memory pointed to by this calculated sum into register a1. °Load Byte Example: ldrh a1, [v1, v2] This instruction will take the pointer in v1, add an index offset in register v2 to it, and then load the half word value from the memory pointed to by this calculated sum into register a1. 1 word = 4 Bytes v1 a

COMP3221 lec-12-mem-II.8 Saeid Nooshabadi Data Transfer: More Reg to Mem Variants (#1/2) °Store Byte Example: strb a1, [v1,#12] This instruction will take the pointer in v1, add 12 bytes to it, and then store the value from lsb Byte of register a1 into the memory address pointed to by the calculated sum. °Store Byte Example: strb a1,[v1, v2] This instruction will take the pointer in v1, adds register v2 to it, and then store the value from lsb Byte of register a1 into the memory address pointed to by the calculated sum. 1 word = 4 Bytes v1 a1 +12

COMP3221 lec-12-mem-II.9 Saeid Nooshabadi Data Transfer: More Reg to Mem Variants (#2/2) °Store Half Word Example: strh a1, [v1,#12] This instruction will take the pointer in v1, add 12 bytes to it, and then store the value from half word of register a1 into the memory address pointed to by the calculated sum. °Store Half Word Example: strh a1,[v1, v2] This instruction will take the pointer in v1, adds register v2 to it, and then store the value from half word of register a1 into the memory address pointed to by the calculated sum. 1 word = 4 Bytes v1 a

COMP3221 lec-12-mem-II.10 Saeid Nooshabadi Compilation with Memory (Byte Addressing) °What offset in ldr to select my_Array[8] (defined as Char ) in C? ° 1x8=8 to select my_Array[8] : byte °Compile by hand using registers: g = h + my_Array[8]; g : v1, h : v2, v3 :base address of my_Array °1st transfer from memory to register: ldrb v1, [v3,#8] ; v1 gets my_Array[8] Add 8 to r3 to select my_Array[8], put into v1 °Next add it to h and place in g add v1,v2,v1; v1 = h+ my_Array[8]

COMP3221 lec-12-mem-II.11 Saeid Nooshabadi Compilation with Memory (half word Addressing) °What offset in ldr to select my_Array[8] (defined as halfword) in C? ° 2x8=16 to select my_Array[8] : byte °Compile by hand using registers: g = h + my_Array[8]; g : v1, h : v2, v3 :base address of my_Array °1st transfer from memory to register: ldrh v1, [v3, #16] ; v1 gets my_Array[8] Add 16 to r3 to select my_Array[8], put into v1 °Next add it to h and place in g add v1,v2,v1; v1 = h+ my_Array[8]

COMP3221 lec-12-mem-II.12 Saeid Nooshabadi More Notes about Memory: Word °How are bytes numbered in a word? msblsb little endian byte big endian byte 0 ‘P’ ’M’ ‘O’ ‘C’ ‘1’ ‘2’ ‘3’ “C O M P ” “ ” ‘C’ ‘O’ ’M’ ‘P’ ‘3’ ‘2’ ‘1’ Gulliver’s Travels: Which end of egg to open? Cohen, D. “On holy wars and a plea for peace (data transmission).” Computer, vol.14, (no.10), Oct p Little Endian address of least significant byte: Intel 80x86, DEC Alpha, Big Endian address of most significant byte HP PA, IBM/Motorola PowerPC, SGI, Sparc ARM is Little Endian by default, However it can be made Big Endian by configuration.

COMP3221 lec-12-mem-II.13 Saeid Nooshabadi Endianess Example r0 = 0x Little-endian r1 = 0x r2 = 0x r2 = 0x11 LDRB r2, [r1] Big-endian r1 = 0x100 STR r0, [r1] Memory

COMP3221 lec-12-mem-II.14 Saeid Nooshabadi Code Example °Write a segment of code that add together elements x to x+(n-1) of an array, where the element x = 0 is the first element of the array. °Each element of the array is word sized (ie. 32 bits). °The segment should use post-indexed addressing. °At the start of your segments, you should assume that: a1 points to the start of the array. a2 = x a3 = n a1 x + (n - 1) x + 1 x Elements { n elements 0

COMP3221 lec-12-mem-II.15 Saeid Nooshabadi Code Example: Sample Solution add a1, a1, a2, lsl #2; Set a1 to address ; of element x add a3, a1, a3, lsl #2; Set a3 to address ; of element x +(n-1) mov a2, #0; Initialise ;accumulator Loop: ldr a4, [a1], #4; Access element and ; move to next add a2, a2, a4; Add contents to ; counter cmp a1, a3; Have we reached ; element x+n? blt loop; If not - repeat ; for next element ; on exit sum ; contained in a2

COMP3221 lec-12-mem-II.16 Saeid Nooshabadi Sign Extension and Load Byte & Load Half Word °ARM instruction ( ldrsb ) automatically extends “sign” of byte for load byte SSSSSSSSSSSSSSSSSSSSSSSSS S °ARM instruction ( ldrsh ) automatically extends “sign” of half word for load half word SSSSSSSSSSSSSSSS S S 15 ldrsh a1, [v1,#12]ldrsh a1, [v1,v2] ldrsb a1, [v1,#12]ldrsb a1, [v1,v2]

COMP3221 lec-12-mem-II.17 Saeid Nooshabadi Instruction Support for Characters °ARM (and most other instruction sets) include instructions to operate on bytes: move byte ( ldrb ) loads a byte from memory/reg, placing it in rightmost 8 bits of a register, or vice versa °Declares byte variables in C as “ char ” °Assume x, y are declared char. x in memory at [v1,#4] and y at [v1,#0]. What is ARM code for x = y ; ? ldrb a1, [v1,#0] strb a1, [v1,#4]; transfer y to x

COMP3221 lec-12-mem-II.18 Saeid Nooshabadi Strings in C: Example °String simply an array of char void strcpy (char x[], char y[]){ int i = 0; /* declare,initialize i*/ while ((x[i] = y[i]) != ’\0’) /* 0 */ i = i + 1; /* copy and test byte */ } °function i, addr. of x[0], addr. of y[0]: v1, a1, a2, func ret addr. :lr strcpy: mov v1, #-1 ; i = -1 L1:add v1, v1, #1 ; i =i + 1 ldrb a3, [a2,v1] ; a1= y[i] strb a3, [a1,v1] ; x[i]=y[i] cmp a3, #0 bne L1 ; y[i]!=0 ;goto L1 mov pc, lr ; return

COMP3221 lec-12-mem-II.19 Saeid Nooshabadi Strings in C: Example using pointers °String simply an array of char void strcpy2 (char *px, char *py){ while ((*px++ = *py++) != ’\0’) /* 0 */ ; /* copy and test byte */ } °function addr. of x[0], addr. of y[0]: v2, v3 func ret addr. :lr strcpy: L1: ldrb a1, [v3],#1 ;a1= *py, py = py +1 strb a1, [v2],#1 ;*px = *py, px = px +1 cmp a1, #0 bne L1 ; py!=0 goto L1 mov pc, lr ; return °ideally compiler optimizes code for you

COMP3221 lec-12-mem-II.20 Saeid Nooshabadi Block Copy Transfer (#1/5) °Consider the following code: str a1, [v1],#4 str a2, [v1],#4 str a3, [v1],#4 str a4, [v1],#4 v1 Replace this with stmia v1!, {a1-a4} STMIA : SORE MULTIPLE INCREMENT AFTER v1 °Consider the following code: str a1, [v1, #4]! str a2, [v1, #4]! str a3, [v1, #4]! str a4, [v1, #4]! Replace this with stmib v1!, {a1-a4} STMIB : SORE MULTIPLE INCREMENT BEFORE a1 a2 a3 a4 v1 a1 a2 a3 a4 0x100 0x104 0x108 0x112 0x100 0x104 0x108 0x112

COMP3221 lec-12-mem-II.21 Saeid Nooshabadi Block Copy Transfer (#2/5) °Consider the following code: str a1, [v1],#-4 str a2, [v1],#-4 str a3, [v1],#-4 str a4, [v1],#-4 v1 Replace this with stmda v1!, {a1-a4} STMDA : SORE MULTIPLE DECREMENT AFTER v1 °Consider the following code: str a1, [v1, #-4]! str a2, [v1, #-4]! str a3, [v1, #-4]! str a4, [v1, #-4]! Replace this with stmdb v1!, {a1-a4} STMDB : SORE MULTIPLE DECREMENT BEFORE a1 a2 a3 a4 v1 a1 a2 a3 a4 0x100 0x104 0x108 0x112 0x100 0x104 0x108 0x112

COMP3221 lec-12-mem-II.22 Saeid Nooshabadi Block Copy Transfer (#3/5) °Consider the following code: str a1, [v1] str a2, [v1,#4] str a3, [v1,#8] str a4, [v1,#12] v1 Replace this with stmia v1, {a1-a4} STMIA : SORE MULTIPLE INCREMENT AFTER °Consider the following code: str a1, [v1, #4] str a2, [v1, #8] str a3, [v1, #12] str a4, [v1, #16] Replace this with stmib v1, {a1-a4} STMIB : SORE MULTIPLE INCREMENT BEFORE a1 a2 a3 a4 v1 a1 a2 a3 a4 v1 0x100 0x104 0x108 0x112 0x100 0x104 0x108 0x112

COMP3221 lec-12-mem-II.23 Saeid Nooshabadi Block Copy Transfer (#4/5) °Consider the following code: str a1, [v1] str a2, [v1,#-4] str a3, [v1,#-8] str a4, [v1,#-12] v1 Replace this with stmda v1, {a1-a4} STMDA : SORE MULTIPLE DECREMENT AFTER °Consider the following code: str a2, [v1,#-4] str a3, [v1,#-8] str a4, [v1,#-12] str a1, [v1,#16] Replace this with stmdb v1, {a1-a4,} STMDB : SORE MULTIPLE DECREMENT BEFORE a1 a2 a3 a4 v1 a1 a2 a3 a4 v1 0x100 0x104 0x108 0x112 0x100 0x104 0x108 0x112

COMP3221 lec-12-mem-II.24 Saeid Nooshabadi Block Data Transfer (#5/5) °Similarly we have LDMIA : Load Multiple Increment After LDMIB : Load Multiple Increment Before LDMDA : Load Multiple Decrement After LDMDB : Load Multiple Decrement Before For details See Chapter 3, page 61 – 62 Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN:

COMP3221 lec-12-mem-II.25 Saeid Nooshabadi COMP3221 Reading Materials (Week #4) °Week #4: Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN: We use chapters 3 and 5 °ARM Architecture Reference Manual –On CD ROM

COMP3221 lec-12-mem-II.26 Saeid Nooshabadi “And in Conclusion…” (#1/2) °In ARM Assembly Language: Registers replace C variables One Instruction (simple operation) per line Simpler is Better Smaller is Faster °Memory is byte-addressable, but ldr and str access one word at a time. °Access byte and halfword using ldrb, ldrh,ldrsb and ldrsh °A pointer (used by ldr and str ) is just a memory address, so we can add to it or subtract from it (using offset).

COMP3221 lec-12-mem-II.27 Saeid Nooshabadi “And in Conclusion…”(#2/2) °New Instructions: ldr, str ldrb, strb ldrh, strh ldrsb, ldrsh