Hi, I have an assignment where i have to write a C and assembly mixed-mode program.

  1. The c program takes in two characters which stand for two hex characters.
  2. The assembly procedure then passes each character in turn to a C function which determines if the character is a valid ASCII HEX digit (namely 0-F).
  3. If both characters are valid the assembly language procedure then compares the two HEX digits and returns the larger to the main program.
  4. The main C program then prints out the larger digit.

I am unsure how to do no. 2, namely, how to determine if it's a valid ASCII HEX digit or just garbage. Suggestions?

So far I have my C file taking in 2 characters:

char char1, char2;
    printf ("Enter a character: ");
    scanf ("%c", &char1);
    
    while (getchar () != '\n')//so that the program doesn't take in the carriage return as input
        continue;
    
    printf ("Enter another character: ");
    scanf ("%c", &char2);
    
    while (getchar () != '\n')//so that the program doesn't take in the carriage return as input
        continue;

And I'm just not sure how to verify if their HEX or just normal characters (so either 0-9 or A-F). I was thinking I could just do a bunch of if statements comparing each of them (or make a HEX array, and for loop through it comparing along the way), but is there a better way to do this?

So far I have my C file taking in 2 characters:

char char1, char2;
    printf ("Enter a character: ");
    scanf ("%c", &char1);
    
    while (getchar () != '\n')//so that the program doesn't take in the carriage return as input
        continue;
    
    printf ("Enter another character: ");
    scanf ("%c", &char2);
    
    while (getchar () != '\n')//so that the program doesn't take in the carriage return as input
        continue;

And I'm just not sure how to verify if their HEX or just normal characters (so either 0-9 or A-F). I was thinking I could just do a bunch of if statements comparing each of them (or make a HEX array, and for loop through it comparing along the way), but is there a better way to do this?

C has a function called isxdigit which checks if the character is 0 -9 or a - f maybe you could use that...

That worked perfectly thank you, I'll mark it as solved for now if more problems come up I'll change that :)

This question has already been answered. Start a new discussion instead.