home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
assemblr
/
library
/
reform
/
reform.asm
< prev
Wrap
Assembly Source File
|
1993-07-23
|
21KB
|
660 lines
; REFORMAT ASM SOURCE
;
; reformat existing asm source code
; into preferred tab stops.
;
; all chars outside of quotes are set to lower case
;
; DISCLAIMER
; This is an old pgm, uploaded 7/93 by request
; Taking a fast look, there are things I would now do
; differently. But it works for me.
; If run with a single filename, it updates in place!
; "Handyman's special"
;
; Sandy Gant
;-----------------------------------------------------------
.MODEL SMALL
.stack 100h
code segment para public 'code'
assume cs:code, ds:nothing
;-----------------------------------------------------------
inrec db 160 dup(0) ;rec in buffer
inrecaddr dw inrec
inreclen dw 0
outrec db 160 dup(0) ;reformatted rec
outrecaddr dw outrec
outreclen dw 0
cmdptr dw 0
cmdlen dw 0
eof db 0
filespec db 160 dup(0)
inspec db 160 dup(0)
outspec db 'retab.tmp',0
inctr dw 0
inhandle dw 255
opsw db 0
outhandle dw 255
physlen dw 0
psp dw 0
quotesw db 0
rba dw 2 dup(0)
readlen equ 110
errmsg1 db 13,10,"File not found",13,10,"$"
errmsg2 db 13,10,"Disk full",13,10,"$"
errmsg3 db 13,10,"Usage: REFORM file1 is updated",13,10,"$"
;-----------------------------------------------------------
;-----------------------------------------------------------
start:
mov ax, cs
mov ds, ax
mov psp, es ;psp segment
mov es, ax
call parse ;open files
@@:
call getrec ;rec in si/recaddr, len in cx/reclen
cmp eof, 0
jne eoj
call reform
call putrec
jmp @b
eoj:
call close
abort:
mov ax, 4c00h ;eoj
int 21h
;.......................................................
reform:
mov opsw, 0
sub cx, 2 ;dont count cr/lf
ja @f
mov outreclen, 0
ret
@@:
mov quotesw, 0 ;init
mov di, outrecaddr
mov al, 0[si]
cmp al, ' ' ;labeled
je b1 ;no
;intercept db or dw in col 1 ;9/8/89
cmp word ptr [si], 'bd'
je @f
cmp word ptr [si], 'BD'
je @f
cmp word ptr [si], 'wd'
je @f
cmp word ptr [si], 'WD'
jne lbl
@@:
cmp byte ptr 2[si], ' '
je b1 ;yes
;move label
lbl: mov al, 0[si]
cmp al, ' '
je b1
cmp al, ';'
je jneat ;straight copy
cmp al, 'A' ;to l/c
jb @f
cmp al, 'Z'
ja @f
or al, 20h
@@: stosb
inc si
loop lbl
jmp dun
b1:
;skip first blank string (after label)
@@: mov al, 0[si]
cmp al, ' '
jne @f
inc si
loop @b
jdun: jmp dun
@@:
;add blanks thru 14; min=1
push cx
mov cx, 14
add cx, outrecaddr
sub cx, di
ja @f
mov cx, 1
@@: mov al, ' '
rep stosb
pop cx
;si = opcode
cod: mov al, 0[si]
cmp al, ' '
je b2
cmp al, ';'
je jneat ;straight copy
cmp al, 'A' ;to l/c
jb @f
cmp al, 'Z'
ja @f
or al, 20h
@@: stosb
inc si
loop cod
jmp dun
jneat: jmp neat
b2:
;[si] = blank after opcode
@@: mov al, 0[si]
cmp al, ' '
jne @f
inc si
loop @b
jmp dun
@@:
;[si] = field after opcode
;add blanks thru 22; min=1
push cx
mov cx, 22
add cx, outrecaddr
sub cx, di
ja @f
mov cx, 1
@@: mov al, ' '
rep stosb
pop cx
;si = operands or semi
;copy everything until semi
;but no blanks beyond 38
mov dx, di
sub dx, outrecaddr ;output len
op: mov al, 0[si]
cmp al, ';'
jne @f
cmp byte ptr -1[si], "'" ; '; is not ;
je @f
cmp byte ptr -1[si], '"' ; "; is not ;
je @f
jmp sp38
@@: cmp al, ' '
jne op20
cmp dx, 38
jb op20
;skip blanks if next nonblank is semi
push cx
mov bx, si
@@: cmp byte ptr 0[bx], ' '
jne @f
inc bx
loop @b
pop cx
jmp op20
@@: pop cx
cmp byte ptr 0[bx], ';'
jne op20 ;it's not
cmp byte ptr -1[bx], "'" ;'; is not ;
je op20
cmp byte ptr -1[bx], '"' ;"; is not ;
je op20
add cx, si ;skip blanks
sub cx, bx
mov si, bx
xor dx, dx
cmp word ptr -2[di], ' ' ;need 2 preceding blanks
je @f
mov al, ' '
stosb
cmp byte ptr -2[di], ' '
je @f
stosb
@@: mov al, 0[si]
op20:
;convert to l/c
cmp al, '"' ;test for immediate chars
je op21 ;yes
cmp al, "'"
jne op24 ;no
op21: cmp quotesw, 0 ;starting or ending
je op23 ;starting
dec quotesw ;ending- reset
jmp short op25
op23: inc quotesw
jmp short op25
op24: cmp quotesw, 0
jne op25
cmp al, 'A'
jb op25
cmp al, 'Z'
ja op25
or al, 20h ;to lower case
op25: stosb
;add space after comma if needed
cmp al, ',' ;comma
jne @f ;no
cmp quotesw, 0 ;inside a quote
jne @f ;yes
cmp byte ptr 1[si], ' ' ;following space
je @f ;yes- do nothing
mov byte ptr 0[di], ' '
inc di
inc dx
@@:
inc si
inc dx
dec cx
jz @f
jmp op
@@: jmp dun
sp38:
;add blanks thru 38; min=1
push ax
push cx
mov cx, 38
add cx, outrecaddr
sub cx, di
ja @f
mov cx, 1
cmp byte ptr -1[di], ' ' ;prev char bl
jne @f
dec cx ;no fill
@@: mov al, ' '
rep stosb
pop cx
pop ax
;copy remainder as-is
neat:
mov quotesw, 0
neatnext: lodsb
cmp al, 13
je dun
cmp al, '"' ;test for immediate chars
je neat21 ;yes
cmp al, "'"
jne neat24 ;no
neat21: cmp quotesw, 0
je neat23 ;starting
dec quotesw
jmp short neat25
neat23: inc quotesw
jmp short neat25
neat24: cmp quotesw, 0
jne neat25
cmp al, 'A'
jb neat25
cmp al, 'Z'
ja neat25
or al, 20h ;to lower case
neat25: stosb
jmp neatnext
dun:
sub di, outrecaddr
mov outreclen, di
;if line len is too long, left-shift the comment
maxlen equ 72
cmp di, maxlen
ja @f ;too long
ret ;exit
@@:
mov ax, di
sub ax, maxlen ;oversize amt
mov cx, outreclen ;loop ctr
mov di, outrecaddr ;find 1st semi
@@:
cmp byte ptr 0[di], ';'
je @f
inc di
loop @b
ret ;no comment
@@:
mov bx, di ;leave di at semi
mov cx, ax ;oversize amt
add cx, 2 ;need 2 separator spaces also
@@: cmp bx, outrecaddr
je s44 ;forget it
dec bx
cmp byte ptr 0[bx], ' '
jne @f
loop @b
dec bx
@@:
add bx, 3
;if bx < di, move [di] to [bx]
cmp bx, di
jae s44 ;no squeeze space
mov cx, outreclen ;for cx bytes
add cx, outrecaddr
sub cx, bx
add outreclen, bx ;adj len
sub outreclen, di
mov si, di
mov di, bx
rep movsb
s44:
ret
;-----------------------------------------------------------
getrec:
;set rba to read from start of next rec
mov dx, rba
mov cx, rba+2
add dx, physlen
adc cx, 0
mov rba, dx
mov rba+2, cx
call setrba
mov ah, 3fh
mov dx, inrecaddr
mov cx, readlen ;max read len
mov bx, inhandle
int 21h
cmp ax, 2 ;eof
jb seteof ;yes
mov physlen, ax ;save
inc inctr
;set inreclen
mov cx, ax
mov al, 10 ;lf
mov di, inrecaddr
repne scasb
je getlen
;no lf found
;if it has no alphmeric, trash it
mov bx, inrecaddr
mov cx, physlen
@@: cmp byte ptr 0[bx], '0'
jb getbmp
cmp byte ptr 0[bx], 'z'
jna getadj
getbmp: inc bx
loop @b
;abandon data
mov inreclen, 160
mov physlen, 160
jmp short getrec ;beginagin
seteof:
inc eof
ret
getadj:
;9/26 sub di, 2
mov byte ptr 0[di], 13
inc di
mov byte ptr 0[di], 10
inc di
getlen:
sub di, inrecaddr
mov inreclen, di
mov physlen, di
mov cx, di
;remove trailing blanks
mov si, inrecaddr
add si, inreclen
sub si, 2
@@: dec si
cmp si, inrecaddr
jb @f
cmp byte ptr 0[si], ' '
jne @f
dec inreclen
mov byte ptr 0[si], 13
mov byte ptr 1[si], 10
jmp @b
@@:
convtab:
;convert tabs to spaces
tabval equ 4
mov cx, inreclen
cmp cx, 160+1-tabval
ja getrecx ;no room to expand
mov bx, inrecaddr
@@:
cmp byte ptr 0[bx], 9
je @f
inc bx
loop @b
jmp short tabsdun
@@:
mov di, inrecaddr
add di, inreclen
mov si, di
dec si ;from
add di, tabval-2 ;to
mov cx, inreclen
add cx, inrecaddr
sub cx, bx
dec cx
std
rep movsb
cld
mov cx, tabval
@@: mov byte ptr 0[bx], ' '
inc bx
loop @b
add inreclen, tabval-1
jmp convtab ;try again
tabsdun:
;change solo semi to empty line
mov cx, inreclen
cmp cx, 3
jne @f
cmp byte ptr inrec, ';'
jne @f
dec inreclen
@@:
getrecx:
mov si, inrecaddr
mov cx, inreclen
ret
;-----------------------------------------------------------
putrec:
mov ah, 40h ;write one record
mov dx, outrecaddr
mov cx, outreclen
mov si, dx ;add cr/lf
add si, cx
mov byte ptr 0[si], 13
mov byte ptr 1[si], 10
add cx, 2
mov bx, outhandle
int 21h
ret
;-----------------------------------------------------------
;Parse the cmdline and open 2 files
parse:
mov es, psp
mov di, 81h ;point DI to command line text
mov cmdptr, di ;save addr
mov al, es:[di-1] ;len
cmp al, 2 ;less than 2 chars entered?
jb usage ;yes- show usage
@@:
xor ah, ah
mov cmdlen, ax
call parse_one
; mov ax, cmdlen ;compute remaining cmdlen
; add ax, cmdptr
; sub ax, si
; cmp ax, 2 ;less than 2 chars left
; jl usage ;yes- show usage
call open1
;
;new code - no out spec - updt-in-place
; mov cmdptr, si ;save addr of next filespec
; call parse_one
mov es, psp
cmp byte ptr es:0[si], 13
je @f
cmp byte ptr es:1[si], 13
jne usage
@@: push ds
pop es
mov di, offset filespec
mov si, offset outspec
mov cx, 160
rep movsb
;end of no out spec
;
call open2
ret
usage:
push ds
pop es
mov ah, 9 ;usage msg
mov dx, offset errmsg3
int 21h
jmp abort
;-----------------------------------------------------------------------
parse_one:
;Parse the cmdline and create a complete filespec.
;cmdptr = cmd line string
mov si, cmdptr
mov di, offset filespec ;point DI to buffer
@@: cmp byte ptr es:[si], 32 ;advance to first non-space
jne @f
inc si
jmp @b
@@: cmp byte ptr es:[si+1], ":" ;leading drive specifier?
je parse4 ;yes, then filespec is complete
mov ah, 19h ;get current drive
int 21h
add al, 65 ;convert to ASCIIZ
mov ah, ":" ;complete drive specifier
mov word ptr [di], ax ;write drive spec to buffer
add di, 2
cmp byte ptr es:[si], "\" ;leading backslash?
je parse4 ;yes, then filespec is complete
mov byte ptr [di], "\" ;fill in the backslash
inc di
mov byte ptr [di], 0
push si
push di
mov ah, 47h ;get current path at ds:si
mov si, di
xor dl, dl ;default drive
int 21h
pop di
pop si
cmp byte ptr [di], 0 ;anything returned?
je parse4 ;no, then filespec is complete
@@: inc di ;bump to end of string
cmp byte ptr [di], 0
jne @b
mov byte ptr [di], "\" ;insert trailing backslash
inc di
parse4:
mov al, es:[si] ;add filename
cmp al, ' '
je @f
cmp al, 13
je @f
mov 0[di], al
inc si
inc di
jmp parse4
@@: mov byte ptr [di], 0 ;terminator
ret
;-----------------------------------------------------------
open1:
;copy filespec to inspec
mov bx, offset filespec
push bx
mov di, offset inspec
mov cx, 160
@@: mov al, 0[bx]
mov cs:[di], al
inc bx
inc di
loop @b
;open input file
mov ax, 3d00h ;ro
pop dx
int 21h
jc @f ;new
mov inhandle, ax
ret
@@:
mov ah, 9 ;print 'File not found'
mov dx, offset errmsg1
int 21h
jmp abort
;-----------------------------------------------------------
open2:
;open/truncate output file
mov ah, 3ch
mov dx, offset outspec
mov cx, 0 ;attr
int 21h
jc @f
mov outhandle, ax
ret
@@:
mov ah, 9 ;disk full
mov dx, offset errmsg2
int 21h
jmp abort
;-----------------------------------------------------------
close:
mov ah, 3Eh ;close file
mov bx, inhandle
int 21h
mov ah, 3Eh ;close file
mov bx, outhandle
int 21h
;delete input
mov ah, 41h
mov dx, offset inspec
int 21h
;rename outpaut as input
mov ah, 56h
mov dx, offset outspec
mov di, offset inspec
push ds
pop es
int 21h
ret
;-----------------------------------------------------------
setrba:
;set rba=cx, dx
mov ax, 4200h
mov bx, inhandle
int 21h
ret
;-----------------------------------------------------------
code ends
end start