Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I

Slides:



Advertisements
Similar presentations
Multiplication – Microprocessor
Advertisements

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
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
INSTRUCTION SET ARCHITECTURES
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
COMP3221 lec10-logical-II&mul.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 10: C/Assembler Logical and Shift – II & Multiplication.
Chapter 2 Instructions: Language of the Computer
Chapter 2.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Computer Architecture CSCE 350
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
Modified from the notes by
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,
Normal C Memory Management °A program’s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
CS61C L7 MIPS: Load & Store, Decisions (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS 61C L07 MIPS Memory (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #7: MIPS Memory & Decisions.
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
From C to Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
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.
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
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.
Intel has developed a new technique to generate random numbers that is suitable for integration directly into the CPU! This circuit can turn out 2.4 billion.
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, …
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
Computer Architecture CSE 3322 Lecture 2 NO CLASS MON Sept 1 Course WEB SITE crystal.uta.edu/~jpatters.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
MIPS assembly. Computer  What’s in a computer?  Processor, memory, I/O devices (keyboard, mouse, LCD, video camera, speaker), disk, CD drive, …
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
1 TM 1 Embedded Systems Lab./Honam University ARM Microprocessor Programming Model.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Memory Access Instructions Load and Store Addressing Modes Memory Addressing. Base addressing mode. Load byte and store byte: lb, lbu, sb Address alignment.
MIPS: Load & Store, Decisions. Memory management review Three 3’s Picking blocks off free list  best-, first-, next-fit Attempts to solve external fragmentation.
CS2100 Computer Organisation
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
March 2006 Saeid Nooshabadi
Chapter 4 Addressing modes
March 2006 Saeid Nooshabadi
Lecturer SOE Dan Garcia
CS/COE0447 Computer Organization & Assembly Language
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
C second, objective-c, Go up!
Topic 6: Bitwise Instructions
Lecturer SOE Dan Garcia
3.
COMS 361 Computer Organization
COMS 361 Computer Organization
The ARM Instruction Set
COMS 361 Computer Organization
March 2006 Saeid Nooshabadi
Overheads for Computers as Components 2nd ed.
March, 2006 Saeid Nooshabadi
March 2006 Saeid Nooshabadi
9/27: Lecture Topics Memory Data transfer instructions
ARM Load/Store Instructions
Presentation transcript:

Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I August, 2003 Saeid Nooshabadi

Elec2041 lec-11-mem-I.2 Saeid Nooshabadi Overview °Memory Access in Assembly °Data Structures in Assembly

Elec2041 lec-11-mem-I.3 Saeid Nooshabadi Review:Instruction Set (ARM 7TDMI) °Set of instruction that a processor can execute °Instruction Categories Data Processing or Computational (Logical and Arithmetic Load/Store (Memory Access: or transferring data between memory and registers) Control Flow (Jump and Branch) Floating Point -coprocessor Memory Management Special Registers

Elec2041 lec-11-mem-I.4 Saeid Nooshabadi Review: ARM Instructions So far add, sub,mov and,bic, orr, eor Data Processing Instructions with shift and rotate lsl, lsr, asr, ror Multiplications mul, mla,umull, umlal, smull, smlal

Elec2041 lec-11-mem-I.5 Saeid Nooshabadi 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

Elec2041 lec-11-mem-I.6 Saeid Nooshabadi Data Transfer: Memory to Reg (#1/4) °To transfer a word of data, we need to specify two things: Register: specify this by number (r0 – r15) Memory address: more difficult -Think of memory as a single one- dimensional array, so we can address it simply by supplying a pointer to a memory address. -Other times, we want to be able to offset from this pointer. arr[0] arr[1] arr[2] arr[3] arr[4]

Elec2041 lec-11-mem-I.7 Saeid Nooshabadi Data Transfer: Memory to Reg (#2/4) °To specify a memory address to copy from, specify two things: A register which contains a pointer to memory A numerical offset (in bytes), or a register which contain an offset °The desired memory address is the sum of these two values. °Example: [v1, #8] specifies the memory address pointed to by the value in v1, plus 8 bytes °Example: [v1, v2] specifies the memory address pointed to by the value in v1, plus v2

Elec2041 lec-11-mem-I.8 Saeid Nooshabadi Data Transfer: Memory to Reg (#3/4) °Load Instruction Syntax: 1 2, [3, 4] where 1) operation name 2) register that will receive value 3) register containing pointer to memory 4) numerical offset in bytes, or another shifted index register °Instruction Name: ldr (meaning Load register, so 32 bits or one word are loaded at a time from memory to register)

Elec2041 lec-11-mem-I.9 Saeid Nooshabadi Data Transfer: Memory to Reg (#4/4) °Example: ldr a1, [v1, #8] This instruction will take the pointer in v1, add 8 bytes to it, and then load the value from the memory pointed to by this calculated sum into register a1 °Notes: v1 is called the base register 8 is called the offset offset is generally used in accessing elements of array: base reg points to beginning of array °Example: ldr a1, [v1, v2] This instruction will take the pointer in v1, add an index offset in register v2 to it, and then load the value from the memory pointed to by this calculated sum into register a1 °Notes: v1 is called the base register v2 is called the index register index is generally used in accessing elements of array using an variable index: base reg points to beginning of array arr[0] arr[1] arr[2] arr[3] 25 #8

Elec2041 lec-11-mem-I.10 Saeid Nooshabadi Data Transfer: Other Mem to Reg Variants (#1/2) °Pre Indexed Load Example: ldr a1, [v1,#12]! This instruction will take the pointer in v1, add 12 bytes to it, and then load the value from the memory pointed to by this calculated sum into register a1. Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v ). °Pre Indexed Load Example: ldr a1, [v1, v2]! This instruction will take the pointer in v1, add an index offset in register v2 to it, and then load the value from the memory pointed to by this calculated sum into register a1. Subsequently, v1 is updated by computed sum of v1 and v2, ( v1  v1 + v2 ).

Elec2041 lec-11-mem-I.11 Saeid Nooshabadi Data Transfer: Other Mem to Reg Variants (#2/2) °Post Indexed Load Example: ldr a1, [v1], #12 This instruction will load the value from the memory pointed to by value in register v1 into register a1. Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v ). °Example: ldr a1, [v1], v2 This instruction will load the value from the memory pointed by value in register v1, into register a1. Subsequently, v1 is updated by computed sum of v1 and v2, ( v1  v1 + v2 ).

Elec2041 lec-11-mem-I.12 Saeid Nooshabadi Data Transfer: Reg to Memory (1/2) °Also want to store value from a register into memory °Store instruction syntax is identical to Load instruction syntax °Instruction Name: str (meaning Store from Register, so 32 bits or one word are stored from register to memory at a time)

Elec2041 lec-11-mem-I.13 Saeid Nooshabadi Data Transfer: Reg to Memory (2/2) °Example: str a1,[v1, #12] This instruction will take the pointer in v1, add 12 bytes to it, and then store the value from register a1 into the memory address pointed to by the calculated sum °Example: str a1,[v1, v2] This instruction will take the pointer in v1, adds register v2 to it, and then store the value from register a1 into the memory address pointed to by the calculated sum.

Elec2041 lec-11-mem-I.14 Saeid Nooshabadi Data Transfer: Other Reg to Mem Variants (#1/2) °Pre Indexed Store Example: str a1, [v1,#12]! This instruction will take the pointer in v1, add 12 bytes to it, and then store the value from register a1 into the memory address pointed to by the calculated sum. Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v ). °Pre Indexed Store Example: str a1,[v1, v2]! This instruction will take the pointer in v1, adds register v2 to it, and then store the value from register a1 into the memory address pointed to by the calculated sum. Subsequently, v1 is updated by computed sum of v1 and v2 ( v1  v1 + v2 ).

Elec2041 lec-11-mem-I.15 Saeid Nooshabadi Data Transfer: Other Reg to Mem Variants (#2/2) °Post Indexed Store Example: str a1, [v1],#12 This instruction will store the value from register a1 into the memory address pointed to by register v1. Subsequently, v1 is updates by computed sum of v1 and 12, ( v1  v ). °Post Indexed Store Example: str a1,[v1], v2 This instruction will store the value from register a1 into the memory address pointed to by register v1. Subsequently, v1 is updated by computed sum of v1 and v2, ( v1  v1 + v2 ).

Elec2041 lec-11-mem-I.16 Saeid Nooshabadi Pointers v. Values °Key Concept: A register can hold any 32-bit value. That value can be a (signed) int, an unsigned int, a pointer (memory address), etc. °If you write addv3,v2,v1 then v1 and v2 better contain values °If you write ldr a1,[v1] then v1 better contain a pointer °Don’t mix these up!

Elec2041 lec-11-mem-I.17 Saeid Nooshabadi Addressing: Byte vs. halfword vs. word °Every word in memory has an address, similar to an index in an array °Early computers numbered words like C numbers elements of an array: Memory[0], Memory[1], Memory[2], … °Computers needed to access 8-bit bytes, half words (2 bytes/halfword) as well as words (4 bytes/word) °Today machines address memory as bytes, hence Half word addresses differ by 2 Memory[0], Memory[2], Memory[4], … word addresses differ by 4 Memory[0], Memory[4], Memory[8], … Called the “address” of a word

Elec2041 lec-11-mem-I.18 Saeid Nooshabadi Compilation with Memory °What offset in ldr to select my_Array[8] in C? ° 4x8=32 to select my_Array[8] : byte v. word °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: ldrv1, [v3,#32] ; v1 gets my_Array[8] Add 32 to v3 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]

Elec2041 lec-11-mem-I.19 Saeid Nooshabadi Same thing in pictures v1 v2 v3 g h my_Array[0] 0 0xFFFFFFFF my_Array[8] my_Array °ldr v1, [v3,#32] Adds offset “8 x 4 = 32” to select my_Array[8], and puts into a1 °The value in register v3 is an address °Think of it as a pointer into memory °v3 contains the address of the Base of the my_Array. °add v1, v2,v1 The value in register v1 is the sum of v2 and v1. v1 + v2 32

Elec2041 lec-11-mem-I.20 Saeid Nooshabadi Compile with variable index °What if array index not a constant? g = h + my_Array[i]; g: v1, h: v2, i: v3, v4 : base address of my_Array °To load my_Array[i] into a register, first turn i into a byte address; multiply by 4 °How multiply using adds? i + i = 2i, 2i + 2i = 4i mov a1,v3 ; a1 = i add a1,a1 ; a1 = 2*i add a1,a1 ; a1 = 4*i Better alternative: mov a1, v3, lsl #2

Elec2041 lec-11-mem-I.21 Saeid Nooshabadi Compile with variable index, con’t °Now load my_Array[i]= my_Array[0] + 4*i into v1 register: ldr v1, [v4, a1] ;v1= my_Array[i] °Finally add to h to it and put sum in g : add v1,v1, v2 ;g = h + my_Array[i]

Elec2041 lec-11-mem-I.22 Saeid Nooshabadi Compile with variable index: Summary °C statement: g = h + my_Array[i]; °Compiled ARM assembly instructions: mov a1, v3, lsl #2 ; a1 = 4*i ldr v1, [v4, a1] ;v1= my_Array[i] °Finally add to h to it and put sum in g : add v1,v1, v2 ;g = h + my_Array[i] Base Reg Index Reg

Elec2041 lec-11-mem-I.23 Saeid Nooshabadi Compile with variable index Example °Compile this into ARM code: B_Array[i] = h + A_Array[i]; h : v1, i:v2, v3 :base address of A_Array, v4: base address of B_Array

Elec2041 lec-11-mem-I.24 Saeid Nooshabadi Compile with variable index Example (Solution) °Compile this C code into ARM: B_Array[i] = h + A_Array[i]; h : v1, i:v2, v3 :base address of A_Array, v4: base address of B_Array mov a1, v2, lsl #2 ;a1 = 4*i ldr a2, [v3, a1] ;v4 + a1 = ;addrB_Array[i] ;a2= A_array[i] add a2, a2, v1 ;a2 = h + A_Array[i]; str a2, [v4, a1] ;v4 + a1 = ;addrB_Array[i] ;B_Array[i]= a2 Base Reg Index Reg

Elec2041 lec-11-mem-I.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

Elec2041 lec-11-mem-I.26 Saeid Nooshabadi Notes about Memory °Pitfall: Forgetting that sequential word addresses in machines with byte addressing do not differ by 1. Many an assembly language programmer has 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. 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)

Elec2041 lec-11-mem-I.27 Saeid Nooshabadi More Notes about Memory: Alignment (#1/2) 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

Elec2041 lec-11-mem-I.28 Saeid Nooshabadi More Notes about Memory: Alignment (#2/2) °Non-Aligned memory access causes byte rotation in right direction within the word x a2 2e 0x83 0x82 0x81 0x80 ldr a1, 0x80 a1 = 0x0982a22e ldr a1, 0x81 a1 = 0x2e0982a2 ldr a1, 0x82 a1 = 0xa22e0982 ldr a1, 0x83 a1 = 0x82a22e09

Elec2041 lec-11-mem-I.29 Saeid Nooshabadi Role of Registers vs. Memory °What if more variables than registers? Compiler tries to keep most frequently used variable in registers Writing less common to memory: spilling °Why not keep all variables in memory? Smaller is faster: registers are faster than memory Registers more versatile: -ARM Data Processing instructions can read 2, operate on them, and write 1 per instruction -ARM data transfer only read or write 1 operand per instruction, and no operation

Elec2041 lec-11-mem-I.30 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. °A pointer (used by ldr and str ) is just a memory address, so we can add to it or subtract from it (using offset). °Word Addresses n Memory should be word aligned

Elec2041 lec-11-mem-I.31 Saeid Nooshabadi “And in Conclusion…”(#2/2) °New Instructions: ldr, str