You are on page 1of 8

CS224

Lab01
001
Mehmet Emre Kantaş
22003693
10/05/2022

-------------------------Lab01 Program1.asm-------------------------
##
## Program1.asm - prints out "hello world"
##
## a0 - points to the string
##

#################################
# #
# text segment #
# #
#################################

.text
.globl __start

__start: # execution starts here


la $a0,str # put string address into a0
li $v0,4 # system call to print
syscall # out a string

li $v0,10 # system call to exit


syscall # bye bye

#################################
# #
# data segment #
# #
#################################

.data
str: .asciiz "hello Pouya Ghahramanian"
n: .word 10

##
## end of file Program1.asm

-------------------------Lab01 Program2.asm-------------------------
##
## Program2.asm asks user for temperature in Celsius,
## converts to Fahrenheit, prints the result.
##
## v0 - reads in Celsius
## t0 - holds Fahrenheit result
## a0 - points to output strings
##

#################################
# #
# text segment #
# #
#################################

.text
.globl __start

__start:
la $a0,prompt # output prompt message on terminal
li $v0,4 # syscall 4 prints the string
syscall

li $v0, 5 # syscall 5 reads an integer


syscall

mul $t0,$v0,9 # to convert,multiply by 9,


div $t0,$t0,5 # divide by 5, then
add $t0,$t0,32 # add 32

la $a0,ans1 # print string before result


li $v0,4
syscall

move $a0,$t0 # print integer result


li $v0,1 # using syscall 1
syscall

la $a0,endl # system call to print


li $v0,4 # out a newline
syscall

li $v0,10 # system call to exit


syscall # bye bye

#################################
# #
# data segment #
# #
#################################

.data
prompt: .asciiz "Enter temperature (Celsius): "
ans1: .asciiz "The temperature in Fahrenheit is "
endl: .asciiz "\n"

##
## end of file Program2.asm

-------------------------Lab01 Program3.asm-------------------------

##
## Program3.asm is a loop implementation
## of the Fibonacci function
##

#################################
# #
# text segment #
# #
#################################
.text
.globl __start

__start: # execution starts here


li $a0,7 # to calculate fib(7)
jal fib # call fib
move $a0,$v0 # print result
li $v0, 1
syscall

la $a0,endl # print newline


li $v0,4
syscall

li $v0,10
syscall # bye bye

#------------------------------------------------

fib: move $v0,$a0 # initialise last element


blt $a0,2,done # fib(0)=0, fib(1)=1

li $t0,0 # second last element


li $v0,1 # last element

loop: add $t1,$t0,$v0 # get next value


move $t0,$v0 # update second last
move $v0,$t1 # update last element
sub $a0,$a0,1 # decrement count
bgt $a0,1,loop # exit loop when count=0
done: jr $ra

#################################
# #
# data segment #
# #
#################################

.data
endl: .asciiz "\n"

##
## end of Program3.asm
-------------------------Lab01 Part2_3-------------------------
#lab1_3.asm
.data
A: .word 0
B: .word 0
entera: .asciiz "Enter a: "
enterb: .asciiz "Enter b: "

.text
main:

#enter number A prompt


la $a0, entera
li $v0, 4
syscall

#taking input number A


li $v0, 5
syscall

sw $v0, A

#enter number B prompt


la $a0, enterb
li $v0, 4
syscall

#taking input number B


li $v0, 5
syscall

sw $v0, B

#saving variables in save registers to do computation


lw $s0, A
lw $s1, B

#to multiply by A
addi $t2, $0, 2

mul $t0, $t2, $s0 #$t0 = 2 * A


sub $t0, $t0, $s1 #$t0 = 2 * A - B

add $t1, $s0, $s1 #$t1 = A + B

div $t3, $t0, $t1 #$t3 = (2 * A - b) / (A + B)

#outputting result ($t3)


move $a0, $t3
li $v0, 1
syscall

#system exit
li $v0, 10
syscall

-------------------------Lab01 Part2_4-------------------------
.data

array: .space 800


