#------------------------------------------------------------------------------
# A simple diagnostic that will execute most instructions
# at least once and them dump the register file so that you
# can see it on the memory output bus
#------------------------------------------------------------------------------

#  Try modifying R0
main:  addi $0, $0, 1         # R0 had better still be 0         

#------------------------------------------------------------------------------
# misc ALU tests
#------------------------------------------------------------------------------
# addsub
      sub   $24, $0, $0       #  r24 = 0               
      addi  $2, $0, 1         #  r2=1               
      addi  $3, $0, -1        #  r3=-1               
      add   $4, $3, $2        #  r4=0 (no exceptions)               
      andi  $5, $3, 255       #  r5=255=0x000000FF               
      addu  $6, $5, $2        #  r6=256=0x00000100               
      addiu $7, $6, 768       #  r7=1024=0x00000400               
      subu  $8, $7, $6        #  r8=768=0x00000300            
# logic
      ori   $9, $0, 21845     #  r9=21845=0x00005555               
      or    $9, $9, $0        #  r9=21845=0x00005555 =unchanged               
      and   $9, $9, $3        #  r9=21845=0x00005555 =unchanged               
      nor   $10, $9, $0       #  r10=-21846=0xFFFFAAAA               
      xor   $11, $10, $10     #  r11=0 =0x00000000              
      xori  $12, $9, 65522    #  r12=0x0000AAA7           
# shift
      sll   $13, $9, 8        #  r13=5592320=0x00555500               
      sllv  $14, $2, $2       #  r14=2        =0x00000002       
      srl   $15, $10, 5       #  r15=134217045=0x07fffD55               
      srlv  $16, $9, $2       #  r16=10922=0x00002AAA               
      mult  $15, $16          #  {hi,lo} = 0x7fffd55 * 0x2aaa
      mfhi  $15               #  r15 = 0x00000155
      mflo  $16               #  r16 = 0x4f8e2c72
      div   $16, $15          #  hi = 0x4f8e2c72 mod 0x155, lo = 0x4f8e2c72 / 0x155
      mfhi  $15               #  r15 = 0x000000f7
      mflo  $16               #  r16 = 0x003bb98f
      mthi  $5                #  use hi and lo to swap registers r5 and r6
      mtlo  $6
      mfhi  $6                # r6 = =0x000000FF
      mflo  $5                # r5 = 0x000000100
      sra   $17, $3, 4        #  r17=-1=0xFFFFFFFF               
      srav  $18, $9, $14      #  r18=5461=0x00001555               
      slt   $19, $9, $9       #  r19=0=0x00000000               
      slt   $20, $5, $6       #  r20=0=0x00000000               
      sltu  $21, $4, $3       #  r21=1=0x00000001               
      sltiu $22, $3, 0        #  r22=0=0x00000000               

#------------------------------------------------------------------------------
# test the jump instructions
#------------------------------------------------------------------------------
      addi  $23,$0,0          # set register 23 to zero
      j jumpok
      addi  $23,$0,-1         # -1 in r23 means jump failed
jumpok:
      addi  $24,$0,0          # initialize  register 24 to zero
      jal jalok               # this is taken w/$31 = PC
      addi  $24,$24,1         # should not come here after jal, but will after jalr below
      j out# 
jalok:  
      addi  $24,$0,100        # re-init $24 to 100
      jalr  $31               # now jump back 3 instructions 
out:                          # will test jr below

#------------------------------------------------------------------------------
# test branch instructions
# for these test r2 = 1, r3 = -1 (see above)
#------------------------------------------------------------------------------

        beq   $3,$3 taken     # taken branch: PC moves ahead by 8
stuck:  j     stuck           # don't execute this instruction
taken:  beq   $3, $2, stuck   # no take branch (failures get stuck)

        bne   $3,$2 taken1    # taken branch: PC moves ahead by 8
stuck1: j     stuck1          # don't execute this instruction
taken1: bne   $3, $3, stuck1  # no take branch (failures get stuck)

        bltz  $3, taken2      # taken branch: PC moves ahead by 8
stuck2: j     stuck2          # don't execute this instruction
taken2: bltz  $2, stuck1      # no take branch (failures get stuk)

        blez  $0, taken3      # taken branch: PC moves ahead by 8
