[英]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:为了可读性,您可以:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.