Digital Design and Computer Architecture, 2: Edition
Digital Design and Computer Architecture, 2: Edition
Chapter 6 <1>
Arrays
• Access large amounts of similar data
• Index: access each element
• Size: number of elements
Chapter 6 <2>
Arrays
• 5-element array
• Base address = 0x12348000 (address of first element,
array[0])
• First step in accessing an array: load base address into a
register
0x12340010 array[4]
0x1234800C array[3]
0x12348008 array[2]
0x12348004 array[1]
0x12348000 array[0]
Chapter 6 <3>
Accessing Arrays
// C Code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
Chapter 6 <4>
Accessing Arrays
// C Code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
Chapter 6 <5>
Arrays using For Loops
// C Code
int array[1000];
int i;
Chapter 6 <6>
Arrays Using For Loops
# MIPS assembly code
# $s0 = array base address, $s1 = i
# initialization code
lui $s0, 0x23B8 # $s0 = 0x23B80000
ori $s0, $s0, 0xF000 # $s0 = 0x23B8F000
addi $s1, $0, 0 # i = 0
addi $t2, $0, 1000 # $t2 = 1000
loop:
slt $t0, $s1, $t2 # i < 1000?
beq $t0, $0, done # if not then done
sll $t0, $s1, 2 # $t0 = i * 4 (byte offset)
add $t0, $t0, $s0 # address of array[i]
lw $t1, 0($t0) # $t1 = array[i]
sll $t1, $t1, 3 # $t1 = array[i] * 8
sw $t1, 0($t0) # array[i] = array[i] * 8
addi $s1, $s1, 1 # i = i + 1
j loop # repeat
done:
Chapter 6 <7>
Multiplication, Division
• Special registers: lo, hi
• 32 × 32 multiplication, 64 bit result
– mult $s0, $s1
– Result in {hi, lo}
• 32-bit division, 32-bit quotient, remainder
– div $s0, $s1
– Quotient in lo
– Remainder in hi
• Moves from lo/hi special registers
– mflo $s2
– mfhi $s3
Chapter 6 <8>
ASCII Code
• American Standard Code for Information
Interchange
• Each text character has unique byte
value
– For example, S = 0x53, a = 0x61, A = 0x41
– Lower-case and upper-case differ by 0x20 (32)
Chapter 6 <9>
Cast of Characters
Chapter 6 <10>