Maciej Radzikowski
Maciej Radzikowski

Reputation: 2477

Use user's input as loop counter

First of all, yes, I know it's 21st century, but I'm learning old assembler with DosBox and NASM in my college. I have to get number from input and print as many asterisks (*) as user defined. For example:

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

Pretty simple, right? But I don't know how to use readed value as counter. My current code:

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 "$"

Currently I'm getting 4.5 lines of *, no matter what number I write. I know I have to convert buffertxt to int/hex to use as counter in cx, but I have no idea how.

I've read a lot in Google and here, but I didn't find answer.

Upvotes: 1

Views: 2964

Answers (1)

Michael
Michael

Reputation: 58437

This line:

mov cx, buffertxt           ; HERE IS THE PROBLEM

should be changed to:

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)

By the way, you can use INT 21H / AH=2 to print a single character, instead of passing a single-character string to INT 21H / AH=9.

Upvotes: 1

Related Questions