Monday, May 18, 2020

Volume and surface area of a rectangular parallelepiped

This is a sample MIPS program that computes the volume and surface area of  a rectangular parallelepiped. Its sides are defined by the A, B and C variables the values of which can be edited to carry out further tests. The code is fully documented.

Launch EzMIPSthe MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ---------------------------------------------------------------- #

# Formulas used:
# Volume = A * B * C
# Surface Area = 2 * ( A * B + A * C + B * C)

# ...................... Data declarations ....................... #

.data

# A, B, and C sides are initialized to arbitrary integer values, edit as you wish
A: .word 4
B: .word 5
C: .word 6

V: .word 0    # Volume
S: .word 0    # Surface Area

szVolume: .asciiz "The volume is: "
szArea:   .asciiz "\nThe surface area is: "

# ....................... Text/code section ...................... #

.text

main:

    # ............................................................ #

    # Load variables into registers
    lw $t0, A
    lw $t1, B
    lw $t2, C
    
    # ............................................................ #

    # Find Volume of the rectangular parallelpiped
    # Volume = A * B * C
    
    mul $t3, $t0, $t1
    mul $t4, $t3, $t2
    
    sw $t4, V                   # store the Volume
    
    # ............................................................ #

    # Find Surface Area of the rectangular parallelepiped
    # Surface Area = 2*(A*B+A*C+B*C)

    mul $t3, $t0, $t1 # A * B
    mul $t4, $t0, $t2 # A * C
    mul $t5, $t1, $t2 # B * C
    add $t6, $t3, $t4
    add $t7, $t6, $t5

    sll $t7, $t7, 1             # equivalent to $t7 = $t7 * 2

    sw $t7, S                   # store the Surface Area
    
    # ............................................................ #

    la $a0, szVolume            # "The volume is: "
    li $v0, 4                   # print string code
    syscall                     # print it!

    la $a0, V                   # load address of V into $a0
    lw $a0, 0($a0)              # put value of V into $a0
    li $v0, 1                   # print integer code
    syscall                     # print it!

    la $a0, szArea              # "\nThe surface area is: "
    li $v0, 4                   # print string code
    syscall                     # print it!

    la $a0, S                   # load address of S into $a0
    lw $a0, 0($a0)              # put value of S into $a0
    li $v0, 1                   # print integer code
    syscall                     # print it!

    # ............................................................ #

    # Done, terminate program
    
    li $v0, 10                  # call code for terminate
    syscall                     # system call (terminate)

# ---------------------------------------------------------------- #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment