sippycup
sippycup

Reputation: 145

Undesired loop in assembly

I've been messing with this code for awhile now and I can't seem to find out what I'm doing wrong. I'm trying to get 2 different triangles to print based on a users input and so far I have only gotten the first triangle to print correctly.

For instance, If a user enters 4 the output should be like so:

*
* *
* * *
* * * *

* * * *
* * *
* *
*

My code produces the first triangle correctly but for the second it adds one and outputs like this

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

My question is how do I get the correct output

org 100h


.data
Input db "Enter size of the triangle between 2 to 9: $" ;String to prompt the user
Size dw ?               ; variable to hold size of triangle


.code
Main proc
Start:
Mov ah, 09h
Mov dx, offset input ;prompts user for input
int 21h

mov ah, 01h
int 21h; takes user input

sub al, '0'

mov ah, 0   ;blank top half of ax reigster

mov size, ax ; we use ax instead of al because we used dw instead of db
mov cx, ax ; copy size into variable size and cx reigster     

mov bx, 1                    

call newline


lines:                 ; outer loop for number of lines
push cx
mov cx,bx





stars:                 ; inner loop to print stars

mov ah, 02h   
mov dl, '*'
int 21h


loop stars

inc bx

call newline
pop cx


loop lines
call newline  




; second triangle   

mov cx, size 


lines2:  

push cx
mov cx,bx





stars2:
mov ah, 02h
mov dl, '*'
int 21h

loop stars2

 dec bx

 call newline
 pop cx
loop lines2 
;end 



call newline
mov cx, size
; third triangle   
mov cx, size

lines3:
push cx
mov cx,bx

main endp   


proc newline
mov ah, 02h        ; go to a new line after input
mov dl, 13
int 21h
mov dl, 10
int 21h

ret ;returns back


newline endp

end main

Upvotes: 1

Views: 45

Answers (2)

kay27
kay27

Reputation: 909

Move dec bx from line after loop stars2 to next line after lines2:.

Upvotes: 1

After the first triangle, BX is one number above the desired, so, just above the lines2: label add the line dec bx :

.
.
.
; second triangle   

mov cx, size 
dec bx         ;<========================

lines2:  

push cx
mov cx,bx
.
.
.

Upvotes: 1

Related Questions