     1                                  ; ****************************************************************************
     2                                  ; counter.s (TRDOS 386, TRDOS v2.0 - sample binary file, 'counter.s'
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; COUNTER.PRG ! TEST program !
     5                                  ; TRDOS 386 timer interrupt & callback function test program (by Erdogan Tan)
     6                                  ;
     7                                  ; 19/12/2016
     8                                  ;
     9                                  ; [ Last Modification: 15/01/2017 ]
    10                                  ;
    11                                  ; ****************************************************************************
    12                                  
    13                                  [BITS 32]
    14 00000000 BE[E4000000]                    mov     esi, prg_msg
    15 00000005 E8C7000000              	call	print_msg
    16                                  
    17 0000000A E8BD000000              	call	getch
    18                                  
    19 0000000F 3C1B                    	cmp	al, 1Bh  ; ESC key
    20 00000011 7469                    	je	short terminate	
    21                                  
    22 00000013 66C705[4F010000]22-     	mov	word [color], 22h
    22 0000001B 00                 
    23                                  
    24                                  	; start timer function
    25                                  	;;mov	bl, 0
    26                                          ;;mov	bh, 84h	 ; Current Timer setup, Callback method 	
    27 0000001C 66BB0084                        mov	bx, 8400h
    28                                  	;mov	ecx, 1	 ; 1 tick 
    29                                  	;mov	bl, 0FFh
    30                                  	;mov	bh, 4
    31 00000020 B101                    	mov	cl, 1
    32 00000022 BA[88000000]            	mov	edx, t_callback ; timer callback service address
    33                                  
    34                                  	;mov	edx, t_event
    35 00000027 B821000000              	mov	eax, 33	; 'systimer'
    36 0000002C CD40                    	int	40h	; TRDOS 386 system call
    37 0000002E 724C                    	jc	terminate
    38                                  
    39 00000030 A2[51010000]            	mov	[timer_event_number], al 
    40                                  
    41 00000035 B400                    	mov 	ah, 0   ; set video mode
    42 00000037 B013                    	mov 	al, 13h ; 320x200 graphics mode (256 colors)
    43 00000039 CD31                    	int	31h     ; TRDOS 386 video interrupt
    44                                  			; (IBM PC/AT ROMBIOS, INT 10h) 
    45                                  pc_1:
    46 0000003B 30FF                    	xor	bh, bh
    47 0000003D 383D[52010000]          	cmp	[timer_event_status], bh ; 0
    48                                  	;cmp	byte [t_event], bh ; 0
    49 00000043 7617                    	jna	short pc_2
    50                                  
    51 00000045 883D[52010000]          	mov	[timer_event_status], bh ; 0
    52                                  	;mov	byte [t_event], bh ; 0
    53                                  
    54                                  	;call	t_callback
    55                                  
    56                                  	; Set cursor position
    57 0000004B 6631D2                  	xor	dx, dx  ; row 0, column 0
    58                                  	;xor	bh, bh  ; Video page 0
    59 0000004E B402                    	mov	ah, 2	; set cursor position
    60 00000050 CD31                    	int	31h 	; TRDOS 386 video interrupt
    61                                  
    62                                  	; Print Count
    63 00000052 BE[55010000]            	mov	esi, counter_str
    64 00000057 E875000000              	call	print_msg
    65                                  pc_2:
    66                                  	; Check keyboard buffer
    67 0000005C B411                    	mov	ah, 11h
    68 0000005E CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
    69                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
    70 00000060 74D9                    	jz	short pc_1 ; keyboard buffer empty
    71                                  
    72 00000062 E865000000              	call	getch
    73 00000067 3C1B                    	cmp	al, 1Bh  ; ESC key
    74 00000069 77D0                    	ja	short pc_1
    75 0000006B 740F                    	je	short terminate
    76                                  
    77                                  	; Stop timer event
    78 0000006D 8A1D[51010000]          	mov	bl, [timer_event_number]
    79 00000073 B780                    	mov	bh, 80h	; stop timer event
    80                                  			; and cancel callback service	
    81 00000075 B821000000              	mov	eax, 33	; 'systimer'
    82 0000007A CD40                    	int	40h	; TRDOS 386 system call
    83                                  
    84                                  terminate:
    85 0000007C B801000000              	mov	eax, 1	; 'sysexit'
    86 00000081 CD40                    	int	40h	; TRDOS 386 system call
    87                                  hang:
    88 00000083 90                      	nop
    89 00000084 90                      	nop
    90 00000085 90                      	nop
    91 00000086 EBFB                    	jmp	short hang
    92                                  
    93                                  ;t_event:
    94                                  ;	db	0
    95                                  
    96                                  t_callback:
    97 00000088 FE05[52010000]          	inc	byte [timer_event_status]
    98                                  
    99 0000008E 29D2                    	sub	edx, edx
   100 00000090 0FB705[53010000]        	movzx	eax, word [tcount]
   101 00000097 BF[5A010000]            	mov	edi, counter_str+5
   102 0000009C B90A000000              	mov	ecx, 10
   103                                  _tcb1:
   104 000000A1 F7F1                    	div	ecx
   105 000000A3 80C230                  	add	dl, '0'
   106 000000A6 4F                      	dec 	edi
   107 000000A7 8817                    	mov	[edi], dl
   108 000000A9 28D2                    	sub	dl, dl 
   109 000000AB 21C0                    	and	eax, eax
   110 000000AD 75F2                    	jnz	short _tcb1
   111                                  	
   112 000000AF 89F9                    	mov	ecx, edi
   113 000000B1 BF[55010000]            	mov	edi, counter_str
   114 000000B6 29F9                    	sub	ecx, edi
   115 000000B8 7604                    	jna	short _tcb3
   116                                  _tcb2:
   117 000000BA B030                    	mov	al, '0'
   118 000000BC F3AA                    	rep	stosb	
   119                                  _tcb3:
   120 000000BE 66FF05[53010000]        	inc	word [tcount]
   121                                  
   122 000000C5 B827000000              	mov	eax, 39 ; 'sysrele'
   123 000000CA CD40                    	int	40h
   124                                  ;here:
   125                                  ;	nop
   126                                  ;	nop
   127                                  ;	nop
   128                                  ;	jmp	short here
   129                                  
   130                                  getch:
   131                                  	; Getchar by using keyboard interrupt
   132 000000CC B410                    	mov	ah, 10h
   133 000000CE CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
   134                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
   135 000000D0 C3                      	retn
   136                                  
   137                                  print_msg:
   138 000000D1 B40E                    	mov	ah, 0Eh
   139 000000D3 668B1D[4F010000]        	mov	bx, [color]
   140                                  pmsg_loop:
   141 000000DA AC                      	lodsb
   142 000000DB 20C0                    	and	al, al
   143 000000DD 7404                    	jz	short pmsg_ok
   144 000000DF CD31                    	int	31h	; TRDOS 386 video interrupt
   145 000000E1 EBF7                    	jmp	short pmsg_loop	
   146                                  pmsg_ok:
   147 000000E3 C3                      	retn
   148                                  
   149                                  ; /// MESSAGE - DATA ///
   150                                  
   151                                  prg_msg:
   152 000000E4 0D0A07                  	db 0Dh, 0Ah, 07h
   153 000000E7 5452444F5320333836-     	db 'TRDOS 386 timer callback test program by Erdogan Tan [15/01/2017]'
   153 000000F0 2074696D6572206361-
   153 000000F9 6C6C6261636B207465-
   153 00000102 73742070726F677261-
   153 0000010B 6D206279204572646F-
   153 00000114 67616E2054616E205B-
   153 0000011D 31352F30312F323031-
   153 00000126 375D               
   154 00000128 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   155 0000012C 28507265737320616E-             db '(Press any key to continue...)'
   155 00000135 79206B657920746F20-
   155 0000013E 636F6E74696E75652E-
   155 00000147 2E2E29             
   156 0000014A 0D0A                    	db 0Dh, 0Ah
   157 0000014C 0D0A00                  	db 0Dh, 0Ah, 0
   158                                  color:
   159 0000014F 0F00                    	dw 0Fh
   160                                  timer_event_number:
   161 00000151 00                      	db 0
   162                                  timer_event_status:
   163 00000152 00                      	db 0
   164                                  tcount:
   165 00000153 0000                    	dw 0
   166                                  
   167                                  counter_str:
   168 00000155 3939393939              	db '99999'
   169 0000015A 00                      	db 0
