# ------------------------------------------------
# KAAU CS212 - Spring 2005
# Sample MIPS assembly program (MAL)
# Dr. Muhammad Al-Hashimi

# program purpose:
# return the opcode of an instruction in the program

# program parameters:
# $t2 <- program length (instruction count)
# $s0 <- instruction index (instr numbered 0 to $t2-1) 
# $t4 <- program start address (value of the label main)

# program output:
# $t0 <- opcode of instruction at index $s0

# ------------------------------------------------

main:                                  # label program start (instr 0)

# ------------------------------------------------
# Program inputs section
   
            li       $t2,13            # direct (MAL) load of data: the program is 13 instr long (why?)
            li       $s0,11            # now lw is at line 11 (not 7)
            la       $t4,0x00400024    # direct load of addr

# ------------------------------------------------

            slt      $t3,$s0,$zero     # begin check instr number in range
            bne      $t3,$zero,Exit
            slt      $t3,$s0,$t2
            beq      $t3,$zero,Exit    # end check instr number in range
            add      $t1,$s0,$s0       # multiply instr index by 2 
            add      $t1,$t1,$t1       # multiply instr index by 4
            add      $t1,$t1,$t4       # compute instr address
            lw       $t0,0($t1)        # fetch instr word
            srl      $t0,$t0,26        # isolate upper 6 bits (opcode)

Exit:       ori      $2,$0,10          # system call: exit program
            syscall
