Arrays In high-level languages, we have several technigues available for constucting data stuctures: −One-dimensional arrays −Multi-dimensional arrays.

Slides:



Advertisements
Similar presentations
Branches Two branch instructions:
Advertisements

1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Chapter 6 Data Structures
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.
There are two types of addressing schemes:
COMP3221 lec18-pointers.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly
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 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
Chapter 11 Instruction Sets
Topics covered: ARM Instruction Set Architecture CSE 243: Introduction to Computer Architecture and Hardware/Software Interface.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
ARM C Language & Assembler. Using C instead of Java (or Python, or your other favorite language)? C is the de facto standard for embedded systems because.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Thumb Instruction Set.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE Lecture 15 & 16 Stacks, Endianness Addressing Modes Course Instructor: Engr. Aisha Danish.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
CSC 3210 Computer Organization and Programming Chapter 5 THE STACK D.M. Rasanjalee Himali.
Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
4-Oct Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept  direct mode: OK for static addresses  indirect register mode:
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Computer Organization
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Instruction Sets: Addressing modes and Formats Group #4  Eloy Reyes  Rafael Arevalo  Julio Hernandez  Humood Aljassar Computer Design EEL 4709c Prof:
Addressing Modes and Formats
Assembly - Arrays תרגול 7 מערכים.
Arrays and Strings in Assembly
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
Addressing Modes Dr. Hadi Hassan.  Two Basic Questions  Where are the operands?  How memory addresses are computed?  Intel IA-32 supports 3 fundamental.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Computer Architecture
Displacement (Indexed) Stack
Computer Architecture Instruction Set Architecture
Chapter 15: Higher Level Constructs
CS2100 Computer Organisation
ECE 3430 – Intro to Microcomputer Systems
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
Processor Instructions set. Learning Objectives
Alvaro Mauricio Peña Dariusz Niworowski Frank Rodriguez
Chapter 4 Addressing modes
William Stallings Computer Organization and Architecture 8th Edition
ARM Arrays.
Passing by-value vs. by-reference in ARM
ARM Load/Store Instructions
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
April 2006 Saeid Nooshabadi
You are the manager of a team of ARM programmers
ARM Load/Store Instructions
Presentation transcript:

Arrays In high-level languages, we have several technigues available for constucting data stuctures: −One-dimensional arrays −Multi-dimensional arrays −Structs −Bit sets −Linked lists −Trees −etc At the assembly language level, all of these approaches map onto single-dimension arrays alone. In order to emulate one of the other structures, we must create a mapping from the high-level approach to an offset into a linear list of memory bytes.

Arrays The origin of an array is not always the same: In C, C++, and Java, arrays start with index 0 In BASIC and FORTRAN, arrays start with index 1 In Pascal and Delphi arrays may start at any index chosen by the programmer The simplest data structure is the one-dimensional array A one-dimensional array closely matches its equivalent structure in assembly language. Associated with each array is the base address, usually denoted by the name of the array

One-dimensional Arrays Storage allocation low addresses address of a[i] = base address base address + i * element_size) for 0-origin indexing index i address of a[i] address of a[i] = base address + ((i – origin) * element_size) for nonzero-origin indexing high addresses a[0] a[1]... a[i]...

One-dimensional Arrays (low addresses) base address base + 4 base + 8 base + i*4 base + (i+1)*4 (high addresses)... other... stuff... a[0] a[1]... a[i]... a[n-1] other stuff... Index i

One-dimensional Arrays In order to work with arrays, we must first learn how to represent an array. Consider the following C declaration: int a[100]; This is just 100 integers. In ARM, we have to make room for 400 bytes (4 * 100).data.align 2 a:.skip 400

One-dimensional Arrays To reference any element in an array we need to have both the starting address of the array (the base address) and the index of the desired element. In ARM, the base address of an array must be in a register. The easiest method to get the base address of an array into a register in ARM assembly language is to use the ldr pseudo-instruction ldr rb, =a

