I am trying to resolve this exercise: Given a sequence of bits, knowing that every n bit (of data) there is a parity bit (1 if the number of bits at 1 among the previous n is odd), check if there are errors. I have done it in C.
#include <stdio.h>
void main()
{
// Input
unsigned char vet[] = { 2,4,67,2,2,58,99 };
unsigned int len = 55; // lunghezza (numero di bit)
unsigned char n = 4; // numero di bit dati
// Output
unsigned char errori = 0; // 1 = errori; 0 = no errori
unsigned int i, j, cnt_1;
for (i = 0; i < len; i += 9)
{
cnt_1 = 0;
for (j = 0; j < 8 && i + j < len; j++)
{
unsigned char mask = 1 << j;
if (vet[i + j] & mask)
cnt_1++;
}
if (cnt_1 % 2 != 0)
{
if (!(vet[i + 8] & 1))
{
errori = 1;
break;
}
}
else
{
if (vet[i + 8] & 1)
{
errori = 1;
break;
}
}
}
// Stampa su video
printf("La sequenza di bit %scontiene errorin", (errori ? "" : "non "));
}
The problem is that I need to translate it in inline assembly:
#include <stdio.h>
void main()
{
// Input
unsigned char vet[] = { 2,4,67,2,2,58,99 };
unsigned int len = 55; // lunghezza (numero di bit)
unsigned char n = 4; // numero di bit dati
// Ouput
unsigned char errori = 0; // 1 = errori; 0 = no errori
__asm
{
}
// Stampa su video
printf("La sequenza di bit %scontiene errorin", (errori ? "" : "non "));
}
I have tried to resolve it but didnt make it. If anyone can help me to translate this part, I will appreciate it so much.
for (i = 0; i < len; i += 9)
{
cnt_1 = 0;
for (j = 0; j < 8 && i + j < len; j++)
{
unsigned char mask = 1 << j;
if (vet[i + j] & mask)
cnt_1++;
}
if (cnt_1 % 2 != 0)
{
if (!(vet[i + 8] & 1))
{
errori = 1;
break;
}
}
else
{
if (vet[i + 8] & 1)
{
errori = 1;
break;
}
}
}
New contributor
Alion Emini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1