ASM无限循环

时间:2016-04-26 06:27:11

标签: assembly

到目前为止,我已经有了这个

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 :(得分:0)

你的循环正在从[size]向下运行CX到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