Add 4 Parity bit with 8 Data bit
Description:
Write an x86 assembler program that will use the Hamming Code to generate an encoded 12-bit word for a byte of data. The resulting 12-bit word will have 8 data bits and 4 parity bits. The program will work for either an even or odd parity scheme (chosen by the user).
Example session – Even Parity:
Example session – Odd Parity:
Solution:
INCLUDE Irvine32.inc
.data
initialMsg BYTE "CPSC 232 - Hamming Coder for Data Byte", 0
promptParity BYTE "Input the parity type (0-even, *-odd) >> ", 0
promptByte BYTE "Input a byte (two hex digits) >> ", 0
hammingMsg BYTE "The Hamming encoded message is: ", 0
.code
main PROC
; print initial message
mov edx, OFFSET initialMsg ; load string address
call WriteString ; print it on screeen
call CrLF ; print newline
call CrLF ; print newline
; Prompt user to enter parity
mov edx, OFFSET promptParity ; load string address
call WriteString ; print it on screeen
; read input
call ReadDec ; read a number
cmp eax, 0 ; see if user entered 0
je evenParity ; if even , jump to label
mov ebx, 1 ; set odd parity
jmp readByte ; go to read byte
evenParity:
mov ebx, 0 ; set even parity
readByte:
call CrLF ; print newline
; Prompt user to enter byte
mov edx, OFFSET promptByte ; load string address
call WriteString ; print it on screeen
; read input
call ReadHex ; read a hex number
call hamming ; calculate the hamming encoding with the read byte and parity ebx
mov ebx, eax ; load result in ebx
call CrLF ; print newline
; print result message
mov edx, OFFSET hammingMsg ; load string address
call WriteString ; print it on screeen
; print result encoding
mov eax, ebx ; load calculated hammind encoding
call WriteHex ; print it as hexadecimal
call CrLF ; print newline
exit ; terminate the program
main ENDP
;-----------------------------------------------------------------------
parity PROC
;
; Procedure to calculate the parity of the 12 bits in EAX,
; the type of parity is given in EBX, 0 = even, 1 = odd
; the bits to use for the parity is given in ECX, it can be (1, 2 or 4)
;-----------------------------------------------------------------------
push ebx ; save registers used
push ecx
push edx
mov edx, 1 ; start in bit 1
parityLoop:
test edx, ecx ; see if current bit must be checked
je paritySkip ; if not, skip parity test
shr eax, 1 ; shift lowest bit to C flag
jnc parityNext ; if zero, don't count
xor ebx, 1 ; else, add to total parity
jmp parityNext ; go to next bit
paritySkip:
shr eax, 1 ; shift to test next bit
parityNext:
inc edx ; go to next bit
cmp edx, 12 ; see if we checked all bits
jle parityLoop ; if not, repeat loop
mov eax, ebx ; return resulting parity
pop edx ; pop registers
pop ecx
pop ebx
ret
parity ENDP
;-----------------------------------------------------------------------
hamming PROC
;
; Procedure to encode a byte using 12 bit hamming code,
; the byte is given in EAX, the type of parity is given in EBX,
; it returns the encoding in EAX
;-----------------------------------------------------------------------
and eax, 0ffh ; clear upper bits if any
; make 12 bit encoding
mov edx, eax ; save byte in edx
and eax, 1 ; leave first bit
shl eax, 2 ; move to position 3
mov ecx, eax ; save in ecx
mov eax, edx ; restore byte
shr eax, 1 ; move bit 2 to lowest position
and eax, 7 ; leave bits 2,3 and 4
shl eax, 4 ; move to position 5
or ecx, eax ; add to encoding
mov eax, edx ; restore byte
shr eax, 4 ; move bit 5 to lowest position
shl eax, 8 ; move to position 9
or ecx, eax ; add to encoding
mov edx, ecx ; get encoded byte in edx
mov eax, edx ; get 12 bit encoding of byte
mov ecx, 1 ; use bit 1
call parity ; calculate parity
or edx, eax ; save result in encoding
mov eax, edx ; get 12 bit encoding of byte
mov ecx, 2 ; use bit 2
call parity ; calculate parity
shl eax, 1 ; move bit one position up
or edx, eax ; save in encoding
mov eax, edx ; get 12 bit encoding of byte
mov ecx, 4 ; use bit 4
call parity ; calculate parity
shl eax, 3 ; move bit 3 positions up
or edx, eax ; save in encoding
mov eax, edx ; get 12 bit encoding of byte
mov ecx, 8 ; use bit 8
call parity ; calculate parity
shl eax, 7 ; move bit 7 positions up
or edx, eax ; save in encoding
mov eax, edx ; return encoded byte
ret
hamming ENDP
END main
Output:
Screenshot 1:
Screenshot 2: