; STARS.ASM
;
; Input a single digit (an ASCII char) and output that many asterisks.
; (Doesn't check for valid digits, so if you type in a non-digit you
; may get a lot more asterisks, depending on the ASCII code entered.)
;
; The comments in this file are intended to teach students about the
; meaning of various assembler instructions.  They are NOT suitable
; for students documenting their own code, since they do not refer to
; any process or problem being solved.
;
; Students: Do NOT copy these teacher-style comments into your own code.
; 
; Original program idea by: Alan Pinck
; -Ian! D. Allen - idallen@idallen.ca - www.idallen.com
;  November 1999, November 2009
;----------------------------------------------------------------------
; Both the Code Segment (CS) and Data Segment (DS) are the same.
Stars   segment
        assume  CS:Stars,DS:Stars
        org     100h            ; .COM requires this
;----------------------------------------------------------------------
start:
        ; Print a prompt
        ;
        mov     dx,offset msg   ; offset address of the message text
        mov     ah,09h          ; code for "display ASCII$ string"
        int     21h             ; Note: don't forget the 'h' on 21h!

        ; Get and echo one keyboard character input
        mov     ah,01h          ; code for "get+echo one key"
        int     21h             ; ASCII char is returned in AL
        sub     al,"0"          ; convert char from ASCII to decimal

        mov     count,al        ; store this value as our count

        ; WHILE-style loop printing stars until count goes to zero
        ;
loop:   mov     al,count        ; load current count
        cmp     al,0h           ; compare count against zero
        je      done            ; we're done if it's equal

        ; Output one star
        ;
        mov     dl,"*"          ; load the char we're going to print
        mov     ah,02h          ; code for "output one ASCII char"
        int     21h

        ; Decrement the count (there is a shorter way to do this)
        ;
        mov     al,count        ; update count by loading it,
        sub     al,1h           ; subtracting one, and
        mov     count,al        ; storing it back

        jmp     loop            ; go back up and see if we do more

        ; End the program
        ;
done:   mov     al,0h           ; successful return code is zero
        mov     ah,4Ch          ; code for "terminate program"
        int     21h
;----------------------------------------------------------------------
; Data and variables down here.
;
msg     db      "Please push a key: "   ; define bytes (db)
        db      "$"             ; marks end of ASCII$ string
count   db      0               ; one-byte (unsigned) counter
;----------------------------------------------------------------------
Stars   ends
        end start               ; "start" is the label on the first instruction

