2016-04-26 27 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