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

Lecture4.2

The document discusses MIPS programming, focusing on array data structures and their manipulation in both C and MIPS assembly languages. It includes examples of accessing and modifying array elements, as well as converting characters from lowercase to uppercase using ASCII values. Additionally, it provides MIPS assembly code corresponding to C code for array operations and a take-home task for practice.

Uploaded by

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

Lecture4.2

The document discusses MIPS programming, focusing on array data structures and their manipulation in both C and MIPS assembly languages. It includes examples of accessing and modifying array elements, as well as converting characters from lowercase to uppercase using ASCII values. Additionally, it provides MIPS assembly code corresponding to C code for array operations and a take-home task for practice.

Uploaded by

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

CMPE361

Computer
Organization
Department of Computer Engineering
TED University- Fall 2024

MIPS programming - III


These Slides are mainly based on slides of
the text book (downloadable from the
book’s website).
COMPUTER ARCHITECTURE Arrays
• Array is a data structure used to access large amounts of
similar data
• You need
Index: to access each element
Size: to know total number of elements

Chapter 6 <3>
COMPUTER ARCHITECTURE Array example
• 5-element array in memory
• Base address = 0x12348000 (address of the first element,
array[0])
• First step in accessing an array is to know the base address

Chapter 6 <4>
COMPUTER ARCHITECTURE Accessing Array
// C Code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;

Chapter 6 <5>
COMPUTER ARCHITECTURE Accessing Array Example
// A simple piece of C Code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
• A corresponding MIPS assembly code
# 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 <6>
COMPUTER ARCHITECTURE 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, $s1 = i
#assume base address is 0x23B8F000

Chapter 6 <7>
COMPUTER ARCHITECTURE 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 <8>
COMPUTER ARCHITECTURE 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>
COMPUTER ARCHITECTURE Cast of Characters in Hexadecimal

Chapter 6 <10>
COMPUTER ARCHITECTURE Take home task (Example 6.7)
• Translate following C program to MIPS assembly
language.
• The program converts a ten-entry array of characters
from lowercase to upper-case by subtracting 32 from
each array entry.

// high-level code
char chararray[10]; // an array of 10 lowercase chars

int i;
for (i = 0; i != 10; i = i + 1)
chararray[i] = chararray[i] – 32;

Chapter 6 <11>
COMPUTER ARCHITECTURE

Chapter 6 <12>

You might also like