Using MIPS assembly code: write a program that computes and prints out the first
ID: 3746013 • Letter: U
Question
Using MIPS assembly code: write a program that computes and prints out the first eleven rows of Pascal's Triangle. Your code must follow the outline and requirements given below. In your .data segment, store the following 'array' of eleven integers in memory: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}. Use a single appropriate label to mark the beginning of these integers in memory. In your .text segment, use a "main:" label to mark the start of your program, and use the syscall to properly terminate execution when main ends. Somewhere after the end of your main code, write a function that loops and prints out only the non-zero integers in the array on one console line of output. Somewhere after the end of your main code, write another function that loops through the first 10 integers in the array in forward order. Replace each integer with the sum of itself and the integer that follows it in memory. In code, Data[i] = Data[i] + Data[i+1]. Do this for i = [0..9]. This will properly compute the next row of Pascal's Triangle. In the main part of your code, use these two functions and a loop to compute and print out Pascal's Triangle.
I'm mostly confused about the part where it says "write a function that loops through the first 10 integers in the array in forward order. Replace each integer with the sum of itself". The other layout and printing are pretty basic. Thanks for the help! Below is some of the stuff I've already worked on.
.data
title: .asciiz " Pascal's Triangle "
prompt2: .asciiz " Thank you. "
myArray: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
arraySize: .word 11
.text
main:
# Initialize variables
la $s0, myArray
la $s1, 0($s0)
la $s2, 4($s0)
lw $s7, arraySize
lw $s6, arraySizeMinusOne
addi $t2, $zero, 0
# begin main functions
jal printTitle
jal computePascal
jal printArray
la $a0, prompt2
syscall
j exit
printTitle:
la $a0, title
li $v0, 4
syscall
j done
Explanation / Answer
Screenshot
---------------------------------------------------------------
Program
#Declaration section
.data
title: .asciiz " Pascal's Triangle "
prompt2: .asciiz " Thank you. "
myArray: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
arraySize: .word 11
arraySizeMinusOne: .word 10
nextLine: .asciiz " "
space: .asciiz " "
#Main program
.text
main:
# Initialize variables
la $s0, myArray
lw $s7, arraySize
lw $s6, arraySizeMinusOne
addi $t2, $zero, 0
# begin main functions
jal printTitle #print title
done:
beq $t2,$s7,end #print until array size loop
jal printArray #call print array function
jal computePascal #call compute pascal function
la $a0,nextLine #Print next row
li $v0,4
syscall
addi $t2,$t2,1 #Increment counter
j done
#Print thank yoy
end:
la $a0, prompt2
li $v0,4
syscall
j exit
#End of the program
exit:
li $v0,10
syscall
#Title printing function
printTitle:
la $a0, title
li $v0, 4
syscall
j done
#Pascal calculation
computePascal:
#set variables
li $t1,0
la $t0,myArray
#Loop until array index 9
looping:
beq $t1,$s6,ret
#Get first value
lw $t2,0($t0)
#Get second value
lw $t3,4($t0)
#add
add $t2,$t2,$t3
#store in first index
sw $t2,0($t0)
#Increment counters
addi $t1,$t1,1
addi $t0,$t0,4
j looping
#end of the function
ret:
jr $ra
#Print array values
printArray:
#Initialize
la $t0,myArray
li $t1,0
#Loop for print array
loop:
beq $t1,11,return
#get value
lw $a0,($t0)
#Check !=0
ble $a0,0,next
#print
li $v0,1
syscall
#Put space between numbers
la $a0,space
li $v0,4
syscall
#Increment counters
addi $t0,$t0,4
addi $t1,$t1,1
j loop
#Increment counters
next:
addi $t0,$t0,4
addi $t1,$t1,1
j loop
#End of the function
return:
jr $ra
------------------------------------
Output
Pascal's Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Thank you.
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.