简体   繁体   English

程序集 8086 中的嵌套循环(

[英]Nested loop in Assembly 8086 (

; ; ( iam facing problem in printing this series 122222223222) can you plz look on code and point my mistake ) (我在打印这个系列时遇到问题 122222223222)你能不能看看代码并指出我的错误)

.model small 
.stack 100h 
.code 
start:
mov cx,3

l1:

mov ah, 02
mov dl, 49
int 21h 
inc dl

mov bx,cx
mov cx, 3

l2:

mov ah, 02
mov dl,50
int 21h 

loop l2

mov cx,bx
inc dl
loop l1

mov ah, 4ch
int 21h 

end start

Your program is almost OK.你的程序几乎没问题。 The output currently is "122212221222".当前的输出是“122212221222”。

That "1" character that keeps repeating, comes from the fact that you re-use the immediate value 49 within the loop where you should have kept this number outside the loop! “1”字符不断重复,来自于一个事实,你的循环,你应该保持循环这个数字之内重复使用的立即值49!
I suggest you store it in the DH register, retrieve it from there and increment DH on each iteration:我建议您将它存储在DH寄存器中,从那里检索它并在每次迭代时增加DH

    mov  dh, "1"   ; ASCII=49
    mov  cx, 3
outerLoop:
    mov  ah, 02h
    mov  dl, dh    ; Successively "1", "2", "3"
    inc  dh
    int  21h

    mov  bx, 3
innerLoop:
    mov  ah, 02h
    mov  dl, "2"   ; ASCII=50
    int  21h 
    dec  bx
    jnz  innerLoop

    dec  cx
    jnz  outerLoop

Please notice that instead of using BX to preserve the outer loop counter, you can just as easily use BX as the counter for the inner loop.请注意,您可以像使用BX一样轻松地将BX用作内循环的计数器,而不是使用BX来保留外循环计数器。

For readability you can:为了可读性,您可以:

  • use more descriptive labels使用更多描述性标签
  • insert blank lines to make the innner loop stand out插入空行使内循环突出
  • write functional comments编写功能注释

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

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