Pointers and Arrays

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 94

Pointers and arrays

Anthony J Souza

Sourced : Dr. Hsu


Topics Covered / Reading

• Layout of code and data in memory


• Loads and stores
• Registers as pointers
• Translating code with arrays
• Programming with strings
• Reading:
• Patterson and Hennessy 2.14
Layout of Code and Data in Memory

• So far, all variables are in registers.


• But number of registers is limited!
• Too many variables: spill to memory.
• [Arrays, more complex objects also in memory.]

C++ Source Code : MIPS Source Code :


.data
int x=4; int y=-1; x: .word 4 # int x=4;
y: .word -1 # int y=-1;
int main() {
.text
} main:
# code not shown
Layout of Code and Data in Memory

• x and main are labels; they mark locations in memory.


• Layout in memory depends on compiler.
• For spim:
• main is usually at 0x400024
• first address in .data is at 0x10010000
0x400024 # 1st instruction of program
0x400028 # 2nd instruction of program

X: 0x10010000 0x00000004
y: 0x10010004 0xffffffff
Layout of Code and Data in Memory

• .data, .text, .word are assembler directives, not MIPS instructions.


• They tell the (spim) assembler to do specific things (manage memory
layout), but the MIPS CPU doesn’t execute directives.
• .word
• allocates a 32-bit word in memory
• No type! (Type is managed by the compiler/assembler, or the programmer.)
• .data
• data allocation section follows
• .text
• program text/code section follows (read-only)
• Remember: MIPS arithmetic instructions have register operands only.
• To work with data in memory, need:
• Load instructions  data from memory loaded into registers.
• Store instructions  data from registers stored into memory.
Load word

• Load words move data from memory to registers.


• Sample Instruction:
• lw R, ADDR
• R represents any register
• ADDR represents a memory address
• Note that ADDR can be 1 of the following
• A register containing an address
• A label that represents an address
• A register and constant that together make up an address.

• There are three ways to use a Load Word


Load word – Case 1

• Load word address is a label.


• For example,
• lw $13, x
• Before executed load word

$13
x: 0x10010000 0x1234 abcd
0x10010004

• After executed load word

$13 0x1234 abcd


x: 0x10010000 0x1234 abcd
0x10010004
Load word – Case 2
• Load word address is a register with no constant.
• The register is normally called Rb
• lw R, (Rb)
• The contents of Rb is an address.
• R = M[contents of Rb]
• For example,
• lw $13, ($23)
• $13 = M[$23]

Before executed Load Word After executed Load Word

x: 0x10010000 0x1234 abcd x: 0x10010000 0x1234 abcd


0x10010004 0x10010004

$13 $13 0x1234 abcd

$23 0x10010000 $23 0x10010000


Load word – Case 2
• Load word address is a register with no constant.
• The register is normally called Rb
• lw R, (Rb)
• The contents of Rb is an address.
• R = M[contents of Rb]
• For example, What do you think Rb actually
• lw $13, ($23) is?

• $13 = M[$23]

Before executed Load Word After executed Load Word

x: 0x10010000 0x1234 abcd x: 0x10010000 0x1234 abcd


0x10010004 0x10010004

$13 $13 0x1234 abcd

$23 0x10010000 $23 0x10010000


Load word – Case 2
• Load word address is a register with no constant.
• The register is normally called Rb
• lw R, (Rb)
• The contents of Rb is an address.
• R = M[contents of Rb]
• For example, What do you think Rb actually
is?
• lw $13, ($23)
• $13 = M[$23] IT’S A POINTER

Before executed Load Word After executed Load Word

x: 0x10010000 0x1234 abcd x: 0x10010000 0x1234 abcd


0x10010004 0x10010004

$13 $13 0x1234 abcd

$23 0x10010000 $23 0x10010000


Load word – Case 3
• Load word address is a register with a constant.
• The register is normally called Rb
• The constant is called K
• lw R, K(Rb)
• The contents of Rb is an address.
• R = M[contents of Rb + K]
• For example,
• lw $13, 4($23)
• $13 = M[4 + $23]  0x10010004

Before executed Load Word After executed Load Word

x: 0x10010000 0x1234 abcd


x: 0x10010000 0x1234 abcd
0x10010004 0x8090 a0b0
0x10010004 0x8090 a0b0

$13 $13 0x8090 a0b0


$23 0x10010000
$23 0x1001 0000
Store word

