1	#####################################################################
     2	#       Title:   CSCI 221 - Print the contents of an array of ints
     3	#       Author:  Bary W Pollack
     4	#       File:    PArray.asm
     5	#       Date:    Mar. 29, 2008 - 1800
     6	#####################################################################
     7	#       Register - Variable Cross Reference
     8	#                a0: address of the array
     9	#                a1: length of the array
    10	#####################################################################

    11	        .data
    12	titl:   .ascii      "\n  **** Print Array Program ****\n\n"
    13	        .ascii      "  Author: Bary W Pollack\n"
    14	        .ascii      "  File:   PArray.asm\n"
    15	        .asciiz     "  Date:   Mar. 29, 2008\n"
    16	prmt:   .asciiz     "\n\n  Print the integers up to N = "
    17	bye:    .asciiz     "\n  **** Fin ****\n"

    18	arr:    .word       3, 1, 4, 1, 5, 9, 2, 6, 5     # the array
    19	n:      .word       9                             # length of the array

    20	#####################################################################

    21	        .globl    main

    22	        .text
    23	main:
    24	        li  $v0, 4            # code for Print String
    25	        la  $a0, titl         # load address of titl into $a0
    26	        syscall               # print the title message

    27	        la  $a0, arr          # load address of array into $a0
    28	        lw  $a1, n            # load length of array into $a1
    29	        jal parray            # call the print array routine

    30	        li  $v0, 4            # code for Print String
    31	        la  $a0, bye          # load address of "bye" message into $a0
    32	        syscall               # print the string

    33	        li  $v0, 10           # terminate program and
    34	        syscall               # return control to the system

    35	#####################################################################
    36	#       PArray - print an array
    37	#####################################################################
    38	#       Register - Variable Cross Reference
    39	#                a0: address of the array (input arg)
    40	#                a1: length of the array, N (input arg)
    41	#                t0: copy of address of the array
    42	#                t1: counter: 0..(N-1)
    43	#                t2: saved return address
    44	#####################################################################

    45	        .data
    46	ptt1:   .asciiz   "\n  This array contains "
    47	ptt2:   .asciiz   " elements\n"
    48	nl:     .asciiz   "\n"

    49	        .text
    50	parray:                       # beginning of the print array routine
    51	        move $t0, $a0         # save address of array in $t0
    52	        move $t2, $ra         # save return address in $t2
    53	        li  $v0, 4            # code for Print String
    54	        la  $a0, ptt1         # load address of ptt1 into $a0
    55	        syscall

    56	        li  $v0, 1            # code for Print Integer
    57	        move $a0, $a1         # copy length of array into $a0
    58	        syscall

    59	        li  $v0, 4            # code for Print String
    60	        la  $a0, ptt2         # load address of ptt2 into $a0
    61	        syscall

    62	        li  $t1, 0            # load $t1 with 0 (counter)

    63	loop:
    64	        jal psp2              # print two spaces

    65	        li  $v0, 1            # code for Print Integer
    66	        move $a0, $t1         # copy counter to $a0
    67	        syscall

    68	        jal psp2              # print two spaces

    69	        li  $v0, 1            # code for Print Integer
    70	        lw  $a0, 0($t0)       # load next element of the array
    71	        syscall

    72	        li  $v0, 4            # code for Print String
    73	        la  $a0, nl           # load address of nl into $a0
    74	        syscall

    75	        addi $t0, $t0, 4      # move to next array element
    76	        addi $t1, $t1, 1      # increment counter
    77	        blt $t1, $a1, loop    # jump to loop while counter < N

    78	        jr  $t2               # return to caller

    79	#####################################################################
    80	#       psp2 just prints two spaces
    81	#####################################################################

    82	        .data
    83	sp2:    .asciiz "  "          # two spaces

    84	        .text
    85	psp2:
    86	        li  $v0, 4            # code for Print String
    87	        la  $a0, sp2          # address of two spaces
    88	        syscall
    89	        jr  $ra               # return to caller

    90	        .end

    91	#####################################################################