Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Similar documents
CS429: Computer Organization and Architecture

Computer Organization: A Programmer's Perspective

u Arrays One-dimensional Multi-dimensional (nested) Multi-level u Structures Allocation Access Alignment u Floating Point

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly IV: Complex Data Types

Machine-level Programs Data

Machine- Level Representation: Data

Introduction to Computer Systems , fall th Lecture, Sep. 14 th

Machine Level Programming: Arrays, Structures and More

Lecture 7: More procedures; array access Computer Architecture and Systems Programming ( )

Systems I. Machine-Level Programming VII: Structured Data

Floating Point Stored & operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12/16 long double

Machine- Level Programming IV: x86-64 Procedures, Data

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Machine-Level Prog. IV - Structured Data

Arrays and Structs. CSE 351 Summer Instructor: Justin Hsia. Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan.

MACHINE-LEVEL PROGRAMMING IV: Computer Organization and Architecture

Machine-Level Programming IV: Structured Data

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

2 Systems Group Department of Computer Science ETH Zürich. Argument Build 3. %r8d %rax. %r9d %rbx. Argument #6 %rcx. %r10d %rcx. Static chain ptr %rdx

Basic Data Types. Lecture 6A Machine-Level Programming IV: Structured Data. Array Allocation. Array Access Basic Principle

CISC 360. Machine-Level Programming IV: Structured Data Sept. 24, 2008

Memory Organization and Addressing

Areas for growth: I love feedback

CSC 252: Computer Organization Spring 2018: Lecture 9

Data Structures in Memory!

Basic Data Types The course that gives CMU its Zip! Machine-Level Programming IV: Data Feb. 5, 2008

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture

Machine Programming 4: Structured Data

Integral. ! Stored & operated on in general registers. ! Signed vs. unsigned depends on instructions used

Page 1. Basic Data Types CISC 360. Machine-Level Programming IV: Structured Data Sept. 24, Array Access. Array Allocation.

Basic Data Types The course that gives CMU its Zip! Machine-Level Programming IV: Structured Data Sept. 18, 2003.

Giving credit where credit is due

Giving credit where credit is due

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Question Points Score Total: 100

Computer Systems CEN591(502) Fall 2011

Structs and Alignment CSE 351 Spring

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

Machine Level Programming: Control

CS241 Computer Organization Spring Loops & Arrays

Referencing Examples

CSC 252: Computer Organization Spring 2018: Lecture 6

Building an Executable

Data Representa/ons: IA32 + x86-64

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 33. Architecture and Optimization (1) CS33 Intro to Computer Systems XV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Machine/Assembler Language Putting It All Together

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

MACHINE LEVEL PROGRAMMING 1: BASICS

Assembly II: Control Flow

Compiling C Programs Into X86-64 Assembly Programs

The Stack & Procedures

Machine-Level Programming II: Control

x86 Programming II CSE 351 Winter

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Computer Organization and Architecture

Machine-Level Programming II: Control

Assembly Programming IV

How Software Executes

h"p://news.illinois.edu/news/12/0927transient_electronics_johnrogers.html

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

Lecture 4: x86_64 Assembly Language

x86-64 Programming III & The Stack

CSC 252: Computer Organization Spring 2018: Lecture 14

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements

Prac*ce problem on webpage

Machine-Level Programming (2)

Machine-Level Programming V: Unions and Memory layout

Structs and Alignment

