The Geek Forum

Main Forums => Homework Help => Topic started by: Gamine123 on November 11, 2009, 05:49:34 AM

Title: Computing - How to insert a Parity Bit
Post by: Gamine123 on November 11, 2009, 05:49:34 AM
Hi,

I am a Computer Science fresher and I missed the first 3 weeks of class due to admin problems so I get a bit lost for my assignments...

Anyway I've got this assignment that requires me to write a very simple assembly program on Microvision that will take a 7-bit value (0x59) in R1 and insert an odd parity bit at bit position 4, storing the parity-encoded value in R0.

I know I am supposed to use AddC, EOR and MOV functions but I don't know how to do it.
I am not asking for the answer to this particular problem but could anyone give me a general (simple) explanation of how it's done???

Help!!!

Thanks very much
Title: Re: Computing - How to insert a Parity Bit
Post by: Probie on November 11, 2009, 06:14:38 AM

A general simple explanation sounds like an answer to me. But I don't actually have the answer.
Title: Re: Computing - How to insert a Parity Bit
Post by: ivan on November 11, 2009, 02:48:24 PM
Well, you need to examine every bit of your value and count the 1s to determine parity. The easiest way to do that is with a shift or rotate command in a loop. Without a shift or rotate command, you can increment your mask instead. So something like this:

VALUE = the value you are testing
MASK = 1 (starting value of mask is 00000001)
LOOPCOUNTER = 0
PARITY = 1 (starting value of PARITY is odd)

start:
VALUE AND MASK --> RESULT (the result of ANDing the mask and value)
PARITY EXCLUSIVEOR RESULT --> PARITY (toggle PARITY if RESULT is 1)
MASK * 2 --> MASK (Advance masked bit to next position)
LOOPCOUNTER + 1 --> LOOPCOUNTER (increment loop counter)
if LOOPCOUNTER < 8 goto start: (repeat loop 7 times)

There might be more elegant ways of counting bits, like with a shift or rotate command. If you're being graded for elegance, this might not pass muster.

And as for inserting the 4th bit, again, there are probably elegant ways of doing this, but you can brute force it like this:

ORIGINALVALUE AND 00000111 --> RIGHTBITS
ORIGINALVALUE AND 11111000 --> LEFTBITS
LEFTBITS * 0x2 --> LEFTBITS
PARITY * 0x8 --> PARITY

LEFTBITS + PARITY + RIGHTBITS --> FINALRESULT

So here we go:

Value to test 1011001 (0x59)

First loop iteration
1011001 AND 00000001 = 1 (value AND mask)
00000001 XOR 1 = 0 (Toggle parity)
00000001 * 10 = 00000010 (shift mask bit for next iteration)

Second iteration
1011001 AND 00000010 = 0 (value AND mask)
00000000 XOR 0 = 0 (parity not toggled)
00000010 * 10 = 00000100 (shift mask bit for next iteration)

etc.

After 7th iteration, Parity has been toggled for each 1 bit, and
ends up set to 1.

Insertion:

1011001 AND 00000111 = 00000001 (right bits)
1011001 AND 11111000 = 01011000 (left bits)
1 * 1000 = 1000 (parity bit)
01011000 * 10 = 10110000 (shift left bits to the left by one position)
10110000 + 1000 + 00000001 = 10111001 (shifted left bits + parity bit + right bits)

The result is 0xb9



Title: Re: Computing - How to insert a Parity Bit
Post by: Clear_Runway on November 11, 2009, 05:47:43 PM
...wow. really am bad at programming
Title: Re: Computing - How to insert a Parity Bit
Post by: ivan on November 11, 2009, 06:08:27 PM
...wow. really am bad at programming

I doubt it. Not knowing a language or any specifics about any one system does not make one a bad programmer.

However, if you want to feel like a good programmer, learn a macro assembler language (no matter which one). It's easier than C and makes you feel all hacky inside.

(Oh look! I just used seventeen lines of code to draw a pixel!)


Title: Re: Computing - How to insert a Parity Bit
Post by: sociald1077 on November 12, 2009, 03:34:08 PM
I liked using Motorola assembly for the time I was in a microcontroller class. I have to admit though that using C to do the same opperations was so much easier.
Title: Re: Computing - How to insert a Parity Bit
Post by: xolik on November 13, 2009, 10:45:44 AM
1. Cut a hole in a bit
2. Stick your parity in the bit
3. Have her open the bit
Title: Re: Computing - How to insert a Parity Bit
Post by: ivan on November 13, 2009, 01:02:22 PM
I suddenly crave pizza.