#####################################################################
#       Title:   CSCI 221 - Print the contents of an array of ints
#       Author:  Bary W Pollack
#       File:    PArray.asm
#       Date:    Mar. 29, 2008 - 1800
#####################################################################
#       Register - Variable Cross Reference
#                a0: address of the array
#                a1: length of the array
#####################################################################

        .data
titl:   .ascii      "\n  **** Print Array Program ****\n\n"
        .ascii      "  Author: Bary W Pollack\n"
        .ascii      "  File:   PArray.asm\n"
        .asciiz     "  Date:   Mar. 29, 2008\n"
prmt:   .asciiz     "\n\n  Print the integers up to N = "
bye:    .asciiz     "\n  **** Fin ****\n"

arr:    .word       3, 1, 4, 1, 5, 9, 2, 6, 5     # the array
n:      .word       9                             # length of the array

#####################################################################

        .globl    main

        .text
main:
        li  $v0, 4            # code for Print String
        la  $a0, titl         # load address of titl into $a0
        syscall               # print the title message

        la  $a0, arr          # load address of array into $a0
        lw  $a1, n            # load length of array into $a1
        jal parray            # call the print array routine

        li  $v0, 4            # code for Print String
        la  $a0, bye          # load address of "bye" message into $a0
        syscall               # print the string

        li  $v0, 10           # terminate program and
        syscall               # return control to the system

#####################################################################
#       PArray - print an array
#####################################################################
#       Register - Variable Cross Reference
#                a0: address of the array (input arg)
#                a1: length of the array, N (input arg)
#                t0: copy of address of the array
#                t1: counter: 0..(N-1)
#                t2: saved return address
#####################################################################

        .data
ptt1:   .asciiz   "\n  This array contains "
ptt2:   .asciiz   " elements\n"
nl:     .asciiz   "\n"

        .text
parray:                       # beginning of the print array routine
        move $t0, $a0         # save address of array in $t0
        move $t2, $ra         # save return address in $t2
        li  $v0, 4            # code for Print String
        la  $a0, ptt1         # load address of ptt1 into $a0
        syscall

        li  $v0, 1            # code for Print Integer
        move $a0, $a1         # copy length of array into $a0
        syscall

        li  $v0, 4            # code for Print String
        la  $a0, ptt2         # load address of ptt2 into $a0
        syscall

        li  $t1, 0            # load $t1 with 0 (counter)

loop:
        jal psp2              # print two spaces

        li  $v0, 1            # code for Print Integer
        move $a0, $t1         # copy counter to $a0
        syscall

        jal psp2              # print two spaces

        li  $v0, 1            # code for Print Integer
        lw  $a0, 0($t0)       # load next element of the array
        syscall

        li  $v0, 4            # code for Print String
        la  $a0, nl           # load address of nl into $a0
        syscall

        addi $t0, $t0, 4      # move to next array element
        addi $t1, $t1, 1      # increment counter
        blt $t1, $a1, loop    # jump to loop while counter < N

        jr  $t2               # return to caller

#####################################################################
#       psp2 just prints two spaces
#####################################################################

        .data
sp2:    .asciiz "  "          # two spaces

        .text
psp2:
        li  $v0, 4            # code for Print String
        la  $a0, sp2          # address of two spaces
        syscall
        jr  $ra               # return to caller

        .end

#####################################################################
