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

Your proof-of-study task is to write a program that computes and prints out the

ID: 3746674 • Letter: Y

Question

Your proof-of-study task is to 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.

(Clever students will write small pieces of this code and test them as they go. Students who like spending many extra hours working on schoolwork will attempt to write the whole thing before testing.)

Example output for your program is as follows. Note: My output is nicely formatted. Your output does not need to be formatted like this.

Your code must meet the following requirements. The structure given below is not optional - you must use it.

The only pseudoinstructions you may use are load immediate (li) and load address (la). Do not use any other pseudoinstructions. Be very careful to not use pseudoinstructions for loading or storing to memory.

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. The functions will help you keep your code simple, so keep it simple.

Your code must assemble, run correctly, and be professionally presented. (Indent uniformly and use comments to document and describe the operation of your code.)

When you are done, submit your .asm file.

Extra credit:

Before starting the extra credit, make sure you have a functioning solution. Back it up so you have something you can submit if you don't finish the extra credit. Never copy or share ideas, strategies, or code for extra credit solutions (instant failing grade for the class).

This extra credit, if done entirely correctly, will earn up to 10 points on midterm #1 (but your midterm total cannot exceed 100 points). These points will be applied at the end of the semester only after grading scales are set and letter grades have been initially assigned. Extra credit will not help students who fail the class for failing all the exams.

Students wanting to earn extra credit should make the following changes to their code before submitting:

Ask the user how many lines of Pascal's Triangle they would like displayed. Require non-negative input from the user before printing the triangle, repeat asking if needed. Display the requested number of rows of the triangle.

Instead of creating an array in the .data section, your program should create, initialize, and keep track of the needed memory in the heap as your code executes. (You'll need to figure out how to do this on your own.)

When computing a row, if at any point there is integer overflow, print out a nice message and terminate the program. Don't start printing any rows that would have incorrect integers in them (but still print the preceding rows).

Format the output in a more reasonable manner (appropriate for the number of rows that are printed, and appropriate for Pascal's triangle). The exact details of this are up to you.

Adhere to the other requirements in the assignments (avoid pseudoinstructions, comments, proper use of functions, etc.).

When you're done, replace your original submission with your extra credit submission (same filename).

Explanation / Answer

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