; Timer functions - Prints how many times we loop for every clock tic. Runs 18 ticks
;                   which is about a second
IDEAL
MODEL small
STACK 20h
DATASEG
clock         equ  es:6ch    ; we use the extended segment register to hold seg 40h
buffer        db  10 dup(?)   

CODESEG
 
proc          numprt
              push bp
              mov  bp,sp 
; save registers used in proc, to be restored upon return to the main program
              push ax
              push dx
              push si
;------------------------------------------------------------------------------- 
              mov [buffer+9],'$'
              lea si,[buffer+9]
              mov ax,[bp+4]   ; number (parameter) to print
              mov bx,10         
asc2:
              mov dx,0            ; clear dx prior to dividing dx:ax by bx
              div bx               ;DIV AX/10
              add dx,48            ;ADD 48 TO REMAINDER TO GET ASCII CHARACTER OF NUMBER 
              dec si              ; store characters in reverse order
              mov [si],dl
              cmp ax,0            
              jz  extt             ;IF AX=0, END OF THE PROCEDURE
              jmp asc2            ;ELSE REPEAT
extt:
              push dx
			  mov  dl,10             ;new line
			  mov  ah,2h
			  int  21h
			  pop  dx 
              mov  ah,9            ; print string
              mov  dx,si
              int  21h
; restore registers to the way they were before calling the proc, upon return.
			  pop  si
			  pop  dx
			  pop  ax
;------------------------------------------------------------------------------- 
              pop bp
              ret 2
endp          numprt               
 
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               
              mov  cx,18             ; 18X0.055 = 0.99 (close to one second) for loop1
loop1:        mov  ax,[clock]
              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 
              push si
              call numprt
              loop loop1       
			  mov  dl,10             ;new line
			  mov  ah,2h
			  int  21h                                                 
              mov  dl,'1'
              mov  ah,2h             ;display what's in dl
              int  21h                                                      
exit:         mov  ax, 4c00h
              int  21h
END start   