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

Write a program using notepad to run on Qtspim The program is to convert a posit

ID: 3759860 • Letter: W

Question

Write a program using notepad to run on Qtspim
The program is to convert a positive decimal number of 1 to 8 digits to its Hex
representation using divide by 16 method (use div and rem functions). Use the stack to
push each remainder to the stack that you will pop later and print the corresponding
ASCII character.
Your program is to do the following:
1. Prompt for a decimal number (1-8 digits)
2. Input a number (using syscall 5)
3. Check the number to assure it is within the limits.
4. If not, print error message and try again (go back to step 1)
5. Convert this decimal number to hexadecimal representation and output it to the
console (0x means Hex representation). Print a header before the Hex
characters as defined in label answer: below.
6. Use linefeed as defined in label crlf: to make output easy to read
The beginning of the program and the data declaration are given below
# Program to convert a decimal number to its Hex representation using divide by
# 16 method and the stack to print each Hex digit to the console.

.text

main:


.data
prompt: .asciiz "Input a positive decimal number of 1 to 8 digits"
answer: .asciiz " Hexadecimal number is 0x "
crlf: .asciiz " "
err: .asciiz "Error – try again!"

Explanation / Answer

#include<stdio.h>

int main(){

    long int decimalNumber,remainder,quotient;

    int i=1,j,temp;

    char hexadecimalNumber[100];

    printf("Enter any decimal number: ");

    scanf("%ld",&decimalNumber);

    quotient = decimalNumber;

    while(quotient!=0){

         temp = quotient % 16;

      //To convert integer into character

      if( temp < 10)

         temp =temp + 48;

      else

         temp = temp + 55;

    hexadecimalNumber[i++]= temp;

    quotient = quotient / 16;

}

    printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);

    for(j = i -1 ;j> 0;j--)

    printf("%c",hexadecimalNumber[j]);

    return 0;

}

Sample output:

Enter any decimal number: 45

Equivalent hexadecimal value of decimal number 45: 2D

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