Chapter 6 Data Structures

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Lectures 10 & 11.
Lecture By SHERY KHAN Assembly Language Lecture By SHERY KHAN
Cosc 2150: Computer Organization
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Introduction to arrays
One Dimensional Arrays
Cosc 2150 Arrays in assembly code. Variables and addresses Uncompiled ld [a], %r1 addcc %r1, 2, %r3 ARC has three addressing modes —immediate, direct,
Princess Sumaya Univ. Computer Engineering Dept. Chapter 2: IT Students.
There are two types of addressing schemes:
Systems Architecture Lecture 5: MIPS Instruction Set
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Chapter 7: Subroutines Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition,
Chapter 9 Imperative and object-oriented languages 1.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
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.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010 Slides revised 2/2/2014 by Patrick Kelley.
8.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 08 Main Memory (Page table questions)
A First Book of ANSI C Fourth Edition
Registers and MAL Lecture 12. The MAL Architecture MAL is a load/store architecture. MAL supports only those addressing modes supported by the MIPS RISC.
Natawut NupairojAssembly Language1 Memory and Stack.
CSC 3210 Computer Organization and Programming Chapter 5 THE STACK D.M. Rasanjalee Himali.
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
by Richard P. Paul, 2nd edition, 2000.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 8 – Machine Instructions These are lecture notes to accompany the book.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Accessing Memory Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois, Abinashi Dhungel.
INTRODUCTION OF ARRAY. Topics To Be Discussed………………………. Introduction Types of array One Dimensional Array Internal representation of one-dimensional array.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Windows Programming Lecture 03. Pointers and Arrays.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 8 – Machine Instructions
CS2100 Computer Organisation
Computer Programming BCT 1113
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
Arrays.
Chapter 3 – Digital Logic and Binary Numbers
Chapter 7 Subroutines Dr. A.P. Preethy
Microprocessor and Assembly Language
by Richard P. Paul, 2nd edition, 2000.
Lecture 06 Programming language.
3.
Arrays.
Arrays.
CS-401 Computer Architecture and Assembly Language Programming
Presentation transcript:

Chapter 6 Data Structures Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2nd edition, 2000 Abinashi Dhungel 1

Arrays and Structures Cannot be stored in registers Necessary to perform address arithmetic Registers do not have addresses Array – indexable data structure whose elements are all of same type and given an index i, j , k etc. the corresponding array element can be accessed 2

One-Dimensional Array Address of ith array element = address_of_first_element + i*size_of_array_element_in_bytes (Chap 5) Accessing ith element (shift, add, load/store) sll %ir, 2, %o0 !o0 = i * 4 add %fp, %o0, %o0 !o0 = %fp + i*4 ld [%o0 + a_offset], %o0 !o0 = [%fp + a_offset + %ir * 4] 3

Multidimensional Array Mapping may take many different forms Row major and Column major ordering For 2-D array arranged in row major order address a[i][j] = address of a[0][0] + (number of rows above ith row) * (number of elements in a row * size of an element) + (number of elements left of the jth column) * size of an element Assuming m rows and n columns and size_el for each element = address of a[0][0] + (i*n + j)*size_el Column major order ? 4

Multidimensional Array Mapping ith, jth and kth element of 3-D array int arr[di][dj][dk] in row major order = address of arr[0][0] + i*dj*dk*size_el + j*dk*size_el + k*size_el = %fp + arr_offset + ((i*dj + j)*dk + k)*4 Many languages except Fortran – row major 5

An example of multi-dimensional array access short ary[16][3][4][15] accessing i, j, kth element : (%fp + ary_offset) + (((i*d2 + j)*d3 + k)*d4 + l)*2 mov %i_r, %o0 call .mul mov d2, %o1 !%o0 = i*d2 add %j_r, %o0, %o0 !%o0 = i*d2 + j mov d3, %o1 !%o0 = (i*d2 + j)*d3 add %k_r, %o0, %o0 !%o0 = (i*d2 + j)*d3 + k .mul mov d4, %o1 !%o0 = ((i*d2 + j)*d3 + k)*d4 add %l_r, %o0, %o0 !%o0 = ((i*d2 + j)*d3 + k)*d4 + l sll %o0, 1, %o0 !%o0 = (((i*d2 + j)*d3 + k)*d4 + l) * 2 add %fp, %o0, %o0 ldsh [%o0 + ary_offset], %o0 !%o0 = ary[i][j][k][l] Address computation of an element in an n-dimensional array in general requires n-1 multilication and n additions 6

Lower bound different from zero Example as in pascal, b: array[l1..u1, l2..u2, l3..u3] of integer d1 = u1 – l1 +1 d2 = u2 – l2 + 1 d3 = u3 – l3 + 1 Address of ith ,jth, and kth element is %fp + b_offset + (((i – l1) * dj + (j – l2)) * dk + (k – l3)) * 4 7

Array Bound Checking Can be checked with the lower or upper bound of an array to prevent invalid memory access. lower_bound = 3 ... .global main main: ... subcc %i_r, lower_bound, %g0 bl error ... error: Accessing a constant subscript may be done by an assembler, for example to access arr[-1][3][3] ld [%fp + b0 + (((-1*d2+3) * d3 + 3) << 2)], %o0 8

Address Arithmetic (multiplication by positive constant) Multiplication by a constant can be done by shifting and adding mov %o0, %o1 !times one sll %o0, 2, %o0 !times four add %o0, %o1, %o1 !times five Easier with binary numbers Multiply %o0 by (03514)8 = 011 101 001 100 binary sll %o0, 2, %o1 sll %o1, 1, %o0 add %o0, %o1, %o1 sll %o0, 3, %o0 sll %o0, 2, %o0 sll %o0, 1, %o0 9

Multiplying with string of 1’s 111 is same as multiplying with (1000 – 1) sub %g0, %o0, %o1 sll %l0, 3, %o0 add %o0, %o1, %o1 !times seven 1110 is same as multiplying with (10000 – 10) 10

Structures Block of contiguous memory to store variables (fields) Fields may be of different types Must be aligned to the highest sized data element struct example { int a, b; char d; short x, y; int u, v; } a_offset = 0 b_offset = 4 d_offset = 8 x_offset = 10 y_offset = 12 u_offset = 16 v_offset = 20 str_size = 24 str_offset = -str_size & -4 = -24 ld [%fp + str_offset + a_offset], %o0 !o0 = a ld [%fp + str_offset + b_offset], %o0 !o0 = b ldub [%fp + str_offset + d_offset], %o0 !o0 = d ldsh [%fp + str_offset + x_offset], %o0 !o0 = x ldsh [%fp + str_offset + y_offset], %o0 !o0 = y ld [%fp + str_offset + u_offset], %o0 !o0 = u ld [%fp + str_offset + v_offset], %o0 !o0 = v 11

Alignining fields struct ex { int a, char b; short c; int d; } a_offset = 0 b_offset = align(a_offset + sizeof(a)) c_offset = align(b_offset + sizeof(b)) d_offset = align(c_offset + sizeof(c )) aligning any size x with b bytes can be done by (x + (b-1)) & -b a_offset = 0 b_offset = ((0+4) + 3) & -4 = 4 c_offset = ((4+1) + 1) & -2 = 6 d_offset = ((6+2) + 3) & -4 = 8 str_size = ((8+4) + 3) & -4 = 12 str_offset = -12&-4 = -12 12