; stopw - stop watch. This is an example that display 10 seconds (0-9) from
;         zero to 9, at row 5 (out of 24) and column 10 (out of 40).
;
;
IDEAL
MODEL small
STACK 20h
DATASEG
clock         equ  es:6ch    ; we use the extended segment register to hold seg 40h
buffer        db  10 dup(?)   
listsec       db  '0123456789$'
counter       db  ? 
location      equ [bp+6]
dig           equ [bp+4]

CODESEG
proc          printit
; Gets 2 parameters the number of single digit to be displayed (dig)
; and the location on the screen: location (first byte row, second column)
; Displays the digit at the row/column of the second parameter. 
              push bp
              mov  bp,sp
              push ax
              push bx
              push dx     
              mov  dx,location
              mov  bh, 0    ;Display page
              mov  ah, 02h  ;SetCursorPosition
              int  10h
              mov  ax, dig
              mov  bl, 0Ch  ;Color is red
              mov  bh, 0    ;Display page
              mov  ah, 0Eh  ;Teletype
              int  10h 
              pop  dx
              pop  bx
              pop  ax             
              pop  bp
              ret  4
endp          printit        
;----------------------------------- MAIN -------------------------------------- 
start:        mov  ax, @data
              mov  ds, ax
              mov  ax,40h
              mov  es,ax 
              mov  ax,[clock]        ;take first clock reading & loop until it changes
nochange:     cmp  ax,[clock]
              je   nochange                                                            
              mov  dl,'0'            ;display the zero after the first change.
              mov  ah,2h             ;from here we'll start measuring the one second. 
              int  21h               
;graphics mode
              mov  ax,13h
              int  10h
                           
              mov  bx, offset listsec
              xor  di,di
;------------------------------------------------------ start of 10 second loop.                                          
              mov  [counter],10      ;loop 10 seconds       
loop0:        cmp  [counter],0  
              je   gotextmode            
              mov  cx,18             ; 18X0.055 = 0.99 (close to one second) for loop1                                                     
loop1:        mov  ax,[clock]        ; A one second loop.
              xor  si,si 
clocktick:    inc  si
              cmp  ax,[clock]        ; This is much faster then a clock tick, so we need 
              je   clocktick         ; to loop quite a bit for one tick              
              loop loop1  
              xor  dx,dx
              mov  dh,5              ; line
              mov  dl,10             ; column
              push dx
              xor  dh,dh
              mov  dl,[byte ptr bx+di] ;digit to be displayed
              push dx
              call printit
              inc  di
              dec  [counter]
              jmp  loop0               
;---------------------------------------------------- end of 10 second loop                  
			  mov  dl,10             ;new line
			  mov  ah,2h
			  int  21h                                                 
              mov  dl,'1'
              mov  ah,2h             ;display what's in dl
              int  21h   
gotextmode:
              mov  ah,0
              mov  al,2
              int  10h                                                   
exit:         mov  ax, 4c00h
              int  21h
END start   