“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code.

Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers.



Task

Show how two loops may be nested within each other, with the number of iterations performed by the inner for loop being controlled by the outer for loop.

Specifically print out the following pattern by using one for loop nested in another:

*
**
***
****
*****


Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges




Reference

 For loop Wikipedia.


org &4000		; put code at memory address 0x4000
wr_char equ &bb5a ; write ASCII character in register A to screen
				; (jumps into CPC ROM)
 
; put registers on stack so we can return to BASIC later
push bc
push de
push hl
 
ld b,5		; loop from 5 to 1
 
row:
 
push bc	; save outer loop variable
 
; calculate inner loop limit (6 - outer loop variable)
ld a,6
sub b
ld b,a
 
column:
 
ld a,42	; asterisk in ASCII
call wr_char
djnz column ; decrement B, jump to label if non-zero
 
pop bc	; restore outer loop
 
; print carriage return/line feed
ld a,13
call wr_char
ld a,10
call wr_char
 
djnz row
 
; restore registers
pop hl
pop de
pop bc
ret	; return to BASIC interpreter