stuck3: j     stuck3          # don't execute this instruction
taken3: blez  $2, stuck3      # no take branch (failures get stuck)

        blez  $3, taken4      # taken branch: PC moves ahead by 8
stuck4: j     stuck4          # don't execute this instruction
taken4: blez  $2, stuck4      # no take branch (failures get stuck)

        bltzal  $3, taken5    # taken branch: PC moves ahead by 8
stuck5: j      stuck6         # don't execute this instruction
taken5: bltzal   $2, stuck5   # no take branch (failures get stuck); 
        add $25,$0,$31        # link address in r25

        bgtz   $2, taken6     # taken branch: PC moves ahead by 8
stuck6: j      stuck6         # don't execute this instruction
taken6: bgtz   $3, stuck6     # no take branch (failures get stuck)

        bgez   $0, taken7     # taken branch: PC moves ahead by 8
stuck7: j      stuck7         # don't execute this instruction
taken7: bgez   $3, stuck7     # no take branch (failures get stuck)

        bgez   $2, taken8     # taken branch: PC moves ahead by 8
stuck8: j      stuck8         # don't execute this instruction
taken8: bgez   $3, stuck8     # no take branch (failures get stuck)

        bgezal  $2, taken9    # taken branch: PC moves ahead by 8
stuck9: j      stuck9         # don't execute this instruction
taken9: bgezal  $3, stuck9    # no take branch (failures get stuck); 
        add $26,$0,$31        # link address in r26

#------------------------------------------------------------------------------
# test load/store
#------------------------------------------------------------------------------
        sw  $17,4($0)
        sh  $17,10($0)
        sb  $17,11($0)
        
        lw  $25,4($0)         # r25 = 0xffffffff
        lhu $26,4($0)         # r26 = 0x0000ffff
        lbu $27,4($0)         # r27 = 0x000000ff
        lh  $28,4($0)         # r28 = 0xffffffff
        lb  $29,4($0)         # r29 = 0xffffffff
        
        sub $30,$0,$0         # r30 = 0
        lui   $1, 2           # r1 = 0x00020000

#------------------------------------------------------------------------------
# this is your output..
#------------------------------------------------------------------------------
        sw $1, 0($30)                   
        addi $30,$30,4                 
        sw $2, 0($30)                   
        addi $30,$30,4                 
        sw $3, 0($30)                   
        addi $30,$30,4                 
        sw $4, 0($30)                   
        addi $30,$30,4                 
        sw $5, 0($30)                   
        addi $30,$30,4                 
        sw $6, 0($30)                   
        addi $30,$30,4                 
        sw $7, 0($30)                   
        addi $30,$30,4                 
        sw $8, 0($30)                   
        addi $30,$30,4                 
        sw $9, 0($30)                   
        addi $30,$30,4                 
        sw $10, 0($30)                  
        addi $30,$30,4                 
        sw $11, 0($30)                  
        addi $30,$30,4                 
        sw $12, 0($30)                  
        addi $30,$30,4                 
        sw $13, 0($30)                  
        addi $30,$30,4                 
        sw $14, 0($30)                  
        addi $30,$30,4                 
        sw $15, 0($30)                  
        addi $30,$30,4                 
        sw $16, 0($30)                  
        addi $30,$30,4                 
        sw $17, 0($30)                  
        addi $30,$30,4                 
        sw $18, 0($30)                  
        addi $30,$30,4                 
        sw $19, 0($30)                  
        addi $30,$30,4                 
        sw $20, 0($30)                  
        addi $30,$30,4                 
        sw $21, 0($30)                  
        addi $30,$30,4                 
        sw $22, 0($30)                  
        addi $30,$30,4                 
        sw $23, 0($30)                  
        addi $30,$30,4                 
        sw $24, 0($30)                  
        addi $30,$30,4                 
        sw $25, 0($30)                  
        addi $30,$30,4                 
        sw $26, 0($30)                  
        addi $30,$30,4                 
        sw $27, 0($30)                  
        addi $30,$30,4     
        sw $28, 0($30)                  
        addi $30,$30,4     
        sw $29, 0($30)                  
        addi $30,$30,4     
        
        add $31,$0,$0          
        jr   $31
        
.data
str: .asciiz "simple test\n"

#------------------------------------------------------------------------------
# end of simple test
#------------------------------------------------------------------------------