• Storewords move data from registers to Memory.


• Same Instruction:
• sw R, ADDR
• R represents any register
• ADDR represents a memory address
• Note that ADDR can be 1 of the following
• A register containing an address
• A label that represents an address
• A register and constant that together make up an address.

• There are three ways to use a Store Word


Store word – Case 1

• Store word address is a label.


• For example,
• sw $13, x
• Before executed Store word

$13 0x1234 abcd


x: 0x10010000
0x10010004

• After executed Store word

$13 0x1234 abcd


x: 0x10010000 0x1234 abcd
0x10010004
Store word – Case 2
• Store word address is a register with no constant.
• The register is normally called Rb
• sw R, (Rb)
• The contents of Rb is an address.
• R = M[contents of Rb]
• For example,
• sw $13, ($23)
• $13 = M[$23]

Before executed Store Word After executed Store Word

x: 0x10010000 x: 0x10010000 0x1234 abcd


0x10010004 0x10010004

$13 0x1234 abcd $13 0x1234 abcd

$23 0x10010000 $23 0x10010000


Store word – Case 3
• Store word address is a register with a constant.
• The register is normally called Rb
• The constant is called K
• sw R, K(Rb)
• The contents of Rb is an address.
• M[contents of Rb + K] = R
• For example,
• sw $13, 4($23)
• M[4 + $23] = M[0x1001 0004] = R

Before executed Store Word After executed Store Word

x: 0x10010000 0x1234 abcd


x: 0x10010000 0x1234 abcd
0x10010004 0x8090 a0b0
0x10010004

$13 0x8090 a0b0


$13 0x8090 a0b0
$23 0x1001 0000
$23 0x1001 0000
Negative Indexing with LW and SW

• For case 3 of load and store words, the examples use a positive value
for K.
• It is possible to use a negative value for K.
• For example:

sw $13, -4($23)
0x10010000-4 = 0x1000fffc

0x1000 FFFC 0x0000 00ab $13 0xab


x: 0x1001 0000 0x1234 abcd
0x8090a0b0 $23 0x10010000
0x1001 0004
More load and Store instructions

• In addition to lw and sw, we also have lb, lbu, and sb.


• There is also, lh, lhu, and sh but these will not be covered.
• These are for working with halfwords.
• Instead of loading 4 bytes like load word or 1 byte for load byte, lh loads 2
bytes.
Load Byte

• lb R, ADDR
• Here R, represents any register
• ADDR Represents an address.
• lb loads the contents of byte at ADDR into to lower 8 bits ( bits 0 to 7)
of R.
• Note that lb has to deal with both signed and unsigned numbers.
• Therefore the sign bit must be maintained.
• lb R, ADDR  𝑅 = 𝑚 𝐴𝐷𝐷𝑅 7 24 ||𝑚 𝐴𝐷𝐷𝑅
Load Byte Example

lb $13, ($23)
Before Execution of Load Byte

$13
$23 0x1001 0008 x: 0x1001 0008 0x9876 5432
0x1001 000C

After Execution of Load Byte

$13 0xFFFF FF98


$23 0x1001 0008 x: 0x1001 0008 0x9876 5432
0x1001 000C
Load Byte Example

• Note that register $13 now contains the byte at ADDR, which is stored
in register $23.
• Note the value is 0xFFFF FF98 and NOT 0x0000 0098.
• Why?
Load Byte Example

• Note that register $13 now contains the byte at ADDR, which is stored
in register $23.
• Note the value is 0xFFFF FF98 and NOT 0x0000 0098.
• Why?
• This is because the byte 0x98 is negative.
• This means bit 7 is a 1 and is duplicated 24 times when being loaded.
• Recall: lb R, ADDR  𝑅 = 𝑀 𝐴𝐷𝐷𝑅 7 24 ||𝑀 𝐴𝐷𝐷𝑅
• M[ADDR] = 0x9876 5432
• M[ADDR]_7 = 1 (98 hex to bin  1001 1000)
• Therefore:
• 𝑀 𝐴𝐷𝐷𝑅 7 24 ||𝑀 𝐴𝐷𝐷𝑅  1111 1111 1111 1111 1111 1111 || 1001
1000
• Which gives 1111 1111 1111 1111 1111 1111 1001 1000
• 0xFFFF FF98
Load Byte Unsigned

