Professional Documents
Culture Documents
Pointers and Arrays
Pointers and Arrays
Pointers and Arrays
Anthony J Souza
X: 0x10010000 0x00000004
y: 0x10010004 0xffffffff
Layout of Code and Data in Memory
$13
x: 0x10010000 0x1234 abcd
0x10010004
• $13 = M[$23]
• 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
• 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
• 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
$13
$23 0x1001 0008 x: 0x1001 0008 0x9876 5432
0x1001 000C
• 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
• Suppose
Address of x is 0x1001 0000
Address of y is 0x1001 0004
• 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
• 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.
ptr_ch
*ptr_ch
output
*ptr_ch ‘G’
output
*ptr_ch ‘G’
*ptr_ch ‘y’
output
*ptr_ch ‘y’
output y
*ptr_ch ‘s’
output
*ptr_ch ‘s’
ptr_ch
*ptr_ch
output
ptr_ch ‘G’
output
ptr_ch ‘i’
output
ptr_ch ‘i’
output i
ptr_ch ‘i’
output s
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
char str[6];
char *ptr;
ptr = &str[0];
# $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