Johns Hopkins University
Department of Computer Science
Fall 2010

Computer Systems Fundamentals
Laboratory
(Course 600.334)

Programming lab 1:
Bit Manipulation and Data Representation

Pre-requisites: CSF Programming Assignment #1 completed.

(Due on Friday TBA )

1. Objectives

2. General Instructions

    For this lab you are required to implement several programs using the MIPS programming language and test them with the simulator SPIM. The questions associated with each program should serve you as guidelines to prepare a report regarding how did you solve the problem.

    The report should to be submitted electronically on the due date to the e-mail address cs333 [at] cs.jhu.edu. Very important: Start the subject with "CS334:", followed by your name and programming project number.

    IMPORTANT: Read carefully the information provided in this handout to have a better understanding of the background you might require to complete this lab, particularly sections 4 and 5.

    Additional resources:

3. Programming Activities

    A. Displaying bit-mapped characters

  1. Given a 10-element vector (that is, a sequence of 10 integer numbers), each representing a row in a bit-mapped character (BMC, see section 4), display the corresponding character.

    Notes
    • All the numbers should be read from the keyboard, and stored into memory, before the BMC is displayed.

    • Use 9-bit integers; that is, numbers between 0 and 511. That will produce a matrix of 9 columns and 10 rows, with each cell holding one bit.

    • Use two different characters to represent the 0's and the 1's inside the registers (the selection is up to you).

    Example: If using the character "M" to represent bit 1, and the character "-" to represent bit 0, after decoding the bit-map vector [16, 56, 84, 80, 48, 24, 20, 84, 56, 16] the output will be (note, extra spaces were added for readability):

    - - - - - M - - - -
    - - - - M M M - - -
    - - - M - M - M - -
    - - - M - M - - - -
    - - - - M M - - - -
    - - - - - M M - - -
    - - - - - M - M - -
    - - - M - M - M - -
    - - - - M M M - - -
    - - - - - M - - - -
    

    Hint: Perform a hardcoded initialization of the required memory for all arrays. For example, to initialize an 8-cell array use:

    .data
    seq: .word 0 0 0 0 0 0 0 0 0

    Type the previous code before the program section (the one indicated with the directive .text)
    Also, keep in mind that bitmaps use decimal numbers as shortcut for binary patterns.


    Note: A bit-mapped characte i encoded as a numerical sequence, namely, a data vector (1D), however, once it is displayed it becomes a data matrix (2D).
    Questions
    1. Given this sequence [60, 66, 157, 293, 329, 329, 334, 304, 131, 124], what character appears on the screen?
    2. Given this sequence [0, 56, 72, 80, 102, 420, 296, 274, 236, 0], what character appears on the screen?
    3. What vector do you need to print the letter "A"?
    4. What vector do you need to print the letter "B"?

    B. Visual results of logical operations

  2. Given the vectors A and B, each containing ten 8-bit integers, perform several operations over them and display the results like bit-mapped characters.

    Note: Apply the techniques developed in section 3 to visualize the shape of the vectors A and B, as well as the result of the operations AND, OR, and XOR, over each cell in the arrays.

    Example: Let A, B and C be linear arrays storing ten 8-bit positive numbers.

    A = [255, 255, 255, ..., 255, 255] (i.e., a 10x8 square block), B = [1, 2, 4, ..., 64, 128, 0, 0] (i.e., a forward slash), and C the result of performing A AND B.

    The values of C are obtained by applying the operator AND to each item in the corresponding cell of each array (the BMCs are shown below):

         Cell #    1   2   3        8     
            A: 255 255 255 ...  255  
               B:   1   2   4 ...    0     
              ------------------------    
    C = A AND B:   1   2   4 ...    0    
    
       A                B                C
    ----------------------------------------------
    MMMMMMMM (255)     -------M (1)     -------M
    MMMMMMMM (255)     ------M- (2)     ------M-
    MMMMMMMM (255)     -----M-- (4)     -----M--
    MMMMMMMM (255)     ----M--- (8)     ----M---
    MMMMMMMM (255)     ---M---- (16)    ---M----
    MMMMMMMM (255)     --M----- (32)    --M-----
    MMMMMMMM (255)     -M------ (64)    -M------
    MMMMMMMM (255)     M------- (128)   M-------
    MMMMMMMM (255)     -------- (0)     --------
      MMMMMMMM (255)     -------- (0)     --------  
    

    Hint: This section is a very experimental one; you may want to hardcode the sequences instead of getting them from the keyboard on each trial.

    Questions

    Let A, B and C bit-mapped characters, where C is the result of operating over A and B.
    If A = [65, 34, 20, 8, 20, 34, 65, 0, 0, 0] and B = [8, 8, 8, 127, 8, 8, 8, 0, 0, 0], then C = A + B is:

    A = [65, 34, 20,   8, 20, 34, 65, 0, 0, 0]
    B = [ 8,  8,  8, 127,  8,  8,  8, 0, 0, 0]
    C = [83, 42, 28, 135, 28, 42, 73, 0, 0, 0]
    
    1. What symbols are produced by the sequences on arrays A and B?
    2. What symbol is produced by the operation C = A AND B?
    3. What symbol is produced by the operation C = A OR B?
    4. What symbol is produced by the operation C = A XOR B?

    C. Binary representation of decimal digits

  3. Simulate the addition of a "long" decimal number, as if performed with an old computer which registers were just 4-bit long.

    Notes
    • Because of the nature of the problem, you will be using the BCD method to represent decimal digits (see section 5.)

    • You are required to add two numbers, each up to 8 decimal digits, and show the result. To avoid wasting memory under our MIPS architecture, you must pack several BCD digits (each 4-bits long) into the same 32-bit MIPS register (That is, the MIPS register will be storing up to 8 BCD numbers.)

    • Because the same register will contain different BCD digits inside, you cannot directly use the instruction ADD on the MIPS register (otherwise results will be erroneous.)

    • Given the previous constraints, your program should accomplish the following tasks:

      • Read each operand as a sequence of BCD numbers (e.g. integers between 0 and 9) and pack them into a 32-bit register.
      • Once the operands have been stored in two 32-bit registers, unpack the BCD sequences and add each digit properly. Then, pack the result back into a 32-bit register.
      • Unpack and print the BCD digits contained inside a 32-bit register.

      Example: The numbers 157 and 102 do not fit in 4-bit registers, so, we require a different approach for data representation, for the purposes of this lab it will be Binary-Coded Decimal (BCD).

      This encoding method assigns four bits to each decimal digit. Thus, any integer with several decimal digits has to be represented by concatenating 4-bit sequences, for example:

      (1 5 7)10 = (0001 0101 0111)BCD and (1 0 2)10 =(0001 0000 0010)BCD.

      (Note that extra spaces where added for readability.)

      Our hypothetical machine has 4-bit registers only, so, we require six registers to hold all the decimal digits, three for each operand, $a1, $a2, $a3, $b1, $b2 and $b3.

      $a1 -> 1 -> 0001 $b1 -> 1 -> 0001
      $a2 -> 5 -> 0101 $b2 -> 0 -> 0000
      $a3 -> 7 -> 0111 $b3 -> 2 -> 0010
      

      To perform the addition, we sum the corresponding registers (the ones with the same index.)

        $a1 $a2 $a3
      + $b1 $b2 $b3
      ---------------
        $c1 $c2 $c3 
      

      The 32-bit MIPS architecture allows storing all the 4-bit numbers ($a1, $a2, $a3) in just one register (the unused part to the left of the register should be set to 0). Thus, the previous computation would look (in binary) as:

       
        $s1 -> ... 0001 0101 0111 
      $s2 -> ... 0001 0000 0010
      ------------------------------- 
      $s3 -> ... 0010 0101 1001 
      

      Once data in $s3 is unpacked, we get the correct result: $c1=2, $c2=5 and $c3=9.

      Do not forget to handle the carry on from each BCD digit to the next.

      Very important: Because the actual MIPS register doing the encoding is 32-bit long (i. e., 8 BCD numbers), the maximum decimal value that can be stored is 108-1 (i.e., 99999999).

      Thus, if the sum produces a greater result (for example, 99999999 + 00000001), then overflow occurs and you should decide the best way to handle the situation and justify your decision.

    Questions

    1. Describe your technique to pack and unpack BCD's into 32-bit registers.
    2. How did you handle the carry out problem?
    3. How did you handle the overflow problem?

