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

Exercise Chap4

The document contains exercises for learning MIPS assembly language programming including writing programs that print strings, calculate sums, sort arrays, and implement string functions. Code examples are provided to declare variables and arrays, implement conditional statements and loops, and call system routines to print output. Detailed instructions and explanations are given for each exercise to practice common programming tasks in MIPS assembly.

Uploaded by

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

Exercise Chap4

The document contains exercises for learning MIPS assembly language programming including writing programs that print strings, calculate sums, sort arrays, and implement string functions. Code examples are provided to declare variables and arrays, implement conditional statements and loops, and call system routines to print output. Detailed instructions and explanations are given for each exercise to practice common programming tasks in MIPS assembly.

Uploaded by

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

Chapter 4 Exercises

MARS intro
Hello World
.text #Code segment to store instructions
addi $t1,$zero,2 #$t1 = 2
addi $t2,$zero,3 #$t2 = 3
add $t0, $t1, $t2 #$t0 = $t1 + $t2
Ex.1
• Implement the following pseudo program in MIPS
assembly.
• Assume that x, y, z, i, j are in s1, s2, s3, t0, t1.

if (i<=j){
x=x+1;
z=1;
}
else {
y=y-1;
z=2*z;
}
Solution
start:
slt $t0,$s2,$s1 # j<i
bne $t0,$zero,else # branch to else if j<i
addi $t1,$t1,1 # then part: x=x+1
addi $t3,$zero,1 # z=1
j endif # skip “else” part
else: addi $t2,$t2,-1 # begin else part: y=y-1
add $t3,$t3,$t3 # z=2*z
endif:
Ex.2
Find the sum of all elements in array A

loop: i = i + step;
sum=sum + A[i];
if (i != n) goto loop;
Solution
.text
loop:
add $s1,$s1,$s4 #i=i+step
add $t1,$s1,$s1 #t1=2*s1
add $t1,$t1,$t1 #t1=4*s1
add $t1,$t1,$s2 #t1 store the address of A[i]
lw $t0,0($t1) #load value of A[i] in $t0
add $s5,$s5,$t0 #sum=sum+A[i]
bne $s1,$s3,loop #if i != n, goto loop

How to declare variables, array?


2nd Hello World
.data #Data segment
x: .word 0x01020304 # x is a word
message: .asciiz "Dept. of Computer Engineering"
.text #Code segment to store instructions
la $a0, message #load string address to a0
li $v0, 4 #function $v0 = 4
syscall #call system routine

addi $t1,$zero,2 #$t1 = 2


addi $t2,$zero,3 #$t2 = 3
add $t0, $t1, $t2 #$t0 = $t1 + $t2

• Run and observe


• Text and data segment
• la, li, syscall instructions
• How to use variable x?
Directives
• Instruct assembler to store or allocate program
objects.
• Program structure
.text: store objects in the code segment (these are
instructions)
.data: store objects in data segment (static variables)
.ktext, .kdata: code and data segments for kernel (OS)
• Variable declaration (allocation)
.byte/.half/.word: store listed values as bytes/halfs/words
.ascii/.asciiz: string and null-terminated string
.space: reserved specified number of bytes
MIPS memory map
• Settings → Memory Configuration
Exercise
• Use .word directive to allocate a 4-byte integer
• Use code to assign value of 0x01020304 to that
integer
• Check the endianness used by the simulator
Exercise
• Draw the map of .data
this data segment arr16: .space 16
arr: .byte 'a','b','c'
s1: .ascii "abcd"
X1: .word 0xFFFFFFFF
s2: .ascii "efgh"
X2: .word 0xFFFFFFFF
s3: .asciiz "1234"
X3: .word 0xFFFFFFFF
s4: .asciiz "5678"
X4: .word 0xFFFFFFFF
Exercise
• Use .word directive to allocate a static array of 10
elements, initialize data for each element, and
calculate the sum of all element items.
Exercise
• Declare and initialize an array of integers. The
number of element in that array is already known.
• Find the largest value in the array.
Exercise
• Write a program
- Print “Hello!” to console
- Wait for user to press any key
- Print “Have a good day!”
Exercise
• Write a program to ask for a name, then print
“Hello ” + the name that was read.
Exercise
• Write a program to read two integers from console,
then print the sum of that two integers.
Exercise
• Write a program to read a positive value n from
console, then calculate and print the sum of all
numbers from 1 to n.
Exercise
• Write a program for printing an array value to
output.
- Allocate a static array of n integers, n is a known
value
- Write a subroutine for printing array, with two
arguments: array A and number of elements n
- Use the subroutine to print array value
Exercise
• Write a program based on the given sorting
function.
- Allocating a static array of 10 integers
- Print original value of array
- Sort the array
- Print value of array after sorting
Exercise
• Develop a simple implementation of strlen function
size_t strlen(const char *str)
• Write a program to input a string (max of 255
chars), print out its length
Exercise
• Develop a function to accept an input string, then
revers that string to a new string.

You might also like