如果我打印换行,那么我的循环不会停止

时间:2017-02-16 20:31:11

标签: assembly x86 dos microprocessors emu8086

我想要打印星号和用户的输入值一样多,但是当我打印换行符时,我的循环不起作用,否则不打印换行符我的代码工作正常。 为什么会这样,我不明白。

.model small
.stack 100h
.data
   msg1 db "How many star do you want to print: $"
   newline db 10,13,"$"
.code
    main proc

    mov ax,@data
    mov ds,ax 

    mov ah,9
    lea dx,msg1
    int 21h

    mov ah,1   ;taking input number
    int 21h

    mov bl,al
    sub al,48

    ;if i add newline code here then my loop doesn't stop 

    ;   mov ah,9
    ;   mov ah,newline
    ;   int 21h

    loop:
    mov cx,0
    mov cl,al
    mov ah,2
    mov dl,'*'

    Top:
    int 21h
    loop Top


    Exit:   
    endp
    end main

1 个答案:

答案 0 :(得分:1)

指令loop使用注册cx计算并重复此过程,因此您可以将星号从al移至cl

mov ah,1   ;taking input number
int 21h

xor cx,cx     ;◄■■ CLEAR CX.
mov cl,al     ;◄■■ CX NOW HOLDS THE NUMBER OF ASTERISKS.
sub cl,48

;if i add newline code here then my loop doesn't stop 
   mov ah,9
   lea dx,newline     ;◄■■ UNCOMMENT LINE BREAK.
   int 21h

loop:
;mov cx,0      ;◄■■ HERE WE CANNOT CHANGE CX BECAUSE
;mov cl,al     ;◄■■ IT IS THE COUNTER FOR THE LOOP.
mov ah,2
mov dl,'*'

Top:
int 21h
loop Top     ;◄■■ CX--. IF CX>0 JUMP.