################################################################################
##
## LSU EE 4720 Fall 2017 Homework 1 -- SOLUTION
##
##
 ## Due Monday, 6 December 2017

.data
name:
        .asciiz ""  # Put your name between the quotes.


################################################################################
## Problem 1 -- Split Routine -- SOLUTION

        .text

split:
        ## Register Usage
        #
        # CALL VALUES:
        #  $a0: Number of elements in input array.
        #  $a1: Address of input array. Elements are 4 bytes each.
        #  $a2: Address of output array, elements are 4 byte each.
        #  $a3: Address of output array, elements are 2 byte each.
        #
        # RETURN:
        #  $v0, Number of elements in 4-byte output array.
        #
        # Note:
        #  Can modify $t0-$t9, $a0-$a3


        ## SOLUTION
        sll $t1, $a0, 2
        add $t9, $a1, $t1
        addi $v0, $a2, 0
LOOP:
        beq $a1, $t9, DONE
        lw $t0, 0($a1)
        srl $t1, $t0, 16
        beq $t1, $0, SMALL
        addi $a1, $a1, 4
        sw $t0, 0($a2)
        j LOOP
        addi $a2, $a2, 4

SMALL:
        sh $t0, 0($a3)
        j LOOP
        addi $a3, $a3, 2

DONE:
        sub $v0, $a2, $v0
        jr $ra
        srl $v0, $v0, 2


################################################################################
## Testbench Routine
#
# 


        .data
array_a:
        .word 0x1ae2d15, 0xc87, 0x0, 0x2ae89, 0x11c70, 0x7a36b, 0x5, 0x1e78aa7, 0x1457c01f, 0x12, 0x21623, 0x8c86, 0x5, 0x2139f25, 0x0, 0x10
array_b1:
        .space 32
array_b4:
        .space 64

ans_b1:
        .half 0xc87, 0x0, 0x5, 0x12, 0x8c86, 0x5, 0x0, 0x10
ans_b4:
        .word 0x1ae2d15, 0x2ae89, 0x11c70, 0x7a36b, 0x1e78aa7, 0x1457c01f, 0x21623, 0x2139f25
msg_oor:
        .asciiz "The number of elements in b4 is out of range: %/s4/d.\n"
msg_elt:
        .asciiz "   0x%/t2/8x   0x%/t3/8x  %/t5/s\n"
msg_b1_head:
        .asciiz "   Should Be    Written    Array B1\n"
msg_b4_head:
        .asciiz "   Should Be    Written    Array B4\n"
text_xxx:
        .asciiz "incorrect"
text_z:
        .asciiz ""


        .text
        .globl __start
__start:
        addi $a0, $0, 16
        la $a1, array_a
        la $a2, array_b4
        la $a3, array_b1
        jal split
        addi $v0, $0, 4720

        addi $s4, $v0, 0
        addi $s1, $0, 16
        sub $s1, $s1, $v0

        sltiu $t0, $s4, 17
        bne $t0, $0, okay_range
        nop

        la $a0, msg_oor
        addi $v0, $0, 11
        syscall
        addi $v0, $0, 10
        syscall

okay_range:
        addi $t0, $0, 0
        la $t0, ans_b1
        la $t1, array_b1
        add $t4, $t0, $s1
        add $t4, $t4, $s1
        la $a0, msg_b1_head
        addi $v0, $0, 11
        syscall
        la $t6, text_z
        la $t7, text_xxx
        la $a0, msg_elt
loop_1:
        lh $t2, 0($t0)
        lh $t3, 0($t1)
        beq $t2, $t3, skip_1
        add $t5, $t6, $0
        add $t5, $t7, $0
        addi $t8, $t8, 1
skip_1:
        syscall
        addi $t0, $t0, 2
        bne $t0, $t4, loop_1
        addi $t1, $t1, 2

        la $a0, msg_b4_head
        addi $v0, $0, 11
        syscall
        addi $t0, $0, 0
        la $t0, ans_b4
        la $t1, array_b4
        sll $t4, $s1, 2
        add $t4, $t4, $t0
        la $t6, text_z
        la $t7, text_xxx
        la $a0, msg_elt
loop_2:
        lw $t2, 0($t0)
        lw $t3, 0($t1)
        beq $t2, $t3, skip_2
        add $t5, $t6, $0
        add $t5, $t7, $0
        addi $t8, $t8, 1
skip_2:
        syscall
        addi $t0, $t0, 4
        bne $t0, $t4, loop_2
        addi $t1, $t1, 4

        li $v0, 10
        syscall
        nop