The file assign2.asm holds a partial program. The purpose of the program is to r
ID: 3887612 • Letter: T
Question
The file assign2.asm holds a partial program. The purpose of the program is to read in a list of 10 integers and then print them out. There are 4 functions in the program; 2 of which you are to complete. Main: This function will call getdata and print. Main is complete and should not be changed. Getdata: This function reads 10 integers and stores them into memory. There is one parameter ($a0) which holds the address of where the list of numbers is to be stored. Getdata calls the function printstr to print the prompt. Getdata is complete and should not be changed. Printstr: This function is to print a string. There is one parameter ($a0) which holds the address of the string to print. You are to complete this function so it performs as required. This function is call from the getdata function and the print function. Print: This function is to print the list of integers. The integers must all be on the same line of output and be separated by a comma and space (already in the data segment). See the example input and output below. This function must call the printstr function to print the comma string. Remember that as this function calls another, it must save and restore the $ra
Explanation / Answer
Code: (Explanation in comments / Modified code in bold)
# add your name and comments
.data
.space 40 # set aside 40 bytes for 10 integers
prompt: .asciiz "Enter a number " # address 0x10010028
comma: .asciiz ", " # address 0x10010038
.globl main
.text
main:
lui $a0, 0x1001 # get start of data segment
jal getdata # call getdata function
lui $a0, 0x1001 # get start of data segment
jal print # call print function
ori $v0, $0, 10 # set command to exit
syscall # end program
# printstr
# paramters: $a0 holds address of string to print
# purpose: print the string parameter
printstr:
#to be completed
li $v0, 4 # set command to print string
la $a0, prompt # load address of string
syscall # print string
#################
jr $ra # return from call
getdata:
addi $sp, $sp, -4 # allocate space on stack
sw $ra, 0($sp) # save $ra
ori $t1, $a0, 0 # address of array
lui $t1, 0x1001 #start of data
ori $a0, $a0, 0x0028 # address of prompt
ori $t0, $0, 10 # counter
top: beq $t0, $0, ret # while not 0
jal printstr # call function to print prompt
ori $v0, $0, 5 # set command to read integer
syscall # read int
sw $v0, 0($t1) # save int in memory
addi $t1, $t1, 4 # increment to next location
addi $t0, $t0, -1 # decrement counter
j top # repeat
ret: lw $ra, 0($sp) # restore return address
addi $sp, $sp, 4 # clean up stack
jr $ra # return from call
# parameter: $a0 holds address of list in memory
# purpose: print the list separated by commas
print:
#to be completed
li $t0, 1 # initialize count variable
li $v0, 1 # set command to print integer
ori $t1, $a0, 0 # address of array
lw $a0, 0($t1) # load word from array
syscall # print integer output
loop: # loop to print all values of array
addi $t0, $t0, 1 # decrement count variable
addi $t1, $t1, 4 # point to next element of array
li $v0, 4 # set command to print string
la $a0, comma # address of string
syscall # print comma and space
lw $a0, 0($t1) # load element of array
li $v0, 1 # set command to print integer
syscall # print integer output
bne $t0, 10, loop # iterate loop till all elements are printed
#########################
jr $ra # return from call
Output:
Enter a number 10
Enter a number 20
Enter a number 30
Enter a number 40
Enter a number 50
Enter a number 60
Enter a number 70
Enter a number 80
Enter a number 90
Enter a number 100
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
-- program is finished running --
Hope it is clear. Do upvote :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.