home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch14 / masm / ports.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  7.8 KB  |  226 lines

  1.         title   PORTS -- IOPL Demo Program
  2.         page    55,132
  3.         .286
  4.  
  5. ;
  6. ; PORTS.ASM
  7. ;
  8. ; An OS/2 IOPL demonstration program with that reads and displays the
  9. ; first 256 I/O ports.    Requires the separate module PORTIO.ASM.
  10. ;
  11. ; Assemble with:  C> masm ports.asm;
  12. ; Link with:  C> link ports+portio,ports,,os2,ports
  13. ;
  14. ; Usage is:  C> ports
  15. ;
  16. ; Copyright (C) 1988 Ray Duncan
  17. ;
  18.  
  19. cr      equ     0dh                     ; ASCII carriage return
  20. lf    equ    0ah            ; ASCII linefeed
  21. blank   equ     20h                     ; ASCII space code
  22.  
  23. stdout  equ     1                       ; standard output handle
  24. stderr  equ     2                       ; standard error handle
  25.  
  26. bport   equ     0                       ; first port to display
  27. eport   equ     255                     ; last port to display
  28. request equ     0                       ; request port access
  29. release equ     1                       ; release port access
  30.  
  31.         extrn   DosPortAccess:far       ; kernel API functions
  32.         extrn   DosWrite:far
  33.         extrn   DosExit:far
  34.         
  35.         extrn   rport:far               ; PORTIO.ASM functions
  36.         extrn   wport:far
  37.  
  38. DGROUP  group   _DATA
  39.  
  40. _DATA   segment word public 'DATA'
  41.  
  42. wlen    dw      0                       ; actual number of bytes
  43.                                         ; written by DosWrite
  44.  
  45. msg1    db      cr,lf                   ; error message
  46.         db      'DosPortAccess failed.'
  47.         db      cr,lf
  48. msg1_len equ    $-msg1
  49.  
  50. msg2    db      cr,lf                   ; heading
  51.         db      '        0  1  2  3  4  5  6'
  52.         db      '  7  8  9  A  B  C  D  E  F'
  53. msg2_len equ    $-msg2
  54.  
  55. msg3    db      cr,lf                   ; display port number
  56. msg3a   db      'NNNN  '
  57. msg3_len equ    $-msg3
  58.  
  59. msg4    db      ' '                     ; display port data
  60. msg4a   db      'NN'
  61. msg4_len equ    $-msg4
  62.  
  63. _DATA   ends    
  64.  
  65.  
  66. _TEXT   segment word public 'CODE'
  67.  
  68.         assume  cs:_TEXT,ds:DGROUP
  69.  
  70. main    proc    far                     ; entry point from OS/2
  71.  
  72.         push    ds                      ; make DGROUP addressable
  73.         pop     es                      ; with ES too
  74.  
  75.                                         ; request port access...
  76.         push    0                       ; reserved
  77.         push    request                 ; request/release
  78.         push    bport                   ; first port number
  79.         push    eport                   ; last port number
  80.         call    DosPortAccess           ; transfer to OS/2
  81.         or      ax,ax                   ; call successful?
  82.         jz      main1                   ; yes, jump
  83.  
  84.                                         ; display error message...
  85.         push    stderr                  ; standard error handle
  86.         push    ds                      ; message address
  87.         push    offset DGROUP:msg1
  88.         push    msg1_len                ; message length
  89.         push    ds                      ; receives bytes written
  90.         push    offset DGROUP:wlen
  91.         call    DosWrite                ; transfer to OS/2
  92.  
  93.                                         ; now terminate process...
  94.         push    1                       ; terminate all threads
  95.     push    1            ; return code = 1 (error)
  96.         call    DosExit                 ; transfer to OS/2
  97.  
  98. main1:                                  ; print heading...
  99.         push    stdout                  ; standard output handle
  100.         push    ds                      ; address of heading
  101.         push    offset DGROUP:msg2
  102.         push    msg2_len                ; length of heading
  103.         push    ds                      ; receives bytes written        
  104.         push    offset DGROUP:wlen
  105.         call    DosWrite                ; transfer to OS/2      
  106.  
  107.         mov     dx,bport                ; initialize port number
  108.         
  109. main2:  test    dx,0fh                  ; new line needed?
  110.         jnz     main3                   ; no, jump
  111.         
  112.         mov     ax,dx                   ; convert port number
  113.         mov     di,offset DGROUP:msg3a  ; to ASCII for display
  114.         call    wtoa
  115.  
  116.                                         ; display port number...
  117.         push    stdout                  ; standard output handle
  118.         push    ds                      ; message address
  119.         push    offset DGROUP:msg3
  120.         push    msg3_len                ; message length
  121.         push    ds                      ; receives bytes written        
  122.         push    offset DGROUP:wlen
  123.         call    DosWrite                ; transfer to OS/2      
  124.  
  125. main3:  push    dx                      ; call 'rport' to read port
  126.         call    rport                   ; returns AX = data
  127.  
  128.         mov     di,offset msg4a         ; convert port data 
  129.         call    btoa                    ; to ASCII for display
  130.  
  131.                                         ; display port data...
  132.         push    stdout                  ; standard output handle
  133.         push    ds                      ; message address
  134.         push    offset DGROUP:msg4
  135.         push    msg4_len                ; message length
  136.         push    ds                      ; receives bytes written        
  137.         push    offset DGROUP:wlen
  138.         call    DosWrite                ; transfer to OS/2      
  139.  
  140.         inc     dx                      ; increment port number
  141.         cmp     dx,eport                ; done with all ports?
  142.         jbe     main2                   ; not yet, read another
  143.  
  144.                                         ; release port access...
  145.         push    0                       ; reserved
  146.         push    release                 ; request/release
  147.         push    bport                   ; first port number
  148.         push    eport                   ; last port number
  149.         call    DosPortAccess           ; transfer to OS/2
  150.  
  151.                                         ; final exit to OS/2...
  152.         push    1                       ; terminate all threads
  153.     push    0            ; return code = 0 (success)
  154.         call    DosExit                 ; transfer to OS/2
  155.         
  156. main    endp
  157.  
  158. ; WTOA:         Convert word to hex ASCII
  159. ; Call with:    AX    = data to convert
  160. ;               ES:DI = storage address
  161. ; Returns:      nothing
  162. ; Uses:         AX, CL, DI
  163.  
  164. wtoa    proc    near
  165.  
  166.         push    ax                      ; save original value
  167.         mov     al,ah
  168.         call    btoa                    ; convert upper byte    
  169.  
  170.         pop     ax                      ; restore original value
  171.         call    btoa                    ; convert lower byte
  172.  
  173.         ret                             ; back to caller
  174.  
  175. wtoa    endp
  176.  
  177.  
  178. ; BTOA:         Convert byte to hex ASCII
  179. ; Call with:    AL    = data to convert
  180. ;               ES:DI = storage address
  181. ; Returns:      nothing
  182. ; Uses:         AX, CL, DI
  183.  
  184. btoa    proc    near
  185.  
  186.         sub     ah,ah                   ; clear upper byte
  187.  
  188.         mov     cl,16                   ; divide by 16
  189.         div     cl
  190.  
  191.         call    ascii                   ; convert quotient
  192.         stosb                           ; store ASCII character
  193.  
  194.         mov     al,ah
  195.         call    ascii                   ; convert remainder 
  196.         stosb                           ; store ASCII character
  197.  
  198.         ret                             ; back to caller
  199.  
  200. btoa    endp
  201.  
  202.  
  203. ; ASCII:        Convert nibble to hex ASCII
  204. ; Call with:    AL    = data to convert in low 4 bits
  205. ; Returns:      AL    = ASCII character
  206. ; Uses:         nothing
  207.  
  208. ascii   proc    near
  209.  
  210.         add     al,'0'                  ; add base ASCII value  
  211.         cmp     al,'9'                  ; is it in range 0-9?
  212.         jle     ascii2                  ; jump if it is
  213.  
  214.         add     al,'A'-'9'-1            ; no, adjust for range A-F
  215.  
  216. ascii2: ret                ; return ASCII character in AL
  217.  
  218. ascii   endp
  219.  
  220. _TEXT   ends
  221.  
  222.         end     main                    ; defines entry point
  223.