Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finish the code in the \"printstr\" and \"print\" functions of this code. So far

ID: 3876959 • Letter: F

Question

Finish the code in the "printstr" and "print" functions of this code. So far the code stores ten integers, it needs to prints them out upon completion. "main" and "getdata" functions are complete and don't need to be touched. Only the print functions. INSTRUCTIONS: 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. NO PSUEDO CODE ALLOWED, ex. Li, La basically, complete these two sections to make the code print the ten integers. data: .space 40 # set aside 40 bytes for 10 integers prompt: .asciiz "Enter a number" # address 0 times 10010028 comma: .asciiz "," # address 0 times 10010038 .globl main .text main: lui $a0, 0 times 1001 # get start of data segment jal getdata # call getdata function lui $a0, 0 times 1001 # 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 jr $ra getdata: addi $sp, $sp, -4 # allocate space on stack sw $ra, 0($sp) # save ?ra ori $t1, $a0, 0 # address of array lui $t1, 0 times 1001 #start of data ori $a0, $a0, 0 times 0028 # 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: 1w $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: jr $ra

Explanation / Answer

Given below is the completed code and also output. Hope it helps. If it does, please don't forget to rate the answer. Thank you very much.


.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:
ori $v0, $0, 4
syscall
jr $ra
  
  
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
  
# print
# parameter: $a0 holds address of list in memory
# purpose: print the list separated by commas
print:
addi $sp, $sp, -4 # allocate space on stack
sw $ra, 0($sp) # save $ra

ori $t1, $a0, 0 # address of array
ori $a0, $a0, 0x0038 # address of commal
addi $t2, $a0, 0 #save the address of comma in $t2
ori $t0, $0, 9 # counter
  
#display 1st int
lw $a0, 0($t1) #load int from array
ori $v0, $0, 1 #command to print int
syscall

addi $t1, $t1, 4 #address of 2nd int
#display the other 9 numbers
loop:   
beq $t0, $0, print_ret # while not 0
addi $a0, $t2, 0 #store address of comma
jal printstr # call function to print comma
lw $a0, 0($t1) #load int from array
ori $v0, $0, 1 #command to print int
syscall
addi $t1, $t1, 4 # increment to next location
  
addi $t0, $t0, -1 # decrement counter
j loop # repeat


print_ret:
lw $ra, 0($sp) # restore return address
addi $sp, $sp, 4 # clean up stack
jr $ra # return from call

output

Enter a number 2
Enter a number 4
Enter a number 6
Enter a number 8
Enter a number 10
Enter a number 12
Enter a number 14
Enter a number 16
Enter a number 18
Enter a number 20
2, 4, 6, 8, 10, 12, 14, 16, 18, 20

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote