1
2;--- Masm2Htm.asm
3;--- simple masm source to html converter. Public Domain.
4;--- it's a sample for a mixed-language application (uses CRT)
5
6;--- Win32 binary:
7;--- assemble: jwasm -coff masm2htm.asm crtexe.asm
8;--- link:     link masm2htm.obj crtexe.obj msvcrt.lib
9
10;--- Linux binary:
11;--- assemble: jwasm -zcw -elf -D?MSC=0 -Fo masm2htm.o masm2htm.asm
12;--- link:     gcc -o masm2htm masm2htm.o
13
14    .386
15    .MODEL FLAT, c
16    option casemap:none
17
18?USEDYN  equ 1  ;0=use static CRT, 1=use dynamic CRT
19ifndef ?MSC
20?MSC     equ 1  ;0=use gcc, 1=use ms crt
21endif
22
23BG_COLOR     equ <E0E0E0>   ;background color
24REGS_COLOR   equ <00B000>   ;register's text color
25COM_COLOR    equ <808080>   ;comment's text color
26INST_COLOR   equ <0000E0>   ;instruction text color
27OPTR_COLOR   equ <808000>   ;operator text color
28TYPE_COLOR   equ <800000>   ;basic types text color
29
30REGS_COLORED equ 1          ;if registers are to be colored
31REGS64       equ 1          ;scan for amd64 registers?
32INST_COLORED equ 1          ;if instructions are to be colored
33OPTR_COLORED equ 1          ;if text operators are to be colored
34TYPE_COLORED equ 1          ;if basic types are to be colored
35
36printf  proto c :ptr BYTE, :VARARG
37fopen   proto c :ptr BYTE, :ptr BYTE
38fclose  proto c :ptr
39fseek   proto c :ptr, :DWORD, :DWORD
40ftell   proto c :ptr
41fread   proto c :ptr BYTE, :DWORD, :DWORD, :ptr
42fwrite  proto c :ptr BYTE, :DWORD, :DWORD, :ptr
43strcat  proto c :ptr BYTE, :ptr BYTE
44strcpy  proto c :ptr BYTE, :ptr BYTE
45strlen  proto c :ptr BYTE
46_stricmp  proto c :ptr BYTE, :ptr BYTE
47malloc  proto c :DWORD
48free    proto c :ptr
49
50lf  equ 10
51cr  equ 13
52
53SEEK_SET equ 0
54SEEK_END equ 2
55NULL     equ 0
56EOL      equ <cr,lf>
57
58CStr macro text
59local xxx
60    .const
61xxx db text,0
62    .code
63    exitm <offset xxx>
64    endm
65
66;--- errno access
67ife ?USEDYN
68externdef c errno:dword   ;errno is global var
69else
70__errno macro
71;--- if errno is to be defined as a function call
72 if ?MSC
73_errno proto c            ;ms crt
74    call _errno
75 else
76__errno_location proto c  ;gcc
77    call __errno_location
78 endif
79    mov eax,[eax]
80    exitm <eax>
81    endm
82errno textequ <__errno()>
83endif
84
85@DD macro list:VARARG
86    for directive, <list>
87local xxx
88    .const
89xxx db "&directive&",0
90    .data
91    dd offset xxx
92    endm
93    endm
94
95    .data
96
97;--- array of directives which are to be displayed "bold"
98
99dirtab label dword
100    @DD <.186>,      <.286>,      <.286c>,      <.286p>
101    @DD <.287>,      <.386>,      <.386c>,      <.386p>
102    @DD <.387>,      <.486>,      <.486p>,      <.586>
103    @DD <.586p>,     <.686>,      <.686p>,      <.8086>
104    @DD <.8087>,     <.allocstack>, <.alpha>,   <.break>
105    @DD <.code>,     <.const>,    <.continue>,  <.cref>
106    @DD <.data>,     <.data?>,    <.dosseg>,    <.else>
107    @DD <.elseif>,   <.endif>,    <.endprolog>, <.endw>
108    @DD <.err>,      <.err1>,     <.err2>,      <.errb>
109    @DD <.errdef>,   <.errdif>,   <.errdifi>,   <.erre>
110    @DD <.erridn>,   <.erridni>,  <.errnb>,     <.errndef>
111    @DD <.errnz>,    <.exit>,     <.fardata>,   <.fardata?>
112    @DD <.if>,       <.k3d>,      <.lall>,      <.lfcond>
113    @DD <.list>,     <.listall>,  <.listif>,    <.listmacro>
114    @DD <.listmacroall>, <.mmx>,  <.model>,     <.no87>
115    @DD <.nocref>,   <.nolist>,   <.nolistif>,  <.nolistmacro>
116    @DD <.pushframe>,<.pushreg>,  <.radix>,     <.repeat>
117    @DD <.safeseh>,  <.sall>,     <.savereg>,   <.savexmm128>
118    @DD <.seq>,      <.setframe>, <.sfcond>,    <.stack>
119    @DD <.startup>,  <.tfcond>,   <.until>,     <.untilcxz>
120    @DD <.while>,    <.x64>,      <.x64p>,      <.xall>
121    @DD <.xcref>,    <.xlist>,    <.xmm>
122    @DD <alias>,     <align>,     <assume>,     <catstr>
123    @DD <comm>,      <comment>,   <db>,         <dd>
124    @DD <df>,        <dosseg>,    <dq>,         <dt>
125    @DD <dw>,        <echo>,      <else>,       <elseif>
126    @DD <elseif1>,   <elseif2>,   <elseifb>,    <elseifdef>
127    @DD <elseifdif>, <elseifdifi>,<elseife>,    <elseifidn>
128    @DD <elseifidni>,<elseifnb>,  <elseifndef>, <end>
129    @DD <endif>,     <endm>,      <endp>,       <ends>
130    @DD <equ>,       <even>,      <exitm>,      <extern>
131    @DD <externdef>, <extrn>,     <for>,        <forc>
132    @DD <goto>,      <group>,     <if>,         <if1>
133    @DD <if2>,       <ifb>,       <ifdef>,      <ifdif>
134    @DD <ifdifi>,    <ife>,       <ifidn>,      <ifidni>
135    @DD <ifnb>,      <ifndef>,    <incbin>,     <include>
136    @DD <includelib>,<instr>,     <invoke>,     <irp>
137    @DD <irpc>,      <label>,     <local>,      <macro>
138    @DD <name>,      <option>,    <org>,        <page>
139    @DD <popcontext>,<proc>,      <proto>,      <public>
140    @DD <purge>,     <pushcontext>,<record>,    <repeat>
141    @DD <rept>,      <segment>,   <sizestr>,    <struc>
142    @DD <struct>,    <substr>,    <subtitle>,   <subttl>
143    @DD <textequ>,   <title>,     <typedef>,    <union>
144    @DD <while>
145
146enddirtab label dword
147
148if OPTR_COLORED
149optrtab label dword
150    @DD <.type>,     <high>,      <high32>,     <highword>
151    @DD <imagerel>,  <length>,    <lengthof>,   <low>
152    @DD <low32>,     <lowword>,   <lroffset>,   <mask>
153    @DD <offset>,    <opattr>,    <sectionrel>, <seg>
154    @DD <short>,     <size>,      <sizeof>,     <this>
155    @DD <type>,      <width>
156    @DD <eq>,        <ne>,        <ge>,         <gt>
157    @DD <le>,        <lt>,        <mod>,        <ptr>
158    @DD <dup>
159endoptrtab label dword
160endif
161
162if TYPE_COLORED
163typetab label dword
164    @DD <byte>,      <sbyte>,     <word>,       <sword>
165    @DD <dword>,     <sdword>,    <real4>,      <fword>
166    @DD <qword>,     <sqword>,    <real8>,      <tbyte>
167    @DD <real10>,    <oword>,     <ymmword>,    <near>
168    @DD <far>,       <near16>,    <near32>,     <far16>
169    @DD <far32>,     <mmword>,    <xmmword>
170endtypetab label dword
171endif
172
173if REGS_COLORED
174
175;--- array of register names which are to be displayed "colored"
176
177regtab label dword
178
179    @DD <ax>,      <bx>,      <cx>,      <dx>
180    @DD <bp>,      <sp>,      <si>,      <di>
181    @DD <al>,      <bl>,      <cl>,      <dl>
182    @DD <ah>,      <bh>,      <ch>,      <dh>
183    @DD <eax>,     <ebx>,     <ecx>,     <edx>
184    @DD <ebp>,     <esp>,     <esi>,     <edi>
185    @DD <cs>,      <ds>,      <es>,      <ss>
186    @DD <fs>,      <gs>
187    @DD <mm0>,     <mm1>,     <mm2>,     <mm3>
188    @DD <mm4>,     <mm5>,     <mm6>,     <mm7>
189    @DD <xmm0>,    <xmm1>,    <xmm2>,    <xmm3>
190    @DD <xmm4>,    <xmm5>,    <xmm6>,    <xmm7>
191    @DD <cr0>,     <cr2>,     <cr3>,     <cr4>
192if REGS64
193    @DD <rax>,     <rbx>,     <rcx>,     <rdx>
194    @DD <rbp>,     <rsp>,     <rsi>,     <rdi>
195    @DD <r8>,      <r9>,      <r10>,     <r11>
196    @DD <r12>,     <r13>,     <r14>,     <r15>
197    @DD <spl>,     <bpl>,     <sil>,     <dil>
198    @DD <r8b>,     <r9b>,     <r10b>,    <r11b>
199    @DD <r12b>,    <r13b>,    <r14b>,    <r15b>
200    @DD <r8w>,     <r9w>,     <r10w>,    <r11w>
201    @DD <r12w>,    <r13w>,    <r14w>,    <r15w>
202    @DD <r8d>,     <r9d>,     <r10d>,    <r11d>
203    @DD <r12d>,    <r13d>,    <r14d>,    <r15d>
204    @DD <xmm8>,    <xmm9>,    <xmm10>,   <xmm11>
205    @DD <xmm12>,   <xmm13>,   <xmm14>,   <xmm15>
206    @DD <cr8>
207endif
208
209endregtab label dword
210
211endif
212
213if INST_COLORED
214
215stdinsttab label dword
216    @DD <call>, <jmp>
217    @DD <ja>,  <jae>,  <jb>,  <jbe>, <jc>, <je>, <jg>, <jge>, <jl>, <jle>
218    @DD <jna>, <jnae>, <jnb>, <jnbe>,<jnc>,<jne>,<jng>,<jnge>,<jnl>,<jnle>
219    @DD <jo>,  <jp>,   <js>,  <jz>,  <jpe>
220    @DD <jno>, <jnp>,  <jns>, <jnz>, <jpo>
221    @DD <jcxz>, <jecxz>
222    @DD <loop>,  <loope>, <loopz>, <loopne>, <loopnz>
223    @DD <loopd>, <looped>,<loopzd>,<loopned>,<loopnzd>
224    @DD <loopw>, <loopew>,<loopzw>,<loopnew>,<loopnzw>
225    @DD <add>,<or>,<adc>,<sbb>,<and>,<sub>,<xor>,<cmp>
226    @DD <rcl>,<rcr>,<rol>,<ror>,<sal>,<sar>,<shl>,<shr>
227    @DD <cmps>,<cmpsb>,<cmpsd>,<cmpsw>
228    @DD <ins>,<insb>,<insd>,<insw>
229    @DD <lods>,<lodsb>,<lodsd>,<lodsw>
230    @DD <movs>,<movsb>,<movsd>,<movsw>
231    @DD <outs>,<outsb>,<outsd>,<outsw>
232    @DD <scas>,<scasb>,<scasd>,<scasw>
233    @DD <stos>,<stosb>,<stosd>,<stosw>
234    @DD <aaa>,<aad>,<aam>,<aas>
235    @DD <daa>,<das>,<lds>,<les>
236    @DD <popa>,<popad>,<popaw>,<popfd>
237    @DD <pusha>,<pushad>,<pushaw>,<pushfd>
238    @DD <pushd>,<retd>,<retw>
239    @DD <arpl>,<lldt>,<lsmw>,<ltr>,<sldt>,<smsw>,<str>,<verr>,<verw>
240    @DD <div>,<idiv>,<mul>,<neg>,<not>
241    @DD <lock>,<rep>,<repe>,<repne>,<repnz>,<repz>
242    @DD <bound>,<bsf>,<bsr>,<bswap>,<bt>,<btc>,<btr>,<bts>
243    @DD <cbw>,<cdq>,<clc>,<cld>,<cli>,<clts>,<cmc>,<cmpxchg>
244    @DD <cmpxchg8b>,<cpuid>,<cwd>,<cwde>,<dec>,<enter>,<hlt>,<imul>
245    @DD <in>,<inc>,<int>,<into>,<invlpg>,<iret>,<iretd>,<iretdf>
246    @DD <iretf>,<lahf>,<lar>,<lea>,<leave>,<lfs>,<lgdt>,<lgs>
247    @DD <lidt>,<lsl>,<lss>,<mov>,<movsx>,<movzx>,<nop>,<out>
248    @DD <pop>,<popf>,<push>,<pushw>,<pushf>,<rdmsr>,<rdpmc>,<rdtsc>
249    @DD <ret>,<retn>,<retf>,<rsm>,<sahf>,<sgdt>,<shld>,<shrd>
250    @DD <sidt>,<stc>,<std>,<sti>,<sysenter>,<sysexit>,<test>,<ud2>
251    @DD <wait>,<wbinvd>,<wrmsr>,<xadd>,<xchg>,<xlat>,<xlatb>
252    @DD <seta>,<setae>,<setb>,<setbe>,<setc>,<sete>,<setg>
253    @DD <setge>,<setl>,<setle>,<setna>,<setnae>,<setnb>,<setnbe>
254    @DD <setnc>,<setne>,<setng>,<setnge>,<setnl>,<setnle>,<setno>
255    @DD <setnp>,<setns>,<setnz>,<seto>,<setp>,<setpe>,<setpo>,<sets>,<setz>
256    @DD <cmova>,  <cmovae>,  <cmovb>,  <cmovbe>, <cmovc>, <cmove>, <cmovg>, <cmovge>, <cmovl>, <cmovle>
257    @DD <cmovna>, <cmovnae>, <cmovnb>, <cmovnbe>,<cmovnc>,<cmovne>,<cmovng>,<cmovnge>,<cmovnl>,<cmovnle>
258    @DD <cmovo>,  <cmovp>,   <cmovs>,  <cmovz>,  <cmovpe>
259    @DD <cmovno>, <cmovnp>,  <cmovns>, <cmovnz>, <cmovpo>
260endstdinsttab label dword
261
262fpuinsttab label dword
263    @DD <f2xm1>,  <fabs>,   <fadd>,    <faddp>,  <fbld>
264    @DD <fbstp>,  <fchs>,   <fclex>,   <fcmovb>, <fcmovbe>
265    @DD <fcmove>, <fcmovnb>,<fcmovnbe>,<fcmovne>,<fcmovnu>
266    @DD <fcmovu>, <fcom>,   <fcomi>,   <fcomip>, <fcomp>
267    @DD <fcompp>, <fcos>,   <fdecstp>, <fdisi>,  <fdiv>
268    @DD <fdivp>,  <fdivr>,  <fdivrp>,  <feni>,   <ffree>
269    @DD <ffreep>, <fiadd>,  <ficom>,   <ficomp>, <fidiv>
270    @DD <fidivr>, <fild>,   <fimul>,   <fincstp>,<finit>
271    @DD <fist>,   <fistp>,  <fisub>,   <fisubr>, <fld>
272    @DD <fld1>,   <fldcw>,  <fldenv>,  <fldenvd>,<fldenvw>
273    @DD <fldl2e>, <fldl2t>, <fldlg2>,  <fldln2>, <fldpi>
274    @DD <fldz>,   <fmul>,   <fmulp>,   <fnclex>, <fndisi>
275    @DD <fneni>,  <fninit>, <fnop>,    <fnsave>, <fnsaved>
276    @DD <fnsavew>,<fnstcw>, <fnstenv>, <fnstenvd>,<fnstenvw>
277    @DD <fnstsw>, <fpatan>, <fprem>,   <fprem1>, <fptan>
278    @DD <frndint>,<frstor>, <frstord>, <frstorw>,<fsave>
279    @DD <fsaved>, <fsavew>, <fscale>,  <fsetpm>, <fsin>
280    @DD <fsincos>,<fsqrt>,  <fst>,     <fstcw>,  <fstenv>
281    @DD <fstenvd>,<fstenvw>,<fstp>,    <fstsw>,  <fsub>
282    @DD <fsubp>,  <fsubr>,  <fsubrp>,  <ftst>,   <fucom>
283    @DD <fucomi>, <fucomip>,<fucomp>,  <fucompp>,<fwait>
284    @DD <fxam>,   <fxch>,   <fxrstor>, <fxsave>, <fxtract>
285    @DD <fyl2x>,  <fyl2xp1>
286endfpuinsttab label dword
287
288mmxsseinsttab label dword
289;--- todo
290endmmxsseinsttab label dword
291
292endif
293
294startstring label byte
295    db "<!DOCTYPE HTML PUBLIC ",22h,"-//W3C//DTD HTML 3.2 FINAL//EN",22h,">",EOL
296    db "<HTML>",EOL
297    db "<HEAD>",EOL
298    db "<TITLE>"
299    db 0
300startstring2 label byte
301    db "</TITLE>",EOL
302    db "</HEAD>",EOL
303    db "<BODY>",EOL
304    db "<TABLE BORDER=0 CELLSPACING=4 CELLPADDING=4 WIDTH=",22h,"100%",22h,">",EOL
305    db '<TR BGCOLOR="#', @CatStr(!', %BG_COLOR,!'), '"><TD>', EOL
306    db "<pre>",EOL
307    db 0
308endstring label byte
309    db "</pre>",EOL
310    db "</TD></TR>",EOL
311    db "</TABLE>",EOL
312    db "</BODY>",EOL
313    db "</HTML>",EOL
314    db 0
315
316fgcolor label byte
317    db '<font color="#'
318    db 0
319
320fgcolorrest label byte
321    db "</font>"
322    db 0
323
324    .CODE
325
326is_valid_id_first_char proc stdcall
327    cmp al,'.'
328    jz yes
329    cmp al,'$'
330    jz yes
331    cmp al,'?'
332    jz yes
333    cmp al,'@'
334    jz yes
335    cmp al,'_'
336    jz yes
337    cmp al,'A'
338    jb no
339    cmp al,'Z'
340    jbe yes
341    cmp al,'a'
342    jb no
343    cmp al,'z'
344    jbe yes
345no:
346    stc
347    ret
348yes:
349    clc
350    ret
351    align 4
352is_valid_id_first_char endp
353
354is_valid_id_char proc stdcall
355    cmp al,'$'
356    jz yes
357    cmp al,'?'
358    jz yes
359    cmp al,'@'
360    jz yes
361    cmp al,'_'
362    jz yes
363    cmp al,'0'
364    jb no
365    cmp al,'9'
366    jbe yes
367    cmp al,'A'
368    jb no
369    cmp al,'Z'
370    jbe yes
371    cmp al,'a'
372    jb no
373    cmp al,'z'
374    jbe yes
375no:
376    stc
377    ret
378yes:
379    clc
380    ret
381    align 4
382is_valid_id_char endp
383
384scanreswords proc stdcall uses esi edi ebx ecx pres:ptr BYTE, tab:ptr BYTE, endtab:ptr BYTE
385    mov esi, tab
386    mov edi, pres
387    .while (esi < endtab )
388        lodsd
389        push esi
390        mov esi, eax
391        invoke _stricmp, esi, edi
392        and eax, eax
393        jz found
394        pop esi
395    .endw
396    clc
397    ret
398found:
399    mov eax, esi
400    pop esi
401    stc
402    ret
403    align 4
404scanreswords endp
405
406;--- convert masm text found in buffer
407;--- 1. put comments in gray fgcolor
408;--- 2. write directives in bold.
409;--- 3. convert '<', '>' and '&' to '&lt;', '&gt;' and '&amp;'
410
411convertbuffer proc uses ebx esi edi fname:ptr BYTE, buffer:ptr BYTE, size_:DWORD, psize:ptr DWORD
412
413local outb:dword
414local startword:dword
415local firstdir:byte
416local lastchar:byte
417local inquotes:byte
418local inangles:byte
419local incomment:byte
420local cnt:word
421local startline:dword
422
423    mov eax, size_
424    shl eax, 2          ;use size*4 for output buffer size
425    invoke malloc, eax
426    .if ( eax == 0 )
427        invoke printf, CStr(<"out of memory",lf>)
428        mov ecx, psize
429        xor eax, eax
430        mov [ecx], eax
431        ret
432    .endif
433    mov ebx, eax
434    mov edi, eax
435    mov startline, edi
436    mov startword, 0
437    mov firstdir, 0
438    mov inquotes, 0
439    mov inangles, 0
440    mov incomment, 0
441    mov esi, offset startstring
442    .while ( byte ptr [esi] )
443        movsb
444    .endw
445    mov esi, fname
446    .while ( byte ptr [esi] )
447        movsb
448    .endw
449    mov esi, offset startstring2
450    .while ( byte ptr [esi] )
451        movsb
452    .endw
453    mov esi, buffer
454    mov ecx, size_
455    .while (ecx)
456        lodsb
457        .if ( incomment == 0 && inquotes == 0 && inangles == 0 )
458            .if ( startword == 0 )
459                call is_valid_id_first_char
460                jc @F
461                .if ( edi > startline )
462                   push eax
463                   mov al,[edi-1]
464                   call is_valid_id_char
465                   pop eax
466                   jnc @F
467                .endif
468                mov startword, edi
469@@:
470            .else
471                call is_valid_id_char
472                .if (CARRY?)
473                    ;end of id found.
474                    push eax
475                    ;terminate id with a 0
476                    mov byte ptr [edi], 0
477                    .if ( firstdir == 0 )
478                        ;check for directive
479                        invoke scanreswords, startword, offset dirtab, offset enddirtab
480                        .if (CARRY?)  ;found?
481                            mov firstdir,1
482                            mov edi, startword
483                            push esi
484                            mov esi, eax
485                            mov ax,"b<"
486                            stosw
487                            mov al,'>'
488                            stosb
489                            .while ( byte ptr [esi] )
490                                movsb
491                            .endw
492                            mov eax,">b/<"
493                            stosd
494                            pop esi
495                            mov startword, 0
496                        .endif
497                    .endif
498if INST_COLORED
499                    mov edx, startword
500                    .if ( edx && ( edx == startline || byte ptr [edx-1] != '.' ) )
501                        ;check for instruction
502                        mov al,[edx]
503                        or al,20h
504                        .if ( al == 'f')
505                            invoke scanreswords, startword, offset fpuinsttab, offset endfpuinsttab
506                        .else
507                            invoke scanreswords, startword, offset stdinsttab, offset endstdinsttab
508                        .endif
509                        .if (CARRY?)  ;found?
510                            mov edi, startword
511                            push esi
512                            mov esi, offset fgcolor
513                            .while ( byte ptr [esi] )
514                                movsb
515                            .endw
516                            mov esi, CStr( @CatStr(!", %INST_COLOR, !" ) )
517                            .while ( byte ptr [esi] )
518                                movsb
519                            .endw
520                            mov esi, eax
521                            mov ax,'>"'
522                            stosw
523                            .while ( byte ptr [esi] )
524                                movsb
525                            .endw
526                            mov esi, offset fgcolorrest
527                            .while ( byte ptr [esi] )
528                                movsb
529                            .endw
530                            pop esi
531                            mov startword, 0
532                        .endif
533                    .endif
534endif
535if REGS_COLORED
536                    .if ( startword )
537                        ;check for register names
538                        invoke scanreswords, startword, offset regtab, offset endregtab
539                        .if (CARRY?)  ;found?
540                            mov edi, startword
541                            push esi
542                            mov esi, offset fgcolor
543                            .while ( byte ptr [esi] )
544                                movsb
545                            .endw
546                            mov esi, CStr( @CatStr(!", %REGS_COLOR, !" ) )
547                            .while ( byte ptr [esi] )
548                                movsb
549                            .endw
550                            mov esi, eax
551                            mov ax,'>"'
552                            stosw
553                            .while ( byte ptr [esi] )
554                                movsb
555                            .endw
556                            mov esi, offset fgcolorrest
557                            .while ( byte ptr [esi] )
558                                movsb
559                            .endw
560                            pop esi
561                            mov startword, 0
562                        .endif
563                    .endif
564endif
565if OPTR_COLORED
566                    .if ( startword )
567                        ;check for operator names
568                        invoke scanreswords, startword, offset optrtab, offset endoptrtab
569                        .if (CARRY?)  ;found?
570                            mov edi, startword
571                            push esi
572                            mov esi, offset fgcolor
573                            .while ( byte ptr [esi] )
574                                movsb
575                            .endw
576                            mov esi, CStr( @CatStr(!", %OPTR_COLOR, !" ) )
577                            .while ( byte ptr [esi] )
578                                movsb
579                            .endw
580                            mov esi, eax
581                            mov ax,'>"'
582                            stosw
583                            .while ( byte ptr [esi] )
584                                movsb
585                            .endw
586                            mov esi, offset fgcolorrest
587                            .while ( byte ptr [esi] )
588                                movsb
589                            .endw
590                            pop esi
591                            mov startword, 0
592                        .endif
593                    .endif
594endif
595if TYPE_COLORED
596                    .if ( startword )
597                        ;check for operator names
598                        invoke scanreswords, startword, offset typetab, offset endtypetab
599                        .if (CARRY?)  ;found?
600                            mov edi, startword
601                            push esi
602                            mov esi, offset fgcolor
603                            .while ( byte ptr [esi] )
604                                movsb
605                            .endw
606                            mov esi, CStr( @CatStr(!", %TYPE_COLOR, !" ) )
607                            .while ( byte ptr [esi] )
608                                movsb
609                            .endw
610                            mov esi, eax
611                            mov ax,'>"'
612                            stosw
613                            .while ( byte ptr [esi] )
614                                movsb
615                            .endw
616                            mov esi, offset fgcolorrest
617                            .while ( byte ptr [esi] )
618                                movsb
619                            .endw
620                            pop esi
621                            mov startword, 0
622                        .endif
623                    .endif
624endif
625                    pop eax
626                    mov startword, 0
627                .endif
628            .endif
629        .endif
630        .if ( al == '<' )
631            mov eax, ";tl&"
632            stosd
633            mov al,'<'
634        .elseif ( al == '>' )
635            mov eax, ";tg&"
636            stosd
637            mov al,'>'
638        .elseif ( al == '&' )
639            mov eax, "pma&"
640            stosd
641            mov al, ";"
642            stosb
643            mov al,'&'
644        .else
645            .if ( incomment && ( al == cr || al == lf ))
646            ;.if ( ( al == cr || al == lf ) && incomment != 0 )
647                push esi
648                mov esi, offset fgcolorrest
649                .while ( byte ptr [esi] )
650                    movsb
651                .endw
652                pop esi
653            .endif
654            stosb
655        .endif
656        .if ( al == lf || al == cr )
657            mov startline, edi
658            mov startword, 0
659            mov firstdir, 0
660            mov inquotes, 0
661            mov inangles, 0
662            mov incomment, 0
663        .elseif ( al == ';' && incomment == 0 )
664            .if ( inquotes == 0 && inangles == 0 )
665                mov incomment, 1
666                push esi
667                push eax
668                mov esi, offset fgcolor
669                .while ( byte ptr [esi] )
670                    movsb
671                .endw
672                mov esi, CStr( @CatStr(!", %COM_COLOR, !" ) )
673                .while ( byte ptr [esi] )
674                    movsb
675                .endw
676                mov ax,'>"'
677                stosw
678                pop eax
679                pop esi
680            .endif
681        .elseif ( al == '<' && incomment == 0 && lastchar != '!' )
682            inc inangles
683        .elseif ( al == '>' && incomment == 0 && lastchar != '!' && inangles )
684            dec inangles
685        .elseif ( al == '"' && incomment == 0 && lastchar != '!' )
686            .if ( inquotes == al )
687                mov inquotes, 0
688            .else
689                mov inquotes, al
690            .endif
691        .elseif ( al == "'" && incomment == 0 && lastchar != '!' )
692            .if ( inquotes == al )
693                mov inquotes, 0
694            .else
695                mov inquotes, al
696            .endif
697        .endif
698        mov lastchar, al
699        dec ecx
700    .endw
701    mov esi, offset endstring
702    .while ( byte ptr [esi] )
703        movsb
704    .endw
705    mov eax, ebx
706    sub edi, eax
707    mov ecx, psize
708    mov [ecx], edi
709    ret
710    align 4
711convertbuffer endp
712
713main proc c argc:dword, argv:ptr
714
715local filename:ptr BYTE
716local filesize:dword
717local buffer:dword
718local outbuf:dword
719local outbsize:dword
720local fname[260]:byte
721
722    .if ( argc < 2 )
723        invoke printf, CStr(<"masm2htm v1.2, Public Domain.",lf>)
724        invoke printf, CStr(<"masm2htm is a masm to html converter.",lf>)
725        invoke printf, CStr(<"usage: masm2htm input_file [output_file]",lf>)
726        mov eax,1
727        ret
728    .endif
729    mov ebx,argv
730    mov ebx,[ebx+1*4]
731    invoke fopen, ebx, CStr("rb")
732    .if ( eax )
733        mov filename, ebx
734
735        mov ebx, eax
736        invoke fseek, ebx, 0, SEEK_END
737        invoke ftell, ebx
738        .if ( eax == -1 )
739            invoke printf, CStr(<"file %s is invalid",lf>), filename
740            invoke fclose, ebx
741            mov eax,1
742            ret
743        .endif
744        mov filesize, eax
745        invoke fseek, ebx, 0, SEEK_SET
746
747        mov eax, filesize
748        inc eax
749        invoke malloc, eax
750        .if ( eax == 0 )
751            invoke printf, CStr(<"out of memory",lf>)
752            invoke fclose, ebx
753            mov eax,1
754            ret
755        .endif
756        mov buffer, eax
757
758        invoke fread, buffer, 1, filesize, ebx
759        push eax
760        invoke fclose, ebx
761        pop eax
762        .if ( eax != filesize )
763            invoke printf, CStr(<"read error [%u]",lf>), errno
764            mov eax,1
765            ret
766        .endif
767
768        mov edx, buffer
769        mov byte ptr [edx+eax],0
770        invoke convertbuffer, filename, buffer, filesize, addr outbsize
771
772        push eax
773        invoke free, buffer
774        pop eax
775        .if ( eax )
776            mov outbuf, eax
777            mov edx, argv
778            mov ebx, [edx+1*4]
779            .if ( argc == 2 )
780                invoke strlen, ebx
781                add eax, ebx
782                .while (eax != ebx && \
783                        byte ptr [eax-1] != ':' && \
784                        byte ptr [eax-1] != '\' && \
785                        byte ptr [eax-1] != '/')
786                    dec eax
787                .endw
788                lea ebx, fname
789                invoke strcpy, ebx, eax
790                invoke strcat, ebx, CStr(".txt")
791            .else
792                mov ebx, argv
793                mov ebx, [ebx+2*4]
794            .endif
795            invoke fopen, ebx, CStr("wb")
796            .if ( eax )
797                mov ebx, eax
798                invoke fwrite, outbuf, 1, outbsize, ebx
799                .if ( eax != outbsize )
800                    invoke printf, CStr(<"write error [%u]",lf>), errno
801                .endif
802                invoke fclose, ebx
803                invoke printf, CStr(<"Done. %u bytes written",lf>), outbsize
804            .else
805                invoke printf, CStr(<"open('%s') failed [%u]",lf>), ebx, errno
806            .endif
807            invoke free, outbuf
808        .endif
809    .else
810        invoke printf, CStr(<"open('%s') failed [%u]",lf>), ebx, errno
811    .endif
812    xor eax,eax
813    ret
814    align 4
815
816main endp
817
818    END
819
820