home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / asm / wasm / bin2dat.asm next >
Assembly Source File  |  1987-08-13  |  11KB  |  420 lines

  1.  
  2.  Title 'Wolfware Assembler Sample Program', 'Binary to DATA Conversion'
  3.  
  4. ;=============================================================================
  5. ; Binary to DATA Statement Conversion
  6. ;
  7. ; This program converts binary files into BASIC compatible DATA statements.
  8. ; This is useful for inserting external binary data, such as separately
  9. ; assembled machine language subroutines, into BASIC programs.
  10. ;
  11. ; The following example shows how to use a machine language subroutine
  12. ; stored in data statements.  It is assumed that the size of the code is
  13. ; in SUBRTSIZE (the number of data components):
  14. ;
  15. ;   10 DEF SEG: OFFSET%=0                       'BASIC data segment
  16. ;   20 SUB$ = STRING$(255,0)                    'create string space
  17. ;   30 SUB = VARPTR(SUB$)                       'string descriptor
  18. ;   40 SUB = PEEK(SUB+1) + (PEEK(SUB+2) * 256)  'string data location
  19. ;   50 FOR OFFSET%= 0 TO SUBRTSIZE-1            'for each byte
  20. ;   60 READ DAT%                                'read a byte of data
  21. ;   70 POKE SUB + OFFSET%, DAT%                 'insert into string
  22. ;   80 NEXT OFFSET%                             'loop to get next byte
  23. ;
  24. ; Once this code has executed, the subroutine may be called through the
  25. ; variable SUB (see the sample program CLP.ASM). Note that the size of the
  26. ; code (defined above by SUFRTSIZE) is same as the size of the original
  27. ; binary file -- minus seven in the case of BSAVEd files.
  28. ;
  29. ; The files IO.MAC and IO.ASM are all needed for assembly.  Make sure that
  30. ; IO.ASM is set up for non-library assembly.  Note: this program does not
  31. ; use the runtime library IO.BIN, but rather includes the IO routines
  32. ; directly in the program.
  33.  
  34.  Display Mes1           ;show opening message
  35.  
  36.  Display Mes2           ;prompt
  37.  Input Namsiz           ;get file name
  38.  Open                   ;open file
  39.  Pop Inphan             ;store handle
  40.  Jc Main1               ;jump if error
  41.  Or Status, Open1       ;set flag
  42.  
  43.  Display Mes3           ;prompt
  44.  Input Namsiz           ;get file name
  45.  Create                 ;open file
  46.  Pop Linhan             ;store handle
  47.  Jc Main2               ;jump if error
  48.  Or Status, Open2       ;set flag
  49.  Jmps Main3
  50.  
  51. ;--- error, could not open input file
  52.  
  53. Main1
  54.  Mov Ax, Offset Err2
  55.  Jmp Criterr
  56.  
  57. ;--- error, could not open output file
  58.  
  59. Main2
  60.  Mov Ax, Offset Err3
  61.  Jmp Criterr
  62.  
  63. ;--- get execution parameters
  64.  
  65. Main3
  66.  Display Mes4   ;prompt
  67.  Input          ;get response
  68.  Uppercase      ;make upper-case
  69.  Display Al     ;display
  70.  Line           ;new line
  71.  Cmp Al, 'Y'    ;check if yes
  72.  Jne Main12     ;jump if not
  73.  Or Status, Bload ;set flag
  74.  
  75. Main12
  76.  Display Mes5   ;prompt
  77.  Input          ;get response
  78.  Uppercase      ;make upper-case
  79.  Display Al     ;display
  80.  Line           ;new line
  81.  Cmp Al, 'Y'    ;check if yes
  82.  Jne Main14     ;jump if not
  83.  Or Status, Lines ;set flag
  84.  
  85.  Display Mes6   ;prompt
  86.  Input 5        ;get string
  87.  Jz Main17      ;jump if use default
  88.  Binary         ;convert to binary
  89.  Pop Bx         ;high word
  90.  Pop Ax         ;low word
  91.  Jc Main13      ;jump if error, skip
  92.  Or Bx, Bx      ;check if too big
  93.  Jnz Main13     ;jump if so, just skip
  94.  Mov Lin, Ax    ;save starting number
  95.  
  96. Main17
  97.  Display Mes7   ;prompt
  98.  Input 5        ;get string
  99.  Jz Main14      ;jump if use default
  100.  Binary         ;convert to binary
  101.  Pop Bx         ;high word
  102.  Pop Ax         ;low word
  103.  Jc Main13      ;jump if error, skip
  104.  Or Bx, Bx      ;check if too big
  105.  Jnz Main13     ;jump if so, just skip
  106.  Mov Inc, Ax    ;save increment
  107.  Jmps Main14
  108.  
  109. ;--- illegal number
  110.  
  111. Main13
  112.  Mov Ax, Offset Err8
  113.  Jmp Criterr
  114.  
  115. ;--- finish up initialization
  116.  
  117. Main14
  118.  Load Linbuf    ;load line buffer location
  119.  
  120.  Test Status, Bload     ;check if bload
  121.  Jz Main4               ;jump if not
  122.  
  123.  Mov Bx, Offset Inpbuf  ;start of input buffer
  124.  Read Inphan, 7, Bx     ;read bload header
  125.  Jc Main15              ;jump if error
  126.  
  127.  Pop Ax         ;bytes read
  128.  Add Sp, 2      ;throw out handle
  129.  
  130.  Cmp Ax, 7              ;check if all bytes read
  131.  Jne Main16             ;jump if not, error
  132.  Cmp Byte [Bx], 0fdh    ;check if BLOAD mark
  133.  Jne Main16             ;jump if not, error
  134.  Mov Ax, [Bx+5]         ;get data size
  135.  Mov Bytcnt, Ax         ;save
  136.  Jmps Main4
  137.  
  138. ;--- error reading file
  139.  
  140. Main15
  141.  Mov Ax, Offset Err5
  142.  Jmp Criterr
  143.  
  144. ;--- illegal bload format
  145.  
  146. Main16
  147.  Mov Ax, Offset Err7
  148.  Jmp Criterr
  149.  
  150. ;--- loop for each line of data
  151.  
  152. Main4
  153.  Clear          ;clear line
  154.  
  155.  Test Status, Lines ;check if include line numbers
  156.  Jz Main5
  157.  Decimal Lin    ;make line number decimal
  158.  Append         ;add to line
  159.  Append ,,Str1  ;add space
  160.  
  161. Main5
  162.  Append ,,Str2  ;add data statment
  163.  Call Get_Byt   ;get first byte
  164.  Jc Main10      ;jump if end of file
  165.  Mov Cx, Across ;set number to put across
  166.  Jmps Main7
  167.  
  168. ;--- loop each value on the line
  169.  
  170. Main6
  171.  Push Cx
  172.  Call Get_Byt   ;get next byte
  173.  Pop Cx
  174.  Jc Main9       ;jump if end of file
  175.  Append ,,Str3  ;add comma and space before previous number
  176.  
  177. Main7
  178.  Sub Ah, Ah
  179.  Decimal Ax     ;convert to decimal string
  180.  Append         ;add to line
  181.  Loop Main6     ;loop back if more values needed on line
  182.  
  183. ;--- end of line
  184.  
  185.  Append ,,Str4  ;add CR and LF
  186.  
  187.  Cmp Cx, Across ;check if any data
  188.  Je Main8       ;jump if not, don't bother with empty DATA
  189.  Call Put_Lin   ;write line
  190.  
  191. Main8
  192.  Mov Ax, Inc    ;increment
  193.  Add Lin, Ax    ;next line number
  194.  Jc Main11      ;jump if overflow
  195.  Jmps Main4
  196.  
  197. ;--- end of file found, finished
  198.  
  199. Main9
  200.  Add Sp, 4      ;throw out line location
  201.  
  202.  Cmp Cx, Across ;check if any data
  203.  Je Main10      ;jump if not, don't bother with empty DATA
  204.  Call Put_Lin   ;write line
  205.  
  206. Main10
  207.  Line Mes8      ;display termination message
  208.  Call Cls_Fil   ;close files
  209.  Mov Ax, 4c00h  ;exit with error code 0
  210.  Int 21h
  211.  
  212. ;--- line number overflow
  213.  
  214. Main11
  215.  Mov Ax, Offset Err4
  216.  
  217. ;--- critical error entry
  218.  
  219. Criterr
  220.  Display Err1   ;display first part
  221.  Line Ax        ;display message
  222.  Call Cls_Fil   ;close files
  223.  Mov Ax, 4c01h  ;exit with error code 1
  224.  Int 21h
  225.  
  226. ;================================================
  227. ; The the next byte from the input file. The 
  228. ; carry is set if end of file, otherwise AL 
  229. ; returns the byte.
  230.  
  231. Get_Byt Proc Near
  232.  Mov Si, Inploc ;input location
  233.  Cmp Si, Inpend ;check if at end of data
  234.  Je Getbyt2
  235.  
  236. ;--- not at end, get byte
  237.  
  238. Getbyt1
  239.  Lodsb          ;get next byte
  240.  Mov Inploc, Si ;save pointer
  241.  Clc
  242.  Ret
  243.  
  244. ;--- end of buffer reached, try to read more
  245.  
  246. Getbyt2
  247.  Mov Ax, Inpsiz ;get input buffer size (max bytes to read)
  248.  
  249.  Test Status, Bload     ;check if bload format
  250.  Jz Getbyt4             ;jump if so, read max bytes
  251.  
  252.  Mov Bx, Bytcnt ;get remaining bytes to read
  253.  Or Bx, Bx      ;check if done
  254.  Jz Getbyt6     ;jump if so
  255.  
  256.  Cmp Ax, Bx     ;check if still more to read
  257.  Jbe Getbyt3    ;jump if so
  258.  Mov Ax, Bx     ;else set to remaining bytes
  259. Getbyt3
  260.  Sub Bytcnt, Ax ;reduce count for bytes read
  261.  
  262. Getbyt4
  263.  Mov Si, Offset Inpbuf  ;start of buffer
  264.  Read Inphan, Ax, Si    ;read bytes
  265.  Jc Getbyt7             ;jump if error 
  266.  
  267.  Pop Bx         ;get bytes read
  268.  Add Sp, 2      ;toss file handle
  269.  
  270.  Test Status, Bload ;check if bload format
  271.  Jz Getbyt5
  272.  Cmp Bx, Ax     ;using bload format, must have read all bytes
  273.  Jne Getbyt8    ;jump if not, error in bload file
  274.  
  275. Getbyt5
  276.  Or Bx, Bx      ;check if no bytes read
  277.  Jz Getbyt6     ;jump if so
  278.  Add Bx, Si     ;get end of buffer
  279.  Mov Inpend, Bx ;save
  280.  Jmps Getbyt1   ;jump back and get byte
  281.  
  282. ;--- finished with input
  283.  
  284. Getbyt6
  285.  Stc
  286.  Ret
  287.  
  288. ;--- error reading file
  289.  
  290. Getbyt7
  291.  Mov Ax, Offset Err5
  292.  Jmp Criterr
  293.  
  294. ;--- error in BLOAD format
  295.  
  296. Getbyt8
  297.  Mov Ax, Offset Err7
  298.  Jmp Criterr
  299.  Endp           ;Get_Byt
  300.  
  301. ;================================================
  302. ; Write the present line to the output file.
  303.  
  304. Put_Lin Proc Near
  305.  Mov Si, Offset Linbuf ;location of string
  306.  Lodsb          ;load length
  307.  Sub Ah, Ah
  308.  
  309.  Write Linhan, Ax, Si ;write line
  310.  Jc Putlin1     ;jump if error
  311.  
  312.  Pop Bx         ;get bytes read
  313.  Add Sp, 2      ;toss file handle
  314.  
  315.  Cmp Bx, Ax     ;check if all bytes written
  316.  Jne Putlin1    ;jump if not, disk full
  317.  Ret
  318.  
  319. ;--- write error or disk full
  320.  
  321. Putlin1
  322.  Mov Ax, Offset Err6
  323.  Jmp Criterr
  324.  Endp           ;Put_Lin
  325.  
  326. ;================================================
  327. ; Close the i/o files.
  328.  
  329. Cls_Fil Proc Near
  330.  Test Status, Open1     ;check if input is opened
  331.  Jz Clsfil1
  332.  Close Inphan           ;close file
  333.  
  334. Clsfil1
  335.  Test Status, Open2     ;check if output is opened
  336.  Jz Clsfil2
  337.  Close Linhan           ;close file
  338.  
  339. Clsfil2 Ret
  340.  Endp           ;Cls_Fil
  341.  
  342. ;================================================
  343. ; Program data.
  344.  
  345. Namsiz Equ 40           ;bytes allowed for file names
  346. Linsiz Equ 79+1         ;line size (plus a byte for the string length)
  347. Across Equ Linsiz-11/5  ;number of values to put on a single DATA line
  348.  
  349. Str1 Db 1,' '           ;single space
  350. Str2 Db 5,'DATA '       ;DATA statement string
  351. Str3 Db 2,', '          ;comma and space
  352. Str4 Db 2,13,10         ;end of line marker
  353.  
  354. Lin Dw 10000            ;present (default) line number
  355. Inc Dw 10               ;increment value
  356.  
  357. ;--- program status
  358.  
  359. Bload Equ 01h           ;bload format
  360. Lines Equ 02h           ;include line numbers
  361. Open1 Equ 04h           ;input file opened
  362. Open2 Equ 04h           ;output file opened
  363. Status Db 0             ;default status
  364.  
  365. ;--- initialized input data
  366.  
  367. Inpsiz Equ 2000         ;bytes in input buffer (must be at least 7)
  368. Inploc Dw Offset Inpbuf ;present input location
  369. Inpend Dw Offset Inpbuf ;end of input data
  370.  
  371. ;--- program messages
  372.  
  373. Mes1 Label Byte
  374.  Db 155
  375.  Db 13,10
  376.  Db '*** Binary to DATA Statement Conversion ***',13,10
  377.  Db 13,10
  378.  Db 'This program converts a binary file to a list of data',13,10
  379.  Db 'statements that can be used in a BASIC program.',13,10
  380.  Db 13,10
  381. Mes2 Db 30,'What file is to be converted? '
  382. Mes3 Db 47,'What file should be used to store the results? '
  383. Mes4 Db 40,13,10,'Do you wish to convert a BSAVEd file? '
  384. Mes5 Db 50,'Should the BASIC statements include line numbers? '
  385. Mes6 Db 49,'What should the starting line number be [10000]? '
  386. Mes7 Db 47,'What should the line number increment be [10]? '
  387. Mes8 Db 29,13,10,'File successfully converted'
  388.  
  389. ;--- program termination messages
  390.  
  391. Err1 Db 11,13,10,'BIN2DAT: '
  392. Err2 Db 25,'Could not open input file'
  393. Err3 Db 26,'Could not open output file'
  394. Err4 Db 25,'Line numbers out of range'
  395. Err5 Db 10,'Read error'
  396. Err6 Db 24,'Write error or disk full'
  397. Err7 Db 21,'Error in BLOAD format'
  398. Err8 Db 15,'Error in number'
  399.  
  400. ;--- external source files
  401.  
  402.  Include 'Io.Mac'       ;i/o macro interface
  403.  Include 'Io.Asm'       ;i/o routines
  404.  
  405. ;--- unitialized data (must be at end of program)
  406.  
  407. Bytcnt Label Word       ;byte count for BLOAD format files
  408.  Org +2
  409.  
  410. Inphan Label Word       ;handle for input file
  411.  Org +2
  412. Linhan Label Word       ;handle for output file
  413.  Org +2
  414.  
  415. Inpbuf Label Byte       ;input buffer
  416.  Org +Inpsiz
  417.  
  418. Linbuf Label Byte       ;output (line) buffer
  419.  Org +Linsiz
  420.