@rem ^
'''
@set comfile=%1
@if defined comfile goto :show
@set asmfile="%~dp0_%~nx0"
@set comfile=%asmfile:.bat=.com%
@python -x "%~f0" %*>%asmfile%
@call %asmfile%
:show
@python -x "%~f0" %comfile%|cols
@pause & goto :eof
'''
#:mode=python:#
import sys
import itertools as it
asmheader=''';@wasm /mt %~sf0 /fo=%~sdpn0.obj >nul
;@wlink file %~sdpn0.obj format dos com >nul
;@del %~sdpn0.obj
;@rem %~sdpn0.com %*
;@goto :eof 
.model tiny; :mode!=assembly-x86:
.code
        db 100h dup(?)
program:
'''
asmfooter='\nend program\n'
def titledcommands(cmds):
    for s in cmds:
        yield s
        yield "db '{%s}', 10"%s
def oneargcommands(cmds, a1s):
    for cmd in cmds:
        for a1 in a1s:
            yield cmd+' '+a1
def twoargcommands(cmds, a1s, a2s):
    for cmd in cmds:
        for a1 in a1s:
            for a2 in a2s:
                yield cmd+' '+a1+', '+a2
#       mov ax, bx
#       db '(mov ax, bx)', 10
#       В командах з адресами не використовувати імен — натомість дати відносні адреси для міток та абсолютні адреси для змінних
regs='ax bx cx dx bp si di'.split()
specregs='cs ds es ss sp'.split()
#mems=['word ptr ds:'+i for i in'[0] [10] [32] [64] [128] [256]'.split()]
mems=['word ptr ds:'+i for i in ('[0] [%d] [256] [%d]'%(ord('M'),ord('M')+ord('m')*256)).split()]
mems2=['word ptr '+i for i in '[bx] [bp] [si] [di] [bx+si] [bx+di] [bp+si] [bp+di]'.split()]
#ims=[str(i) for i in (0, 10, 100, 1000, 10000, 65535)]
ims=[str(i) for i in (0,ord('I'), 256,ord('I')+ord('i')*256)]
labels=['$', '$+%d'%ord('L'), '$+%d'%(ord('L')+ord('l')*256)]
labels2=['$', '$+%d'%ord('L')]
if __name__=='__main__':
    if len(sys.argv)==1:
        #згенерувати асемблерний файл з командами та текстом команд:
        outfile=sys.stdout #наприклад
        print(asmheader, file=outfile)
        for s in titledcommands(it.chain(
            'nop std cld lodsb stosb cmpsb ret'.split(),
            oneargcommands('push pop mul div neg not inc dec'.split(),regs+mems+mems2),
            oneargcommands('push'.split(),specregs),
            oneargcommands('pop'.split(),'ds es ss'.split()),
            twoargcommands(['mov'],regs,regs),
            twoargcommands(['mov'],['ax'],specregs),
            twoargcommands(['mov'],specregs,['ax']),
            twoargcommands(['mov'],['ax'],mems),
            twoargcommands(['mov'],mems,['ax']),
            twoargcommands(['sub'],mems,ims),
            twoargcommands(['xchg'],regs,regs),            
            twoargcommands('mov add sub cmp or xor and test'.split(),['ax'],['ax']),
            ['mov ax,cs', 'mov ds,ax', 'push bp', 'mov bp, sp', 'int 21h',
            'cmp word ptr ds:[65], 33', 'cmp word ptr ds:[65535], 33', 'cmp word ptr ds:[65], 16961',
            'shl al,1', 'shr ax,1', 'shl al, dl', 'shr ax, dl'],
            oneargcommands('jmp call'.split(), labels),
            oneargcommands('loop jne jcxz'.split(), labels2)
            )):
            print(s, file=outfile)
        print(asmfooter, file=outfile)
        #компілювати в .com (реалізовано в генерованому асемблерному файлі-батнику)
    else:
        #прочитати .com і вивести як представлення рядків байтів
        #       розбити на рядки за '\n', вивести кожен рядок як літерал
        f=open(sys.argv[1], 'rb')
        print(*f.read().split(b'\n'),sep='\n')
        f.close()