2016-04-26 117 views
0

到目前为止,我已经有了这个ASM无限循环

org 100h 


.data 
Input db "Enter size of the triangle between 2 to 9: $" 
Size dw ?    


.code 
Main proc 
Start: 
Mov ah, 09h 
Mov dx, offset input 
int 21h 

mov ah, 01h 
int 21h; 

sub al, '0' 

mov ah, 0 

mov size, ax 
mov cx, ax  

mov bx, 1      

call newline 


lines:     
push cx 
mov cx, bx 
lines2:     ; outer loop for number of lines 
push cx 
sub ax,bx 

stars:     

mov ah, 02h 
mov dl, '*' 
int 21h 


loop stars 

inc bx 

call newline 
pop cx 



loop lines 
loop lines2 
exit: 
mov ax, 4C00H 
int 21h  




main endp 


proc newline 
mov ah, 02h   
mov dl, 13 
int 21h 
mov dl, 10 
int 21h 

ret 


newline endp 

end main 

一切正常,并通过循环。举例来说,如果我进入3,我得到

* 
** 
*** 

,并在之后的程序停止但我试图让另一个循环,开始给我这样的事情:

* 
** 
*** 

*** 
** 
* 

,但我一直进入一个无限循环,我无法解决如何解决这个问题。有没有人对我在做什么错误有所了解?

+1

发布的代码是工作还是非工作?如果它是前者,发布非工作代码或者我们不能说出它有什么问题。如果你还可以缩进和评论它,那将是非常甜蜜的。 –

+0

如果用“mov cx,[size]”和“sub cx,bx”代替行中的“mov cx,bx”,您将以相反方式显示金字塔 – Tommylee2k

+0

我运行了代码并且工作良好,没有无限循环,恭喜! –

回答

0

您的循环正在将CX从[size]下降到0,所以对于金字塔增长,您需要显示([size] +1 - CX)星。对于一个收缩,它只是(CX)

我搬到了“打印出BX星”成一个子程序,这使得它更容易阅读

Start: 
    Mov ah, 09h    ; prompt 
    Mov dx, offset input 
    int 21h 

    mov ah, 01h    ; input size 
    int 21h 
    sub al, '0' 
    mov ah, 0 
    mov size, ax 
    mov cx, ax  
    mov bx, 1      
    call newline 

up:     
    mov bx, [size]  ; number of stars: 
    inc bx    ; [size]+1 
    sub bx, cx   ; -CX 
    call stars 
    loop up 

    call newline 

down:  
    mov cx,[size] 
d2:     
    mov bx, cx   ; number of stars: CX 
    call stars 
    loop d2 


exit: 
    mov ax, 4C00H 
    int 21h 

和子功能:

; display BX number of '*' followed by newline 
; uses CX internally, so it's saved and restored before ret 
proc stars 
    push cx 
    mov cx,bx 
s2: 
    mov ah, 02h 
    mov dl, '*' 
    int 21h 
    loop s2 

    call newline 
    pop cx 
    ret 
stars endp