/* * This C program is a simplified variation of PP 1b to illustrate * procedure calls in MIPS. * * It calls the following functions: * int get33ByteStr(char input[]) * precondition: input is an array of at least 33 integers * postcondition: fills input with 32 ASCII characters + null word * int verifyMIPS(input) * precondition: input is a null-terminated string * postcondition: returns 1 if every value before the null * byte is either '0' or '1'; returns 0 otherwise * void testB2D(input) * precondition: input is a null-terminated array of ASCII values * representing a 32-bit (or longer) binary number * postcondition: prints results of several tests * * Alyce Brady * 24 January, 2021 */ int get33ByteStr(int input[]); int verifyMIPS(int input[]); void testB2D(int input[]); int input[33]; int main() { while ( get33ByteStr(input) ) { if ( verifyMIPS(input) ) { testB2D(input); } } return 0; } /* get33ByteStr reads input from ??? and stores up to 32 ASCII characters * plus a null byte into memory starting at the address passed in as a * parameter. * precondition: input is an array of at least 33 bytes * postcondition: fills input with 32 ASCII values + null byte */ int get33ByteStr(int input[]) { /* CODE NOT SHOWN HERE! */ } /* verifyMIPS checks the array provided as a parameter to see whether * every value before the null is a '0' or '1'. * precondition: input is a null-terminated array * postcondition: returns 1 if every value before the null * is either '0' or '1'; returns 0 otherwise */ int verifyMIPS(int input[]) { int * ptr; for ( ptr = input; *ptr != '\0'; ptr++ ) { if ( *ptr != '0' && *ptr != '1' ) return 0; } return 1; } int bin2dec(int * input, int begin, int end); void printIt(int * input, int begin, int end, int intValue); /* testB2D calls bin2dec several times with different test cases and * prints the result. * precondition: input is a null-terminated array of ASCII values * representing a 32-bit (or longer) binary number * postcondition: prints results of several tests */ void testB2D(int * input) { int intValue = bin2dec(input, 0, 5); printIt(input, 0, 5, intValue; printIt(input, 6, 10, bin2dec(input, 6, 10)); printIt(input, 11, 15, bin2dec(input, 11, 15)); printIt(input, 16, 20, bin2dec(input, 16, 20)); printIt(input, 21, 25, bin2dec(input, 21, 25)); printIt(input, 26, 31, bin2dec(input, 26, 31)); } /* bin2dec returns the integer value represented by the binary substring * between input[begin] and input[end], inclusive. * precondition: input is a null-terminated array of ASCII values * representing a binary number; * begin and end are both valid indices with input * (0 <= begin <= end and end < index of null) * postcondition: returns integer value corresponding to the "bits" * between input[begin] and input[end], inclusive */ int bin2dec(int * input, int begin, int end) { int intEquivalent = 0; int powOf2 = 1; char * beginPtr = input + begin; char * endPtr = input + end; char * ptr; for ( ptr = endPtr; ptr >= beginPtr; ptr-- ) { if ( *ptr == '1' ) { intEquivalent += powOf2; } powOf2 *= 2; /* OR, powOf2 << 1 */ } return intEquivalent; } /* printIt prints output in the following format: * "Binary %s is equivalent to decimal %d.\n" * where the first string corresponds to the substring between * input[begin] and input[end], inclusive, and the integer is intValue. * precondition: input is a null-terminated array of ASCII values * representing a binary number; * intValue is its integer equivalent; * begin and end are both valid indices with input * (0 <= begin <= end and end < index of null) */ void printIt(int * input, int begin, int end, int intValue) { /* CODE NOT SHOWN HERE! */ }