The following notes refer to material from Chapter 2.
Masking:
As mentioned last week, the ALU has instructions to accomplish Boolean logic. We observed that the following XOR example essentially "flips" or complements the bits of one of the operands.
11010100 XOR 11111111 -------- 00101011
This is an example of a widely used technique in computing known as masking. You can think of masking as the action of using logical operations and a particular bit pattern (in the example above the pattern 11111111) to alter another bit pattern. The mask may be any bit pattern we choose. The idea is to so choose the mask that it allows you to obtain a desired effect.
Following are examples of masking using the AND, OR logical operators.
10101010 AND 00001111 -------- 00001010
Notice the resulting bit pattern. In general, AND'ing a bit pattern with any other bit pattern that we choose to be the mask will have the following effect:
10101010 OR 00001111 -------- 10101111
Notice the resulting bit pattern. In general, OR'ing a bit pattern with any other bit pattern that we choose to be the mask will have the following effect:
Our hypothetical machine contains instructions for these logical operations in its instruction set. The following examples illustrate how the masking technique may be used for our machine.
Negating a number:
Our machine allows us to add integers that are represented in two's complement notation and so we can do subtraction. However, without an instruction to produce two's complement bit patterns the user of our machine would have to enter integers as two's complement bit patterns. As we saw in the XOR masking example above, we can easily produce a bit pattern that is the complement of another, and so, we may easily produce a short program to produce the two's complement of any given integer. To do this we will need the XOR instruction.
The op-code for the XOR instruction is 916. For example, the instruction 9145 XOR's the contents of R4 and R5 and places the result in R1.
The Negate a number program follows. Assume that memory location 6c contains the bit pattern to be negated. The result will be stored in memory location 6d.
Mem Prog Comment --- ---- ------------------------------------------- a0 136c Load R3 with contents of memory location 6c a2 24ff Load R4 with hex ff (i.e. 11111111) a4 2501 Load R5 with hex 01 (i.e. 00000001) a6 9034 XOR R3 and R4 -> R0 a8 5005 Add R0 and R5 -> R0 b0 306d Store contents of R0 to memory location 6d b2 c000 Halt
Note: Project #2 will require a similar set of instructions for #2 and #4.
Extracting bit pattern segments:
Consider the problem of converting a bit pattern which is in floating point notation to the original number. Also, consider the related problem of converting some number to its floating point format.
To address the first of these problems it is clear that we must somehow accomplish the following steps:
e.g.
Consider a floating point scheme where we have one sign bit, three exponent bits and four mantissa bits. The bit pattern 01101100, for example, is the floating point representation of +0.11*22. That is, the exponent is 110 and the mantissa is 1100. The following AND operation extracts the mantissa and exponent segments.
01101100 AND 00001111 -------- 00001100 01101100 AND 01110000 -------- 01100000
As illustrated in the example above, we may use masking for extraction of bit pattern segments. The masks for the mantissa and exponent are 00001111 and 01110000 respectively. So, we may easily write a program to accomplish extraction given the AND instruction. The op-code for the AND instruction is 816. For example, the instruction 8145 AND's the contents of R4 and R5 and places the result in R1.
The following program extracts the mantissa and then the exponent. Assume memory location 6c contains the bit pattern in floating point notation. The extracted segments will be stored in memory location 6d and 6e.
Mem Prog Comment --- ---- ------------------------------------------- a0 136c Load R3 with contents of memory location 6c a2 24ff Load R4 with hex 0f (i.e. 00001111) a4 8034 AND R3 and R4 -> R0 a6 306d Store contents of R0 to memory location 6d a8 24ff Load R4 with hex 70 (i.e. 01110000) b0 8034 AND R3 and R4 -> R0 b2 306d Store contents of R0 to memory location 6e b4 c000 Halt
Rotating:
The Rotation and Shift Operations section of 2.4 in our text has a nice general discussion of this idea. I will illustrate the idea using the Rotate instruction of our machine to complete the floating point problem above.
Given the extracted exponent segment 01100000 above, we would like to shift this bit pattern four bits to the right to make it more easily interpreted. Remember that although we understand that bits 2-4 (i.e. counting from the left) represents the exponent of our original bit pattern (in excess notation) our machine does not. We will need the ROTATE instruction to shift the pattern to the right. Remember that we must also convert it from excess notation to binary notation (or two's complement).
The op-code for the ROTATE instruction is a16. For example, the instruction a404 rotates the pattern in R4 4 bits to the right in a circular fashion (i.e. the bits that fall off the low order end are moved to the high order end). That is, the pattern 01100000 would become 00000110.
The following program rotates the exponent and then the mantissa. Remember that memory location 6d contains the extracted mantissa segment and 6e the extracted exponent segment.
Mem Prog Comment --- ---- ------------------------------------------- a0 146e Load R4 with contents of memory location 6e a2 a404 Rotate contents of R4 4 bits a4 156d Load R5 with contents of memory location 6d a6 a502 Rotate contents of R5 2 bits a8 c000 Halt
Exercise:
Notice that the program above
is not sufficiently
general. You should be able to use the contents of R4 to
determine the number of bits to rotate the mantissa. How would you
modify this program to accomplish this.
Hint: First convert the contents of R4 from excess
notation to regular binary (to simplify your program,
assume that the exponent will always be positive) and then
use a loop.