Looping in Z80 assembly language using 8 bit and 16 bit counters.
Facebook Page (public):
/ spriteworx
Patreon:
/ spriteworx
Private facebook group for Patreon subscribers:
/ spriteworx
Games & Digital Content (as it becomes available):
https://spriteworx.itch.io/
=========================
8 Bit Loop Counter Program
org 30000
MAIN ;Main program loop.
LD HL, Counter ;Get address of loop count verifier.
LD BC, 0 ;Initialize BC to zero (B to be used as loop counter).
LD B, 10 ;Initialize loop counter to 10.
Loop ;Start of loop.
INC (HL) ;Increment loop count verifier.
DJNZ Loop ;Decrement loop counter.
CALL SUB1 ;[Print loop count verifier]
RET ;Return to BASIC.
;******** Subroutines ************
SUB1 ;Print loop count verifier.
LD A, 2 ;Set output channel value to select top of screen.
CALL ROM1 ;[Activate output channel]
LD BC, (Counter) ;Get value of loop counter verifier.
CALL ROM2 ;[Print value in BC (loop counter verifier)].
RET ;Return from SUB1
;***** ROM Routines ******
ROM1 EQU 5633 ;Activate output channel per A.
ROM2 EQU 6683 ;Print value in BC (9999 max).
;***** Data ********
Counter DW 0 ;Initialize loop count verifier to zero (8 bit).
;*** end ***
16 Bit Loop Counter program:
org 33000
MIAN ;Main program loop.
LD BC, 0 ;Initialize loop counter verifier.
LD HL,300 ;Initialize loop counter.
Loop ;Start of loop.
INC BC ;Increment loop counter verifier.
DEC HL ;Decrement loop counter (does not affect any flags).
LD A, H ;Copy H to A for comparison.
OR L ;Check if H and L are both zero.
JR NZ, Loop ; Jump if HL is not zero (loop not finished).
CALL SUB1 ;[Print loop count verifier]
RET ;Return to BASIC.
;****** Subroutines ********
SUB1 ;Print loop count verifier.
LD A, 2 ;Set output channel value to select top of screen.
PUSH BC ;Save BC value onto stack.
CALL ROM1 ;[Activate output channel]
;LD BC, ;Get value of loop counter verifier.
POP BC ;Retreive BC value from stack.
CALL ROM2 ;Print contents of BC (loop counter verifier).
RET ;Return from SUB1
;**** ROM Routines ******
ROM1 EQU 5633 ;Activate output channel per A.
ROM2 EQU 6683 ;Print value in BC (9999 max).
;*** end ***
Watch video #34: Looping in Z80 Assembly (Sinclair ZX Spectrum) online without registration, duration hours minute second in high quality. This video was added by user Spriteworx 02 January 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 697 once and liked it 37 people.