Example: 00, 02, 04, …., 98
This is based on an assembly 8086. The program uses seven display segment (port 02) to display even numbers where MSB is Most Significant Byte and LSB is Least Significant Byte. [0|0] as shown here, though the issue is that my program enters an infinite loop and never reaches the end even when it should stop at 98.
JMP Start
; Segment patterns for digits 0–9
DB FB ; 0
DB 0B ; 1
DB B7 ; 2
DB 9F ; 3
DB 4F ; 4
DB DD ; 5
DB FD ; 6
DB 8B ; 7
DB FF ; 8
DB DF ; 9
DB DE
Start:
MOV AL, 00
OUT 02
MOV AL, 01
OUT 02
MOV BL, 03
MOV CL, 04
MOV AL, FA
OUT 02
Foo1:
MOV BL, 03
display_loop:
MOV AL, [BL]
ADD BL,2
OUT 02
CMP AL, FF
JZ Foo2
JNZ display_loop
Foo2:
MOV AL, [CL]
DEC AL
OUT 02
JMP Foo3
Foo3:
CMP AL, FF
INC CL
JZ Stop
JMP Foo1
Stop:
END
Why does my current code never stop at 98?
How do I correctly end the loop after displaying the number 98?
Is there a better way to organize MSB and LSB counting logic?
CMP AL, FF
INC CL
JZ Stop
JMP Foo1
If you're expecting this to jump to Stop
when AL = FF
, that may be your bug. INC
modifies the zero flag according to whether its result is zero, so when you get to JZ
, the zero flag no longer contains the result of CMP AL, FF
.
The simplest fix is to reverse the two instructions:
INC CL
CMP AL, FF
JZ Stop
JMP Foo1
There could be other bugs too. You should get familiar with using a debugger to single-step your program.