• lbu R, ADDR
• Here R, represents any register
• ADDR Represents an address.
• lbu loads the contents of byte at ADDR into to lower 8 bits ( bits 0 to
7) of R.
• The result is an unsigned number.
• Note that lbu only results in an unsigned number.
• Therefore the resulted value is padded with 24 0’s
• lbu R, ADDR  𝑅 = 024 ||𝑚 𝐴𝐷𝐷𝑅
Load Byte Unsigned Example

lbu $13, ($23)


Before Execution of Load Unsigned Byte

$13
$23 0x1001 0008 x: 0x1001 0008 0x9876 5432
0x1001 000C

After Execution of Load Unsigned Byte

$13 0x0000 0098


$23 0x1001 0008 x: 0x1001 0008 0x9876 5432
0x1001 000C
Load Byte Unsigned Example

• Note that register $13 now contains the byte at ADDR, which is stored
in register $23.
• Note for lbu, the value is 0x0000 0098 and NOT 0xFFFF FF98.
• Why?
Load Byte Unsigned Example
• Note that register $13 now contains the byte at ADDR, which is stored
in register $23.
• Note for lbu, the value is 0x0000 0098 and NOT 0xFFFF FF98.
• Why?
• This is because even though the byte 0x98 is negative, our instruction only
works with unsigned numbers.
• This means bit 7 is a 1 and is NOT duplicated 24 times when being loaded.
BUT INSTEAD THE NUMBER IS PADDED WITH 0’s
• Recall: lbu R, ADDR  𝑅 = 𝑀 𝐴𝐷𝐷𝑅 7 24 ||𝑀 𝐴𝐷𝐷𝑅
• M[ADDR] = 0x9876 5432
• M[ADDR]_7 = 1 (98 hex to bin  1001 1000)
• Therefore:
• 024 ||𝑀 𝐴𝐷𝐷𝑅  0000 0000 0000 0000 0000 0000 || 1001 1000
• Which gives 0000 0000 0000 0000 0000 0000 1001 1000
• 0x0000 0098 or 0x98
Store Byte

• sb R, ADDR
• Here R, represents any register
• ADDR Represents an address.
• sb stores the lower 8 bits of the contents of R into the byte at the
address ADDR.
• 𝑀 𝐴𝐷𝐷𝑅 = 𝑅 7..0
Store Byte Example

sb $13, ($23)
Before Execution of Load Byte

$13 0x1234 ABCD


$23 0x1001 0008 x: 0x1001 0008 0x9876 5432
0x1001 000C

After Execution of Load Byte

$13 0x1234 ABCD


$23 0x1001 0008 x: 0x1001 0008 0x98CD76 5432
0x1001 000C
Pointers

• A pointer is a variable that contains the address of another variable


(in memory).
• Since addresses are integers, pointers “look” like integers.
• Example:
x is an integer variable (in memory).
ptr_x is a pointer to an integer.
$s0 is also a pointer to an integer.

$s0 0x1001 0000


x: 0x1001 000 7

ptr_x: 0x1001 000 0x1001 0000

• ptr_x and $s0 both point to x


• ptr_x and $s0 contain the address of x.
Declaring Pointers

• To declare a pointer called ptr_x of type [type] you do the following:


• [type] *ptr_x;
• For example:
Initializing Pointers

• Pointers must be initialized before they are used. We initialize the


contents of pointers to the addresses of variables.
• The & operator means “address of a variable”
• &x means address of the variable x.
• To initialize ptr_x to point to the variable x (Or, initialize ptr_x to
contain the address of the variable x):
Dereferencing a Pointer

• To reference a variable that a pointer points to, we dereference the


pointer.
• *ptr_x means the variable that ptr_x points to.
Pointer Operations

• Always think of pointers as variables that contain addresses.


• Example:

• Suppose
Address of x is 0x1001 0000
Address of y is 0x1001 0004

Variable Address Contents


x 0x1001 0000
y 0x1001 0004
ptr_x ??
ptr_y ??
Pointer Operations

• Always think of pointers as variables that con- tain addresses.


• Example:

• Suppose
Address of x is 0x1001 0000 After declaring and
Address of y is 0x1001 0004 initializing x and y.
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ??
ptr_y ??
Pointer Operations

• Always think of pointers as variables that con- tain addresses.


• Example:

• Suppose
Address of x is 0x1001 0000 After initializing ptr_x and
Address of y is 0x1001 0004 ptr_y.
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Some Pointer Examples
Suppose we do : ptr_x = &y
This sets ptr_x to the address
of y
Now ptr_x points to y instead
of x.

