Addressing & Pointers

advertisement

CDA 3101 Spring 2016

Introduction to Computer Organization

Data Types and Addressing

28 January 2016

Topics to Review

• Memory layout

• Text, data (static and heap), and the stack

Procedure conventions

Procedure call bookkeeping

• Caller Saved Registers:

• Return address

• Arguments

• Return value

• $t Registers

• Callee Saved Registers:

• $s Registers

$ra

$a0, $a1, $a2, $a3

$v0, $v1

$t0 - $t9

Procedure structure

• Prologue:

• Body:

• Epilogue:

$s0 - $s7 allocate frame, save registers, assign locals procedure code restore registers, free frame

Overview

• Data types

– Application / HLL requirements

– Hardware support (data and instructions)

• MIPS data types

• Support for bytes and strings

• Addressing Modes

– Data

– Instructions

• Large constants and far target addresses

• SPIM code

• Applications / HLL

– Integer

– Floating point

Character

– String

– Date

– Currency

– Text,

– Objects (ADT)

– Blob

– double precision

– Signed, unsigned

Data Types

• Hardware support

– Numeric data types

– Integers

– 8 / 16 / 32 / 64 bits

– Signed or unsigned

– Binary coded decimal

(COBOL, Y2K!)

• Floating point

• 32 / 64 /128 bits

– Nonnumeric data types

• Characters

• Strings

• Boolean (bit maps)

• Pointers

MIPS Data Types (1/2)

• Basic machine data type: 32-bit word (4 bytes)

– 0100 0011

0100 1001 0101 0011 0100 0101

– Integers (signed or unsigned)

• 1,128,878,917

– Floating point numbers

• 201.32421875

– 4 ASCII characters

• C I S E

– Memory addresses (pointers)

• 0x43495345

– Instructions

MIPS Data Types (2/2)

• 16-bit constants (immediates)

– addi $s0, $s1,

0x8020

– lw $t0, 20($s0)

• Half word (16 bits)

– lh ( lhu ): load half word lh $t0, 20($s0)

– sh : save half word sh $t0, 20($s0)

• Byte (8 bits)

– lb ( lbu ): load byte

– sb : save byte lb $t0, 20($s0) sb $t0, 20($s0)

lb $s1, 4($s0)

$s0 : 0x10000000

$s1 : 0xFFFFFF AA

Byte Instructions

Address Memory Bytes

0x10000000 lbu $s1, 2($s0)

$s0 : 0x10000000

$s1 : 0x000000 AA

10101010

String Manipulation

Void strcpy (char[], char y[]) { int i; i = 0; while ((x[i]=y[i]) != 0) i = i + 1;

}

C convention:

Null byte (00000000) represents end of the string

Importance of comments in MIPS!

strcpy: subi $sp, $sp, 4 sw $s0, 0($sp) add $s0, $zero, $zero

L1: add $t1, $a1, $s0 lb $t2, 0($t1) add $t3, $a0, $s0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1

L2: lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Constants

• Small constants are used frequently (50% of operands)

– e.g., A = A + 5;

• Solutions

– Put 'typical constants' in memory and load them.

– Create hard-wired registers (like $zero) for constants like 1.

• MIPS Instructions: slti $8, $18, 10 andi $29, $29, 6 ori $29, $29, 0x4a addi $29, $29, 4

8 29 29 4

101011 10011 01000 0000 0000 0011 0100

Large Constants

To load a 32 bit constant into a register:

1.

Load (16) higher order bits lui $t0, 1010101010101010

1010 1010 1010 1010 0000 0000 0000 0000

2. Then must get the lower order bits right, i.e., ori $t0, $t0, 1010101010101010 ori

$t0: 1010 1010 1010 1010 0000 0000 0000 0000

0000 0000 0000 0000 1010 1010 1010 1010

1010 1010 1010 1010 1010 1010 1010 1010

Addressing Modes

• Addresses for data and instructions

• Data (operands and results)

– Registers

– Memory locations

– Constants

• Efficient encoding of addresses (space: 32 bits)

– Registers (32) => 5 bits to encode address

