home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / HARDWARE / EPROMTLZ.ZIP / IHEX.ZIP / IHEX.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-09-21  |  7.9 KB  |  315 lines

  1. PAGE    ,132
  2. TITLE    IHEX.ASM
  3. ;
  4. COMMENT *
  5.     Created:    16-SEP-1988        Richard B. Johnson
  6.     Purpose:    Converts the contents of any file to an output
  7.     string consisting of Intel Hexadecimal records. This is the format
  8.     required by many PROM "burners". It also is a way to transfer a
  9.     binary file to a remote computer when all that is available is
  10.     "ASCII capture".
  11.  
  12.     The received data, once "cleaned up" using a word-processor to
  13.     remove any extra characters at the beginning and end, may be changed
  14.     back to binary using MS-DOS's DEBUG.
  15.  
  16.     Example:
  17.     C:\> debug            { Execute debug }
  18.     - ntempfile.hex            { Name the Intel-hex file.}
  19.     - l                { Load the file }
  20.     - nnewfile.com            { Name the output file }
  21.     - w                { Write output file contents }
  22.     - q                { Quit DEBUG }
  23.  
  24.     C:\>                { Back to DOS }
  25.  
  26.     Usage:
  27.     IHEX filename.typ               (Output to screen)
  28.     IHEX filename.typ> Outfile.typ  (Output to a file)
  29.     IHEX filename.typ> COM1:        (Output to a modem)
  30.  
  31.     Revision history:
  32. *
  33. ;
  34. CR    EQU    0AH
  35. LF    EQU    0DH
  36. MS_DOS    EQU    21H
  37. QUIT    EQU    4C00H
  38. ;
  39. VERS    STRUC
  40.     DB    'V1.00'
  41. @VERS    DB    ' '
  42. VERS    ENDS
  43. ;
  44. LINE1    STRUC
  45.     DB    'PROGRAM EXCHANGE'
  46. @LEN1    DB    ' '
  47. LINE1    ENDS
  48. ;
  49. LINE2    STRUC
  50.     DB    'Intel Hex Converter '
  51. @LEN2    DB    ' '
  52. LINE2    ENDS
  53. ;
  54. LINE3    STRUC
  55.     DB    'Created 16-SEP-1988  Richard B. Johnson'
  56. @LEN3    DB    ' '
  57. LINE3    ENDS
  58. ;
  59. PSEG    SEGMENT PARA PUBLIC 'CODE'
  60.     ASSUME CS:PSEG, DS:PSEG, ES:PSEG, SS:PSEG
  61.     ORG    100H
  62. MAIN    PROC    NEAR
  63.     MOV    WORD PTR [CONS],2    ; Use STD error (no redirection)
  64. ;
  65.     MOV    DX,OFFSET PRP0        ; Pick up the logo
  66.     CALL    PROMPT            ; Print to screen
  67.     MOV    SI,80H            ; Where command line should be
  68.     LODSB                ; Get characters typed
  69.     OR    AL,AL
  70.     JNZ    PARSE0            ; Something was typed
  71. ERROR:    MOV    DX,OFFSET PRP4        ; Point to 'usage' prompt
  72.     CALL    PROMPT            ; Print to screen
  73.     JMP    FINIS            ; Exit
  74.  
  75. PARSE0:    CBW
  76.     MOV    CX,AX            ; Character count
  77. PARSE1:    LODSB                ; Get byte
  78.     CMP    AL,' '
  79.     JBE    PARSE2
  80.     JMP    SHORT PARSE3
  81. PARSE2:    LOOP    PARSE1            ; Clear any spaces/tabs, etc
  82. PARSE3:    JCXZ    ERROR            ; Ran out of characters
  83.     DEC    SI
  84.     MOV    DI,OFFSET FNAME        ; Point to the filename
  85.     REP    MOVSB            ; Move command line there
  86.     MOV    BYTE PTR [DI],0        ; Change CR to null
  87.     MOV    DX,OFFSET PRP1        ; Point to 'file'
  88.     CALL    PROMPT            ; Print to screen
  89.     MOV    DX,OFFSET FNAME        ; Point to the file name
  90.     CALL    PROMPT            ; Print to screen
  91.  
  92.     MOV    AX,WORD PTR [LOAD]    ; Pick up load address
  93.     MOV    WORD PTR [ADDR],AX    ; Load address of file
  94.     CALL    OPEN            ; Open the file
  95.     JNC    PARSE4            ; Success
  96.     MOV    DX,OFFSET PRP3        ; Error, point to 'not found'
  97.     CALL    PROMPT            ; Print to screen
  98.     JMP    FINIS            ; Exit
  99. PARSE4:    MOV    DX,OFFSET PRP2        ; Point to 'has been opened'
  100.     CALL    PROMPT            ; Print to screen
  101.     MOV    AH,1            ; Wait for console input
  102.     INT    MS_DOS
  103.     MOV    DX,OFFSET CRLF        ; Next line.
  104.     CALL    PROMPT
  105.     MOV    WORD PTR [CONS],1    ; Change to STD output
  106. ;
  107. CONT:    MOV    SI,OFFSET BUFFER    ; Where file data was put
  108.     MOV    DI,OFFSET SCR_BUF    ; Output buffer
  109.     CALL    READ            ; Read the file
  110.     JZ    DONE            ; No bytes, end of the file
  111. PAGE0:    SUB    WORD PTR [BYTES],32    ; Number of bytes to convert
  112.     JNC    PAGE1            ; Not at end of read yet
  113.     MOV    AX,WORD PTR [BYTES]    ; Get byte count
  114.     ADD    AX,32            ; Add back
  115.     MOV    BYTE PTR [LEN],AL    ; Update the length
  116.     CALL    IHEX            ; Finish string
  117. ;
  118. DONE:    CALL    EOF            ; End of Intel hex record
  119.     CALL    CONOUT            ; Output to screen
  120.     CALL    CLOSE            ; Close the file
  121.     JMP    FINIS            ; Exit
  122. ;
  123. PAGE1:    CALL    IHEX            ; Convert line of Intel Hex
  124.     CMP    WORD PTR [BYTES],0    ; All bytes converted?
  125.     JNZ    PAGE0            ; No, Convert more
  126.     CALL    CONOUT            ; Print results
  127.     JMP    SHORT CONT
  128. MAIN    ENDP
  129. ;
  130. FINIS    PROC    NEAR
  131.     MOV    AX,QUIT
  132.     INT    MS_DOS
  133. FINIS    ENDP
  134. ;
  135. ;    Convert [LEN] bytes addressed by [SI] to Intel-Hex in [DI].
  136. ;    Conversion address is in [ADDR]
  137. ;
  138. IHEX    PROC    NEAR
  139.     MOV    AL,':'            ; Start with a colon
  140.     STOSB                ; Into buffer
  141.     MOV    AL,BYTE PTR [LEN]    ; Get length of the string
  142.     XOR    BX,BX            ; Zero checksup
  143.     ADD    BL,AL            ; Checksum
  144.     PUSH    AX            ; Save length
  145.     CALL    HEXB            ; Convert to hex
  146.     POP    AX            ; Restore length
  147.     XOR    AH,AH            ; Zero high byte
  148.     MOV    CX,AX            ; Use for a count
  149.     MOV    AX,WORD PTR [ADDR]    ; Pick up the address
  150.     ADD    BL,AH            ; Checksum
  151.     ADD    BL,AL
  152.     CALL    HEXW            ; Convert to HEX
  153.     MOV    AL,0            ; Record type
  154.     CALL    HEXB
  155. IHEX0:    LODSB                ; Get byte
  156.     ADD    BL,AL            ; Checksum
  157.     CALL    HEXB            ; Convert to hex
  158.     LOOP    IHEX0            ; Continue for all bytes
  159.     MOV    AL,BL            ; Get checksum
  160.     NOT    AL            ; Twos compliment
  161.     INC    AL            ; I don't know, its the rule
  162.     CALL    HEXB            ; Convert to hex
  163.     MOV    AX,0A0DH        ; CR/LF backwards
  164.     STOSW
  165.     MOV    AL,BYTE PTR [LEN]    ; Pick up length
  166.     XOR    AH,AH            ; Zero high byte
  167.     ADD    WORD PTR [ADDR],AX    ; Update the address
  168.     RET
  169. IHEX    ENDP
  170. ;
  171. ;    Create Intel-hex end-of-file string.
  172. ;
  173. EOF    PROC    NEAR
  174.     MOV    BL,0            ; Checksum
  175.     MOV    AL,':'
  176.     STOSB
  177.     MOV    AL,0            ; Zero length record
  178.     CALL    HEXB
  179.     MOV    AX,WORD PTR [LOAD]    ; Get load address
  180.     ADD    BL,AH            ; Checksum
  181.     ADD    BL,AL            ; Checksum
  182.     CALL    HEXW            ; Convert the word
  183.     MOV    AL,1            ; End of file record
  184.     ADD    BL,AL            ; Checksum
  185.     CALL    HEXB            ; Convert the byte
  186.     MOV    AL,BL            ; Get checksum
  187.     NOT    AL
  188.     INC    AL
  189.     CALL    HEXB            ; Put in checksum
  190.     MOV    AX,0A0DH        ; CR/LF backwards
  191.     STOSW
  192.     MOV    AL,'Z'-64        ; Control-Z
  193.     STOSB
  194.     RET
  195. EOF    ENDP
  196. ;
  197. ;    Write string in [SCR_BUF] to the output device. [DI] points one-
  198. ;    byte past the last byte in the buffer.
  199. ;
  200. CONOUT    PROC    NEAR
  201.     MOV    DX,OFFSET SCR_BUF    ; Screen buffer
  202.     MOV    CX,DI            ; Last byte used
  203.     SUB    CX,DX            ; First byte location
  204.     MOV    BX,1            ; Handle for console
  205.     MOV    AH,40H            ; Write to file/device
  206.     INT    MS_DOS
  207.     RET
  208. CONOUT    ENDP
  209. ;
  210. ; Convert Word in AX to ASCII hex string indexed by DI. AX is destroyed.
  211. ;
  212. HEXW    PROC    NEAR            ;CONVERT HEX WORD IN AX TO ASCII
  213.     PUSH    AX            ;SAVE WORD
  214.     MOV    AL,AH
  215.     CALL    HEXB            ;DISPLAY HEX BYTE
  216.     POP    AX            ;GET WORD BACK AND FALL THROUGH
  217. ;
  218. ; Convert byte in AL to ASCII hex in string indexed by DI. AX is destroyed.
  219. ;
  220. HEXB    PROC    NEAR
  221.     MOV    AH,AL            ;SAVE BYTE
  222.     SAR    AL,1
  223.     SAR    AL,1
  224.     SAR    AL,1
  225.     SAR    AL,1
  226.     CALL    HEX1
  227.     MOV    AL,AH            ;GET STARTING BYTE
  228. HEX1:    AND    AL,0FH            ;SAVE ONLY LOW FOUR BITS
  229.     ADD    AL,90H            ;ADD BIAS
  230.     DAA                ;OLD INTEL TRICK
  231.     ADC    AL,40H
  232.     DAA
  233.     STOSB
  234.     RET
  235. HEXB    ENDP
  236. HEXW    ENDP
  237. ;
  238. ;    Write string addressed by [DI] to the console until a NULL is en-
  239. ;    countered. First find the null.
  240. ;
  241. PROMPT    PROC    NEAR
  242.     MOV    DI,DX            ; Start of string
  243.     MOV    CX,MAX            ; Not more than this
  244.     XOR    AL,AL            ; Byte to look for
  245.     REPNZ    SCASB            ; Look for the byte
  246.     MOV    CX,DI            ; One past where it was
  247.     SUB    CX,DX            ; First byte of string
  248.     DEC    CX            ; Back past the null
  249.     MOV    BX,WORD PTR [CONS]    ; Handle for console
  250.     MOV    AH,40H            ; Write to file/device
  251.     INT    MS_DOS
  252.     RET
  253. PROMPT    ENDP
  254. ;
  255. OPEN    PROC    NEAR
  256.     MOV    AX,3D20H        ; Open for read/deny nothing
  257.     MOV    DX,OFFSET FNAME        ; File name
  258.     INT    MS_DOS
  259.     MOV    WORD PTR [HANDLE],AX    ; Save file access word
  260.     RET
  261. OPEN    ENDP
  262. ;
  263. CLOSE    PROC    NEAR
  264.     MOV    AX,3E00H        ; Close file
  265.     MOV    BX,WORD PTR [HANDLE]    ; Get file handle
  266.     INT    MS_DOS
  267.     RET
  268. CLOSE    ENDP
  269. ;
  270. READ    PROC    NEAR
  271.     MOV    AX,3F00H        ; Read from file
  272.     MOV    CX,512            ; Bytes to read
  273.     MOV    DX,OFFSET BUFFER    ; Where to put the date
  274.     MOV    BX,WORD PTR [HANDLE]    ; File access handle
  275.     INT    MS_DOS
  276.     MOV    WORD PTR [BYTES],AX    ; Save bytes read.
  277.     OR    AX,AX            ; Check for zero bytes read
  278.     RET
  279. READ    ENDP
  280. ;
  281. PRP0    DB    CR,LF
  282.     DB    (80 - @LEN1) /2 DUP (' ')
  283.     LINE1    <>
  284.     DB    CR,LF
  285.     DB    (80 - (@LEN2+@VERS)) /2 DUP (' ')
  286.     LINE2    <>
  287.     VERS    <>
  288.     DB    CR,LF
  289.     DB    (80 - @LEN3) /2 DUP (' ')
  290.     LINE3    <>
  291.     DB    CR,LF,0
  292. PRP1    DB    CR,LF,'File ',0
  293. PRP2    DB    ' has been opened.'
  294.     DB    CR,LF,'Hit [Enter] to start data flow: ',0
  295. PRP3    DB    ' not found. Aborting.',0
  296. PRP4    DB    CR,LF,'Usage:'
  297.     DB    CR,LF,'IHEX filename.typ               (Output to screen)'
  298.     DB    CR,LF,'IHEX filename.typ> Outfile.typ  (Output to a file) '
  299.     DB    CR,LF,'IHEX filename.typ> COM1:        (Output to a modem)'
  300. CRLF    DB    CR,LF,0
  301. MAX    EQU    $ - PRP4        ; Longest string
  302. FNAME    DB    64 DUP (0)
  303. HANDLE    DW    ?            ; File handle.
  304. CONS    DW    1            ; Console handle.
  305. LOAD    DW    100H            ; Normal load address
  306. ADDR    DW    ?            ; Running address
  307. BYTES    DW    ?            ; Bytes read.
  308. LEN    DB    32            ; String length
  309. SCR_BUF    LABEL    BYTE            ; Output buffer
  310. BUFFER    EQU    $ + 4192
  311. ;
  312. PSEG    ENDS
  313.     END    MAIN
  314.  
  315.