home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / FILE / XLAT11.ZIP / XLAT.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-10-19  |  9.9 KB  |  273 lines

  1. ;   XLAT.ASM  --  configurable char-to-char file converter
  2. ;
  3. ;   Freeware by TapirSoft Gisbert W.Selke, Oct 89
  4. ;
  5.  
  6. ;   TASM       xlat
  7. ;   TLINK  /T  xlat
  8.  
  9. Tab             Equ     09h
  10. Return          Equ     0Dh
  11. LineFeed        Equ     0Ah
  12. CtrlZ           Equ     1Ah
  13.  
  14.  
  15. CSEG            Segment
  16.                 Assume  CS:CSEG, DS:CSEG, ES:CSEG, SS:CSEG
  17.  
  18.                 Org     0000h
  19. CSegOfs         Label   Byte                    ; start of core image
  20.  
  21.                 Org     0080h
  22. CmdLine         Label   Byte
  23.  
  24.  
  25.                 Org     0100h
  26. Entry:          Jmp Begin
  27.  
  28. ;
  29. ;       Data:
  30. ;
  31. ProgInfo        db      Low(Offset ProgVersion) ; file offset of patch info
  32. ProgName        db      Return, 'Configurable text file converter'
  33. CopyRight       db      Return, LineFeed
  34.                 db      'FreeWare by TapirSoft Gisbert W.Selke, Oct 1989'
  35.                 db      Return, LineFeed
  36. Marker1         db      '<<<< '                 ; marker for patches
  37. Description     db      'Identity mapping as a starting point    '
  38. ; the length of the preceding string should remain at 40!
  39. Marker2         db      ' >>>>', CtrlZ          ; marker for patches
  40. EndHeader       Label   Byte
  41.  
  42. ProgVersion     db      'XLAT10'                ; standard name and version
  43. DescOff         db      Low(Offset Description) ; file offset of descr. text
  44. DescLength      db      Marker2-Description     ; length of description text
  45. InterNameOff    dw      (Offset InterName)-100h ; file offset of usage text
  46. MainTableOff    dw      MainTable-100h          ; file offset of main table
  47.  
  48. ; the next few lines fill the 'default' translation table with the identity
  49. ; mapping:
  50. MainTable       Label   Byte
  51.                 outcode = 0
  52. Rept 256
  53.                 db      outcode
  54.                 outcode = outcode + 1
  55. EndM
  56. MainTableEnd    Label Byte
  57.  
  58. InverseFlag     db      0                       ; 0 = normal; 1 = inverse
  59.  
  60. ReadErrString   db      'Error reading from file', Return, LineFeed
  61. ReadErrEnd      Label word
  62. WriteErrString  db      'Error writing to file', Return, LineFeed
  63. WriteErrEnd     Label word
  64.  
  65.  
  66. Begin:          Cld                             ; All string directions forward
  67.                 Call    ParseArgs               ; scan command line
  68.                 Cmp     InverseFlag, 0FFh       ; illegal parrameter?
  69.                 Je      Usage                   ; skip if so
  70.                 Cmp     InverseFlag, 1          ; use inverted table?
  71.                 Je      UseInverse              ; skip if so
  72.                 Mov     bx, Offset MainTable    ; else use table as specified
  73.                 Push    bx                      ; put on stack
  74.                 Jmp Short NextBuffer
  75.  
  76. UseInverse:     Call    BuildInverse            ; build inverse table
  77.                 Mov     bx, Offset InverseTable ; use inverted table
  78.                 Push    bx                      ; put address on stack
  79.  
  80. NextBuffer:     Mov     ah, 3Fh                 ; read buffer from stdin
  81.                 Xor     bx, bx
  82.                 Mov     cx, BufferLength
  83.                 Mov     dx, offset Buffer
  84.                 Mov     si, dx          ; jot down for later use
  85.                 Mov     di, dx          ; ditto
  86.                 Int     21h
  87.  
  88.                 Jc      ReadError       ; jump if error on read
  89.                 Or      ax, ax
  90.                 Jz      FinishIt        ; jump to end if no chars read
  91.  
  92.                 Pop     bx              ; get back tranlation table address
  93.                 Mov     cx, ax          ; number of chars actually read
  94.                 Mov     dx, ax          ; also, save for later use
  95.  
  96. ProcChar:       Lodsb                   ; process characters
  97.                 Xlat                    ; translate according to table
  98.                 Stosb                   ; stuff it back in
  99.                 Loop    ProcChar        ; process next character, if available
  100.  
  101.                 Push    bx              ; otherwise save table address
  102.                 Mov     ah, 40h
  103.                 Mov     bx, 1           ; write to stdout
  104.                 Mov     cx, dx          ; get back number of characters
  105.                 Mov     dx, offset Buffer
  106.                 Int     21h
  107.  
  108.                 Jc      WriteError
  109.                 Cmp     ax, cx
  110.                 Jne     WriteError
  111.  
  112.                 Jmp short NextBuffer    ; get next buffer
  113.  
  114.  
  115. ;
  116. ;   Handle read error:
  117. ;
  118.  
  119. ReadError:      Mov     dx, Offset ReadErrString
  120.                 Mov     cx, ReadErrEnd-ReadErrString
  121.                 Mov     ah, 40h         ; output message to stderr
  122.                 Mov     bx, 2
  123.                 Int     21h
  124.  
  125.                 Pop     bx              ; remove table address from stack
  126.                 Mov     al, 02h         ; set error flag
  127.                 Jmp Short Terminate
  128.  
  129.  
  130. ;
  131. ;   Handle write error:
  132. ;
  133.  
  134. WriteError:     Mov     dx, Offset WriteErrString
  135.                 Mov     cx, WriteErrEnd-WriteErrString
  136.                 Mov     ah, 40h         ; output message to stderr
  137.                 Mov     bx, 2
  138.                 Int     21h
  139.  
  140.                 Pop     bx              ; remove table address from stack
  141.                 Mov     al, 03h         ; set error flag
  142.                 Jmp Short Terminate
  143.  
  144.  
  145. ;
  146. ; show usage screen:
  147. ;
  148.  
  149. Usage:          Mov     dx, Offset ProgName     ; display programme header
  150.                 Mov     cx, ProgName-EndHeader
  151.                 Mov     ah, 40h                 ; output message to stderr
  152.                 Mov     bx, 2
  153.                 Int     21h
  154.  
  155.                 Mov     dx, Offset UsageText    ; display usage screen
  156.                 Mov     cx, UsageEnd-UsageText
  157.                 Mov     ah, 40h                 ; output message to stderr
  158.                 Int     21h
  159.                 Mov     al, 01h
  160.                 Jmp Short Terminate
  161.  
  162.  
  163. ;
  164. ;   We are done.
  165. ;
  166.  
  167.  
  168. FinishIt:       Pop     bx              ; remove table address from stack
  169.                 Xor     al, al          ; set no-error flag
  170.                 Jmp Short Terminate
  171.  
  172.  
  173. ;
  174. ;       Terminate
  175. ;
  176.  
  177. Terminate:      Mov     ah, 4Ch                 ; error flag was set elsewhere
  178.                 Int     21h                     ; Terminate
  179.  
  180.  
  181. ;
  182. ;       Subroutines:
  183. ;
  184.  
  185. BuildInverse    Proc    Near
  186. ;
  187. ;       Build inverse mapping table as well as we can.
  188. ;       Scan backwards; we assume that in case of non-injectivity,
  189. ;       the preferrred values are the lower character codes
  190. ;       (i.e., choose the lowest pre-image).
  191. ;       (Just as well as any other assumption, but easier to imagine)
  192. ;
  193.  
  194.                 Mov     di, Offset InverseTable ; point to inverse table
  195.                 Xor     ax, ax                  ; initialization value: 0
  196.                 Mov     cx, 256                 ; length of table
  197.                 Rep     Stosb                   ; fill table
  198.  
  199.                 Std                             ; scan original table backward
  200.                 Mov     si, Offset MainTableEnd - 1; point to end of original table
  201.                 Mov     cx, 256                 ; length of table
  202.                 Xor     dl, dl                  ; set dl to 'last char'
  203.               ; Xor     ax, ax                  ; ax is still 0
  204.  
  205. BINext:         Lodsb                           ; get next table entry
  206.                 Mov     di, ax
  207.                 Dec     dl
  208.                 Mov     byte ptr [di+InverseTable], dl; Table[Maintable[dl]] <- dl
  209.                 Loop    BINext
  210.  
  211.                 Cld                             ; scan forward again
  212.                 Ret
  213.                 EndP    BuildInverse
  214.  
  215.  
  216. ParseArgs       Proc    Near
  217. ;
  218. ;       scan command line for arguments; only arguments supported:
  219. ;       /i            : use inverse mapping
  220. ;       anything else : usage screen (sent to stderr)
  221. ;
  222.  
  223.                 Mov     si, Offset CmdLine + 1  ; point to command line
  224.  
  225. PANext:         Lodsb                           ; get next char
  226.                 Cmp     al, Return              ; at end?
  227.                 Je      PADone                  ; if so, finish
  228.                 Cmp     al, ' '                 ; ignore this?
  229.                 Je      PANext
  230.                 Cmp     al, ','                 ; ignore this?
  231.                 Je      PANext
  232.                 Cmp     al, Tab                 ; ignore this?
  233.                 Je      PANext
  234.                 Cmp     al, '/'                 ; switch char?
  235.                 Je      PASwitch                ; skip if so
  236.                 Cmp     al, '-'                 ; switch char?
  237.                 Jne     PAUsage                 ; skip if not
  238.  
  239. PASwitch:       Lodsb                           ; which switch?
  240.                 Or      al, 20h                 ; convert to lower case
  241.                 Cmp     al, 'i'                 ; request for inversion?
  242.                 Jne     PAUsage                 ; skip if not
  243.                 Mov     InverseFlag, 1          ; mark for to use inverse
  244.                 Jmp Short PANext                ; and scan on
  245.  
  246. PAUsage:        Mov     InverseFlag, 0FFh       ; otherwise illegal arg
  247.  
  248. PADone:         Ret
  249.                 EndP    ParseArgs
  250.  
  251.  
  252. ; room for inverted table:
  253. InverseTable    Label   Byte                    ; room for inverted table
  254. InverseTableEnd Equ     InverseTable + 256      ; pointer to end of same
  255.  
  256. ; text of usage screen, will be overwritten by InverseTable:
  257. UsageText       db      Return, LineFeed, 'Usage: '
  258. InterName       db      'xlat      [/i]  <input  >output', Return, LineFeed
  259.                 db      '       Specifying /i will invert the mapping.'
  260.                 db      Return, Linefeed
  261. UsageEnd        Label   Byte
  262.  
  263. ;
  264. ;       File Buffer
  265. ;
  266.  
  267. Buffer          Equ     InverseTable + 256      ; file data buffer...
  268. BufferLength    Equ     (Offset CSegOfs - Offset buffer) - 40h   ; free length
  269.                                                 ; (small stack is enough)
  270.  
  271. CSEG            EndS                            ; End of segment
  272.                 End     Entry                   ; Denotes entry point
  273.