I'm trying to make a rectangular star shape with 8086 assembly using 1 loop and making the outer one with cmp to 0 and jne the code is below.
Here's my code:
.model small
.stack 100h
.data
ml db 'how many lines you want',13,10,'$'
ms db 'how many stars in each line',13,10,'$'
lines db 0
stars db 0
nl db 13,10,'$'
.code
Main Proc
mov ax, @data
mov ds, ax
mov dx, OFFSet ml
mov ah,9h
int 21h
mov ah,1h
int 21h
sub al,'0'
mov [line],al
int 21h
xor ah,ah
mov dx, OFFSet ms
mov ah,9h
int 21h
mov ah,1h
int 21h
sub al,'0'
mov [stars],al
int 21h
mov cx,[stars]
int 21h
outer:
dec [line]
mov cl,[stars]
int 21h
inner:
mov dl, '*'
mov ah, 2h
int 21h
loop inner
mov dx, OFFSet nl
mov ah,9h
int 21h
cmp [line],0
jne outer
MOV AH,4CH
INT 21H
Main ENDP
END Main
a recktagular star shape
It is true that this wording is confusing, but the code clearly wants to draw a rectangle made out of asterisks.
The good news is that your nested loops are rather fine. However you have written lots of redundant int 21h
instructions that serve no real purpose and are harmful. To be removed.
A big problem is that you are loading the CH
register with a non-zero value and as a consequence, the first run of the inner loop will draw too many stars.
stars db 0 nl db 13,10,'$'
The mov cx,[stars]
instruction not only loads stars in CL
but also it loads the number 13 in CH
.
This is a cleaned version of your code:
mov dx, OFFSet ml
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, '0'
mov [line], al
mov dx, OFFSet ms
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, '0'
mov [stars], al
outer:
mov cl, [stars]
mov ch, 0 <<<<< Because LOOP uses CX
inner:
mov dl, '*'
mov ah, 02h
int 21h
loop inner
mov dx, OFFSet nl
mov ah, 09h
int 21h
dec [line]
jnz outer
Instead of dec [line]
and cmp [line], 0
, you can conditionally jump ( jnz outer
) based on the Zero Flag that is already defined by the dec
instruction.
It's just as easy to not use LOOP
at all (because later you'll have to un-learn it, really)
outer:
mov cl, [stars]
inner:
mov dl, '*'
mov ah, 02h
int 21h
dec cl <<<<<<
jnz inner <<<<<<
mov dx, OFFSet nl
mov ah, 09h
int 21h
dec [line]
jnz outer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.