Computer Systems Organization I - Prof. Grishman

Assignment #4

Write a C function which interprets a string as a hexadecimal value.  This is the C equivalent of the assembly language program which you wrote for assignment #3. 

More precisely, you are to write a C function hexval which accepts a single argument, a string of 4 or fewer characters, interprets the string as a hexadecimal integer, and returns the value of the integer.  The input string should consist of the characters '0' through '9' and 'A' through 'F';  an input containing any other characters should return -1 (this is different from the assembly language version, which did not have to check for invalid input).

Use the following main program to test your hexval function: 

#include <stdio.h>
main() {
    char input[5];
    int value;
    int hexval (char[]);
    while (1) {
        scanf("%s", &input);
        value = hexval(input);
        if (value == 0) return;
        printf ("value = %x\n", value);

Create a program file consisting of this main plus your hexval function, and name it lastname.c (for example, my file would be grishman.c).  Submit your program (.c file) by email, as an attachment, to the e-tutor, Ian Lau <>, by one minute before midnight on Thursday, November 4th. (Late assignments will be penalized 1/2 point for each day late, out of a total of 5 points.) Label your email "CSO Asgn 4".