#include #include #include #include "numscan.h" #define MAXLEN 80 #define BITS 16 #define DPRINT(f,x) printf(f,x); static unsigned long memory[10]; #define char2index(x) (isdigit(x) ? (x) - '0' : 0) unsigned long getnum(char *str) { switch(*str) { case 'm': case 'r': return memory[char2index(str[1])]; default: return numscan(str); } } int main(int argc, char *argv[]) { unsigned long x = 0, y; int bits = 16; int mask; char row[MAXLEN]; if (argc>1) sscanf(argv[1],"%d",&bits); printf("Using %d bits\n", bits); printf("Ctrl-d or q to quit\n"); mask = ~(~0 << bits); while (1) { printf("? "); if (NULL==fgets(row, MAXLEN, stdin)) exit(EXIT_SUCCESS); switch(row[0]) { case '\n': break; case 'q': exit(EXIT_SUCCESS); case '~': x = ~x; break; case '&': y = getnum(row+1); x = x & y; break; case '|': y = getnum(row+1); x = x | y; break; case '^': y = getnum(row+1); x = x ^ y; break; case '+': y = getnum(row+1); x = x + y; break; case '-': y = getnum(row+1); x = x - y; break; case '*': y = getnum(row+1); x = x * y; break; case '/': y = getnum(row+1); x = x / y; break; case '<': if(row[1] == '<') { y = numscan(row+2); x = x << y; break; } else { fprintf(stderr, "syntax error\n"); break; } case '>': if(row[1] == '>') { y = numscan(row+2); x = x >> y; break; } else { fprintf(stderr, "syntax error\n"); break; } case 'd': case 'o': case 'x': case 'b': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'm': x = getnum(row); break; case 's': memory[char2index(row[1])] = x; break; default: fprintf(stderr, "unknown command %d\n", row[0]); continue; } x &= mask; printf("b"); printbin(x, bits); printf(", o%lo, d%lu, x%lx\n", (long) x, (long) x, (long)x); } return 0; }