# CS 2734, Computer Organization II, Fall 2001
# MIPS program showing arrays and functions
# #include <stdio.h>
# #include <stdlib.h>
# void Write_array(int *, int);
# void main(void)
# {
#    int *A = (int *) malloc(40);
#    int *p = A;
#    int i;
#    int j = 10;
#    for (j = 10; j<= 100; j +=10)
#        *(p++) = j;
#    *(A + 3) = 47;
#    i = *(A + 5);
#    printf("Value in i: %i\n", i);
#    Write_array(A, 10);
# }
# void Write_array(int *A, int n)
# {
#    int j;
#    printf("Values in array A: ");
#    for (j = 0; j < n; j++)
#       printf("%i ", *(A + j));
#    printf("\n");
# }
# ten42% write_array2
# Value in i: 60
# Values in array A: 10 20 30 47 50 60 70 80 90 100
######################################################### 
        .globl main
main:   addu    $s7, $zero, $ra
        add     $sp, $sp, -40
        move    $t0, $sp	   # address of A is $sp 
        move    $t3, $t0           # pointer into array A
        addi    $t4, $0, 10        # initial value for A
Loop:   sw      $t4, 0($t3)
        addi    $t3, $t3, 4        # move pointer
        addi    $t4, $t4, 10       # update value
        slti    $t5, $t4, 100
        bne     $t5, $0, Loop
        addi    $t2, $zero, 47  
        sw      $t2, 12($t0)    
        lw      $t1, 20($t0)    
    ## Print value in $t1
        li      $v0, 4
        la      $a0, Regt1
        syscall
        li      $v0, 1
        move    $a0, $t1
        syscall
        jal     Newl
    ## Print the array
        li      $v0, 4
        la      $a0, Amess
        syscall
        move    $a0, $sp
        li      $a1, 10
        jal     Write_array
        jal     Newl
########## Finish main###################
        add     $sp, $sp, 40
        addu    $ra, $zero, $s7
        jr      $ra
########## End of main function #########

########## function definitions #########

########## write an array ###############
Write_array:
        addi    $sp, $sp, -4    # room for $ra on stack
        sw      $ra, 0($sp)     # save $ra because not leaf
    ## initialization for loop
        move    $s0, $a0        # $s0 = $a0 = start of A
        move    $s1, $a1        # $s1 = $a1 = N
        move    $t1, $zero      # start $t1 = 0, the index
LoopA:  beq     $s1, $t1, EndA  # if (N == index) goto EndA
    ## write value for A[i]
        addu    $t2, $t1, $t1
        addu    $t2, $t2, $t2
        addu    $t2, $s0, $t2   # $t2 = index*4 + start of A
        li      $v0, 1
        lw      $a0, 0($t2)     # integer to print
        syscall
    ## write a blank
        jal     Blan
        addi    $t1, $t1, 1
        j       LoopA
EndA:   lw      $ra, 0($sp)
        addi    $sp, $sp, 4
        jr      $ra

########## write newline ################
Newl:   li      $v0, 4
        la      $a0, Newline
        syscall
        jr      $ra

########## write blank ##################
Blan:   li      $v0, 4
        la      $a0, Blank
        syscall
        jr      $ra

        .data	
Blank:     .asciiz  " "
Newline:   .asciiz  "\n"
Regt1:     .asciiz "Value in Register $t1: "
Amess:     .asciiz "Values in array A: "
Thatsall:  .asciiz "Th-th-th-thats all folks!\n"


############### output #############################
# four06% spim -file write_array.s.s
# Value in Register $t1: 60
# Values in array A: 10 20 30 47 50 60 70 80 90 100 
####################################################