– Destructive instructions: reg2 = reg2 + reg1

– Accumulator

– Stack

Orthogonality of opcodes and addressing modes

Data Addressing Modes

Register addressing

– The most common (fastest and shortest)

– add $3, $2, $1

Base addressing

– Operand is at a memory location with offset

– lw $t0,

20 ($t1)

Immediate addressing

– Operand is a small constant within the instruction

– addi $t0, $t1,

4 (signed 16-bit integer)

MIPS Addressing Modes

1. Immediate addressing op rs rt Immediate

Hint: This will be on a Homework and at least one exam…

2. Register addressing op rs rt rd . . .

funct Registers

Register

3. Base addressing op rs rt Address Memory

+

Register Byte Halfword Word

4. PC-relative addressing op rs rt

PC

5. Pseudodirect addressing op Address

* 4

PC

Address

* 4

+

Memory

Word

Memory

Word

Instruction Addressing Modes

• Addresses are 32 bits long

• Special purpose register

PC ( program counter ) stores the address of the current instruction

PC-relative addressing (branches)

– Address: PC + (constant in the instruction) * 4

– beq $t0, $t1, 20 (0x1509 0005 )

Pseudodirect addressing (jumps)

– Address:

PC[31:28] : (constant in the instruction) * 4

SPIM Code

PC main

[0x00400020]

[0x00400024]

[0x00400028]

[0x0040002c]

[0x00400030]

[0x00400034]

[0x00400038]

[0x0040003c]

[0x00400040]

[0x00400044] exit

[0x00400048]

MIPS machine code Pseudo MIPS add $9, $10, $11 (0x014b4820) main: add $t1, $t2, $t3 j 0x00400048 [exit] (0x08100012) j exit addi $9, $10, -50 (0x2149ffce) addi $t1, $t2, -50 lw $8, 5($9) lw $8, -5($9)

(0x8d280005) lw $t0, 5($t1)

(0x8d28fffb) lw $t0, -5($t1) bne $8, $9, 20 [exit-PC] (0x15090005) bne $t0, $t1, exit addi $9, $10, 50 (0x21490032) addi $t1, $t2, 50 bne $8, $9, -28 [main-PC] (0x1509fff9) bne $t0, $t1, main lb $8, -5($9) (0x8128fffb) lb $t0, -5($t1) j 0x00400020 [main] (0x08100008) j main add $9, $10, $11 (0x014b4820) exit: add $t1, $t2, $t3

Far Target Address

0x00400000

Text Segment (252MB)

(0x07fe0000)

-2 17

PC

(0x08000000)

+2 17

(0x08020000)

(0x08200000) L1:

0x10000000 beq $s0, $s1, L1

L2: bne $s0, $s1, L2 j L1

Overview

• Pointers (addresses) and values

• Argument passing

• Storage lifetime and scope

• Pointer arithmetic

• Pointers and arrays

• Pointers in MIPS

Pointers

• Pointer: a variable that contains the address of another variable

– HLL version of machine language memory address

• Why use Pointers?

– Sometimes only way to express computation

– Often more compact and efficient code

• Why not?

– Huge source of bugs in real software, perhaps the largest single source

1) Dangling reference (premature free)

2) Memory leaks (tardy free): can't have long-running jobs without periodic restart of them

C Pointer Operators

• Suppose c has value 100, it is located in memory at address 0x10000000

• Unary operator & gives address: p = &c; gives address of c to p ;

– p “ points to

” c (p == 0x10000000) (Referencing)

• Unary operator * gives value that pointer points to

– if p = &c => * p == 100 (Dereferencing a pointer)

• Deferencing  data transfer in assembler

– ... = ... *p ...

;  load

(get value from location pointed to by p)

– *p = ...

;  store

(put value into location pointed to by p)

Pointer Arithmetic

int x = 1, y = 2; /* x and y are integer variables */ int z[10]; /* an array of 10 ints, z points to start */ int *p; /* p is a pointer to an int */ x = 21; /* assigns x the new value 21 */ z[0] = 2; z[1] = 3 /* assigns 2 to the first, 3 to the next */ p = &z[0]; p = z;

