Assembly 打印出一定数量的'#';取决于数组中的值
当我试图打印“#”时,我的程序陷入无限循环。其工作原理是用户将输入2、5、8,并打印:Assembly 打印出一定数量的'#';取决于数组中的值,assembly,x86-16,dosbox,Assembly,X86 16,Dosbox,当我试图打印“#”时,我的程序陷入无限循环。其工作原理是用户将输入2、5、8,并打印: Each # represents 1 ## ##### ######## 但如果最大值超过80,例如,输入为82、10、2,则输出为: Each # represents 2 ######################################### ##### # 这是分解的部分: include pcmac.inc .MODEL SMALL
Each # represents 1
##
#####
########
但如果最大值超过80,例如,输入为82、10、2,则输出为:
Each # represents 2
#########################################
#####
#
这是分解的部分:
include pcmac.inc
.MODEL SMALL
.586
.STACK 100h
.DATA
Val DW 6
Count DW 3
Array DW 2, 3, 6
DisplayCH DB 13, 10, 'Each # respresents ' , '$'
.CODE
extrn GetDec:near
extrn PutDec:near
PrintChar Proc
_begin
mov ax, @data
mov ds, ax
mov cx, 3
sub si, si
lea bx, Array
mov bx, offset Array
jcxz Ended
mov cx, 3
For1: mov dx, [bx +si]
mov Count, cx
add si, 2
_Putstr tested
While1: mov ax, 0
cmp ax, dx
je EndWhile1
mov Val, dx
_PutCh '#'
mov dx, Val
dec dx
;inc ax
jmp While1
EndWhile1:
;add si, 2
mov cx, Count
dec cx
jnz For1
Ended: _exit 0
PrintChar endp
end PrintChar
对于初学者来说,
Array
应该是DW
而不是DB
。您做了哪些工作来调试它,并显示在何处划分和测试代码段。你的算法在编码之前是什么,等等(理想情况下是用高级语言原型化并测试,然后移植到汇编语言)。如果你认为错误在于打印,你应该举一个最小的例子,只打印恒定数量的字符,看看是否行得通。您不需要输入、最大值和内容来测试打印。在您当前的版本中,For1
使用3重新加载cx
,因此这确实是一个无休止的循环。这个标签应该在mov-dx[bx+si]
行上。所以问题中的代码不再有问题,也与文本不匹配了?现在这里没有真正的问题了。不要编辑问题使其成为非问题。要么删除它,要么将答案作为答案发布,而不是对问题进行编辑。很高兴看到你有一个有效的解决方案。然而,你的答案应该用你自己的话解释这个版本是如何解决最初的问题的,而不仅仅是显示最终的代码(并且没有嵌入注释)
include pcmac.inc
.MODEL SMALL
.586
.STACK 100h
.DATA
Val DW 6
Count DW 3
Array DW 1, 2, 80
DisplayCH DB 13, 10, 'Each # respresents ' , '$'
Tested DB 13, 10, 'Test' , '$'
.CODE
extrn GetDec:near
extrn PutDec:near
PrintChar Proc
_begin
mov ax, @data
mov ds, ax
mov cx, 3
sub si, si
lea bx, Array
mov bx, offset Array
_PutStr DisplayCH
jcxz Ended
mov cx, 3
For1: _PutCh 13,10
mov dx, [bx +si]
mov Count, cx
While1: mov ax, 0
cmp ax, dx
je EndWhile1
mov Val, dx
_PutCh '#'
mov dx, Val
dec dx
jmp While1
EndWhile1:
add si, 2
mov cx, Count
dec cx
jnz For1
Ended: _exit 0
PrintChar endp
end PrintChar