• Suppose Before ptr_x = &y


Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Some Pointer Examples

Suppose we do : ptr_x = &y


This sets ptr_x to the address
of y
Now ptr_x points to y instead
of x.

• Suppose After ptr_x = &y


Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0004
ptr_y ?? 0x1001 0004
Some Pointer Examples
Suppose we do : ptr_y = ptr_x

This sets ptr_y equal to ptr_x;


hence, they both point to
what ptr_x points to, which is
x.
• Suppose Before ptr_y = ptr_x
Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Some Pointer Examples
Suppose we do : ptr_y = ptr_x

This sets ptr_y equal to ptr_x;


hence, they both point to
what ptr_x points to, which is
x.

• Suppose Afterptr_y = ptr_x


Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0000
Some Pointer Examples
Suppose we do : *ptr_x= y

This sets what ptr_x points to,


which is x, to the contents of
the variable y.

• Suppose Before *ptr_x = y


Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Some Pointer Examples
Suppose we do : *ptr_x= y

This sets what ptr_x points to,


which is x, to the contents of
the variable y.

• Suppose After *ptr_x = y


Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 13
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Some Pointer Examples
Suppose we do :
*ptr_x = *ptr_x + *ptr_y;
x = x+y
This sets what ptr_x points to
equal to the sum of what ptr_x
points to and what ptr_y points
to
• Suppose Before *ptr_x = *ptr_x + *ptr_y;
Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 7
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Some Pointer Examples
Suppose we do :
*ptr_x = *ptr_x + *ptr_y;
x = x+y
This sets what ptr_x points to
equal to the sum of what ptr_x
points to and what ptr_y points
to
• Suppose After *ptr_x = *ptr_x + *ptr_y;
Address of x is 0x1001 0000
Address of y is 0x1001 0004
Variable Address Contents
x 0x1001 0000 20
y 0x1001 0004 13
ptr_x ?? 0x1001 0000
ptr_y ?? 0x1001 0004
Character Arrays

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]

• In C/C++, characters are encoded in ASCII.


• Each character is 8 bits.
• ‘G’ is 0x47  (71), ‘y’ is 0x79 (121)
• Java uses Unicode
• Each character is 16 bits(2 bytes)
Character Arrays

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]

• The base address of str[] is &str[0] = str = 0x1001 0000

• Address of str[i] = &str[0] + i


• NOTE this only works for char arrays not usable for other types.
Using Pointers to Access Char Arrays

• To initialize ptr_ch to point to str[0]:

• Simple Pointer Arithmetic: means “add one to ptr_ch


so that ptr_ch contains
the address of the next
char”
Simple Code sample with array pointers.

ptr_ch

*ptr_ch

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch 0x1001 0000

*ptr_ch ‘G’

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch 0x1001 0000

*ptr_ch ‘G’

output 0x1001 0000

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch 0x1001 0001

*ptr_ch ‘y’

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch 0x1001 0001

*ptr_ch ‘y’

output y

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch 0x1001 0002

*ptr_ch ‘s’

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch 0x1001 0002

*ptr_ch ‘s’

output 0x1001 0002

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Adding values to Pointers

• Similarly, ptr_ch = ptr_ch + K means “add K to contents of ptr_ch, so


that ptr_ch contains the address of the char that is K chars after the
original char that ptr_ch pointed to”
• True for char arrays and char pointers only! int arrays and int pointers
slightly different
Simple Code sample with array pointers.

ptr_ch

*ptr_ch

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch ‘G’

*ptr_ch 0x1001 0000

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch ‘i’

*ptr_ch 0x1001 0003

output

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch ‘i’

*ptr_ch 0x1001 0003

output i

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Simple Code sample with array pointers.

ptr_ch ‘i’

*ptr_ch 0x1001 0003

output s

str: 0x1001 0000 ‘G’ str[0]


0x1001 0001 ‘y’ str[1]
0x1001 0002 ‘s’ str[2]
0x1001 0003 ‘i’ str[3]
0x1001 0004 ‘n’ str[4]
0x1001 0005 ‘\0’ str[5]
Working with char arrays in C/C++ and MIPS:

• C/C++ (sequential array access or stepping through an array):


char str[6];