Machine Representa/on of Programs: Arrays, Structs and Unions. This lecture. Arrays Structures Alignment Unions CS Instructor: Sanjeev Se(a

Optimization part 1 1

Machine Programming 3: Procedures

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

x86 64 Programming II

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

CS201- Lecture 7 IA32 Data Access and Operations Part II

Assembly Language II: Addressing Modes & Control Flow

Machine Program: Procedure. Zhaoguo Wang

Machine- level Programming IV: Data Structures. Topics Arrays Structs Unions

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

Corrections made in this version not seen in first lecture:

CS , Fall 2007 Exam 1

Do not turn the page until 5:10.

x86 Programming I CSE 351 Winter

The Hardware/So=ware Interface CSE351 Winter 2013

Procedures and the Call Stack

Changelog. Corrections made in this version not in first posting:

void twiddle1(int *xp, int *yp) { void twiddle2(int *xp, int *yp) {

Machine Language CS 3330 Samira Khan

CS367. Program Control

Transcription:

Basic Data Types CS429: Computer Organization and Architecture Dr Bill Young Department of Computer Sciences University of Texas at Austin Last updated: October 31, 2017 at 09:37 Integral Stored and operated on in general registers Signed vs unsigned depends on instructions used Intel GAS Bytes C byte b 1 [unsigned] char word w 2 [unsigned] short double word l 4 [unsigned] int quad word q 8 [unsigned] long int Floating Point Stored and operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12 long double CS429 Slideset 10: 1 CS429 Slideset 10: 2 Array Allocation Array Access Basic Principle: T A[L] Array (named A) of data type T and length L Contiguously allocated region of L * sizeof(t) bytes int val[5]; x x+4 x+8 x+12 x+16 x+20 char string[12]; int val[5]; double a[3]; char *p[3]; x x+12 x x+4 x+8 x+12 x+16 x+20 x x+8 x+16 x+24 x x+8 x+16 x+24 Reference Type Value val[4] int 3 val int * x val+1 int * x +4 &val[2] int * x +8 val[5] int?? *(val+1) int 5 val+j int * x +4j Note the use of pointer arithmetic CS429 Slideset 10: 3 CS429 Slideset 10: 4

Array Example Array Accessing Example zip_dig cmu; zip_dig mit; zip_dig ucb; #define ZLEN 5 typedef int zip dig [ZLEN]; zip dig cmu = 1, 5, 2, 1, 3 ; zip dig mit = 0, 2, 1, 3, 9 ; zip dig ucb = 9, 4, 7, 2, 0 ; Declaration zip_dig cmu is equivalent to int cmu[5] 16 20 24 28 32 36 0 2 1 3 9 36 40 44 48 52 56 9 4 7 2 0 56 60 64 68 72 76 Example arrays were allocated in successive 20 byte block That s not guaranteed to happen in general int get digit ( zip dig z, int dig ) return z[ dig ]; Memory Reference Code # %rdi = z # %rsi = dig movl (%rdi,%rsi,4),%eax # z[ dig ] Computation Register %rdi contains the starting address of the array Register %rsi contains the array index The desired digit is at %rdi + (4 * %rsi) User memory reference (%rdi,%rsi,4) CS429 Slideset 10: 5 CS429 Slideset 10: 6 Array Loop Example void zincr ( zip dig z ) for ( i = 0; i < ZLEN; i++ ) z[ i]++; # %rdi = z movl $0, %eax # i = 0 jmp L3 # goto middle L4 : # loop : addl $1, (%rdi, %rax, 4) # z[ i]++ addq $1, %rax # i++ L3 : # middle : cmpq $4, %rax # i :4 jbe L4 # if <=, goto loop ret # return Multidimensional (Nested) Arrays Declaration: T A[R][C]; 2D array of data type T R rows, C columns Type T element requires K bytes Array Size: R C K bytes Arrangement: Row-Major ordering (guaranteed) A][R 1][0] A[0][C 1] A[R 1][C 1] Row major order means the elements are stored in the following order: [A 0,0,,A 0,C 1,A 1,0,,A 1,C 1,,A R 1,0,,A R 1,C 1 ] CS429 Slideset 10: 7 CS429 Slideset 10: 8

Multidimensional Array Access Nested Array Example Declaration: T A[R][C]; 2D array of data type T R rows, C columns Type T element requires K bytes A][R 1][0] A[0][C 1] A[R 1][C 1] #define PCOUNT 4 zip dig pgh[pcount] = 1, 5, 2, 0, 6, 1, 5, 2, 1, 3, 1, 5, 2, 1, 7, 1, 5, 2, 2, 1; zip_dig pgh[4]; 1 5 2 0 6 1 5 2 1 7 1 5 2 2 1 76 96 116 136 156 To access element A[i][j], perform the following computation: A+i C K +j K Declaration zip_dig pgh[4] is equivalent to int pgh[4][5] Variable pgh denotes an array of 4 elements allocated contiguously Each element is an array of 5 ints, which are allocated contiguously This is row-major ordering of all elements, guaranteed CS429 Slideset 10: 9 CS429 Slideset 10: 10 Nested Array Row Access Nested Array Element Access Row Vectors: Given a nested array declaration T A[R][C], you can think of this as an array of arrays A[i] is an array of C elements Each element of A[i] has type T, and requires K bytes The starting address of A[i] is A + i * C * K Array Elements A[i][j] is an element of type T, which requires K bytes The address is A + (i * C + j)*k A[0] A[i] A[R 1] A[0][C 1] A[i][j] A[R 1][0] A[R 1][C 1] A[0] A[i] A[R 1] A[0][C 1] A[i][0] A[i][C 1] A[R 1][0] A[R 1][C 1] A A+i*C+4 A+(i*C+j)*4 A+(R 1)*C*4 A A+i*C*4 A+(R 1)*C*4 CS429 Slideset 10: 11 CS429 Slideset 10: 12

Multi-Level Array Example zip dig cmu = 1, 5, 2, 1, 3 ; zip dig mit = 0, 2, 1, 3, 9 ; zip dig ucb = 9, 4, 7, 2, 0 ; #define UCOUNT 3 int univ [UCOUNT] = mit, cmu, ucb; 160 36 168 170 univ 16 56 cmu mit ucb Variable univ denotes an array of 3 elements Each element is a pointer (8 bytes) Each pointer points to an array of ints (may vary in length) 16 20 24 28 32 36 0 2 1 3 9 36 40 44 48 52 56 9 4 7 2 0 Element Access in a Multi-Level Array int get univ digit ( size t index, size t dig ) return univ [ index ][ dig ]; Computation Element access Mem[Mem[univ+8*index] + 4*dig] Must do two memory reads: First get pointer to row array Then access element within the row salq $2, %rsi # 4 dig addq univ(,%rdi,8),%rsi # p = univ [ dig ] + 4 dig movl (%rsi ), %eax # return p ret 56 60 64 68 72 76 CS429 Slideset 10: 13 CS429 Slideset 10: 14 Array Element Accesses N x N Matrix Code Nested Array int get pgh digit ( size t index, size t dig ) return pgh[ index ][ dig ]; Element at Mem[pgh+20*index+4*dig] 1 5 2 0 6 1 5 2 1 7 1 5 2 2 1 76 96 116 136 156 Multi-Level Array int get univ dig ( size t index, size t dig ) return univ [ index ][ dig ]; Element at Mem[Mem[univ+8*index]+4*dig] 160 36 168 170 univ 16 56 cmu mit ucb 16 20 24 28 32 36 0 2 1 3 9 36 40 44 48 52 56 9 4 7 2 0 Similar C references, but different address computations 56 60 64 68 72 76 Fixed dimensions: Know value of N at compile time Variable dimensions, explicit indexing: Traditional way to implement dynamic arrays Variable dimensions, implicit indexing: Now supported by gcc #define N 16 typedef int fix matrix [N][N]; int fix ele ( fix matrix a, size t i, size t j ) #define IDX(n, i, j ) (( i ) (n) + ( j )) int vec ele ( size t n, int a, size t i, size t j ) return a[idx(n, i, j ) ]; int var ele ( size t n, int a[n ][ n], size t i, size j ) CS429 Slideset 10: 15 CS429 Slideset 10: 16

16 x 16 Matrix Access Array Elements Address A+i (C K)+j K C = 16, K = 4 int fix ele ( fix matrix a, size t i, size t j ) # a in %rdi, i in %rsi, j in %rdx salq $6, %rsi # 64 i addq %rsi, %rdi # a + 64 i movl (%rdi, %rdx, 4), %eax # M[a + 64 i + 4 j ] n x n Matrix Access Array Elements Address A+i (C K)+j K C = n, K = 4 Must perform integer multiplication int var ele ( size t n, int a[n ][ n], size t i, size j ) # n in %rdi, a in %rsi, i in %rdx, j in %rcx imulq %rdx, %rdi # n i leaq (%rsi, %rdi, 4), %rax # a + 4 n i movl (%rax, %rcx, 4), %eax # a + 4 n i + 4 j ret CS429 Slideset 10: 17 CS429 Slideset 10: 18 Structure Representation struct rec int a [4]; struct rec next ; ; Structure represented as block of memory Big enough to hold all the fields Fields ordered according to declaration r a i next 0 16 24 32 Even if another ordering could yield a more compact representation Compiler determines overall size and position of fields Machine-level program has no understanding of the structures in the source code Generating Pointer to Structure Member struct rec int a [4]; struct rec next ; ; Generating Pointer to Array Element Offset of each structure member determined at compile time Compute as r + 4*idx r r+4*idx a i next 0 16 24 32 int get ap ( struct rec r, size t idx ) return &r >a[ idx ]; # r in %rdi, idx in %rsi leaq (%rdi, %rsi, 4), %rax ret BTW: why does r->i need 8 bytes? Alignment (Next slide set) CS429 Slideset 10: 19 CS429 Slideset 10: 20

Aside on Structures: Arrow vs Dot Following Linked List void set val ( struct rec r, int val ) while (r) int i = r >i ; r >a[ i ] = val ; r = r >next ; struct rec int a [4]; struct rec next ; ; r a i next 0 16 24 32 If you have a pointer r to a structure, use r->x to access component x If you have the structure s itself, use sx r->x is just syntactic sugar for (*r)x CS429 Slideset 10: 21 L11 : # loop : testq %rdi, %rdi # Test r je L12 # if = 0, goto done movq 16(%rdi ), %rax # i = M[ r+16] movl %esi, (%rdi, %rax, 4) # M[ r+4 i ] = val movq 24(%rdi ), %rdi # r = M[ r+24] jmp L11 # goto loop L12 : # done : CS429 Slideset 10: 22