2013-11-20 31 views
1

首先,是的,我知道它是21世纪的,但我正在学习DosBox和NASM的旧汇编。我必须从输入中获取数字并根据用户定义打印多个星号(*)。例如:使用用户的输入作为循环计数器

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

很简单吧?但我不知道如何使用累加值作为计数器。我当前的代码:

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

这条线:

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

+0

很好,谢谢。我尝试了一些与sub类似的东西,但我不知道如何处理ch。 – Radzikowski