/* p refers to the first element of z */

/* same thing; p[ i ] == z[ i ]*/ p = p+1; p++;

*p = 4; p = 3; p = &x; z = &y

/* now it points to the next element, z[1] */

/* now it points to the one after that, z[2] */

/* assigns 4 to there, z[2] == 4*/

/* bad idea! Absolute address!!! */

/* p points to x, *p == 21 */ illegal!!!!! array name is not a variable p: z[1] z[0] y: x:

2

2 1

4

3

2

Assembly Code

c is int , has value 100, in memory at address

0x10000000, p in $a0 , x in $s0

1. p = &c; /* p gets 0x10000000*/ lui $a0,0x1000 # p = 0x10000000

2. x = *p; /* x gets 100 */ lw $s0, 0($a0) # dereferencing p

3. *p = 200; /* c gets 200 */ addi $t0,$0,200 sw $t0, 0($a0) # dereferencing p

Example

int strlen(char *s) { char *p = s; /* p points to chars */ while (*p != ’\0’) p++; /* points to next char */ return p - s; /* end - start */

} mov $t0,$a0 lbu $t1,0($t0) /* derefence p */ beq $t1,$zero, Exit

Loop:addi $t0,$t0,1 /* p++ */ lbu $t1,0($t0) /* derefence p */ bne $t1,$zero, Loop

Exit:sub $v0,$t0,$a0 jr $ra

Argument Passing Options

• 2 choices

– “

Call by Value

”: pass a copy of the item to the function/procedure

– “

Call by Reference

”: pass a pointer to the item to the function/procedure

• Single word variables passed by value

• Passing an array? e.g., a[100]

– Pascal (call by value) copies 100 words of a[] onto the stack

– C (call by reference) passes a pointer

(1 word) to the array a[] in a register

Lifetime of Storage and Scope

• Automatic (stack allocated)

– Typical local variables of a function

– Created upon call, released upon return

– Scope is the function

• Heap allocated

– Created upon malloc, released upon free

– Referenced via pointers

• External / static

– Exist for entire program

Code

Static

Heap

Stack

Arrays, Pointers, and Functions

4 versions of array function that adds two arrays and puts sum in a third array ( sumarray )

1. Third array is passed to function

2. Using a local array (on stack) for result and passing a pointer to it

3. Third array is allocated on heap

4. Third array is declared static

Purpose of example is to show interaction of C statements, pointers, and memory allocation

Version 1

int x[100], y[100], z[100]; sumarray(x, y, z);

• C calling convention means: sumarray(&x[0], &y[0], &z[0]);

• Really passing pointers to arrays addi $a0,$gp,0 # x[0] starts at $gp addi $a1,$gp,400 # y[0] above x[100] addi $a2,$gp,800 # z[0] above y[100] jal sumarray

Version 1: Compiled Code

void sumarray(int a[], int b[], int c[]) { int i; for(i = 0; i < 100; i = i + 1) c[i] = a[i] + b[i];

} addi

Loop: beq lw lw add

Exit: sw addi addi addi j jr

$t0,$a0,400 # beyond end of a[]

$a0,$t0,Exit

$t1, 0($a0) # $t1=a[i]

$t2, 0($a1) # $t2=b[i]

$t1,$t1,$t2 # $t1=a[i] + b[i]

$t1, 0($a2) # c[i]=a[i] + b[i]

$a0,$a0,4 # $a0++

$a1,$a1,4 # $a1++

$a2,$a2,4 # $a2++

Loop

$ra

int *sumarray(int a[],int b[]) { int i, c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c;

}

$sp c[100] a[100]

B[100]

Version 2

addi $t0,$a0,400 # beyond end of a[] addi $sp,$sp,-400 # space for c addi $t3,$sp,0 # ptr for c addi $v0,$t3,0 # $v0 = &c[0]

Loop: beq $a0,$t0,Exit lw $t1, 0($a0) # $t1=a[i] lw $t2, 0($a1) # $t2=b[i] add $t1,$t1,$t2 # $t1=a[i] + b[i] sw $t1, 0($t3) # c[i]=a[i] + b[i] addi $a0,$a0,4 # $a0++ addi $a1,$a1,4 # $a1++ addi $t3,$t3,4 # $t3++ j Loop

