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.
2 Answers
3
votes
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.