home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / pascal / tplib21.zip / INSTALL.EXE / SUCNVRT.ASM < prev    next >
Assembly Source File  |  1993-06-24  |  5KB  |  109 lines

  1. ;               TURBO PASCAL LIBRARY 2.1
  2. ;               Number-to-string conversion module
  3.  
  4.                 TITLE   UNIT STRINGS: Number-to-string conversion module
  5.                 PAGE    66,132
  6.                 %BIN    12
  7.  
  8. CODE            SEGMENT WORD
  9.                 ASSUME  CS:CODE
  10.                 LOCALS
  11.                 PUBLIC  HEXSTR,OCTSTR,BINSTR
  12.  
  13.                 PAGE
  14.  
  15.  
  16. ;               FUNCTION HexStr (n: WORD; count: BYTE): STRING
  17. ;               Return hex. representation of n in string of count characters
  18.  
  19. HEXSTR          PROC    FAR
  20.                 PUSH    BP
  21.                 MOV     BP,SP                   ; Set up stack frame pointer
  22.                 MOV     CX,[BP+6]
  23.                 XOR     CH,CH                   ; Get count in CX
  24.                 MOV     DX,[BP+8]               ; Get word to convert in DX
  25.                 LES     DI,[BP+10]              ; Point ES:DI to output string
  26.                 MOV     ES:[DI],CL              ; Store output string length
  27.                 JCXZ    @@L3
  28.                 ADD     DI,CX                   ; Point DI to last character
  29.                 STD
  30.                 MOV     BL,04H                  ; Shift value for conversion
  31. @@L1:           MOV     AX,DX
  32.                 AND     AL,0FH                  ; Mask to leave one digit only
  33.                 OR      AL,30H                  ; Convert to ASCII character
  34.                 CMP     AL,3AH                  ; Check for hex. A thru F
  35.                 JB      @@L2
  36.                 ADD     AL,07H
  37. @@L2:           STOSB                           ; Store hex. digit in string
  38.                 XCHG    BX,CX
  39.                 SHR     DX,CL                   ; Shift out digit converted
  40.                 XCHG    BX,CX
  41.                 LOOP    @@L1                    ; Go do next character
  42. @@L3:           POP     BP
  43.                 RET     4
  44. HEXSTR          ENDP
  45.  
  46.                 PAGE
  47.  
  48.  
  49. ;               FUNCTION OctStr (n: WORD; count: BYTE): STRING
  50. ;               Return octal representation of n in string of count characters
  51.  
  52. OCTSTR          PROC    FAR
  53.                 PUSH    BP
  54.                 MOV     BP,SP                   ; Set up stack frame pointer
  55.                 MOV     CX,[BP+6]
  56.                 XOR     CH,CH                   ; Get count in CX
  57.                 MOV     DX,[BP+8]               ; Get word to convert in DX
  58.                 LES     DI,[BP+10]              ; Point ES:DI to output string
  59.                 MOV     ES:[DI],CL              ; Store output string length
  60.                 JCXZ    @@L2
  61.                 ADD     DI,CX                   ; Point DI to last character
  62.                 STD
  63.                 MOV     BL,03H                  ; Shift value for conversion
  64. @@L1:           MOV     AX,DX
  65.                 AND     AL,07H                  ; Mask to leave one digit only
  66.                 OR      AL,30H                  ; Convert to ASCII character
  67.                 STOSB                           ; Store octal digit in string
  68.                 XCHG    BX,CX
  69.                 SHR     DX,CL                   ; Shift out digit converted
  70.                 XCHG    BX,CX
  71.                 LOOP    @@L1                    ; Go do next character
  72. @@L2:           POP     BP
  73.                 RET     4
  74. OCTSTR          ENDP
  75.  
  76.                 PAGE
  77.  
  78.  
  79. ;               FUNCTION BinStr (n: WORD; count: BYTE): STRING;
  80. ;               Return binary representation of n using count characters
  81.  
  82. BINSTR          PROC    FAR
  83.                 PUSH    BP
  84.                 MOV     BP,SP                   ; Set up stack frame pointer
  85.                 MOV     CX,[BP+6]
  86.                 XOR     CH,CH                   ; Get count in CX
  87.                 LES     DI,[BP+10]              ; Point ES:DI to output string
  88.                 MOV     ES:[DI],CL              ; Store output string length
  89.                 JCXZ    @@L4
  90.                 MOV     DX,[BP+8]               ; Get word to convert in DX
  91.                 MOV     AL,30H                  ; ASCII zero
  92.                 ADD     DI,CX                   ; Point DI to last character
  93.                 STD
  94. @@L1:           SHR     DX,1                    ; Shift bit to carry for test
  95.                 JNC     @@L2                    ; If 1, change AL to ASCII one
  96.                 INC     AL
  97. @@L2:           STOSB                           ; Store digit
  98.                 JNC     @@L3                    ; Restore AL if necessary
  99.                 DEC     AL
  100. @@L3:           LOOP    @@L1                    ; Go do next bit
  101. @@L4:           POP     BP
  102.                 RET     4
  103. BINSTR          ENDP
  104.  
  105. CODE            ENDS
  106.  
  107.                 END
  108.  
  109.