arraySize: .word 0
enterSize: .asciiz "Enter array size(max 100): "
enterElem: .asciiz "Enter element: "
select: .asciiz "-------Enter an instruction-------\n1 - Find sum of the
elems which is greater than an input\n"
select2: .asciiz "2 - Sum of even and odd numbers\n3 - Number of elements that
are divisible by your input\n"
select3: .asciiz "4 - Quit\nEnter your selection: "
yourInput: .asciiz "Your input: "
goodbye: .asciiz "\nGoodBye!"
select1out: .asciiz "The sum is: "
select2evenOut: .asciiz "Even sum: "
select2oddOut: .asciiz "\nOdd sum: "
select3Out: .asciiz "Number of array elems divisible by your input: "

.text
main:

#prompting the user to enter the array size


la $a0, enterSize
li $v0, 4
syscall

#taking array size as input


li $v0, 5
syscall

sw $v0, arraySize #storing array size

la $t0, array #assigning base address of the array to t0

# 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

la $a0, select #
li $v0, 4 #
syscall #
#
la $a0, select2 #
li $v0, 4 #Displaying menu
syscall #
#
la $a0, select3 #
li $v0, 4 #
syscall #

#taking selection as input


li $v0, 5
syscall

#branching according to the user selection


beq $v0, 1, selection1
beq $v0, 2, selection2
beq $v0, 3, selection3
beq $v0, 4, done

selection1:

#prompting the user and taking input to choose greater than number
la $a0, yourInput
li $v0, 4
syscall

li $v0, 5
syscall
move $t0, $v0 #t0 is input

la $t1, array #t1 is base address of the array


lw $t2, arraySize #t2 is arraySize
addi $t3, $0, 0 #t3 is sum
addi $t4, $s0, 0 #t4 is index

loop1:
bge $t4, $t2, print1 #if(i >= arraysize) -> print1
lw $t5, 0($t1) #t5 is elem at index i
addi $t1, $t1, 4 #next element of the array
addi $t4, $t4, 1 #next index
sgt $t6, $t5, $t0 #copy of the elem at current index because $t5 should
not be corrupted
beq $t6, 0, loop1
add $t3, $t3, $t5
j loop1
print1:

la $a0, select1out
li $v0, 4
syscall

move $a0, $t3


li $v0, 1
syscall
j done

selection2:

la $t0, array #t0 is base address of the array


lw $t1, arraySize #t1 is arraySize
addi $t2, $0, 0 #t2 is even sum
addi $t3, $0, 0 #t3 is odd sum
addi $t4, $0, 0 #t4 is index
loop2:
bge $t4, $t1, print2
lw $t5, 0($t0)#elem at index i
addi $t0, $t0, 4
addi $t4, $t4, 1
addi $t6, $t5, 0
mod:
blt $t6, 2, doneMod
addi $t6, $t6, -2
j mod
doneMod:
beq $t6, 0, evenSum
beq $t6, 1, oddSum
evenSum:
add $t2, $t2, $t5
j loop2
oddSum:
add $t3, $t3, $t5
j loop2

print2:

la $a0, select2evenOut
li $v0, 4
syscall

move $a0, $t2


li $v0, 1
syscall

la $a0, select2oddOut
li $v0, 4
syscall

move $a0, $t3


li $v0, 1
syscall
j done

selection3:

la $a0, yourInput
li $v0, 4
syscall

li $v0, 5
syscall
move $t0, $v0 #t0 is input

la $t1, array #t1 is base address of the array


lw $t2, arraySize #t2 is arraySize
addi $t3, $0, 0 #t3 is sum
addi $t4, $s0, 0 #t4 is index

loop3:
bge $t4, $t2, print3
lw $t5, 0($t1)#elem at index i
addi $t1, $t1, 4
addi $t4, $t4, 1
addi $t6, $t5, 0
mod3:
blt $t6, $t0, doneMod3
sub $t6, $t6, $t0
j mod3
doneMod3:

beq $t6, 0, increment


j loop3
increment:
addi $t3, $t3, 1
j loop3

print3:

la $a0, select3Out
li $v0, 4
syscall

move $a0, $t3


li $v0, 1
syscall
j done

done:

la $a0, goodbye
li $v0, 4
syscall

li $v0, 10
syscall

You might also like