Commit f3f6e120 authored by CU2018's avatar CU2018
Browse files

Labs&Projects

parent 53ea57e0
.data #data segment
hello_message: .asciiz "Hello World!"
.text #.data and .text == switch to the data and text(code) segment, must switch back after to write more
#----------------------------------------------------
print_int:
li v0, 1 #print integer, == System.out.print() in java
syscall
li a0, '\n'
li v0, 11
syscall
jr ra #"return" instruction; == "jump register"
#----------------------------------------------------
print_string:
li v0, 4
syscall
li a0, '\n'
li v0, 11
syscall
jr ra
#----------------------------------------------------
print_123: #nested function calls
push ra
li, a0, 1
jal print_int
li, a0, 2
jal print_int
li, a0, 3
jal print_int
pop ra
jr ra
#----------------------------------------------------
.globl main #the .globl directive is only needed for main. Most of our functions won't need it
main: #main is a label
jal print_123
la a0, hello_message #la == "load address", cannot use (li)load immediate
jal print_string
li a0, 1234 #li = loading immediate(a constant that is written inside the instruction),
#put a constant variable into a register; a0, arguement register,a function call is about to happen, print_int(1234)
jal print_int #"jump and link"
li a0, 5678 #since print_int take one arguement from a0
jal print_int #call the function
.data
hello_message: .asciiz "Hello World"
.text
print_int:
li v0, 1
syscall
li a0, '\n'
li v0, 11
syscall
jr ra
print_string:
li v0, 4
syscall
li a0, '\n'
li v0, 11
syscall
jr ra
print_123:
push ra
li a0, 1
jal print_int
li a0, 2
jal print_int
li a0, 3
jal print_int
pop ra
jr ra
.globl main
main:
jal print_123
la a0, hello_message
jal print_string
li a0, 1234
jal print_int
li a0, 5678
jal print_int
.data
small: .byte 200
medium: .half 400
large: .word 0
.eqv NUM_ITEMS 5 #like #define in C, name a constant
values: .word 0:NUM_ITEMS #make an array containing 5 0's
.text
.globl main
main:
lbu t0, small #zero extension: lbu vs. signed extension: lb
lh t1, medium
lw a0, large
mul a0, t0, t1
li v0, 1
syscall
li a0, '\n'
li v0, 11
syscall
push s0 #when using s0 as a counter, push it before the loop
li s0, 0 #the comparing variable and the offset of the arr
ask_loop_top: #while(...)
blt s0, NUM_ITEMS, ask_loop_body #if(s0 < NUM_ITEMS)->body
j ask_loop_exit #if(s0 >= NUM_ITEMS)->exit
ask_loop_body:
la t0, values #load address of an array; t0 is A in A+B*i
li t2, 4 #a word is 4 bytes; t2 is B in A+B*i
mul t1, s0, t2 #s0 i in A + B*i; t1 is B*i
add t0, t0, t1 #now t0 is at the first address of an element
li v0, 5 #get the number from user
syscall
sw v0, (t0) #put user's input(vo) into the t0 position, sw is left-->right
add s0, s0, 1 #s0++
j ask_loop_top
ask_loop_exit: #codes after the loop
pop s0 #pop the s0 back and next time it will still be 0 when being used as a counter
\ No newline at end of file
.data
small: .byte 200
medium: .half 400
large: .word 0
.eqv NUM_ITEMS 5
values: .word 0:NUM_ITEMS
.text
.globl main
main:
lbu t0, small
lh t1, medium
lw a0, large
mul a0, t0, t1
li v0, 1
syscall
li a0, '\n'
li v0, 11
syscall
push s0
li s0, 0
ask_loop_top:
blt s0, NUM_ITEMS, ask_loop_body
j ask_loop_exit
ask_loop_body:
la t0, values
li t2, 4
mul t1, s0, t2
add t0, t0, t1
li v0, 5
syscall
sw v0, (t0)
add s0, s0, 1
j ask_loop_top
ask_loop_exit:
pop s0
# YOUR NAME (yourusername)
.data
.eqv ARRAY_LENGTH 10
array: .word 100 200 300 400 500 600 700 800 900 1000
.text
# -----------------------------------------------
.globl main
main:
###################################################################
# PROBLEM 1: this prints 0, not 45.
# print_int(45)
li a0, 45
jal print_int
###################################################################
# PROBLEM 2: this doesn't print what the user typed.
# print_int(read_int())
jal read_int
move a0, v0
jal print_int
###################################################################
# PROBLEM 3: this function goes into an infinite loop.
# read_int_plus_one()
jal read_int_plus_one
###################################################################
# PROBLEM 4: this doesn't print 5 twice, like I expected.
# print_int_plus_one(4)
# print_int_plus_one(4)
li a0, 4
jal print_int_plus_one
li a0, 4
jal print_int_plus_one # a0 already has 4 in it, right?
###################################################################
# PROBLEM 5: this function crashes.
# change_array()
jal change_array
###################################################################
# PROBLEM 6: this function does not print 10 times.
# print_array()
jal print_array
###################################################################
# exit()
li v0, 10
syscall
# -----------------------------------------------
# void print_int(int x)
print_int:
li v0, 1
syscall
li a0, '\n'
li v0, 11
syscall
jr ra
# -----------------------------------------------
# int read_int()
read_int:
li v0, 5
syscall
jr ra
# -----------------------------------------------
# int read_int_plus_one()
read_int_plus_one:
push ra
jal read_int
add v0, v0, 1
pop ra
jr ra
# -----------------------------------------------
# int print_int_plus_one()
print_int_plus_one:
push ra
add a0, a0, 1
jal print_int
pop ra
jr ra
# -----------------------------------------------
# void change_array()
change_array:
# i = t0
# for(i = 0; i < ARRAY_LENGTH; i++) {
li t0, 0
_change_array_loop:
bge t0, ARRAY_LENGTH, _change_array_loop_exit
# array[i]++
la t1, array # array + (i * 4)
mul t2, t0, 4
add t2, t1, t2
lw t1, (t2)
add t1, t1, 1
sw t1, (t2)
# }
add t0, t0, 1
j _change_array_loop
_change_array_loop_exit:
jr ra
# -----------------------------------------------
# void print_array()
print_array:
push ra
push s0
# i = t0
# for(i = 0; i < ARRAY_LENGTH; i++) {
li s0, 0
_print_array_loop:
bge s0, ARRAY_LENGTH, _print_array_loop_exit
# print_array_item(i)
move a0, s0
jal print_array_item
# }
add s0, s0, 1
j _print_array_loop
_print_array_loop_exit:
pop s0
pop ra
jr ra
# -----------------------------------------------
# void print_array_item(int i)
print_array_item:
push ra
# print_int(array[i])
la t0, array
mul a0, a0, 4
add t0, t0, a0
lw a0, (t0)
jal print_int
pop ra
jr ra
# Solution to lab 3
.data
.eqv ARRAY_LENGTH 10
array: .word 100 200 300 400 500 600 700 800 900 1000
.text
# -----------------------------------------------
.globl main
main:
###################################################################
# SOLUTION 1: should use a0, not a1.
# print_int(45)
li a0, 45
jal print_int
###################################################################
# SOLUTION 2: the value needs to be copied from v0 to a0.
# print_int(read_int())
jal read_int
move a0, v0
jal print_int
###################################################################
# SOLUTION 3: read_int_plus_one needs to push/pop ra.
# read_int_plus_one()
jal read_int_plus_one
###################################################################
# SOLUTION 4: 'a' registers do not keep their values across a jal.
# print_int_plus_one(4)
# print_int_plus_one(4)
li a0, 4
jal print_int_plus_one
li a0, 4 # MUST reload a0 with the value we want to print.
jal print_int_plus_one
###################################################################
# SOLUTION 5: change_array forgot to add the address to the index.
# change_array()
jal change_array
###################################################################
# SOLUTION 6: print_array used a 't' register as its loop counter.
# print_array()
jal print_array
###################################################################
# exit()
li v0, 10
syscall
# -----------------------------------------------
# void print_int(int x)
print_int:
li v0, 1
syscall
li a0, '\n'
li v0, 11
syscall
jr ra
# -----------------------------------------------
# int read_int()
read_int:
li v0, 5
syscall
jr ra
# -----------------------------------------------
# int read_int_plus_one()
read_int_plus_one:
push ra # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< oops, forgot this
jal read_int
add v0, v0, 1
pop ra # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< oops, forgot this
jr ra
# -----------------------------------------------
# int print_int_plus_one()
print_int_plus_one:
push ra
add a0, a0, 1
jal print_int
pop ra
jr ra
# -----------------------------------------------
# void change_array()
change_array:
# i = t0
# for(i = 0; i < ARRAY_LENGTH; i++) {
li t0, 0
_change_array_loop:
bge t0, ARRAY_LENGTH, _change_array_loop_exit
# array[i]++
la t1, array # array + (i * 4)
mul t2, t0, 4
add t2, t2, t1 # <<<<<<<<<<<<<<<<<<<< oops, forgot this
lw t1, (t2)
add t1, t1, 1
sw t1, (t2)
# }
add t0, t0, 1
j _change_array_loop
_change_array_loop_exit:
jr ra
# -----------------------------------------------
# void print_array()
print_array:
push ra
push s0 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< must do this!
# i = s0 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< we're using s0 for i instead.
# for(i = 0; i < ARRAY_LENGTH; i++) {
li s0, 0
_print_array_loop:
bge s0, ARRAY_LENGTH, _print_array_loop_exit
# print_array_item(i)
move a0, s0
jal print_array_item
# }
add s0, s0, 1
j _print_array_loop
_print_array_loop_exit:
pop s0 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< must do this!
pop ra
jr ra
# -----------------------------------------------
# void print_array_item(int i)
print_array_item:
push ra
# print_int(array[i])
la t0, array
mul a0, a0, 4
add t0, t0, a0
lw a0, (t0)
jal print_int
pop ra
jr ra
\ No newline at end of file
# Siyu Zhang (siz24)
.data
.eqv ARRAY_LENGTH 10
array: .word 100 200 300 400 500 600 700 800 900 1000
user_input: .asciiz "**** user input: "
.text
# -----------------------------------------------
.globl main
main:
###################################################################
# PROBLEM 1: this prints 0, not 45. --> solved
# should use a0 as the first arguement, not a1
# print_int(45)
li a0, 45
jal print_int
###################################################################
# PROBLEM 2: this doesn't print what the user typed. --> solved
# a0 is not the user's input, but in v0, so move v0, a0
# print_int(read_int())
jal read_int
move a0, v0
jal print_int
###################################################################
# PROBLEM 3: this function goes into an infinite loop. --> solved
# call another function in plus_one, need to push and pop ra
# read_int_plus_one()
jal read_int_plus_one
###################################################################
# PROBLEM 4: this doesn't print 5 twice, like I expected. -->solved
# when print_int_plus_one called print_int, a0 changed to '\n' which is 10 in register
# print_int_plus_one(4)
# print_int_plus_one(4)
li a0, 4
jal print_int_plus_one
li a0, 4
jal print_int_plus_one # a0 already has 4 in it, right?
###################################################################
# PROBLEM 5: this function crashes. --> solved
# use s0 as a counter (need push and pop s0) to save, and use t0, t1 to find the corret slot
# change_array()
jal change_array
###################################################################
# PROBLEM 6: this function does not print 10 times. --> solved
# # use s0 as a counter (need push and pop s0) to save
# print_array()
jal print_array
###################################################################
# exit()
li v0, 10
syscall
# -----------------------------------------------
# void print_int(int x)
print_int:
li v0, 1
syscall
li a0, '\n'
li v0, 11
syscall
jr ra
# -----------------------------------------------
# int read_int()
read_int:
la a0, user_input
li v0, 4
syscall
li v0, 5
syscall
jr ra
# -----------------------------------------------
# int read_int_plus_one()
read_int_plus_one:
push ra
jal read_int
add v0, v0, 1
pop ra
jr ra
# -----------------------------------------------
# int print_int_plus_one()
print_int_plus_one:
push ra
add a0, a0, 1
jal print_int
pop ra
jr ra
# -----------------------------------------------
# void change_array()
change_array:
# i = t0
# for(i = 0; i < ARRAY_LENGTH; i++) {
li t0, 0
_change_array_loop:
bge t0, ARRAY_LENGTH, _change_array_loop_exit #i >= ARRAY_LENGTH, exit
# array[i]++
la t1, array # array + (i * 4)