Exercise 2 A parity check is one way of detecting errors in bytes that have been…
- User
Exercise 2
A parity check is one way of detecting errors in bytes that have been transmitted. Before sending the bytes, a decision is made that each byte must have an even number of ones (even parity) or an odd number of ones (odd parity). Any byte that is received with the wrong parity can then be identified as an error. Write a program in the Brookshear machine code which determines whether the bit pattern stored in memory location A0 has even or odd parity and place the result in memory location B0: • If the bit pattern in A0 has even parity, B0 should contain 00. • If the bit pattern in A0 has odd parity, B0 should contain FF.
Here are some example inputs and the expected outputs Value in memory location A0 before running the program Value in memory location B0 after running the program 35 (00110101 in binary) 00 (even parity as there are four 1’s in the bit pattern) A8 (10101000 in binary) FF (odd parity as there are three 1’s in the bit pattern) 00 (00000000 in binary) 00 (even parity as there are zero 1’s in the bit pattern, and zero is an even number)
There are several ways to solve this problem, but most solutions will require you to be able to: • Use a bit mask to copy the bit in a particular position from a bit string, while setting the other bits to 0. For example, if you copy first bit in 10101000 and 00110101, you will get 10000000 and 00000000 respectively. • Rotate the bits in a bit pattern so that the bit you want is in the last position. For example, moving the first bit in 10000000 to get 00000001 It is possible to solve the problem without a loop, but most efficient solutions will use a loop to reduce the number of instructions and registers used.
- Assistant
Here is one possible solution for the problem using the Brookshear Machine:
// Initialize registers, constants and memory locations. 00 C6 // LOADI 06 R6 // R6 will contain the value 6 (number of remaining bits to check) 01 C7 // LOADI 07 R7 // R7 will contain the value 7 (number of bit positions to rotate) 02 C8 // LOADI 08 R8 // R8 will contain the value 8 (bit mask) 04 C0 // LOADI 00 R0 // R0 will contain the value 0 (counter for number of 1's) // Load the value stored in A0 into R1 05 A1 A0 // LOAD 1 A0 R1 // Loop to count the number of 1's in the bit pattern 06 52 // ANDI 02 R1 R2 // R2 = R1 & 00000010 (isolate the leftmost bit) 07 92 // ROTL 02 R2 R2 // Rotate the leftmost bit of R2 so it's in the least significant bit 08 60 // ADD 00 R0 R0 // Add the rotated bit to the counter in R0 0A 53 // ROTR 03 R1 R3 // Put the remaining bits in R3 // Decrement the number of remaining bits and rotations needed 0D 76 // DECR 6 R6 0E 77 // DECR 7 R7 // Check if we've finished going through every bit 0F 63 // JNEZR 3 R6 // If R6 is not zero, continue checking the remaining bits // Otherwise, check if the number of 1's is odd or even 11 64 // JNEZR 4 R0 // If R0 is nonzero, the parity is odd // Store the result for even parity in B0 14 B1 B0 // STOREI 1 B0 // End the program 15 E0 // HALT // Store the result for odd parity in B0 18 CF // LOADI 0F R15 1A B1 B0 // STOREI 1 B0 // End the program 1B E0 // HALT - User
(a) An explanation of your algorithm to solve the problem. You may use text, a flow chart or a combination of both to explain your algorithm. [10 marks] (b)
- A screenshot of your program in the memory list view of the Brookshear Machine Simulator showing the text explanation of each instruction and
- The URL of the program, which your assessor can click on to test your program. [30 marks]
(c) An explanation of the how optimised your program is. You may explain what you have done to reduce the number of steps, the number of instructions or the number of registers used. You may also explain what can be done to your finished program to make it more optimised. [10 marks]
- Assistant
(a) Algorithm Explanation:
- Initialize registers, constants and memory locations.
- Load the value stored in memory location A0 into R1.
- Loop through the bit pattern (8 bits): a. Use a bitmask to copy the leftmost bit of the bit pattern stored in R1 while setting the other bits to 0 in R2. b. Rotate the leftmost bit of R2 so it's in the least significant bit. c. Add the rotated bit to the counter in R0, which will track the number of 1's found in the bit pattern. d. Rotate the remaining bits of R1 so that we can continue checking the remaining bits in the next iteration. e. Decrement the number of remaining bits and rotations needed. f. Check if we've finished going through every bit. If not, continue to the next iteration.
- User
Convert this code into hexadecimal so i can import it directly
- Assistant
Here's the Brookshear Machine code in hexadecimal format:
00C606,01C707,02C808,04C000,05A1A0,0652,0792,0860,0A53,0D76,0E77,0F63,1164,14B1B0,15E0,18CF,1AB1B0,1BE0Copy this hexadecimal code and use the "Load program from hexadecimal" option in the Brookshear Machine Simulator to run the program directly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.