Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Similar documents
Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Applications of Arrays (1A) Young Won Lim 3/15/17

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

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Applications of Arrays (1A) Young Won Lim 2/11/17

Link 2. Object Files

Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

Link 2. Object Files

CS241 Computer Organization Spring Loops & Arrays

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Machine-Level Prog. IV - 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

Arrays (1A) Young Won Lim 1/27/17

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26

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

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

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

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

Process Layout, Function Calls, and the Heap

Systems I. Machine-Level Programming VII: Structured Data

Process Layout and Function Calls

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

CS241 Computer Organization Spring 2015 IA

Machine-Level Programming IV: Structured Data

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

Giving credit where credit is due

Giving credit where credit is due

Machine-level Programming (3)

Data Structures in Memory!

CMSC 313 Lecture 12 [draft] How C functions pass parameters

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

Referencing Examples

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

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

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Credits and Disclaimers

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

Machine-Level Programming II: Control and Arithmetic

CS , Fall 2001 Exam 1

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

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

Credits to Randy Bryant & Dave O Hallaron

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Sungkyunkwan University

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

Assembly III: Procedures. Jo, Heeseung

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

Machine Programming 4: Structured Data

The Hardware/So=ware Interface CSE351 Winter 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

Machine-Level Programming III: Procedures

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly Language: Function Calls

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

EECS 213 Fall 2007 Midterm Exam

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Assembly Language: Function Calls" Goals of this Lecture"

CS429: Computer Organization and Architecture

Chapter 3 Machine-Level Programming II Control Flow

Assembly Language: Function Calls" Goals of this Lecture"

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

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

Link 7.A Static Linking

Summary. Alexandre David

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

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

Machine Level Programming II: Arithmetic &Control

CS213. Machine-Level Programming III: Procedures

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Pointers (1A) Young Won Lim 11/1/17

Link 7. Dynamic Linking

Instructor: Alvin R. Lebeck

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

CS241 Computer Organization Spring Addresses & Pointers

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

COMP 210 Example Question Exam 2 (Solutions at the bottom)

CPSC W Term 2 Problem Set #3 - Solution

Instruction Set Architecture

Instruction Set Architecture

Applications of Pointers (1A) Young Won Lim 12/26/17

Transcription:

Arrays Young W. Lim 2017-02-06 Mon Young W. Lim Arrays 2017-02-06 Mon 1 / 17

Outline 1 Introduction References Array Background Young W. Lim Arrays 2017-02-06 Mon 2 / 17

Based on "Self-service Linux: Mastering the Art of Problem Determination", Mark Wilding "Computer Architecture: A Programmer s Perspective", Bryant & O Hallaron I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Arrays 2017-02-06 Mon 3 / 17

Array Declaration (1) T A[N] allocation of contiguous region of NL bytes L : the byte size of the data type T x A denote the starting address of the region index between 0 and N-1 the i-th element is at x_a + il Young W. Lim Arrays 2017-02-06 Mon 4 / 17

Array Declaration (2) char A[12]; char * B[8]; double C[6]; double * D[5]; x A + i x B + 4i x C + 8i x D + 4i Young W. Lim Arrays 2017-02-06 Mon 5 / 17

Array Declaration (3) int E[12]; E[i] access %edx : the starting address of E %ecx : the index value i movl (%edx,%ecx,4), %eax (%edx + %ecx) x E + 4i Young W. Lim Arrays 2017-02-06 Mon 6 / 17

Pointer Arithmetic (1) leal - to generate and address movl - to reference memory movl %edx, %eax ; (int *) E x E movl %(%edx),%eax ; (int) E[0] M[$x_E$] movl (%edx,%ecx,4), %eax ; (int) E[i] M[$x_E$+4i] Young W. Lim Arrays 2017-02-06 Mon 7 / 17

Pointer Arithmetic (2) leal 8(%edx),%eax ; (int *) &E[2] x E + 8 leal -4(%edx,%ecx,4),%eax ; (int *) E+i-1 x E + 4i 4 movl(%edx,%ecx,8),%eax ; (int) *(&E[i]+i) x E + 8i movl %ecx,%eax ; (int) &E[i]-E i Young W. Lim Arrays 2017-02-06 Mon 8 / 17

