
;------------------------------------------------------------------------------------------
IDEAL
MODEL small
p186
STACK 0f500h


DATASEG
numstr          db '$$$$$'
	
;x db 10
;bg equ 9        ; screen background color
;color equ 4     ; lines color (that make the board)

;clock data
count dw 0
seconds db 99
location equ [bp+6]  ;parameters for proc 'printit'
dig equ [bp+4] 

CODESEG
;----------------------------------------------------------------------

;---------------------------------------------------------------------------      
; Convert number to a string of digits for print. Requires in DATASEG:
; numstr db '$$$$$'. Proc dollars fills numstr with $ signs and proc
; number2str receive a number in ax and create the string in numstr.

proc  dollars        ;No parms. Fills area in memory with "$" 
      push cx  
      push di              
      mov  cx, 5
      mov  di, offset numstr
dollars_loop: 
      mov  bl, '$'      
      mov  [ di ], bl
      inc  di
      loop dollars_loop
      pop  di
      pop  cx	
      ret
endp  dollars
;---------------------------------------------------------------------------
;----------------------------------------------------------------------
proc  SetGraphic
; Sets the video mode to graphics.
  mov ax,13h   ; 320 X 200 ;Mode 13h is an IBM VGA BIOS mode.  
  int 10h      ; It is the specific standard 256-color mode
  ret
endp SetGraphic
;----------------------------------------------------------------------

;---------------------------------------------------------------------------
; number2string converts the number in ax into printable (ASCII) number
; The result is placed in memory location: numstr. For example a number
; like 435 will turn into: '343335' in ASCII code.
;
proc  number2string  
      push cx
      push bx
      push dx
      call dollars               ;prepare numstr with $ signs.
      mov  bx, 10                ;extracting a digit at a time
      mov  cx,0    
;extract digits from ax, by dividing to 10 and pushing the remainder to the stack.               
cycle1:       
      mov  dx,0                  ;to facilitate division       
      div  bx                    ;dx:ax / 10 =  ax - quotient  dx -remainder 
      push dx                    ;stack will allow fetching in the opposite order 
      inc  cx        
      cmp  ax,0                  ;done if number is zero
      jne  cycle1     
;Retrieve pushed digits (opposite order)
      mov  si, offset numstr
cycle2:       
      pop  dx           
      add  dl,48                 ;convert dec digit to ASCII - 48 is '0' in ASCII 
      mov  [si],dl
      inc  si
      loop cycle2  
      pop  dx
      pop  bx
      pop  cx
      ret
endp  number2string 
;---------------------------------------------------------------
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, 0Eh  ;Color is ?
      mov  bh, 0    ;Display page
      mov  ah, 0Eh  ;Teletype
      int  10h 
      pop  dx
      pop  bx
      pop  ax             
      pop  bp
      ret  4
endp  printit 
;----------------------------------------------------------------------
proc timeheading
; This proc only prints the header of the clock: TIME
   mov dh,6
   mov dl,18
   mov bh,0
   mov ah,02h
   int 10h
   mov al,'T'
   mov bl,0eh
   mov bh,0
   mov ah,0eh
   int 10h
   mov al,'I'
   mov bl,0eh
   mov bh,0
   mov ah,0eh
   int 10h
   mov al,'M'
   mov bl,0eh
   mov bh,0
   mov ah,0eh
   int 10h
    mov al,'E'
   mov bl,0eh
   mov bh,0
   mov ah,0eh
   int 10h
   ret
endp timeheading
;----------------------------------------------------------------------

;------------------------------------------------------------------------------------------------
; MAIN program
;-------------------------------------------------------------------------------------------------

start:
  mov ax, @data
  mov ds, ax
  call SetGraphic

timerloop: 
  mov  ax,1h
  int  33h  
  call timeheading  
  mov ah,2ch
  int 21h
  cmp dl, [seconds] ; dh for seconds
  je waitformouse
  jmp generateclock 
waitformouse:

  jmp  timerloop  

generateclock:
   mov [seconds],dl ; dh for seconds 
   mov si,offset numstr
   mov ax,[count]
   inc [count]
   call number2string
   mov dh,8
   mov dl,18
   mov bx,offset numstr
   xor di,di
loop10:
   push dx
   mov dl,[byte ptr bx+di]
   cmp dl,'$'
   jne prtsecdig
   jmp timerloop
prtsecdig:
    push dx 
    call printit
    inc di
    mov dh,8
    mov dl,18
	add dx,di
	jmp loop10

  mov ax, 4c00h
  int 21h	
END start