home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 181.img / TASM-101.ZIP / CHAPXMPL.ARC / HEXSTR.ASM < prev    next >
Assembly Source File  |  1989-05-02  |  2KB  |  58 lines

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