Pyramid of Letters
With this code, based on the number cx, loops would generate a pyramid of letters.
The soluttion is to have multiple loops. Now the problem with this is that the CX is the only register used to count how many loops. Since we have two loops, we need to store first the CX to another register then store it back again to the CX when doing the outer loop since when doing the inner loop we need to be able to have a new value for CX without loosing the old value of CX.
The data segment:
msg1 db 0ah, 0dh, '$'
ctr1 dw 1
ctr2 dw ?
tem db ?
let db 'A'
The code segment:
mov cx, 5 ; assign the first value of CX to
; a specific number to determine
; the level of the pyramid in this
; situation, we give the pyramid
; with level 5
a: mov ctr2, cx ; store cx to a memory
; alocation
mov cx, ctr1 ; give a new value for cx
; for the inner loop
mov bh, let ; assign the letter to be
; displayed starting from 'A'
mov tem, bh
b: mov ah, 02h
int 21h ; print the character inside tem
inc tem ; increment tem to proceed
; to the next letter
loop b
mov ah, 09h
lea dx, msg1 ; print the new line stored at msg1
int 21h
inc ctr1 ; increment the ctr1 to increase
; the next level
mov cx, ctr2 ; give cx its old value to proceed
; with the outer loop.
loop a ; repeat the process with the old counter
0 comments:
Post a Comment