Review of ARM Addressing Modes Accessing an array involves calculating the address of the accessed item. Recall that the ARM instruction set provides several indexing modes for accessing array elements: [Rn] Register ldr r0, [r1] [Rn, #±imm] Immediate offset ldr r2, [r1, r2 ← *(r1 + 12) [Rn, ±Rm]Register offset [Rn, ±Rm, shift]Scaled register offset

One-dimensional Arrays [Rn, #±imm]! Immediate pre-indexed ldr r2, [r1, ← r then r2 ← *r1 [Rn, ±Rm]!Register pre-indexed [Rn, ±Rm, shift]!Scaled register pre-indexed [Rn], #±imm Immediate post-indexed ldr r2, [r1], r2 ← *r1 then r1 ← r1 + 4 [Rn], ±RmRegister post-indexed [Rn], ±Rm, shiftScaled register post-indexed

One-dimensional Arrays To access an array element in a stack-allocated array, 0- origin using ldr/str, use one of the following address calculations: 1) ldr rd, [rb, ea = value in rb + n 2) ldr rd, [rb, ea = value in rb + value in rn 3) ldr rd, [rb, rn, lsl ea = value in rb + 4*value in rn 4) ldr rd, [rb, updates rb Similarly for ldrh, except use lsl #1 in case 3 and use #2 in case 4. Access an array element for stores in an analogous manner

Load/Store Exercise Assume an array of 25 integers. A compiler associates y with r1. Assume that the base address for the array is located in r0. Translate this C statement/assignment: array[10] = array[5] + y;

Load/Store Exercise Solution Assume an array of 25 integers. A compiler associates y with r1. Assume that the base address for the array is located in r0. Translate this C statement/assignment: array[10] = array[5] + y; mov r2, #5 mov r3, #10 ldr r4, [r0, r2, lsl r4 = array[5] add r4, r4, r4 = array[5] + y str r4, [r0, r3, lsl array[10] = array[5] + y

One-dimensional Arrays /* for (i = 0; i < 100; i++) a[i] = i; */.text.global main.type %function, main main: ldr r1, =a /* r1 ← &a */ mov r2, #0 /* r2 ← 0 */ b test loop: add r3, r1, r2, lsl #2 /* r3 ← r1 + (r2*4) */ str r2, [r3] /* *r3 ← r2 */ add r2, r2, #1 /* r2 ← r2 + 1 */ test: cmp r2, #100 /* Have we reached 100 yet? */ blt loop /* If not, loop again */ end: bx lr.section.bss a:.skip 40

One-dimensional Arrays /* Another Approach for (i = 0; i < 100; i++) a[i] = i; */ mov r2, #0 b test loop: str r2, [r1], +4 /* *r1 ← r2 then r1 ← r1 + 4 */ add r2, r2, #1 /* r2 ← r2 + 1 */ test: cmp r2, #100 /* Have we reached 100 yet? */ blt loop /* If not, keep processing */ end:.section.bss a:.skip 40

Traversing an array (visiting all elements) Two options (using ldr): 1) Adding a scaled index to a base address // element = a[i]; add r3, sp, can set base address outside loop mov index_r, #0... loop:... ldr element_r, [base_r, index_r, lsl #2]... add index_r, index_r, #1...

Traversing an array (visiting all elements) 2) dereferencing a pointer and incrementing the pointer // int *ptr = a; // element = *ptr++ add ptr, sp, init ptr to base_addr outside loop... loop:... ldr element_r, dereference ptr -- *ptr add ptr, ptr, postincrement ptr – ptr++...

Traversing an array (visiting all elements) 2b) an alternate approach to dereferencing a pointer and incrementing the pointer in ARM // int *ptr = a; // element = *ptr++ add ptr, sp, init ptr to base_addr – 4 the loop... loop:... ldr element_r, [ptr, dereference ptr -- and postincrement ptr++...

Traversing an array (visiting all elements) note that in the second approach you can choose to make the loop control a test against an address limit rather than testing a loop counter // int a[N]; // // int *ptr = a; -- like a.begin() for a C++ vector // int *limit = a+N; -- like a.end() for a C++ vector // // while( ptr != limit ) // { //... // element = *ptr++; //... // }

Traversing an array (visiting all elements) add ptr_r, sp, init ptr to base_address-4 mov r0, N, lsl init limit to point one element beyond the last add limit_r, ptr_r, element in the array... loop: cmp ptr_r, limit_r beq done... ldr element_r, [ptr_r, dereference ptr -- and postincrement ptr++... ba loop done:...

Two-dimensional Arrays storage allocation is now more difficult – row-major or column- major (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (0,0) (1,0) (0,1) (1,1) (0,2) (1,2) row 0 row 1 row majorcolumn major column 0 column 1 column 2

Two-dimensional Arrays C is row-major, FORTRAN is column-major e.g., in C int mat[2][2]; address of a[i][j] in row-major storage order = base_address + [(i-origin1)*#_elements_per_row + (j-origin2)] * element_size... mat[0][0] mat + 0 mat[0][1] mat+ 4 mat[1][0] mat + 8 mat[1][1] mat row 0 row 1

Two-dimensional Arrays consider this example C code int main(void){ int a[5][5]; register int i=3,j=4; a[i][j] = 0; }

Two-dimensional Arrays annotated assembly output from compiler.global main main: push {r4, r5, r7} sub sp, sp, save space for 5x5 array add r7, sp, #0 mov r4, r4 = i = 3 mov r5, r5 = j = 4 mov r3, r3 = i lsl r3, r3, r3 = 4*i adds r3, r3, r3 = 4*i + i = 5*i adds r3, r3, r3 = 5*i + j lsl r3, r3, r3 = 4*(5*i + j) add r2, r7, r2 = r adds r3, r2, r3 = r *(5*i + j) mov r2, r2 = 0 str r2, [r3, eff addr = r3 - r *(5*i+j) mov r0, = r *(5*i+j) add r7, r7, #108 mov sp, r7 pop {r3, r4, r5, r7} bx lr

Two-dimensional Arrays A cleaner approach to do a[i][j] = 0; sub sp, sp, #108 add base_r, sp, #0 mov r0 i_r, lsl 4*i add r0, r0, 5*i add r0, r0, 5*i + j mov offset_r, r0, lsl 4*( 5*i + j ) mov r0, #0 str r0, [base_r, offset_r] assuming base_r, i_r, j_r, offset_r are register names for the base address, i, j, and the offset.