for (i=0;i<6;i++)
str[i] = 0xA;
• Need to calculate address of str[i]
• use formula: &str[i] = &str[0] + i
• Need instruction to get address of label
• MIPS instruction for loading an address is la
• la R, label
• Choose some registers: i is $i
• &str[0] (or address of str) is in $base
• $temp is a temporary
Working with char arrays in C/C++ and MIPS:
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0
li $i, 0
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

[Example 3.1: http://unixlab.sfsu.edu/~whsu/csc256/PROGS/3.1.s ]


Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i
str: 0x10010000 trash str[0]
$t1 0x10010001 trash str[1]
$base 0x10010002 trash str[2]
0x10010003 trash str[3]
$temp
0x10010004 trash str[4]
0x10010005 trash str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i
str: 0x10010000 0x0 str[0]
$t1 0x10010001 0x0 str[1]
$base 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 0
str: 0x10010000 0x0 str[0]
$t1 0x10010001 0x0 str[1]
$base 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 0
str: 0x10010000 0x0 str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 0
str: 0x10010000 0x0 str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 0
str: 0x10010000 0x0 str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001000
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 0
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001000
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 1
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001000
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop $i < 6? True goto loop

Register Value label address contents Array


element
$i 1
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001000
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 1
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0x0 str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001001
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 1
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001001
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 2
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001001
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop True go to loop

Register Value label address contents Array


element
$i 2
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001001
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 2
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0x0 str[2]
0x10010003 0x0 str[3]
$temp 0x1001002
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 2
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0x0 str[3]
$temp 0x1001002
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 3
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0x0 str[3]
$temp 0x1001002
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop True go to loop

Register Value label address contents Array


element
$i 3
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0x0 str[3]
$temp 0x1001002
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 3
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0x0 str[3]
$temp 0x1001003
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 3
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001003
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 4
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001003
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop True go to loop

Register Value label address contents Array


element
$i 4
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001003
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 4
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001004
0x10010004 0x0 str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 4
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001004
0x10010004 0xA str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 5
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001004
0x10010004 0xA str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop True go to loop

Register Value label address contents Array


element
$i 5
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001004
0x10010004 0xA str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 5
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001005
0x10010004 0xA str[4]
0x10010005 0x0 str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 5
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001005
0x10010004 0xA str[4]
0x10010005 0xA str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop

Register Value label address contents Array


element
$i 6
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001005
0x10010004 0xA str[4]
0x10010005 0xA str[5]
Tracing the Code w/ Memory
.data
str: .btye 0:6 # at label str, allocate 6 initialize to 0

li $i, 0
li $t1, 0xA
la $base, str #$base = 0x1001000
loop: add $temp, $base, $i #&str[i] = $base+i
sb $ t1, ($temp) #str[i] = 0xA
addi $i, $i, 1
blt $i, 6, loop False, we fall though

Register Value label address contents Array


element
$i 6
str: 0x10010000 0xA str[0]
$t1 0xA 0x10010001 0xA str[1]
$base 0x1001000 0x10010002 0xA str[2]
0x10010003 0xA str[3]
$temp 0x1001005
0x10010004 0xA str[4]
0x10010005 0xA str[5]
Rewriting MIPS w/ Pointers

char str[6];
char *ptr;
ptr = &str[0];

for (i = 0; i < 6; i++){


*ptr = 0xA;
ptr++;
}
Rewriting MIPS w/ Pointers

# $s0 is ptr
# $s1 is i
# $t0 for 6
# $t1 for 0xA
.data
str: .byte 0:6
.text
la $s0, str
li $s1, 0
li $t0, 6
li $t1, 0xA
loop:
sb $t1, ($s0)
addi $s0, $s0, 1
addi $s1, $s1, 1
blt $s1, $t0, loop
Rewrite C/C++ sequential array access code
using pointers:
int x[6];
int *ptr;

ptr = x;

for (i=0;i<6;i++) {
*ptr = i;
ptr++;
}
Rewrite in MIPS:

#ptr is in $s0
#i is in $s1
.data
x: .word 0:6
.text
li $s1, 0
la $s0, x
li $t0, 6
loop:
sw $s1, ($s0)
addi $s0, $s0, 4
addi $s1, $s1, I
blt $s1, $t0, loop
Summary

• New MIPS instructions:


lw load word
sw store word
lb load byte
lbu load byte unsigned
sb store byte
la load address
• Spim assembler directives
.data  data allocations follow
.text  program code follows
.word  allocate a word
.byte  allocate a byte

You might also like