You are on page 1of 6

CS224

Lab 1
001
Mehmet Emre Kantaş
22003693
10/05/2022

------------------------------------------------SWAP (PRELIMINARY WORK


PART1.1)----------------------------------------------

#CS224_Lab1_1.asm --> This program creates an array with the maximum size 20, takes
the actual array length from the user
#as an input, fills the array according to the inputs of the user, displays the
array, reverses the order of the elements,
#displays the array again.

.globl main
.text
main:
#prompting the user to enter the size of the array
la $a0, enterSize
li $v0, 4
syscall

#taking input from the user


li $v0, 5
syscall

#storing the input in the variable "arraySize"


sw $v0, arraySize

#Starting to create array by taking array elements as inputs from the user

# loading the base address of the array into the register t0


la $t0, array

# loading the size of the array into the register t1


lw $t1, arraySize

#register t2 will be used as index


addi $t2, $0, 0

fillingTheArray:

#prompting the user to enter the array elements


la $a0, enterElem
li $v0, 4
syscall

#taking input
li $v0, 5
syscall

#(initially array[0] = input)


sw $v0, 0($t0)

#incrementing the base address by 4 to access the next array element


addi $t0, $t0, 4
#incrementing the index by 1
addi $t2, $t2, 1

#if(index < arraySize) -> continue to fill the array


blt $t2, $t1, fillingTheArray

#displaying the array


jal printArray

#loading the base address of the array to register t1


la $t1, array

#starting to swap array elements


swapItems:

# loading the first and the last elements of the array to register t5
and t6, respectively
lw $t5, 0($t0)
lw $t6, 0($t1)

#swapping the elements


sw $t5, 0($t1)
sw $t6, 0($t0)

#incrementing t1 (was initially holding the base address of the array)


by 4 to access the next element
addi $t1, $t1, 4

#decrementing t0 (was initially holding the address of the last element


of the array) by 4 to access the previous element
addi $t0, $t0, -4

#if bigger and smaller indexes cross, break. If not, continue


bgt $t0, $t1, swapItems

#displaying the reversed array


jal printArray

#exiting the execution


li $v0, 10
syscall

#printArray is a function to print the array elements in order


printArray:

#loading the base address of the array into the register t0


la $t0, array

#loading the array size into the register t1


lw $t1, arraySize

#register t2 will be used as index


addi $t2, $0, 0

#starting to print the array


la $a0, displayMsg
li $v0, 4
syscall
display:
#reading the data stored in the address which is stored in the register
t0
lw $a0, 0($t0)

#incrementing t0 (was initially the base address of the array) by 4 to


access the next element
addi $t0, $t0, 4

#printing the element


li $v0, 1
syscall

#register t4 will be used to test whether we are at the last element of


the array or not
addi $t4, $t2, 1

#if the last element is printed, stop branching into comma and branch
into done
bgt $t1, $t4, putComma
ble $t1, $t4, done

#to print comma


putComma:
la $a0, comma
li $v0, 4
syscall

#branching in when the last comma is printed


done:

# increment the index


addi $t2, $t2, 1

# continue if index < arraySize


blt $t2, $t1, display

# set $t0 adress to the last element


subi $t0, $t0, 4

#leaving a blank line


la $a0, endl
li $v0, 4
syscall

#returning to the next instruction


jr $ra

#data section
.data
array: .space 80
arraySize: .word 0
enterSize: .asciiz "Enter the size of the array(maximum 20): "
enterElem: .asciiz "Enter element: "
displayMsg: .asciiz "Array: "
comma: .asciiz ", "
endl: .asciiz "\n"
--------------------------------------------END OF SWAP (PRELIMINARY WORK
PART1.1)---------------------------------------------
------------------------------------------ARITHMETIC EXPRESSION(PRELIMINARY WORK
PART1.2)--------------------------------------

#CS224_Lab1_2.asm --> This program takes inputs from the user to be used in an
arithmetic expression. 3 inputs will be taken
#as integers: B, C and D. Using these integers, we will have A = (B / C + D * B -
C) mod B as a result. The program will display
#this result at the end of the execution flow.

.globl main
.text
main:

#informing the user about the input they will enter


la $a0, userPrompt
li $v0, 4
syscall

#leaving a blank space


la $a0, endl
li $v0, 4
syscall

#asking for the input number B


la $a0, askB
li $v0, 4
syscall

#taking input number B


li $v0, 5
syscall

#storing the input number B


sw $v0, B

#asking for the input number C


la $a0, askC
li $v0, 4
syscall

#taking input number C


li $v0, 5
syscall

#storing the input number C


sw $v0, C

#asking for the input number D


la $a0, askD
li $v0, 4
syscall

#taking input number D


li $v0, 5
syscall

#storing the input number D


sw $v0, D
#division

#register t0 will be used to hold a copy of B


lw $t0, B

#register t1 will be used to hold a copy of C


lw $t1, C

#register t2 will be used to hold a copy of D


lw $t2, D

#register t3 will hold the quotient for division


addi $t3, $0, 0

#loop for the division algorithm


division:
blt $t0, $t1, doneDiv #if nominator is less then denominator branch
to doneDiv. Else, countinue executing the block
sub $t0, $t0, $t1 #subtracting the denominator from the nominator to
find quotient
addi $t3, $t3, 1 #incrementing quotient held by register t3 by 1
j division

doneDiv:

#refreshing the value of B


lw $t0, B

#register t4 will hold (D * B) for multiplication


addi $t4, $0, 0

#calculating (D * B) and storing in register t4


mul $t4, $t2, $t0

#register t5 will hold (B / C + D * B - C)


addi $t5, $0, 0

#Calculating and storing (B / C + D * B - C) in register t5


add $t5, $t3, $t4 #$t1 = (B / C) + (D * B)
sub $t5, $t5, $t1 #$t1 = (B / C) + (D * B) - C

#taking mod B of the value in the register t1 to find the final result

#loop for taking mod algorithm


mod:
blt $t5, $t0, doneMod #if the value held by register t0 exceeds the
value held by B, branch to doneMod. Else, continue executing the block
sub $t5, $t5, $t0 #subtracting the value held by register t0 by B to
find the result
j mod

doneMod:

#printing results
move $a0, $t5
li $v0, 1
syscall
#exiting
li $v0, 10
syscall

#data section
.data
result: .word 0
B: .word 0
C: .word 0
D: .word 0
userPrompt: .asciiz "You need to enter 3 integer values for the
arithmetic expression (to find A)"
askB: .asciiz "Enter the number B: "
askC: .asciiz "Enter the number C: "
askD: .asciiz "Enter the number D: "
finalMessage: .asciiz "(B / C + D * B - C) mod B = "
endl: .asciiz "\n"

---------------------------------------END OF ARITHMETIC EXPRESSION(PRELIMINARY


WORK PART1.2)-----------------------------------

You might also like