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