Question: Can anyone help with this MIPS # Write a MIPS assembler program to find the how many elements # are in a repeating pattern. Create
Can anyone help with this MIPS
# Write a MIPS assembler program to find the how many elements # are in a repeating pattern. Create a binary bit string of # 1 if the value is larger, or 0 if the value is smaller. # Rotate the up-down pattern one position at a time and XOR # it with the original up-down pattern. If the XOR is zero # or a small number of 1 bits, you've found how often the # pattern repeats. # # SAMPLE OUTPUT: # Binary Pattern: 00000000000000001000100010001000 # # rotate= 00000000000000000001000100010001 xor= 00000000000000001001100110011001 sum= 8 # rotate= 00000000000000000010001000100010 xor= 00000000000000001010101010101010 sum= 8 # rotate= 00000000000000000100010001000100 xor= 00000000000000001100110011001100 sum= 8 # rotate= 00000000000000001000100010001000 xor= 00000000000000000000000000000000 sum= 0 # rotate= 00000000000000000001000100010001 xor= 00000000000000001001100110011001 sum= 8 # rotate= 00000000000000000010001000100010 xor= 00000000000000001010101010101010 sum= 8 # rotate= 00000000000000000100010001000100 xor= 00000000000000001100110011001100 sum= 8 # rotate= 00000000000000001000100010001000 xor= 00000000000000000000000000000000 sum= 0 # rotate= 00000000000000000001000100010001 xor= 00000000000000001001100110011001 sum= 8 # rotate= 00000000000000000010001000100010 xor= 00000000000000001010101010101010 sum= 8 # rotate= 00000000000000000100010001000100 xor= 00000000000000001100110011001100 sum= 8 # rotate= 00000000000000001000100010001000 xor= 00000000000000000000000000000000 sum= 0 # rotate= 00000000000000000001000100010001 xor= 00000000000000001001100110011001 sum= 8 # rotate= 00000000000000000010001000100010 xor= 00000000000000001010101010101010 sum= 8 # rotate= 00000000000000000100010001000100 xor= 00000000000000001100110011001100 sum= 8 # rotate= 00000000000000001000100010001000 xor= 00000000000000000000000000000000 sum= 0 # # Pattern repeats every 4 # # ---------------------------------------------------------- # C++ Equivalent # ---------------------------------------------------------- #include
# //declare variables .eqv SIZE 32 sequence: .half 7, 5, 3, 2, 8, 6, 3, 2, 7, 6, 4, 1, 7, 5, 4, 3 # sequence: .half 7, 5, 3, 8, 6, 3, 7, 6, 4, 7, 5, 4, 8, 6, 3, 0 # sequence: .half 7, 6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 7, 6, 5, 4 xorResults: .space SIZE #16 halfwords updown: .half 0 last: .half 0 rotate: .half 0 xor: .half 0 bit0: .half 0 endl: .asciiz " " endl2: .asciiz " " txPattern: .asciiz " Binary Pattern: " txRotate: .asciiz "\trotate= " txXor: .asciiz "\txor= " txSum: .asciiz "\tsum= " txAnswer: .asciiz " Pattern repeats every " .text .globl main main:
# ---------------------------------------------------------------- # Create the up-down bit pattern from 'sequence' # ---------------------------------------------------------------- la $s1, sequence # s1 = sequence addiu $s7, $s1, SIZE # s7 = SIZE (ending address) lhu $s2, updown # s2 = updown lhu $s3, last # s3 = last udloop: sll $s2, $s2, 1 # shift left s2 updown lhu $s4, ($s1) # get value in sequence[] sltu $s5, $s4, $s3 # sequence[x] < last? bne $s5, $zero, isLess # yes, branch ori $s2, $s2, 1 # bit0 = 1; sequence[x] >= last isLess: move $s3, $s4 # last = sequence[x] addiu $s1, $s1, 2 # move the next sequence[x] halfword sltu $s5, $s1, $s7 # s1 < s7 end_address? bne $s5, $zero, udloop # no, branch sh $s2, updown # save updown bit pattern # Print updown in hex li $v0, SYS_PRINT_TEXT la $a0, txPattern syscall li $v0, SYS_PRINT_BIN lhu $a0, updown syscall li $v0, SYS_PRINT_TEXT la $a0, endl2 syscall # ---------------------------------------------------------------- # Rotate the 'updown' bit pattern and create 16 halfword XORs in xorResult[] # ---------------------------------------------------------------- xorloop: # TODO: get rotated pattern in $s3 # $s1 points to xorResults[]; incremented by 2 (halfword) # $s7 is ending address: s1 + SIZE # print rotate li $v0, SYS_PRINT_TEXT la $a0, txRotate syscall li $v0, SYS_PRINT_BIN move $a0, $s3 syscall # save xor updown ^ rotate in $s5 xor $s5, $s3, $s2 # xorResults[] = rotate ^ updown sh $s5, ($s1) # save to xorResults[]
# print xorResult bits li $v0, SYS_PRINT_TEXT la $a0, txXor syscall li $v0, SYS_PRINT_BIN move $a0, $s5 # print the xorResult[] syscall
# ------------------------------------------------------------ # sum s5 bits in each xorResults[] into s0; save back to xorResults[] # s0 = 0 # sum # while(s5>0) { # s5 = s5 & (s5 - 1) # turns off lowest one bit # s0++ # } # ------------------------------------------------------------ # s5 has xor pattern & we want to count the number of bits in it move $s0, $zero # s0 = sum = 0 sumloop: beqz $s5, sumDone subiu $s6, $s5, 1 and $s5, $s5, $s6 # zeros lowest 1-bit addiu $s0, $s0, 1 j sumloop sumDone: sh $s0, ($s1) # save sum halfword in xorResult[] # print sum of xor bits li $v0, SYS_PRINT_TEXT la $a0, txSum syscall li $v0, SYS_PRINT_WORD move $a0, $s0 # print sum of bits in xorResult[] syscall li $v0, SYS_PRINT_TEXT la $a0, endl syscall
# ................................................. # Bottom of main loop # ................................................. #loop for next xorResult pattern #addiu $s1, $s1, 2 # move to next xorResult[] #bne $s1, $s7, xorloop
# ------------------------------------------------------------ # Find lowest sum in xorResult[]; loop from high to low # ------------------------------------------------------------ #TODO: get index (0..15) of lowest xorResult[] sum value in $s6 prtlow: # print lowest sum of xor bits addiu $s6, $s6, 1 # origin is 0; add one to answer li $v0, SYS_PRINT_TEXT la $a0, txAnswer syscall li $v0, SYS_PRINT_WORD move $a0, $s6 # print index of lowest sum syscall li $v0, SYS_PRINT_TEXT la $a0, endl2 syscall #---- terminate --- exit: li $v0, SYS_EXIT syscall #.end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
