Unit 5.4 - Going through a word, bit by bit
In Unit 2.4, we learned the basic operations for dealing
with words at a bit-level. we learned about the and, or
and exclusive or instructions that perform logical
operations bit by bit. We learned how to shift words to the
left or to the right. We learned how to combine these to
move an arbitrary field of bits from one location in one
word to another location in another word. Unfortunately,
all these operations, particularly the shift instructions,
are restricted to operating on a constant number of bits.
Thus, we learned how to copy bits 5 to 7 from A to bits 29
to 31 of B. However, we couldn't do something as simple as
turn on the Ith bit, where I is a memory location. In this
unit, we will learn how to perform such tasks. Likewise,
we couldn't inspect the Ith bit and determine whether the
bit is on, where I is a memory location containing an
integer telling us which bit we want.
In order to something like this, we have to go through the
word, bit by bit. We have a loop that shifts the word one
bit at a time. We can then inspect the bit by anding with a
fixed mask. And then, we can do whatever we want to do for
the specific bit.
The two templates below show you how to go through a word
from left to right and from right to left. That is we can
go from 0 to 31, processing the leftmost bit first and
proceeding up until the last bit. Or we can go from 31 to
0, processing the rightmost bit first.
We often refer to the leftmost bit as the most significant
as it corresponds to the highest power of two. Likewise, we
can refer to the rightmost bit as the least significant
since it corresponds to the smallest power of two.
Template to go through a word from left to right:
regA - counter from 0 to 31
regB - assumed to contain word to be gone through
regC - temporary
SR regA,regA
LOOPi EQU *
C regA,=f'32'
BE LOOPiE
LR regC,regB
N regC,=X'80000000' extract the left most bit
C regC,=f'0'
BE IFi
whatever code to do if bit is on
B FIi
IFi EQU *
whatever code to do if bit is off
FIi EQU *
SLL regB,1
A regA,=f'1'
B LOOPi
LOOP1E EQU *
Template to go through a word from right to left:
regA - counter from 31 to 0
regB - assumed to contain word to be gone through
regC - temporary
L regA,=f'31'
LOOPi EQU *
C regA,=f'0'
Bc 4,LOOPiE
LR regC,regB
N regC,=X'00000001' extract the left most bit
C regC,=f'0'
BE IFi
whatever code to do if bit is on
B FIi
IFi EQU *
whatever code to do if bit is off
FIi EQU *
SRL regB,1
S regA,=f'1'
B LOOPi
LOOPiE EQU *
Now, we inspect our first example.
It will examine WORD and will tell us whether the bit
indicated by BITN
is on or not.
Upon completion of the program, the memory location BITX
will contain one if the indicated bit is one. Otherwise, it
will be zero.
We use the template to go through a word from right to left
found on Page 101.
RegC in the template is register 2 in our program.
RegB in the template corresponds to register 3 in our
program.
And likewise, regA in the template goes to register 0 in our
program
"whatever code to do if bit is on" becomes:
if bit position (stored in REGA) = BITN
set BITX to one
ENDIF
You can see this code in lines 15 to 19 of the program.
"whatever code to do if bit is off" in the template becomes:
if bit position (stored in REGA) = BITN
set BITX to zero
ENDIF
You can see the assembler for this in lines 22 to 26
40.html