(C)stachan 08.02.2024

YASP :: How to loop

; Beipielcode für eine Schleife
; Denken Sie an die FOR-Schleife in Java: for(i=0;i<=3;i++)

main:			; muss am Beginn stehen
	mov B0, 0	; entspricht i=0, Counter
	mov B1, 3	; entspricht i<=3
loop:			; hier beginnt die Schleife, das Label dient als Sprungmarke
	Pause 60000	; an dieser Stelle stehen alle Befehle, die während eines
                        ; Schleifendurchlaufs ausgeführt werden sollen, kann auch 
                        ; mehrzeilig sein!!!
	inc B0		; entspricht i++, Counter wird inkrementiert
	debug B0	; optional: Ausgabe im Debugger
	cmp B0, B1	; compare: der Inhalt der Register B0 und B1 wird verglichen
	JZ foo		; jump if zero: wenn der Unterschied gleich 0 ist, verlasse
                        ; die Schleife und springe zu foo
	jmp loop	; jump: sonst springe zu loop (neuer Durchlauf) 
foo:
	pause 60000	; Programmcode nach Bedarf, hier nur eine Pause
	jmp main	; springe zu main
END			; muss am Ende stehen

; Um die Schleife leichter zu verstehen, starten sie den Debugger und gehen Sie 
; das Programm Step by Step durch.
; Im Tab "Registers" können Sie die Inhalte der benutzen Register kontrollieren.

; Example code for a loop
; Remember the FOR loop in Java: for(i=0;i<=3;i++)

main: 			; must be at the beginning
	MOV B0, 0 	; corresponds to i=0, counter
	MOV B1, 3 	; corresponds to i<=3
loop: 			; here the loop begins, the label serves as a jump marker
	PAUSE 60000 	; at this point are all commands that are to be executed
                        ; during a loop pass, can also be multiline!
	INC B0 		; corresponds to i++, counter is incremented
	DEBUG B0 	; optional: output in debugger
	CMP B0, B1 	; compare: the contents of registers B0 and B1 are compared
	JZ foo 		; jump if zero: if the difference is 0, leave the loop and
                        ; jump to foo
	JMP loop 	; jump: otherwise jump to loop (new pass) 
foo:
	PAUSE 60000 	; program code as needed, here only a pause
	JMP main 	; jump to main
END 			; must be at the end

; To understand the loop, start the debugger and go through the program step by step.
; In the tab "Registers" you can control the contents of the used registers.