home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 9.ddi / CHAPXMPL.ZIP / HEXSTR.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-02-13  |  2.3 KB  |  67 lines

  1. ; Turbo Assembler    Copyright (c) 1988, 1991 By Borland International, Inc.
  2.  
  3. ; HEXSTR.ASM
  4. ; General purpose HEX conversion routine.
  5. ; A routine to be called from Turbo Pascal.
  6.  
  7. ; From the Turbo Assembler User's Guide - Interfacing Turbo Assembler
  8. ;                                          with Turbo Pascal
  9.  
  10. CODE      SEGMENT
  11.           ASSUME cs:CODE,ds:NOTHING
  12.  
  13. ; Parameters (+2 because of push bp)
  14.  
  15. byteCount equ byte ptr  ss:[bp+6]
  16. num       equ dword ptr ss:[bp+8]
  17.  
  18. ; Function result address (+2 because of push bp)
  19.  
  20. resultPtr equ dword ptr ss:[bp+12]
  21.  
  22.  
  23. HexStr    PROC FAR
  24.           PUBLIC HexStr
  25.  
  26.           push bp
  27.           mov bp,sp                  ;get pointer into stack
  28.           les di,resultPtr           ;get address of function result
  29.           mov dx,ds                  ;save Turbo's DS in DX
  30.           lds si,num                 ;get number address
  31.           mov al,byteCount           ;how many bytes?
  32.           xor ah,ah                  ;make a word
  33.           mov cx,ax                  ;keep track of bytes in CX
  34.           add si,ax                  ;start from MS byte of  number
  35.           dec si
  36.           shl ax,1                   ;how many digits? (2/byte)
  37.           cld                        ;store # digits (going forward)
  38.           stosb                      ;in destination string's length byte
  39. HexLoop:
  40.           std                        ;scan number from MSB to LSB
  41.           lodsb                      ;get next byte
  42.           mov ah,al                  ;save it
  43.           shr al,1                   ;extract high nibble
  44.           shr al,1
  45.           shr al,1
  46.           shr al,1
  47.           add al,90h                 ;special hex conversion sequence
  48.           daa                        ;using ADDs and DAA's
  49.           adc al,40h
  50.           daa                        ;nibble now converted to ASCII
  51.           cld                        ;store ASCII going up
  52.           stosb
  53.           mov al,ah                  ;repeat conversion for low nibble
  54.           and al,0Fh
  55.           add al,90h
  56.           daa
  57.           adc al,40h
  58.           daa
  59.           stosb
  60.           loop HexLoop               ;keep going until done
  61.           mov ds,dx                  ;restore Turbo's DS
  62.           pop bp
  63.           ret 6                      ;parameters take 6 bytes
  64. HexStr    ENDP
  65. CODE      ENDS
  66.           END
  67.