Assignment 4
A Python program with functions and arrays
The following Python program utlizes a function that doubles every item in a list. It is spread across four different functions.
#test out double_items on two different lists
def main():
list1 = [54,26,93,17,77,31,44,55,20]
length1 = 9
list2 = [9,8,7,6,5]
length2 = 5
double_items(list1,length1)
double_items(list2,length2)
print_list(list1,length1)
print()
print_list(list2,length2)
return
#double a single value
def double_value(val):
result = val+val
return result
#double all items in a list
def double_items(a_list, list_length):
i = 0
while i < list_length:
a_list[i] = double_value(a_list[i])
i = i + 1
return
#print a list with one item per line
def print_list(a_list,list_length):
i = 0
while i < list_length:
print(a_list[i])
i = i + 1
return
main()
Here’s the output you get when you run it in the console:
108
52
186
34
154
62
88
110
40
18
16
14
12
10
A start to translating the Python program into MIPS
Here’s an attempt to translate the program into MIPS. The main
, double_value
, and print_list
procedures are finished, but you will need to finish implementing the double_items
procedure.
.data
list1: 54,26,93,17,77,31,44,55,20
length1: 9
list2: 9,8,7,6,5
length2: 5
newline: .asciiz "\n"
.text
###############BEGIN main###############
main:
la $a0, list1
lw $a1, length1
jal double_items
la $a0, list2
lw $a1, length2
jal double_items
la $a0, list1
lw $a1, length1
jal print_list
li $v0, 4 #code for printing a string
la $a0, newline
syscall
la $a0, list2
lw $a1, length2
jal print_list
j exit_program
###############END main###############
###############BEGIN double_value###############
double_value:
add $v0, $a0, $a0
jr $ra
###############END double_value###############
###############BEGIN double_items###############
#this doesn't do anything yet, it just
#jumps back to the calling procedure
#you must implement this function to work
#like the double_items function in the
#Python program
double_items:
jr $ra
###############END double_items###############
###############BEGIN print_list###############
print_list:
move $t0, $a0 #$a0 is the list address
move $t1, $a1 #$a1 is the list length
li $t2, 0 #loop counter i
print_list_loop:
bge $t2, $t1, print_list_end #exit loop if i >= list length
sll $t4, $t2, 2 #multiply loop counter by 4 to get memory offset
add $t3, $t0, $t4 #add base address of list and memory offset
lw $a0, 0($t3) #load a_list[i]
li $v0, 1 #code for printing an integer
syscall
li $v0, 4 #code for printing a string
la $a0, newline
syscall
addi $t2, $t2, 1 #increment loop counter
b print_list_loop
print_list_end:
jr $ra
###############END print_list###############
exit_program:
Assignment
Finish translating the double_items
function into a MIPS procedure.
The translation should do the following (in order of priority):
- allow the function to behave just the same way as the Python program
- call the
double_value
procedure from inside thedouble_items
procedure - inside
double_items
, use the stack (with$sp
) to save$ra
and to make sure you restore any$s
registers that you use to their values that they had before the procedure started
Grading
This is an 8-point assignment. See the rubric from the syllabus, with the following exception:
The full 8 points will be for correct behavior with proper use of the stack and a call to double_value
from within double_items
. You can get 7 points if it behaves correctly and you have utilized at least one of double_value
and the stack. You can get 6 points for something that behaves correctly even if it uses neither of double_value
nor the stack. You will get 5 points for significant progress towards the solution, even if it produces incorrect results.
What to turn in
Turn in the following items
- Your
.asm
file which contains your MIPS program - A
.txt
file (you can use Notepad, TextEdit, or even Visual Studio Code) where you paste in a sample run of your program that shows how it worked when you ran it in MARS
Where to turn it in
Submit your files to the Assignment 4 hand-in form on Blackboard.