// ========================================================================== // Print Bits // ========================================================================== // Print a range of bits from a given input word. // Inf2C-CS Coursework 1. Task A // PROVIDED file, to be used as model for writing MIPS code. // Paul Jackson // 8 Oct 2013 //--------------------------------------------------------------------------- // C definitions for SPIM system calls //--------------------------------------------------------------------------- #include int read_char() { return getchar(); } void read_string(char* s, int size) { fgets(s, size, stdin); } void print_char(int c) { putchar(c); } void print_string(char* s) { printf("%s", s); } //--------------------------------------------------------------------------- // PRINT_BITS function //--------------------------------------------------------------------------- // Print bits n-1 ... 0 of input word w, using a '#' character for each 1 bit, // and a '.' character for each 0 bit. // Assume n > 0 void print_bits(unsigned int w, int n) { unsigned int mask = 0x1 << (n - 1); while (mask != 0) { unsigned int v = w & mask; if (v != 0) { print_char('#'); } else { print_char('.'); } mask = mask >> 1; } return; } //--------------------------------------------------------------------------- // MAIN function //--------------------------------------------------------------------------- // A couple of test cases int main (void) { print_bits(0xfff,10); print_char('\n'); print_bits(0xa5,12); print_char('\n'); return 0; } //--------------------------------------------------------------------------- // End of file //---------------------------------------------------------------------------