     1                                  ; ****************************************************************************
     2                                  ; scancode.s (TRDOS 386, TRDOS v2.0 - sample binary file, 'scancode.prg')
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; SCANCODE.PRG ! TEST program !  INT 32h (Kerboard BIOS) test !
     5                                  ;
     6                                  ; 13/05/2016
     7                                  ;
     8                                  ; Derived from 'scancode.asm' source code for MSDOS (Erdogan Tan, 1998)
     9                                  ;
    10                                  ; [ Last Modification: 13/05/2016 ]
    11                                  ;
    12                                  ; ****************************************************************************
    13                                  ; SCANCODE.ASM (01/02/1998, MSDOS Program by Erdogan Tan) 
    14                                  ; 'scancode' prints character codes
    15                                  
    16                                  ; 29/04/2016
    17                                  ; TRDOS 386 system calls (temporary list!)
    18                                  _ver 	equ 0
    19                                  _exit 	equ 1
    20                                  _fork 	equ 2
    21                                  _read 	equ 3
    22                                  _write	equ 4
    23                                  _open	equ 5
    24                                  _close 	equ 6
    25                                  _wait 	equ 7
    26                                  _creat 	equ 8
    27                                  _link 	equ 9
    28                                  _unlink	equ 10
    29                                  _exec	equ 11
    30                                  _chdir	equ 12
    31                                  _time 	equ 13
    32                                  _mkdir 	equ 14
    33                                  _chmod	equ 15
    34                                  _chown	equ 16
    35                                  _break	equ 17
    36                                  _stat	equ 18
    37                                  _seek	equ 19
    38                                  _tell 	equ 20
    39                                  _mount	equ 21
    40                                  _umount	equ 22
    41                                  _setuid	equ 23
    42                                  _getuid	equ 24
    43                                  _stime	equ 25
    44                                  _quit	equ 26	
    45                                  _intr	equ 27
    46                                  _fstat	equ 28
    47                                  _emt 	equ 29
    48                                  _mdate 	equ 30
    49                                  _stty 	equ 31
    50                                  _gtty	equ 32
    51                                  _ilgins	equ 33
    52                                  _sleep	equ 34
    53                                  _msg    equ 35
    54                                  
    55                                  %macro sys 1-4
    56                                      ; 29/04/2016 - TRDOS 386 (TRDOS v2.0)	
    57                                      ; 03/09/2015	
    58                                      ; 13/04/2015
    59                                      ; Retro UNIX 386 v1 system call.		
    60                                      %if %0 >= 2   
    61                                          mov ebx, %2
    62                                          %if %0 >= 3    
    63                                              mov ecx, %3
    64                                              %if %0 = 4
    65                                                 mov edx, %4   
    66                                              %endif
    67                                          %endif
    68                                      %endif
    69                                      mov eax, %1
    70                                      ;int 30h
    71                                      int 40h ; TRDOS 386 (TRDOS v2.0)		   
    72                                  %endmacro
    73                                  
    74                                  ; TRDOS 386 (and Retro UNIX 386 v1) system call format:
    75                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    76                                  
    77                                  [BITS 32] ; We need 32-bit intructions for protected mode
    78                                  
    79                                  [ORG 0] 
    80                                  
    81                                  start:
    82 00000000 BE[82000000]            	mov     esi, BossMsg
    83 00000005 E84C000000              	call    print_msg
    84                                  again:
    85 0000000A C605[DE000000]20        	mov     byte [Character], 20h
    86 00000011 B410                    	mov	ah, 10h		; Keyboard Service - Extended read
    87 00000013 CD32                    	int	32h		; TRDOS 386 Keyboard BIOS interrtupt
    88                                  				; (INT 16h for Real Mode BIOS)	
    89                                  
    90 00000015 50                      	push	eax
    91 00000016 20C0                    	and	al, al 
    92 00000018 7409                    	jz	short pass_enter
    93 0000001A 3C0D                    	cmp	al, 0Dh
    94 0000001C 7405                    	je	short pass_enter
    95 0000001E A2[DE000000]            	mov	[Character], al
    96                                  pass_enter:    
    97 00000023 88E0                    	mov	al, ah
    98 00000025 E83C000000              	call    bin_to_hex
    99 0000002A 66A3[F0000000]          	mov     [Reg_ScanCode], ax
   100 00000030 8B0424                  	mov	eax, [esp]
   101 00000033 E82E000000              	call    bin_to_hex
   102 00000038 66A3[05010000]          	mov     [Reg_AsciiCode], ax
   103                                  
   104 0000003E BE[D2000000]            	mov     esi, ScancodeMsg
   105 00000043 E80E000000                      call    print_msg
   106 00000048 58                      	pop	eax
   107 00000049 3C0D                    	cmp     al, 0Dh ; ENTER (CR) key
   108 0000004B 75BD                    	jne     short again
   109                                  
   110                                  	sys 	_exit		; TRDOS 386 System call
   110                              <1> 
   110                              <1> 
   110                              <1> 
   110                              <1> 
   110                              <1>  %if %0 >= 2
   110                              <1>  mov ebx, %2
   110                              <1>  %if %0 >= 3
   110                              <1>  mov ecx, %3
   110                              <1>  %if %0 = 4
   110                              <1>  mov edx, %4
   110                              <1>  %endif
   110                              <1>  %endif
   110                              <1>  %endif
   110 0000004D B801000000          <1>  mov eax, %1
   110                              <1> 
   110 00000052 CD40                <1>  int 40h
   111                                  				; (INT 40h, EAX=1)
   112                                  				; (terminate process)
   113                                  here:
   114 00000054 EBFE                    	jmp	short here
   115                                  
   116                                  print_msg:
   117                                   	; 12/05/2016
   118 00000056 BB07000000              	mov	ebx, 07h        ; light gray characters (bl)
   119                                  				; video page 0 (bh)
   120 0000005B B40E                    	mov	ah, 0Eh  	; teletype output (write tty)
   121 0000005D AC                      	lodsb
   122                                  _1:
   123 0000005E CD31                    	int	31h		; TRDOS 386 Video BIOS interrupt
   124                                  				; (INT 10h for real mode BIOS)
   125 00000060 AC                      	lodsb
   126 00000061 20C0                    	and	al, al
   127 00000063 75F9                    	jnz	short _1
   128                                  _2:
   129 00000065 C3                      	retn
   130                                  
   131                                  bin_to_hex:
   132                                  	; binary (byte) to hexadecimal (character) converter
   133                                  	; by Erdogan Tan (1998)
   134                                  	;
   135                                  	; input  -> AL = byte (binary number) to be converted 
   136                                  	; output -> AL = first character of hexadecimal number
   137                                  	;	    AH = second character of hexadecimal number
   138                                  
   139 00000066 88C4                    	mov     ah, al
   140 00000068 80E40F                  	and     ah, 0Fh
   141 0000006B 80C430                  	add     ah, 30h
   142 0000006E 80FC39                  	cmp     ah, 39h
   143 00000071 7603                    	jna     short pass1
   144 00000073 80C407                  	add     ah, 07h
   145                                  pass1:
   146 00000076 C0E804                  	shr     al, 4
   147 00000079 0430                    	add     al, 30h
   148 0000007B 3C39                    	cmp     al, 39h
   149 0000007D 7602                    	jna     short pass2
   150 0000007F 0407                    	add     al, 07h
   151                                  pass2:
   152 00000081 C3                      	retn
   153                                  
   154                                  ;-----------------------------------------------------------------
   155                                  ;  message
   156                                  ;-----------------------------------------------------------------
   157                                  
   158                                  BossMsg:
   159 00000082 0D0A                    	db 0Dh, 0Ah
   160 00000084 5B2028632920457264-     	db '[ (c) Erdogan TAN  1998-2016 ]  Press a key to see ASCII and scan code...'
   160 0000008D 6F67616E2054414E20-
   160 00000096 20313939382D323031-
   160 0000009F 36205D202050726573-
   160 000000A8 732061206B65792074-
   160 000000B1 6F2073656520415343-
   160 000000BA 494920616E64207363-
   160 000000C3 616E20636F64652E2E-
   160 000000CC 2E                 
   161 000000CD 0D0A                    	db 0Dh, 0Ah
   162 000000CF 0D0A00                  	db 0Dh, 0Ah, 0h
   163                                  ScancodeMsg:
   164 000000D2 436861726163746572-     	db 'Character : '
   164 000000DB 203A20             
   165                                  Character:
   166 000000DE 3F                      	db '?'
   167 000000DF 20202020205363616E-     	db '     Scan Code : '
   167 000000E8 20436F6465203A20   
   168                                  Reg_ScanCode:
   169 000000F0 3F3F                    	dw '??'
   170 000000F2 68                      	db 'h'
   171 000000F3 202020202041534349-     	db '     ASCII Code : '
   171 000000FC 4920436F6465203A20 
   172                                  Reg_AsciiCode:
   173 00000105 3F3F                    	dw '??'
   174 00000107 68                      	db 'h'
   175 00000108 0D0A00                  	db 0Dh, 0Ah, 0h
