Adding to a byte again and again makes it wrap round: 200 + 100 = 300 leaves 44 and sets the carry flag. ADC adds a number and the carry flag, so it can collect carries without any jump.
What to build
Read a digit n. Add 100 to a byte register, n times, and print how many of those additions produced a carry.
Requirements
- Start with the byte at 0.
- Repeat
ntimes: ADD 100 to the byte, and add the resulting carry flag to a counter with ADC. - Print the counter as one digit.
Input
One digit n, then Enter.
Output
One digit: the number of carries.
Constraints
0 ≤ n ≤ 9.
Example
Input:
9
Output:
3
Why it works
The byte goes 100, 200, 44 (a carry), 144, 244, 88 (a carry), 188, 32 (a carry), 132. Each ADD can carry at most once, because 100 is less than 256, and a carry is produced each time the total passes a multiple of 256. So the answer for n is the whole number of 256s in 100 × n.
Examples
9
3
0
0
Hints
Hint 1
Keep the total in AL (starting at 0) and a counter of carries in DL (starting at 0).
Hint 2
ADD AL, 100 sets CF when the total wraps past 255. Then ADC DL, 0 adds that carry (0 or 1) to the counter, with no jump.
Hint 3
ADC does not change AL, so it can follow the ADD at once. Print the counter as a digit.
Solution
Show one correct solution (try it yourself first)
; Count the carries
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 1
INT 21H
SUB AL, '0'
MOV CL, AL
MOV CH, 0 ; CX = n
MOV AL, 0 ; the byte
MOV DL, 0 ; the number of carries
JCXZ SHOW
AGAIN:
ADD AL, 100 ; CF = 1 when the byte wraps
ADC DL, 0 ; count the carry
LOOP AGAIN
SHOW:
ADD DL, '0'
MOV AH, 2
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
▶ Open this solution in the compiler, then press Visualize to watch it run step by step