1
2;--- Win32 sample that shows how to use JWasm.dll. Public Domain.
3;--- assemble+link with MS link:
4;---  JWasm -coff JWasmDyn.asm
5;---  link /subsystem:console JWasmDyn.obj
6;--- assemble+link with jwlink:
7;---  JWasm JWasmDyn.asm
8;---  JWLink format win pe file JWasmDyn.obj
9
10    .386
11    .MODEL FLAT, stdcall
12    option casemap:none
13
14?VERBOSE equ 0
15
16STD_OUTPUT_HANDLE equ -11
17
18GetCommandLineA proto
19WriteFile      proto :ptr, :ptr, :dword, :ptr, :ptr
20GetStdHandle   proto :dword
21ExitProcess    proto :dword
22LoadLibraryA   proto :ptr
23FreeLibrary    proto :ptr
24GetProcAddress proto :ptr, :ptr
25wvsprintfA     proto :ptr, :ptr, :ptr
26
27    includelib kernel32.lib
28    includelib user32.lib
29
30CStr macro text:vararg
31local x
32    .const
33x   db text,0
34    .code
35    exitm <offset x>
36    endm
37
38protoParseCmdline   typedef proto :ptr ptr BYTE, :ptr DWORD
39LPFNPARSECMDLINE    typedef ptr protoParseCmdline
40protoAssembleModule typedef proto :ptr BYTE
41LPFNASSEMBLEMODULE  typedef ptr protoAssembleModule
42
43    .DATA
44
45hConsole dd 0
46cmdline  dd 2 dup (0)   ;argument for ParseCmdline is an array of strings!
47
48    .CODE
49
50;--- simple printf()
51
52printf proc c fmt:ptr byte, args:VARARG
53
54local dwWritten:dword
55local szText[128]:byte
56
57    invoke wvsprintfA, addr szText, fmt, addr args
58    lea ecx, dwWritten
59    invoke WriteFile, hConsole, addr szText, eax, ecx, 0
60    ret
61
62printf endp
63
64main proc
65
66local cnt:dword
67local AssembleModule:LPFNASSEMBLEMODULE
68local ParseCmdline:LPFNPARSECMDLINE
69
70    invoke GetStdHandle, STD_OUTPUT_HANDLE
71    mov hConsole,eax
72
73;--- prepare assembly step: load JWasm.dll and its function addresses
74
75    mov esi, CStr("jwasm")
76    invoke LoadLibraryA, esi
77    .if ( !eax )
78        invoke printf, CStr("LoadLibrary('%s') failed",13,10), esi
79        jmp @exit
80    .endif
81    mov ebx, eax
82    mov esi, CStr("ParseCmdline")
83    invoke GetProcAddress, ebx, esi
84    .if ( !eax )
85        invoke printf, CStr("GetProcAddress('%s') failed",13,10), esi
86        jmp @exit
87    .endif
88    mov ParseCmdline, eax
89    mov esi, CStr("AssembleModule")
90    invoke GetProcAddress, ebx, esi
91    .if ( !eax )
92        invoke printf, CStr("GetProcAddress('%s') failed",13,10), esi
93        jmp @exit
94    .endif
95    mov AssembleModule, eax
96
97;--- get cmdline string (skip first name)
98    invoke GetCommandLineA
99    mov esi, eax
100    .while (byte ptr [esi] != ' ' && byte ptr [esi] != 0 )
101        inc esi
102    .endw
103    mov cmdline, esi
104
105;--- do 2 steps in a loop:
106;--- 1. scan commandline, read options and filename
107;--- 2. assemble the file
108
109    mov cnt, 0
110    .while (1)
111        invoke ParseCmdline, offset cmdline, addr cnt
112        .break .if (!eax)
113        mov esi, eax
114if ?VERBOSE
115        invoke printf, CStr("ParseCmdline() returned '%s'",13,10), esi
116endif
117        invoke AssembleModule, esi
118if ?VERBOSE
119        invoke printf, CStr("AssembleModule('%s') returned %u",13,10), esi, eax
120endif
121    .endw
122if ?VERBOSE
123    invoke printf, CStr("ParseCmdline() returned NULL",13,10)
124endif
125
126;--- done. release JWasm.dll
127
128    invoke FreeLibrary, ebx
129@exit:
130    xor eax,eax
131    ret
132main endp
133
134;--- entry
135
136mainCRTStartup proc c
137
138    invoke main
139    invoke ExitProcess, eax
140
141mainCRTStartup endp
142
143    end mainCRTStartup
144