使用用户的输入作为循环计数器

时间:2013-11-20 14:35:39

标签: assembly x86 nasm dos

首先,是的,我知道这是21世纪,但我正在学习DosBox和NASM的老装配工。我必须从输入中获取数字并按用户定义打印尽可能多的星号(*)。例如:

Enter the number (1-9): 5
*****

非常简单,对吧?但我不知道如何使用readed值作为计数器。我目前的代码:

org     100h

mov dx, number_txt
mov ah, 9
int 0x21

mov dx, buffer
mov ah, 0Ah        
int 0x21                    ; read number

mov dx, br
mov ah, 9
int 0x21                ; enter

mov dx, asterysk            ; will write "*"
mov ah, 9

mov cx, buffertxt           ; HERE IS THE PROBLEM
myloop:
    int 0x21            ; write *
loop myloop

mov ax, 0x4C00
int 0x21                ; end

number_txt: db "Enter the number (1-9): $"
br: db 0Ah,0Dh,"$"
asterysk: db "*$"
buffer: db 5,0
buffertxt: times 5 db "$"

目前我正在获得4.5行*,无论我写的是什么号码。我知道我必须将buffertxt转换为int / hex才能在cx中用作计数器,但我不知道如何。

我在谷歌和这里读了很多,但我找不到答案。

1 个答案:

答案 0 :(得分:1)

这一行:

mov cx, buffertxt           ; HERE IS THE PROBLEM

应更改为:

mov cl, [buffertxt]     ; Read the first character of the user input
sub cl,'0'              ; Convert from '0'..'9' to 0..9
xor ch,ch               ; Clear the upper half of CX (the LOOP instruction
                        ; uses all of CX as the counter)

顺便说一下,您可以使用INT 21H / AH=2打印单个字符,而不是将单字符字符串传递给INT 21H / AH=9

相关问题