In this project you will be working individually to creating a program that conv
ID: 667804 • Letter: I
Question
In this project you will be working individually to creating a program that converts an ASCII string into a binary value and displays a character on the UART indicating either an error or a successful conversion. An equals character (‘=’) will be used to indicate the end of a number and a negative sign (’-’) before the number indicates that the value is negative. If it is negative then you will need to convert the value to its two’s compliment. This is done by inverting all of the bits and then adding 1. Inverting, or performing a bitwise NOT on all bits, is equivalent to performing a NOR where both inputs to the NOR are the number you wish to NOT. The result of your signed conversion, either positive or negative, should be put in register $v0. Use numbers through the UART to indicate the state of your program after completing a number input attempt. If a number is successfully converted then it should output a ‘0’ character. If a number contains an invalid character (i.e. 3a4 or #5) then it should output a ‘1’ character. If the value is larger than can be stored in a single 32-bit register then it should output a ‘2’ character. Here are some sample inputs and their corresponding outputs:
Explanation / Answer
void ascii_to_binary(int * option_stats, char * ascii)
{
int i, j, num_value, count, binary[40], length, space=0; /* binary array length of 40 as */
length = strlen(ascii); /* 5*8 bit characters is 40 */
count = 8;
pos = 0
printf("Binary representation: ");
for (i = 0; i < length; i++)
{
num_value = ascii[i];
while(count > 0)
{
if((num_value%2) == 0)
{
binary[pos] = 0;
num_value = num_value/2;
count--;
pos++;
}
else
{
binary[pos] = 1;
num_value = num_value/2;
count--;
pos++;
}
}
count = 8; /*resets loop counter*/
}
for(j = pos-1; j >= 0; j--)
{
printf("%d", binary[j]);
space++;
if(space == 8)
{
printf(" "); /*white space between bytes*/
}
}
read_rest_of_line(); /*used as part of the larger program as a whole,
please ignore*/
}
void run_ascii_binary(void)
{
char input[MAX_STRING_INPUT + EXTRA_SPACES], ascii;
int option_stats;
printf("ASCII to Binary Generator ");
printf("------------------------- ");
printf("Enter a String (1-5 characters): ");
if (fgets(input, MAX_STRING_INPUT+EXTRA_SPACES, stdin) != NULL)
{
sscanf(input, "%s", &ascii);
}
ascii_to_binary(&option_stats, &ascii);
}
void main()
{
run_ascii_binary();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.