I've got a problem looping in assembly language.
When we want to use the counter register for looping in nested loop, what we first do is move the value of counter register into the stack for outer loop and then fetch it back when we're done with the inner loop, this way we're capable of using one counter register for looping into the nested loop with different number of iteration on each loop.
But what about nested loop inside a nested loop?
I want to print a pyramid made of of the character S
. What I am getting is,
SSSSSSSSSS
SSSSSSSSS
SSSSSSSS
SSSSSSS
SSSSSS
SSSSS
SSSS
SSS
SS
S
What I actually want is,
SSSSSSSSSS
SSSSSSSS
SSSSSS
SSSS
SS
S
Here is my code for the program
MOV BX,10 ; HOLD 10 IN BX FOR INNER LOOP
MOV AX,0 ; START ITERATIONS FROM 0
MOV CX,10 ; MAX NUMBER OF ITERATIONS
L2:
PUSH CX ;PUSH CX IN A STACK
MOV CX,BX ;STORE NEW VALUE IN CX FOR INNER LOOP ITERATION
L1:
MOV DX, [SI] ; MOVE THE STRING INTO DX
MOV AH,02H ; DISPLAY EVERYTHING FROM DX
INT 21H
LOOP L1
MOV DX,0AH ;PRINT LINE FEED AFTER PRINTING EACH LINE OF ASTERIKS
MOV AH,02H
INT 21H
SUB BX,01 ;DECREASE THE VALUE OF BX BY 1
POP CX ;RESTORE ORIGINAL VALUE OF CX FOR OUTER LOOP
ADD AX,01 ;INCREMENT VALUE OF AX BY 1
LOOP L2
MOV AH, 4CH ;RETURN CONTROL TO DOS
INT 21H
In order to achieve what I want, i need to add another loop inside the nested loop that prints space characters (i.e 020H). But for this I need another counter register and I am not able to do it. How can I solve this problem?
You are already doing what needs to be done in the given ASM. You can push the current value of CX to the stack (save it) and pop it later on to restore it. You would need to do this when you require additional nesting.
In the code JohnB provided he simply added in a loop to print out the spaces before printing the asterisks. No additional nesting is required, meaning it is fairly straight forward.
It's a bit like this:
For each line
Print an incrementing number of spaces
Print a decrementing number of asterisks
Repeat
Which is exactly what JohnB has shown you.