Instructions
Objective
Write a MIPS assignment program in assembly language to calculate the area of shape and find average, largest and smallest in MIPS assembly language.
Requirements and Specifications
Write a MIPS assembly language program to calculate the area of each trapezoid in a set of trapezoids. The sides should be read from wordsized aSides , cSides, and heights arrays. The result must be stored into the word-sized tAreas array. Below is the formula to calculate the area of a trapezoid:
tAreas[ n] = (heights[ n] × (aSides [n] +2 cSides[ n]) )
After all the trapezoid areas have been calculated, the program should find the minimum, middle value, maximum, sum, and average for the trapezoid areas array.
The program must display the results to the console window. The output should look something like the following (with the correct answers displayed):
Screenshots of output
Source Code
###########################################################################
# Name:
# NSHE ID:
# Section:
# Assignment: MIPS #1
# Description:
# CS 218, MIPS Assignment #1
# Template
###########################################################################
# data segment
.data
aSides: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.word 15, 25, 33, 44, 58, 69, 72, 86, 99, 101
.word 107, 121, 137, 141, 157, 167, 177, 181, 191, 199
.word 202, 209, 215, 219, 223, 225, 231, 242, 244, 249
.word 251, 253, 266, 269, 271, 272, 280, 288, 291, 299
.word 369, 374, 377, 379, 382, 384, 386, 388, 392, 393
.word 1469, 2474, 3477, 4479, 5482, 5484, 6486, 7788, 8492, 1493
cSides: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.word 32, 51, 76, 87, 90, 100, 111, 123, 132, 145
.word 206, 212, 222, 231, 246, 250, 254, 278, 288, 292
.word 332, 351, 376, 387, 390, 400, 411, 423, 432, 445
.word 457, 487, 499, 501, 523, 524, 525, 526, 575, 594
.word 634, 652, 674, 686, 697, 704, 716, 720, 736, 753
.word 1782, 2795, 3807, 3812, 4827, 5847, 6867, 7879, 7888, 1894
heights:
.word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.word 102, 113, 122, 139, 144, 151, 161, 178, 186, 197
.word 203, 215, 221, 239, 248, 259, 262, 274, 280, 291
.word 400, 404, 406, 407, 424, 425, 426, 429, 448, 492
.word 501, 513, 524, 536, 540, 556, 575, 587, 590, 596
.word 782, 795, 807, 812, 827, 847, 867, 879, 888, 894
.word 1912, 2925, 3927, 4932, 5447, 5957, 6967, 7979, 7988, 1994
tAreas: .space 280
len: .word 70
taMin: .word 0
taMid: .word 0
taMax: .word 0
taSum: .word 0
taAve: .word 0
LN_CNTR = 8
# -----
hdr: .ascii "MIPS Assignment #1 \n"
.ascii "Program to calculate area of each trapezoid in a series "
.ascii "of trapezoids. \n"
.ascii "Also finds min, mid, max, sum, and average for the "
.asciiz "trapezoid areas. \n\n"
new_ln: .asciiz "\n"
blnks: .asciiz " "
a1_st: .asciiz "\nTrapezoid min = "
a2_st: .asciiz "\nTrapezoid med = "
a3_st: .asciiz "\nTrapezoid max = "
a4_st: .asciiz "\nTrapezoid sum = "
a5_st: .asciiz "\nTrapezoid ave = "
###########################################################
# text/code segment
.text
.globl main
.ent main
main:
# -----
# Display header.
la $a0, hdr
li $v0, 4
syscall # print header
# --------------------------------------------------------
la $t0, aSides # load address of a sides
la $t1, cSides # load address of c sides
la $t2, heights # load address of heights
la $t3, tAreas # load address of areas
la $t4, len # load address of length
lw $t4, 0($t4) # load length value
# calculate trapezoid areas
calc_loop:
lw $t5, 0($t0) # load a side
lw $t6, 0($t1) # load c side
add $t5, $t5, $t6 # a + c
srl $t5, $t5, 1 # divide by 2 using a shift
lw $t6, 0($t2) # load height
mul $t5, $t5, $t6 # multiply height + (a*c/2)
sw $t5, 0($t3) # save result in areas
addi $t0, $t0, 4 # advance position in a sides
addi $t1, $t1, 4 # advance position in c sides
addi $t2, $t2, 4 # advance position in heights
addi $t3, $t3, 4 # advance position in areas
addi $t4, $t4, -1 # decrement number of remaining areas
bnez $t4, calc_loop # repeat while not zero
# calculate minimum, maximum and sum
la $t0, tAreas # load address of areas
lw $t1, 0($t0) # load first value as minimum
lw $t2, 0($t0) # load first value as maximum
li $t3, 0 # start with sum in zero
la $t4, len # load address of length
lw $t4, 0($t4) # load length value
minmax_loop:
lw $t5, 0($t0) # load area
add $t3, $t3, $t5 # add area to sum
bge $t5, $t1, ifmax # if area >= min, test max
move $t1, $t5 # else, area is new minimum
j skip # jump to next element
ifmax:
ble $t5, $t2, skip # if area <= max, skip
move $t2, $t5 # else, area is new maximum
skip:
addi $t0, $t0, 4 # advance position in areas
addi $t4, $t4, -1 # decrement number of remaining areas
bnez $t4, minmax_loop # repeat while not zero
la $t0, taMin # load address of min variable
sw $t1, 0($t0) # save minimum in variable
la $t0, taMax # load address of max variable
sw $t2, 0($t0) # save maximum in variable
la $t0, taSum # load address of sum variable
sw $t3, 0($t0) # save sum in variable
# calculate average
la $t4, len # load address of length
lw $t4, 0($t4) # load length value
div $t3, $t3, $t4 # divide sum by length to get average
la $t0, taAve # load address of average variable
sw $t3, 0($t0) # save average in variable
# calculate middle value
la $t0, tAreas # load address of areas
srl $t4, $t4, 1 # divide length by 2
sll $t4, $t4, 2 # multiply value by 4 to get offset in array
add $t0, $t0, $t4 # add to address of array to get middle position
lw $t0, 0($t0) # load the middle value
la $t1, taMid # load address of mid variable
sw $t0, 0($t1) # save middle value
# Display area array
la $t0, tAreas # load address of areas
la $t1, len # load address of length
lw $t1, 0($t1) # load length value
li $t2, 0 # number of values in line, start in 0
display_loop:
la $a0, blnks # load blanks string address
li $v0, 4 # syscall to print a string
syscall # print separating blanks
lw $a0, 0($t0) # load area
li $v0, 1 # sycall to print a number
syscall # print the number
addi $t2, $t2, 1 # increment number of values in the line
blt $t2, LN_CNTR, display_next # if num < max, go to next
li $t2, 0 # else, restart counter to zero
la $a0, new_ln # load newline string address
li $v0, 4 # syscall to print a string
syscall # print a newline
display_next:
addi $t0, $t0, 4 # advance position in areas
addi $t1, $t1, -1 # decrement number of remaining areas
bnez $t1, display_loop # repeat while not zero
# --------------------------------------------------------
# Display results.
la $a0, new_ln # print a newline
li $v0, 4
syscall
la $a0, new_ln # print a newline
li $v0, 4
syscall
# Print min message followed by result.
la $a0, a1_st
li $v0, 4
syscall # print "min = "
lw $a0, taMin
li $v0, 1
syscall # print min
# -----
# Print middle message followed by result.
la $a0, a2_st
li $v0, 4
syscall # print "med = "
lw $a0, taMid
li $v0, 1
syscall # print mid
# -----
# Print max message followed by result.
la $a0, a3_st
li $v0, 4
syscall # print "max = "
lw $a0, taMax
li $v0, 1
syscall # print max
# -----
# Print sum message followed by result.
la $a0, a4_st
li $v0, 4
syscall # print "sum = "
lw $a0, taSum
li $v0, 1
syscall # print sum
# -----
# Print average message followed by result.
la $a0, a5_st
li $v0, 4
syscall # print "ave = "
lw $a0, taAve
li $v0, 1
syscall # print average
# -----
# Done, terminate program.
endit:
la $a0, new_ln # print a newline
li $v0, 4
syscall
li $v0, 10
syscall # all done!
.end main
Similar Samples
Discover expertly crafted programming homework samples on ProgrammingHomeworkHelp.com. Each example showcases detailed solutions to common coding challenges, illustrating best practices and efficient techniques. Elevate your coding skills by exploring our comprehensive examples, designed to guide you through complex programming concepts with ease.
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language