Repeat: Difference between revisions

1,246 bytes added ,  2 years ago
z80 assembly
(z80 assembly)
Line 1,969:
rep("myFunc", 4)</lang>
=={{header|Z80 Assembly}}==
This requires the use of a technique known as the "Return Trick," it's efficient but makes the program more difficult to read. It works on the principle that the processor's RET command assumes the return address is the top item of the stack. The programmer can abuse this to "return" to a section of code that has never actually been executed. This is essentially just another form of the computed goto. Most processors that use the stack to store return addresses can use this technique, though the method of doing it depends on the processor itself.
 
<lang z80>
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
or a
ret
</lang>
 
 
=={{header|zkl}}==
1,489

edits