4. Additional information on bitmaps

    A bitmap is a binary representation of an image over a grid. If the image is black-and-white, each cell on the grid represents a bit: 0, if the image has white square at that specific point, 1 if the image has a black cell. For example, if the symbol "$" is placed over a 8 by 10 grid, we get this matrix:

          7 6 5 4 3 2 1 0
     ---|----------------
      1 | 0 0 0 1 0 0 0 0
      2 | 0 0 1 1 1 0 0 0
      3 | 0 1 0 1 0 1 0 0
      4 | 0 1 0 1 0 0 0 0
      5 | 0 0 1 1 0 0 0 0
      6 | 0 0 0 1 1 0 0 0
      7 | 0 0 0 1 0 1 0 0
      8 | 0 1 0 1 0 1 0 0
      9 | 0 0 1 1 1 0 0 0
     10 | 0 0 0 1 0 0 0 0
    

    Having this matrix, we just need to compute the decimal value of each row (by adding the power of 2 corresponding to the columns with 1).

          7 6 5 4 3 2 1 0     
     ---|----------------     
      1 | 0 0 0 1 0 0 0 0 = 16
      2 | 0 0 1 1 1 0 0 0 = 56
      3 | 0 1 0 1 0 1 0 0 = 84
      4 | 0 1 0 1 0 0 0 0 = 80
      5 | 0 0 1 1 0 0 0 0 = 48
      6 | 0 0 0 1 1 0 0 0 = 24
      7 | 0 0 0 1 0 1 0 0 = 20
      8 | 0 1 0 1 0 1 0 0 = 84
      9 | 0 0 1 1 1 0 0 0 = 56
     10 | 0 0 0 1 0 0 0 0 = 16
    
    
    As exercise, consider the following shapes:

    ...M...        MMMMM.
    ...M...        .M...M
    ..M.M..        .M...M
    ..M.M..        .MMMM.
    ..M.M..        .M...M
    .MMMMM.        .M...M
    .M...M.        .M...M
    MMM.MMM        MMMMM.
    

5. Additional information on BCD

    BCD (Binary-Coded decimal) is a technique to represent decimal numbers with small sets of binary digits. Under BCD, each decimal digit (0, 1, 2, ..., 9) is represented with 4-bit sequences (i.e., 0000, 0001, ... 1001).

    To translate a decimal number to BCD, just replace each digit in the original number by the corresponding BCD sequence. For example, (2 9 3 5)10 is equivalent to (0010 1001 0011 0101)BCD. (Please note that spaces in betwen the bit groups were left to remark that BCD sequences are used.)

    To translate from BCD to decimal, just group the bits in sets of four, from right to left, and then replace each set by its corresponding decimal digit. For example, (0111 0001 0110 0100)BCD is equivalent to (7 1 6 4)10.

    The Decimal-BCD equivalence table is shown next.

    Decimal   BCD
       0      0000
       1      0001
       2      0010
       3      0011
       4      0100
       5      0101
       6      0110
       7      0111
       8      1000
       9      1001
    
    Note: Although a 4-bit group enables up to sixteen combinations, the BCD convention does not define any value beyond 1001.



Any concerns about the goals, questions, or wording of this document, please send a message to Jorge Vasconcelos.
Return to the CSF Lab homepage