     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: 02/01/2017 ]
    10                                  ;
    11                                  ; ****************************************************************************
    12                                  
    13                                  [BITS 32]
    14 00000000 BE[E8000000]                    mov     esi, prg_msg
    15 00000005 E8CB000000              	call	print_msg
    16                                  
    17 0000000A E8C1000000              	call	getch
    18                                  
    19 0000000F 3C1B                    	cmp	al, 1Bh  ; ESC key
    20 00000011 7469                    	je	terminate ; short olacak !	
    21                                  
    22 00000013 66C705[53010000]22-     	mov	word [color], 22h
    22 0000001B 00                 
    23                                  
    24                                  	; start (Real Time Clock) 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[55010000]            	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[56010000]          	cmp	[timer_event_status], bh ; 0
    48                                  	;cmp	byte [t_event], bh ; 0
    49 00000043 7617                    	jna	short pc_2
    50                                  
    51 00000045 883D[56010000]          	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[59010000]            	mov	esi, counter_str
    64 00000057 E879000000              	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 E869000000              	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[55010000]          	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 9C                      	pushfd
    98                                  
    99                                  	; save registers
   100 00000089 57                      	push	edi
   101 0000008A 52                      	push	edx
   102 0000008B 50                      	push	eax
   103 0000008C 51                      	push	ecx
   104                                  	
   105 0000008D FE05[56010000]          	inc	byte [timer_event_status]
   106                                  
   107 00000093 29D2                    	sub	edx, edx
   108 00000095 0FB705[57010000]        	movzx	eax, word [tcount]
   109 0000009C BF[5E010000]            	mov	edi, counter_str+5
   110 000000A1 B90A000000              	mov	ecx, 10
   111                                  _tcb1:
   112 000000A6 F7F1                    	div	ecx
   113 000000A8 80C230                  	add	dl, '0'
   114 000000AB 4F                      	dec 	edi
   115 000000AC 8817                    	mov	[edi], dl
   116 000000AE 28D2                    	sub	dl, dl 
   117 000000B0 21C0                    	and	eax, eax
   118 000000B2 75F2                    	jnz	short _tcb1
   119                                  	
   120 000000B4 89F9                    	mov	ecx, edi
   121 000000B6 BF[59010000]            	mov	edi, counter_str
   122 000000BB 29F9                    	sub	ecx, edi
   123 000000BD 7604                    	jna	short _tcb3
   124                                  _tcb2:
   125 000000BF B030                    	mov	al, '0'
   126 000000C1 F3AA                    	rep	stosb	
   127                                  _tcb3:
   128 000000C3 66FF05[57010000]        	inc	word [tcount]
   129                                  
   130                                  	; restore registers
   131 000000CA 59                      	pop	ecx
   132 000000CB 58                      	pop	eax
   133 000000CC 5A                      	pop	edx
   134 000000CD 5F                      	pop	edi
   135                                  
   136 000000CE 9D                      	popfd
   137                                  
   138 000000CF C3                      	retn
   139                                  
   140                                  	;iret	; restore EIP, CS, E-FLAGS	
   141                                  		; return to normal running code
   142                                  
   143                                  getch:
   144                                  	; Getchar by using keyboard interrupt
   145 000000D0 B410                    	mov	ah, 10h
   146 000000D2 CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
   147                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
   148 000000D4 C3                      	retn
   149                                  
   150                                  print_msg:
   151 000000D5 B40E                    	mov	ah, 0Eh
   152 000000D7 668B1D[53010000]        	mov	bx, [color]
   153                                  pmsg_loop:
   154 000000DE AC                      	lodsb
   155 000000DF 20C0                    	and	al, al
   156 000000E1 7404                    	jz	short pmsg_ok
   157 000000E3 CD31                    	int	31h	; TRDOS 386 video interrupt
   158 000000E5 EBF7                    	jmp	short pmsg_loop	
   159                                  pmsg_ok:
   160 000000E7 C3                      	retn
   161                                  
   162                                  ; /// MESSAGE - DATA ///
   163                                  
   164                                  prg_msg:
   165 000000E8 0D0A07                  	db 0Dh, 0Ah, 07h
   166 000000EB 5452444F5320333836-     	db 'TRDOS 386 timer callback test program by Erdogan Tan [02/01/2017]'
   166 000000F4 2074696D6572206361-
   166 000000FD 6C6C6261636B207465-
   166 00000106 73742070726F677261-
   166 0000010F 6D206279204572646F-
   166 00000118 67616E2054616E205B-
   166 00000121 30322F30312F323031-
   166 0000012A 375D               
   167 0000012C 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   168 00000130 28507265737320616E-             db '(Press any key to continue...)'
   168 00000139 79206B657920746F20-
   168 00000142 636F6E74696E75652E-
   168 0000014B 2E2E29             
   169 0000014E 0D0A                    	db 0Dh, 0Ah
   170 00000150 0D0A00                  	db 0Dh, 0Ah, 0
   171                                  color:
   172 00000153 0F00                    	dw 0Fh
   173                                  timer_event_number:
   174 00000155 00                      	db 0
   175                                  timer_event_status:
   176 00000156 00                      	db 0
   177                                  tcount:
   178 00000157 0000                    	dw 0
   179                                  
   180                                  counter_str:
   181 00000159 3939393939              	db '99999'
   182 0000015E 00                      	db 0
