home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / aspisrc.zip / dev / COMM.ASM next >
Assembly Source File  |  1998-11-29  |  7KB  |  155 lines

  1.  
  2. ;; Allow 386 instructions (OS/2 2.0 or better)
  3.  
  4.               .386p
  5.  
  6.  
  7. _TEXT         segment word public use16 'CODE'
  8.               assume cs:_TEXT, ds:DGROUP, es:NOTHING, ss:NOTHING
  9.  
  10.               public ComWriteChar
  11.               public ComWriteStr
  12.               public ComWrite32
  13.  
  14.  
  15. ;; Send a byte through the serial port
  16. ;;
  17. ;; Arguments:
  18. ;;    DX = Base port address for com port (com1 = 3F8, com2 = 2F8)
  19. ;;    AH = Byte to send
  20. ;; Returns
  21. ;;    Carry Clear = success
  22. ;;    Carry Set = error
  23. ;; Modifies
  24. ;;    AL CX SI
  25. ComSendByte   proc near
  26.               mov      si,dx              ;; Save the RS-232 port address
  27.               add      dx,05h             ;; Check the RS-232 line status
  28.               xor      cx,cx              ;; Loop FFFFh times
  29. comsb_wait:   in       al,dx              ;; Get the line status
  30.               test     al,20h             ;; Test the line status
  31.               loopz    comsb_wait         ;; If line not ready, try again
  32.               or       cx,cx              ;; Assume that there was an error
  33.               stc                         ;; If the line never became ready,
  34.               jz       comsb_exit         ;;   then flag an error
  35.               sub      dx,05h             ;; Prepare the RS-232 data port
  36.               mov      al,ah              ;; Load the data to send
  37.               out      dx,al              ;; Send the data
  38.               clc                         ;; Signal success
  39. comsb_exit:   mov      dx,si              ;; Restore the RS-232 port address
  40.               ret                         ;; Exit
  41. ComSendByte   endp
  42.  
  43.  
  44. ;; Write a character to the serial port
  45. ;;
  46. ;; Arguments:
  47. ;;    DX = Base port address for com port (com1 = 3F8, com2 = 2F8)
  48. ;;    AX = Character data
  49. ;; Returns
  50. ;;    AX = 0 for success, 1 for error
  51. ;; Modifies
  52. ;;    AX CX SI
  53. ComWriteChar  proc near
  54.               mov      ah,al              ;; Load the character
  55.               call     ComSendByte        ;; Write the character
  56. comchar_exit: setc     al                 ;; Set the return code in ax
  57.               xor      ah,ah              ;;   (0 = success, 1 = error)
  58.               ret                         ;; Exit
  59. ComWriteChar  endp
  60.  
  61.  
  62. ;; Write a string to the serial port
  63. ;;
  64. ;; Arguments:
  65. ;;    DX = Base port address for com port (com1 = 3F8, com2 = 2F8)
  66. ;;    GS:BX = Far pointer to character string
  67. ;; Returns
  68. ;;    AX = 0 for success, 1 for error
  69. ;; Modifies
  70. ;;    AX BX CX SI
  71. ComWriteStr   proc near
  72.               clc                         ;; Assume no errors
  73. comstr_loop:  mov      ah,gs:[bx]         ;; Load the current character
  74.               or       ah,ah              ;; If this is the end of the string
  75.               jz       comstr_exit        ;;   then exit
  76.               call     ComSendByte        ;; Write the character
  77.               inc      bx                 ;; Advance to the next byte
  78.               jnc      comstr_loop        ;; Loop
  79. comstr_exit:  setc     al                 ;; Set the return code in ax
  80.               xor      ah,ah              ;;   (0 = success, 1 = error)
  81.               ret                         ;; Exit
  82. ComWriteStr   endp
  83.  
  84.  
  85. ;; Write an unsigned long to the serial port
  86. ;;
  87. ;; Arguments:
  88. ;;    SI = Base port address for com port (com1 = 3F8, com2 = 2F8)
  89. ;;    DX:AX = Unsigned long data
  90. ;;    BX = Numeric base (10 = decimal, 16 = hexadecimal)
  91. ;;    DI = Width of number to print (0 = variable width)
  92. ;; Returns
  93. ;;    AX = 0 for success, 1 for error
  94. ;; Modifies
  95. ;;    EAX EBX CX EDX DI SI
  96. ComWrite32    proc near
  97. ;; Split the number into digits pushed onto the stack
  98.               push     bp                 ;; Set up a stack frame
  99.               mov      bp,sp              ;;   (to handle errors more easily)
  100.               xchg     ax,dx              ;; Pack the unsigned long data
  101.               shl      eax,10h            ;;   from dx:ax into a single
  102.               mov      ax,dx              ;;   register (eax)
  103.               and      ebx,0000003Fh      ;; Clear any garbage bits in the base
  104.               xor      cx,cx              ;; Count the number of digits into cx
  105. com32_digit:  inc      cx                 ;; Count one more digit
  106.               cdq                         ;; Determine the current digit into
  107.               div      ebx                ;;   edx and the rest in eax
  108.               cmp      edx,10             ;; If the digit is decimal, then
  109.               jl       com32_dec          ;;   convert it to an ASCII number
  110.               add      dx,'A'-'0'-10      ;;   otherwise, make it a letter
  111. com32_dec:    add      dx,'0'             ;; Store the character in dx
  112.               push     dx                 ;; Save the character
  113.               or       eax,eax            ;; If there are still more digits,
  114.               jnz      com32_digit        ;;   then continue
  115. ;; Print any necessary filler digits to adjust width
  116.               mov      dx,si              ;; Load the com port address
  117.               mov      bx,cx              ;; Save the digit count
  118.               or       di,di              ;; If variable width, then
  119.               jz       com32_print        ;;   print the digits
  120.               cmp      di,cx              ;; If the width is greater than the
  121.               jg       com32_pad          ;;   digit count, then zero pad
  122.               sub      cx,di              ;;   otherwise, truncate
  123.               shl      cx,1               ;; Remove the extra digits from the
  124.               add      sp,cx              ;;   stack
  125.               mov      bx,di              ;; Adjust the digit count
  126.               jmp      com32_print
  127. com32_pad:    sub      di,cx              ;; Compute the number of pad digits
  128.               mov      ah,'0'             ;; Pad with zeros
  129. com32_send:   call     ComSendByte        ;; Print a pad digit
  130.               jc       com32_exit         ;; Exit if an error occured
  131.               dec      di                 ;; Loop again to print the next
  132.               jnz      com32_send         ;;   digit
  133. ;; Print the digits pulled from the stack
  134. com32_print:  pop      ax                 ;; Pull the next digit from the
  135.               mov      ah,al              ;;   stack
  136.               call     ComSendByte        ;; Print the digit
  137.               jc       com32_exit         ;; Exit if an error occured
  138.               dec      bx                 ;; Loop again to print the next
  139.               jnz      com32_print        ;;   digit
  140.               clc                         ;; Signal a happy exit
  141. ;; Clean up, set the result code in ax, and return
  142. com32_exit:   setc     al                 ;; Set the return code in ax
  143.               xor      ah,ah              ;;   (0 = success, 1 = error)
  144.               mov      sp,bp              ;; Restore the stack frame
  145.               pop      bp                 ;;   and the registers
  146.               ret                         ;; Exit
  147. ComWrite32    endp
  148.  
  149.  
  150. _TEXT         ends
  151.  
  152.  
  153.  
  154.               end
  155.