Docsity

CS 213 Class 9: Basic Data Types, Structures, and Byte-Level Memory Operations, Slides of Introduction to Computers

Partial preview of the text

Download CS 213 Class 9: Basic Data Types, Structures, and Byte-Level Memory Operations and more Slides Introduction to Computers in PDF only on Docsity!

Structured Data II

Heterogenous Data

Sept. 22, 1998

Topics

  • Structure Allocation
  • Alignment
  • Operating on Byte Strings
  • Unions
  • Byte Ordering
  • Alpha Memory Organization

class09.ppt

“The course that gives CMU its Zip!”

Basic Data Types

Integral

  • Stored & operated on in general registers
  • Signed vs. unsigned depends on instructions used Alpha Bytes C byte 1 [ unsigned ] char word 2 [ unsigned ] short long word 4 [ unsigned ] int quad word 8 [ unsigned ] long int, pointers

Floating Point

  • Stored & operated on in floating point registers
  • Special instructions for four different formats (only 2 we care about) Alpha Bytes C S_floating 4 float T_floating 8 double

struct rec { long int i; long int a[3]; long int *p; };

find_a: s8addq $17,8,$0 # $0 = 8*idx + addq $16,$0,$0 # $0 += r ret $31,($26),

long int * find_a (struct rec *r, long int idx) { return &r->a[idx]; }

Generating Pointer to Structure Member

Generating Pointer to Array Element

  • Offset of each structure member determined at compile time

i a p 0 8 32

struct rec { long int i; long int a[3]; long int *p; };

set_p: ldq $1,0($16) # get r->i s8addq $1,8,$1 # Compute 8*i+ addq $1,$16,$1 # compute &a[i] stq $1,32($16) # store in p ret $31,($26), 1

void set_p(struct rec *r, long int *ptr) { r->p = &r->a[r->i]; }

Structure Referencing (Cont.)

C Code

i a 0 8 32 Element i

i a p 0 8 32

struct S1 { char c; int i[2]; long int v; } *p;

Satisfying Alignment with Structures

Offsets Within Structure

  • Must satisfy element’s alignment requirement

Overall Structure Placement

  • Each structure has alignment requirement K
    • Largest alignment of any element
  • Initial address + structure length must be multiples of K

Example

  • K = 8, due to long int element c i[0] i[1] v p+0 p+4 p+8 p+16 p+

Multiple of 4 Multiple of 8

Multiple of 8 Multiple of 8

Effect of Overall Alignment Requirement

x i[0] i[1] c p+0 p+8 p+12 p+16 p+

struct S2 { int *x; int i[2]; char c; } *p;

struct S3 { int x[2]; int i[2]; char c; } *p;

i[0] i[1] c p+0 p+8 p+12 p+16 p+

x[0] x[1] p+

p must be multiple of 8

p must be multiple of 4

Arrays of Structures

Principle

  • Allocated by repeating allocation for array type
  • In general, may nest arrays & structures to arbitrary depth

a[0] a+

a[1] a[2] a+24 a+48 a+

a+24 a+32 a+40 a+

struct S6 { int i; long int v; int j; } a[10];

a[1].i a[1].v a[1].j

Accessing Element within Array

  • Compute offset to start of structure
    • Compute 24* i as (4 i – i )*
  • Access element according to its offset within structure - Offset by 8

a[0] a+

a[i] a+24i

  • • • • • •

long int get_v(int i) { return a[i].v; }

s4subq $16,$16,$16 # i*= 3 lda $1,a # $1 = a s8addq $16,$1,$16 # $16 = a+i ldq $0,8($16) # a[i].v

a+24i a+24i+

struct S6 { int i; long int v; int j; } a[10];

a[i].i a[i].v a[i].j

Byte-Level Memory Operation Example

01 23 45 67 89 AB CD EF

0x107 0x103 0x

  • • • • • •

FF EE DD CC BB AA 99 88

0x20f 0x20d 0x

  • • • • • •

char *src, *dest; src = 0x103; dest = 0x20d; *dest = *src;

Increasing address

FF EE 89 CC BB AA 99 88

0x20f 0x20d 0x

  • • • • • •

Desired Effect:

Implementing Byte-Level Operations

Issue

  • Byte addresses have no alignment requirement
  • Alpha memory system only designed for 4-byte and 8-byte aligned memory operations

Method

  • Could use reqular quad-word memory operations + masking and shifting - 17 instructions required to implement statement *dest = *src - single byte transfer
  • Instead, provide set of instructions to perform byte extraction and insertion in quad-words - 7 instructions required to implement statement *dest = *src

Preparing Destination

Step 2. Zero byte at dest = 0x20d

  • Pointer dest in register $ ldq_u $2, 0($16)
    • Rounds effective address 0x20d down to nearest quad word boundary (0x208)
    • Loads entire quad word into register

mskbl $2, $16, $

  • Uses lower 3 bits of $16 as byte offset (= 5)
  • Copies source to destination, but zeros specified byte

Increasing address

$2: FF EE DD CC BB AA 99 88

Byte Number:

FF EE DD CC BB AA 99 88

0x20f 0x20d 0x

  • • • • • •

$2: FF EE 00 CC BB AA 99 88

Byte Number:

Step 3. Position Source Byte

  • Pointer dest in register $ insbl $1, $16, $
    • Uses lower 3 bits of $16 as byte offset (= 5)
    • Shifts low order byte of source into specified byte position

Step 4. Form Destination Quad-Word

bis $1, $2, $

  • Merges new byte into destination

$2: FF EE 00 CC BB AA 99 88

Merging Source into Destination

$2: FF EE 89 CC BB AA 99 88

Union Allocation

Principles

  • Overlay union elements
  • Allocate according to largest element
  • Can only use one field at a time

union U1 { char c; int i[2]; long int v; } *up;

c i[0] i[1] v up+0 up+4 up+ struct S1 { char c; int i[2]; long int v; } *sp;

c i[0] i[1] v sp+0 sp+4 sp+8 sp+16 sp+

Implementing “Tagged” Union

  • Structure can hold 3 kinds of data
  • Only one form at any given time
  • Identify particular kind with flag type

typedef enum { CHAR, INT, LONG } utype;

typedef struct { utype type; union { char c; int i[2]; long int v; } e; } store_ele, *store_ptr;

store_ele k;

k.e.i[0] k.e.i[1] k.e.v k.e

k.e.c

k.type