Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4-P5 Using only the assembly language instructions discussed so far, write a pro

ID: 3674893 • Letter: 4

Question

4-P5 Using only the assembly language instructions discussed so far, write a program to accept a number in hexadecimal form and display the decimal equivalent of the number. A typical interaction of your program is (user input is shown in bold):

                Please input a positive number in hex (4 digits max): A10F

                The decimal equivalent of A10F is 41231

Do you want to terminate the program (Y/N): Y

You should refer to Appendix A for an algorithm to convert from base b to decimal.

Hints:

                1. Required multiplication can be done by the shl instruction.

2. Once you have completed the hex number into the equivalent in binary using the algorithm of Appendix A, you can use the PutInt routine to display the equivalent.

Explanation / Answer

program to accept a number in hexadecimal form and display the decimal equivalent of the number:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
unsigned long convertToDecimal(char hex[]);
int main()
{char hex[9];// 8 characters for 32-bit Hexadecimal Number and one for ''.
unsigned long decimalNumber;
printf(" C Program to Convert Hexadecimal Number to Decimal Number ");
printf(" Enter 32-bit Hexadecimal Number : ");
scanf("%s", hex);
decimalNumber = convertToDecimal(hex);
printf(" Decimal Number is %u ", decimalNumber);
printf(" Press enter to continue... ");
fflush(stdin);
getchar();
return 0;
}

unsigned long convertToDecimal(char hex[])
{char *hexString;
int length = 0;
const int base = 16; // Base of Hexadecimal Number
unsigned long decimalNumber = 0;
int i;
for (hexString = hex; *hexString != ''; hexString++)
{
length++;
}
hexString = hex;
for (i = 0; *hexString != '' && i < length; i++, hexString++)
{
if (*hexString >= 48 && *hexString <= 57) // is *hexString Between 0-9
{
decimalNumber += (((int)(*hexString)) - 48) * pow(base, length - i - 1);
}
else if ((*hexString >= 65 && *hexString <= 70)) // is *hexString Between A-F
{
decimalNumber += (((int)(*hexString)) - 55) * pow(base, length - i - 1);
}
else if (*hexString >= 97 && *hexString <= 102) // is *hexString Between a-f
{
decimalNumber += (((int)(*hexString)) - 87) * pow(base, length - i - 1);
}
else
{
printf(" Invalid Hexadecimal Number ");
printf(" Press enter to continue... ");
fflush(stdin);
getchar();
return 0;
exit(0);
}
}
return decimalNumber;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote