The purpose of the program is to read in a list of 10 integers and then print th
ID: 3801477 • Letter: T
Question
The purpose of the program is to read in a list of 10 integers and then print them out.
Sample input and output. Note there is no comma after the last number.
Enter a number 1
Enter a number 2
Enter a number 9
Enter a number 11
Enter a number 99
Enter a number 12
Enter a number 24
Enter a number 90
Enter a number 17
Enter a number 82
1, 2, 9, 11, 99, 12, 24, 90, 7, 82
Re-write program without pseudo instructions:
******************************************************
.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
li $t3,10
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:
li $v0,4 # system call code for printing string = 4
bne $t3,10,L1
la $a0, prompt # load address of string to be printed into $a0
syscall
jr $ra
L1:
li $v0,4
la $a0,comma
syscall
j print
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
move $t2,$t1
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 $t3,$t3, -1
li $v0, 1
lw $a0,0($t2)
syscall
addi $t2, $t2, 4
bne $t3,$0,printstr
bne $t3,$0,print
jr $ra
Explanation / Answer
a,b,c,d,e,,g,h,i,j=1,2,,11,99,12,24,90,17,82
a = input('Enter a value: ')
b = input('Enter a value: ')
c = input('Enter a value: ')
d = input('Enter a value: ')
e = input('Enter a value: ')
f = input('Enter a value: ')
g = input('Enter a value: ')
h = input('Enter a value: ')
i = input('Enter a value: ')
j = input('Enter a value: ')
list1 = [a, b, c, d, e, f, g, h, i, j]
list2 = []
list3 = (a+b+c+d+e+f+g+h+i+j)
if (list3 % 2 == 0):
if (a % 2 == 0):
print('All declared variables have even values')
else:
for odd in list1:
if (odd % 2 == 1):
list2.append(odd)
odd = str(max(list2))
print ('The largest ODD value is ' + odd)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.