Reputation: 11
I need to display 8 squares on the screen based on a 8-bit binary number. For example, 11111111 would print 8 squares and 10000000 would print just one. I have a subroutine to print each square that works. My problem is I am unsure how to iterate over the binary value to check for ones. If I was using another language like java or C I'd use a for loop to loop over the binary number, checking if each digit was 1, and if it is a one, call the method to print a square. How can I do this in assembly? The value used can come from either memory or a register, but I don't understand how I would go about checking where 1's are in the binary.
Upvotes: 1
Views: 380
Reputation: 39166
Use shl
(or shr
) to shift out one bit at a time into the carry. If the CF=1 then draw the square.
Next code only loops back if the mask has still at least one ON bit (minimizing the number of iterations):
mov al, [Mask]
shl al, 1
jnc .b
.a:
call DrawSquare
.b:
shl al, 1
jc .a
jnz .b
ps: Make sure DrawSquare does not clobber the AL
register. Maybe use a different register.
Upvotes: 3
Reputation: 17630
Try looping 8 times (assuming that you're using a byte value), testing each against an & mask of 0x01 to decide if to print the box, then shifting before going to the next iteration of the loop. Psudeocode below:
for 0..7
if value & 0x01 == 0x01: print box
value = value >> 1
Upvotes: 2