Tuesday, November 4, 2008

Displaying asterisk triangles

This program displays to right triangles that are connected at one point. It uses a looping principle like the other asterisk triangle .
Source Code:

jmp start

mess1 db 10,13, "Enter value for (n) [0-9 only]: $"
mess2 db 10,13, "There is nothing to display $"
mess3 db 10,13, "Do you want to continue (y/n): $"
lincar db 10,13, "$"
space db " $"
asterisk db "*$"
value db 0

start:

xor ax,ax
xor bx,bx
xor cx,cx

mov ax,3
int 10h

lea dx,mess1
mov ah,9
int 21h

mov ah,1
int 21h
sub al,30h

cmp al,0
je zero_bridge
cmp al,0
jl start
cmp al,9
jg start
jmp main

zero_bridge:
call zero
main:
mov value,al
mov bh,al
mov ch,bh
mov cl,al
again:

cmp bh,0
je secondhalf

lea dx,lincar
mov ah,9
int 21h

mov al,value
sub cl,al
back:

cmp cl,0
je display

lea dx,space
mov ah,9
int 21h

lea dx,space
mov ah,9
int 21h

inc cl

jmp back

display:

lea dx,asterisk
mov ah,9
int 21h

lea dx,space
mov ah,9
int 21h

inc cl

cmp cl,ch
jne display

dec bh
dec ch
dec cl

jmp again

;----------------------------socondhalf----------------------------

secondhalf:

mov al,value
sub al,1
mov bh,al
mov ch,2

again2:

cmp bh,0
je continue

lea dx,lincar
mov ah,9
int 21h

mov bl,value
mov al,ch

sub al,bl

mov cl,al

back2:

cmp cl,0
je display2

lea dx,space
mov ah,9
int 21h

lea dx,space
mov ah,9
int 21h

inc cl

jmp back2
display2:

lea dx,asterisk
mov ah,9
int 21h

lea dx,space
mov ah,9
int 21h

inc cl

cmp cl,ch
jne display2

dec bh
inc ch

jmp again2
zero:
lea dx,mess2
mov ah,9
int 21h
continue:
xor dx,dx

lea dx,mess3
mov ah,9
int 21h

mov ah,1
int 21h

cmp al,'y'
je start_bridge
cmp al,'n'
je exit

jmp continue

start_bridge:
call start
exit:
int 20h

No comments: