Instructions
Requirements and Specifications
- The LSB is the rightmost bit; the MSB is the leftmost bit. Given this, three of the first four low order bits are parity bits (1, 2, and 4).
- While Hamming codes are typically covered in CSPC 141, I will do a quick review of the topic in class. I have also provided the related slides in the Zip file. Obviously, you can also Google the topic.
- The 32-bit word input may be up to eight (8) hex digits.
- Only one bit can be in error (data or parity bit).
- Since bit 32 is a parity bit with no data bits. While I could insist that it be set based on the parity type (0 for even, 1 for odd), that would open up the possibility that multiple bits may be in error. Given this, it is best to just ignore it. Note that it is zero in all my sample test cases.
- You should be able to reuse the code in Programming Assignment #4 to help solve this problem (yes—that was intentional). However, if your PA #4 solution did not work perfectly, be sure to fix it in order to use it on this problem.
- Hint: the LSB of an integer determines if it is even or odd.
Source Code
TITLE Hamming code analyzer
; This program asks the user to enter a 32-bit word, up
; to eight hex digists. Then the program determins the powers
; of two that contribute to every set bit in that number.
INCLUDE Irvine32.inc
.data
Proginfo BYTE "CPSC 232 - Hamming code analyzer", 0
PromptParity BYTE "Input the parity type (0-even, *-odd) >> ", 0
PromptWord BYTE "Input the received word (8 hex digits) >> ", 0
NoError BYTE "No bits in error", 0
Error BYTE "The bit in error is: ", 0
Corrected BYTE "The corrected word is: ", 0
parity DWORD 0
number DWORD 0
position DWORD 0
parity1 DWORD 55555555h
parity2 DWORD 66666666h
parity4 DWORD 78787878h
parity8 DWORD 7F807F80h
parity16 DWORD 7FFF8000h
.code
; Number is in eax, parity to use is in ebx, returns the bit error in eax, 0 if no error
getHammingError PROC
sub esp, 4
mov edi, ebx ; copy parity to edi
mov esi, OFFSET parity1 ; point to start of parities
mov edx, 0 ; clear edx to use for error bits
mov ecx, 1 ; check the 5 parities
chkloop:
mov ebx, [esi] ; check for parity 1
and ebx, eax ; isolate bits
mov [esp], ebx ; save result in stack
xor bl, [esp + 1] ; test 32 bit parity
xor bl, [esp + 2]
xor bl, [esp + 3]
cmp edi, 0 ; if even parity
jne chkOdd ; if not, check odd
or bl, bl
jp next ; if parity is ok, go to next
or edx, ecx ; else set error in current bit
jmp next
chkOdd:
or bl, bl
jnp next ; if parity is ok, go to next
or edx, ecx ; else set error in current bit
next:
add esi, 4 ; advance to next parity entry
shl ecx, 1 ; increment parity to check
cmp ecx, 16 ; see if it's the last parity
jle chkloop ; if not, keep checking
mov eax, edx ; return found error
add esp, 4
ret
getHammingError ENDP
; Corrects bit in word eax, bit is in ebx, returns corrected word
correctBit PROC
mov ecx, ebx ; copy bit number to ecx
dec ecx ; decrement bit number
mov ebx, 1 ; load 1 in ebx
shl ebx, cl ; shift 1 to bit position
xor eax, ebx ; invert bit to correct it
ret
correctBit ENDP
main PROC
mov edx, OFFSET Proginfo
call WriteString
call Crlf
call Crlf
mov edx, OFFSET PromptParity
call WriteString
call ReadChar
call WriteChar
call Crlf
cmp al, '0' ; compare input with '0'
je start ; if 0, parity is even, start calculation
mov DWORD PTR [parity], 1 ; else, set odd parity
start:
mov edx, OFFSET PromptWord
call WriteString
call ReadHex
mov [number], eax ; save number
mov ebx, [parity]
call getHammingError ; calculate hamming error
mov [position], eax ; save position in variable
cmp eax, 0 ; see if there were errors
je parityOk ; if not, go to ok
mov edx, OFFSET Error ; if error, print error
call WriteString
call WriteDec ; print position
call CrLf
mov eax, [number] ; load original number
mov ebx, [position] ; load error position
call correctBit ; correct the bit
mov edx, OFFSET Corrected ; print corrected message
call WriteString
call WriteHex ; print corrected word
jmp done
parityOk:
mov edx, OFFSET NoError
call WriteString
done:
call CrLf
exit
main ENDP
END main