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 #include #include #using namespace std; # #// ml06XorPattern C++ Equivalent # #;int main() #{ # const int SIZE = 16; # unsigned short sequence[] = { 7, 5, 3, 2, 8, 6, 3, 2, 7, 6, 4, 1, 7, 4, 4, 3 }; # unsigned short updown = 0; # # //Create the up-down pattern # unsigned short last = 0; # for (int x = 0; x < SIZE; x++) { # updown = (updown << 1); # updown += (sequence[x] > last) ? 1 : 0; # last = sequence[x]; # } # bitset<16> binary{ updown }; # cout << "Binary Pattern: " << binary << endl <> 1; # rotate = rotate | (bit0 << SIZE-1); //put low bit in position 11 # bitset<16> rotateBin{ rotate }; # cout << x << "\trotate= " << rotateBin; # # xorResults[x] = updown ^ rotate; //XOR # bitset<16> xorBin{ xorResults[x] }; # cout << "\txor= " << xorBin; # # int sum = 0; # for (int i = 0; i < SIZE; i++) # if (xorResults[x] & (1 << i)) sum++; //sum 1-bits # cout << "\tsum= " << sum << endl; # xorResults[x] = sum; # } # # // Find the smallest sum # int answer = SIZE-1; # last = 99; # for (int x = SIZE-1; x > 0; x--) { # if (xorResults[x] <= last) { # answer = x; # last = xorResults[x]; # } # } # cout << " Pattern repeats every " << answer << endl; # cin.get(); cin.get(); # return 0; #} .data .eqv SYS_PRINT_WORD 1 #word, byte, character .eqv SYS_PRINT_FLOAT 2 #float .eqv SYS_PRINT_DOUBLE 3 #double .eqv SYS_PRINT_TEXT 4 #text (zero terminated) .eqv SYS_INPUT_WORD 5 #input word .eqv SYS_INPUT_FLOAT 6 #input float .eqv SYS_PRINT_BIN 35 #print binary .eqv SYS_EXIT 10 #terminate

# //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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!