Array and Loop (1) int decimal5(int* x) { int i; int val=0; for (i=0; i<5; ++i) val = (10*val) + x[i]; return(val); int decimal5_opt(int *x) { int val = 0; int *xend = x + 4; do { val = (10*val) + *x; x++; while (x <= xend); return val; Young W. Lim Arrays 2017-02-06 Mon 9 / 17

Array and Loop (2) movl 8(%ebp), %ecx xorl %eax, %eax leal 16(%ecx), %ebx.l12: leal (%eax,%eax,4), %edx movl (%eax,%edx,2), %eax leal (%eax,%edx,2), %eax addl $4, %ecx cmpl %ebx, %ecx jbe.l12 int decimal5_opt(int *x) { int val = 0; int *xend = x + 4; do { val = (10*val) + *x; x++; while (x <= xend); return val; Young W. Lim Arrays 2017-02-06 Mon 10 / 17

Nested Array int A[4][3] typedef int Row[3]; Row A[4]; // Row type definition // A is an array of Row type Young W. Lim Arrays 2017-02-06 Mon 11 / 17

2-D Array (1) #define N 16 typedef int fmatrix[n][n];... fmatrix A; fmatrix B: // int A[16][16] // int B[16][16] Young W. Lim Arrays 2017-02-06 Mon 12 / 17

2-D Array (2) int iprod(fmatrix A, fmatrix B, int i, int k) { int j; int result = 0; for (j=0; j<n; j++) result += A[i][j] * B[j][k]; return result; int iprod_opt(fmatrix A, fmatrix B, int i, int k) { int *Ap = &A[i][0]; int *Bp = &B[0][k]; int result = 0; do { result += (*Ap) * (*Bp); Ap += 1; Bp += N; cnt--; while (cnt >= 0); return result; Young W. Lim Arrays 2017-02-06 Mon 13 / 17

2-D Array (3).L23: movl (%edx), %eax imull (%ecx), %eax addl %eax, %esi addl $64, %ecx addl $4, %edx decl %ebx jns.l23 int iprod_opt(fmatrix A, fmatrix B, int i, int k) { int *Ap = &A[i][0]; int *Bp = &B[0][k]; int result = 0; do { result += (*Ap) * (*Bp); Ap += 1; Bp += N; cnt--; while (cnt >= 0); return result; Young W. Lim Arrays 2017-02-06 Mon 14 / 17

Dynamically Allocated Array (1) typedef int *vmatrix;... vmatrix A // int *A;... vmatrix func(int n) { return (vmatrix) calloc( sizeof(int), n*n);... int foo(vmatrix A, int i, int j, int n) { return A[(i*n) + j); movl 8(%ebp), %edx movl 12(%ebp), %eax imull 20(%ebp), %eax addl 16(%ebp), %eax movl (%eax, %eax, 4), %eax Young W. Lim Arrays 2017-02-06 Mon 15 / 17

Dynamically Allocated Array (2) typedef int *vmatrix; int vprod(vmatrix A, vmatrix B, int i, int k, int n) { int j; int result = 0; for (j=0; j<n; j++) { result += A[i*n+j] * B[j*n+k]; return result; int vprod(vmatrix A, vmatrix B, int i, int k, int n) { int *Ap = &A[i*n]; int nt = n, result = 0; if (n <= 0) return result; do { result += (*Ap) * B[nT]; Ap++; nt+= n; cnt--; while (cnt); return result; Young W. Lim Arrays 2017-02-06 Mon 16 / 17

Dynamically Allocated Array (3).L37: movl 12(%ebp), %eax movl (%ebx), %edi addl $4, %ebx imull (%eax,%ecx,4), %edi addl %edi, %esi addl 24(%ebp), %ecx decl %edx jnz.l37 int vprod(vmatrix A, vmatrix B, int i, int k, int n) { int *Ap = &A[i*n]; int nt = n, result = 0; if (n <= 0) return result; do { result += (*Ap) * B[nT]; Ap++; nt+= n; cnt--; while (cnt); return result; Young W. Lim Arrays 2017-02-06 Mon 17 / 17