Exit: addi $sp,$sp, 400 # pop stack jr $ra

Version 3

int * sumarray(int a[],int b[]) { int i; int *c; c = (int *) malloc(100); for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c;

}

• Not reused unless freed

– Can lead to memory leaks

– Java, Scheme have garbage collectors to reclaim free space

Code

Static c[100]

Heap

Stack

Version 3: Compiled Code

addi $t0,$a0,400 # beyond end of a[] addi $sp,$sp,-12 # space for regs sw $ra, 0($sp) # save $ra sw sw

$a0, 4($sp) # save 1st arg.

$a1, 8($sp) # save 2nd arg.

addi $a0,$zero,400 jal malloc addi $t3,$v0,0 # ptr for c lw $a0, 4($sp) # restore 1st arg.

lw

Loop:

$a1, 8($sp) # restore 2nd arg.

beq $a0,$t0,Exit

...

(loop as before on prior slide ) j Loop

Exit:lw $ra, 0($sp) # restore $ra addi $sp, $sp, 12 # pop stack jr $ra

Version 4

int * sumarray(int a[],int b[]) { int i; static int c[100]; for(i=0;i<100;i=i+1) c[i] = a[i] + b[i]; return c;

}

• Compiler allocates once for function, space is reused

– Will be changed next time sumarray invoked

– Why describe? used in C libraries

Code

Static c[100]

Heap

Stack

Review

MIPS operands

Name Example Comments

$s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform

32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero alw ays equals 0. Register $at is

$fp, $sp, $ra, $at reserved for the assembler to handle large constants.

Memory[0],

2

30

memory Memory[4], ..., words Memory[4294967292]

Accessed only by data transfer instructions. MIPS uses byte addresses, so sequential w ords differ by 4. Memory holds data structures, such as arrays, and spilled registers, such as those saved on procedure calls.

Review

Category add

Instruction

MIPS assembly language

Example add $s1, $s2, $s3

Meaning

$s1 = $s2 + $s3

Comments

Three operands; data in registers

Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3

Three operands; data in registers add immediate load word store word

Data transfer load byte store byte load upper immediate addi $s1, $s2, 100 lw $s1, 100($s2) sw $s1, 100($s2) lb $s1, 100($s2) sb $s1, 100($s2) lui $s1, 100

$s1 = $s2 + 100

$s1 = 100 * 2

16

Used to add constants

$s1 =

Memory[

$s2

+ 100] Word from memory to register

Memory[

$s2

+ 100] = $s1 Word from register to memory

$s1 =

Memory[

$s2

+ 100] Byte from memory to register

Memory[

$s2

+ 100] = $s1 Byte from register to memory

Loads constant in upper 16 bits

Conditional branch

Unconditional jump branch on equal branch on not equal set on less than set less than immediate jump jump register jump and link beq $s1, $s2, 25 bne $s1, $s2, 25 slt $s1, $s2, $s3 slti $s1, $s2, 100 if (

$s1 == $s2

) go to

PC + 4 + 100

Equal test; PC-relative branch if (

$s1 != $s2

) go to

PC + 4 + 100

Not equal test; PC-relative if (

$s2 < $s3

)

$s1

= 1; else

$s1

= 0

Compare less than; for beq, bne if (

$s2 < 100

)

$s1

= 1; else

$s1

= 0

Compare less than constant j 2500 jr $ra jal 2500 go to 10000 go to

$ra

Jump to target address

For switch, procedure return

$ra

= PC + 4; go to 10000 For procedure call

Conclusions

• Data can be anything

– Datatyping restricts data representations

– Applications restrict datatyping

• MIPS Datatypes: Number, String, Boolean

• Addressing: Pointers, Values

– Many addressing modes (direct, indirect,…)

– Memory-based address storage ( jr instruction)

• Arrays: big chunks of memory

– Pointers versus stack storage

– Be careful of memory leaks!

Download