Conditional and Unconditional Branching in MIPS Assembly
# Assignment 4
# File: prog4.asm
# Date: April 2, 2020
# Register usage:
# $s0: memory base address
# $t0: temporary register for calculations and comparisons
# $t1: base address of array1
# $t2: base address of array2
# $t3: base address of array3
# $t4: index of arrays (i)
# $t5: offset in bytes of element at current index
# $t6: used to load array1[i]
# $t7: used to load array2[i]
# $t8: used to hold value to save in array3[i]
.data
array1: .word 303,340,342,390,141,449,189,360,325,338
array2: .word 466,22, 29, 161,465,196,416,123,325,273
array3: .space 40
.text
.globl main
main:
lui $s0, 0x1001 # address of array1
addu $t1, $zero, $s0 # base address for array1
addiu $t2, $s0, 40 # base address for array2
addiu $t3, $s0, 80 # base address for array3
addi $t4, $zero, 0 # initialize index to zero
j compare # start loop
loop:
sll $t5, $t4, 2 # multiply index by 4
addu $t0, $t1, $t5 # get address of array1[i]
lw $t6, 0($t0) # load array1[i]
addu $t0, $t2, $t5 # get address of array2[i]
lw $t7, 0($t0) # load array2[i]
slt $t0, $t7, $t6 # if array1[i] <= array2[i]
bne $t0, $zero, else
add $t8, $t6, $t7 # array3[i] = array1[i] + array2[i];
j endif
else: # else
sub $t8, $t6, $t7 # array3[i] = array1[i] - array2[i];
endif:
addu $t0, $t3, $t5 # get address of array3[i]
sw $t8, 0($t0) # save result in array3[i]
addi $t4, $t4, 1 # increment index
compare:
slti $t0, $t4, 10 # compare with 10
bne $t0, $zero, loop # repeat while index < 10
# Assignment 5
# File: prog5.asm
# Date: April 2, 2020
# Register usage:
# $s0: loop counter for loop1 and loop2
# $s1: accumulator for adding values
# $a0: used to pass values to syscalls and push function
# $v0: used to pass/get values in syscalls and to get value from pop function
# $sp: used to access stack in push and pop
# $ra: return address
.data
prompt: .asciiz "Enter five integers. Press enter after each one.\n"
message1: .asciiz "The numbers entered were: "
message2: .asciiz "\nThe sum of the numbers = "
.text
.globl main
main:
# define values for first loop control
addiu $s0, $zero, 5 # to loop 5 times
# print the user prompt
la $a0, prompt # load string address
addiu $v0, $zero, 4 # syscall number to print string
syscall # print the string
loop1:
# read value from user input
addiu $v0, $zero, 5 # syscall number to read integer
syscall # read the number
# push value onto stack
addu $a0, $zero, $v0 # pass value to push
jal push
# loop control instructions
addi $s0, $s0, -1 # decrement number of times to loop
bne $s0, $zero, loop1 # repeat while not zero
# reset loop control for output
addiu $s0, $zero, 5 # to loop 5 times
addiu $s1, $zero, 0 # initialize accumulator to zero
# print the output message
la $a0, message1 # load string address
addiu $v0, $zero, 4 # syscall number to print string
syscall # print the string
loop2:
# pop value from stack
jal pop
# add value to accumulator
add $s1, $s1, $v0
# print value to console
addu $a0, $zero, $v0 # pass value to print
addiu $v0, $zero, 1 # syscall number to print integer
syscall # print the number
# print space
addiu $a0, $zero, 32 # space ascii value to print
addiu $v0, $zero, 11 # syscall number to print character
syscall # print the character
# loop control instructions
addi $s0, $s0, -1 # decrement number of times to loop
bne $s0, $zero, loop2 # repeat while not zero
# print sum of values
la $a0, message2 # load string address
addiu $v0, $zero, 4 # syscall number to print string
syscall # print the string
addu $a0, $zero, $s1 # pass value to print
addiu $v0, $zero, 1 # syscall number to print integer
syscall # print the number
exit:
addi $v0, $zero, 10 # syscall number to exit program
syscall # exit program
# subroutine to push on stack
push:
addi $sp, $sp, -4 # allocate space in stack
sw $a0, 0($sp) # save value in stack
jr $ra # return
# subroutine to pop from stack
pop:
lw $v0, 0($sp) # load value from stack
addi $sp, $sp, 4 # remove allocated space from stack
jr $ra # return