0% found this document useful (0 votes)
101 views

Digital Design and Computer Architecture, 2: Edition

The chapter discusses arrays, which allow accessing large amounts of similar data through indexing each element. It describes how to access array elements using a base address and offsets. It also shows how to iterate through an array using a for loop in both C code and MIPS assembly. The chapter also briefly covers multiplication and division instructions that use special registers, as well as the ASCII encoding of characters.

Uploaded by

moacyranicio6360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Digital Design and Computer Architecture, 2: Edition

The chapter discusses arrays, which allow accessing large amounts of similar data through indexing each element. It describes how to access array elements using a base address and offsets. It also shows how to iterate through an array using a for loop in both C code and MIPS assembly. The chapter also briefly covers multiplication and division instructions that use special registers, as well as the ASCII encoding of characters.

Uploaded by

moacyranicio6360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 6

Digital Design and Computer Architecture, 2nd Edition


David Money Harris and Sarah L. Harris

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;

# MIPS assembly code


# $s0 = array base address
lui $s0, 0x1234 # 0x1234 in upper half of $s0
ori $s0, $s0, 0x8000 # 0x8000 in lower half of $s0

lw $t1, 0($s0) # $t1 = array[0]


sll $t1, $t1, 1 # $t1 = $t1 * 2
sw $t1, 0($s0) # array[0] = $t1

lw $t1, 4($s0) # $t1 = array[1]


sll $t1, $t1, 1 # $t1 = $t1 * 2
sw $t1, 4($s0) # array[1] = $t1

Chapter 6 <5>
Arrays using For Loops
// C Code
int array[1000];
int i;

for (i=0; i < 1000; i = i + 1)


array[i] = array[i] * 8;

# MIPS assembly code


# $s0 = array base address => $s0 = 0x23B8F000, $s1 = 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>

You might also like