RosettaCodeData/Task/Repeat/Z80-Assembly/repeat-1.z80

27 lines
769 B
Z80 Assembly

ld b,&05 ;load the decrement value into b
ld hl,myFunc ;load the address of "myFunc" into HL
call repeatProcedure
forever:
jp forever ;trap the program counter here
repeatProcedure: ;input: b = times to repeat, hl = which procedure to repeat
call trampoline
; the "ret" in myFunc will bring you here
djnz repeatProcedure
ret ;exit "repeatProcedure" and proceed to "forever"
trampoline:
push hl
ret
;this is effectively a call to whatever is in HL, in this case "myFunc." The "ret" at the end of myFunc will return us to
;just after the line "call trampoline"
myFunc: ;this doesn't do anything useful but that's not the point
push hl ;not needed for this routine but if it altered HL we would need this so that we come back here next time we loop
or a
pop hl
ret