简体   繁体   English

使用 8086 组件制作矩形星形

[英]making a rectangular star shape with 8086 assembly

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.我正在尝试使用 1 个循环制作一个带有 8086 组件的矩形星形,并使用 cmp 制作外部一个为 0 和 jne 代码如下。

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.但是,您编写了许多多余的int 21h指令,这些指令没有实际用途并且有害。 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.一个大问题是您正在使用非零值加载CH寄存器,因此,内部循环的第一次运行将绘制太多星号。

 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 . mov cx,[stars]指令不仅在CL中加载号,还在CH中加载数字 13。

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.代替dec [line]cmp [line], 0 ,您可以根据dec指令已经定义的零标志有条件地跳转( jnz outer )。


It's just as easy to not use LOOP at all (because later you'll have to un-learn it, really)根本不使用LOOP也很容易(因为以后你将不得不取消学习它,真的)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM