Debian Codegraph @

autotests/input/highlight.asm-nasm

raw
1 ; Example file for nasm.xml kate syntax file
2 ; compile with `nasm example.asm -f elf -o example.o`
3 ; and link with 'gcc example.o -o example`
4 ; Public domain
5 ; kate: hl Intel x86 (NASM);
6
7 section .data
8
9 hello dd 'Hello World', 0x0A, 0h
10 printf_param dd '%s', 0q
11
12 section .text
13
14 extern printf
15
16 global main
17 main:
18 push ebp
19 mov ebp, esp
20
21 push hello
22 push printf_param
23 call printf
24
25 mov eax, 0b
26 leave
27 ret
28
29
30 NULL EQU 0 ; Constants
31 STD_OUTPUT_HANDLE EQU -11
32
33 extern GetStdHandle ; Import external symbols
34
35 global Start ; Export symbols. The entry point
36
37 section .data ; Initialized data segment
38 Message db "Console Message 64", 0Dh, 0Ah
39 MessageLength EQU $-Message ; Address of this line ($) - address of Message
40
41 section .bss ; Uninitialized data segment
42 alignb 8
43 Written resq 1
44
45 section .text ; Code segment
46 Start:
47 sub RSP, 8 ; Align the stack to a multiple of 16 bytes
48
49 mov ECX, STD_OUTPUT_HANDLE
50 call GetStdHandle
51 mov qword [REL StandardHandle], RAX
52
53 sub RSP, 32 + 8 + 8 ; Shadow space + 5th parameter + align stack
54 ; to a multiple of 16 bytes
55 mov RCX, qword [REL StandardHandle] ; 1st parameter
56 lea RDX, [REL Message] ; 2nd parameter
57 mov R8, MessageLength ; 3rd parameter
58 mov qword [RSP + 4 * 8], NULL ; 5th parameter
59 ; Message Box, 64 bit. V1.02
60 .DisplayMessageBox:
61 xor ECX, ECX ; 1st parameter
62 lea RDX, [REL MessageBoxText] ; 2nd parameter
63 lea R8, [REL MessageBoxCaption] ; 3rd parameter
64 mov R9D, MB_YESNO | MB_DEFBUTTON2 ; 4th parameter. 2 constants ORed together
65 call MessageBoxA
66
67 cmp RAX, IDNO ; Check the return value for "No"
68 je .DisplayMessageBox
69
70 extern _GetStdHandle@4 ; Import external symbols
71 Start:
72 call _WriteFile@20
73
74 section .data ; Initialized data segment
75 Static1Colour dd 0F0F0F0h,
76 Edit2 resq 1
77
78 %define Screen.Width RBP - 160 ; 4 bytes
79 %define ClientArea RBP - 152 ; RECT structure. 16 bytes
80
81 mov dword [wc.cbSize], 80 ; [RBP - 136]
82 mov dword [wc.style], CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW ; [RBP - 132]
83 mov qword [wc.lpfnWndProc], RAX ; [RBP - 128]
84 mov qword [wc.hIcon], RAX ; [RBP - 104]
85 mov dword [RSP + 4 * 8], EAX ; X position, now centred
86
87 cmp qword [uMsg], WM_CLOSE ; [RBP + 24]
88 je WMCLOSE
89 call DestroyWindow ; Send a WM_DESTROY message
90 jmp Return.WM_Processed
91 mov EDX, 0604060h
92 call SetBkColor
93 mov EDX, 0005000h
94
95 db 0x55 ; just the byte 0x55
96 db 0x55,0x56,0x57 ; three bytes in succession
97 db 'a',0x55 ; character constants are OK
98 db 'hello',13,10,'$' ; so are string constants
99 dw 0x1234 ; 0x34 0x12
100 dw 'a' ; 0x61 0x00 (it's just a number)
101 dw 'ab' ; 0x61 0x62 (character constant)
102 dw 'abc' ; 0x61 0x62 0x63 0x00 (string)
103 dd 0x12345678 ; 0x78 0x56 0x34 0x12
104 dd 1.234567e20 ; floating-point constant
105 dq 0x123456789abcdef0 ; eight byte constant
106 dq 1.234567e20 ; double-precision float
107 dt 1.234567e20 ; extended-precision float
108
109 db 33
110 db (44) ; Integer expression
111 ; db (44,55) ; Invalid - error
112 db %(44,55)
113 db %('XX','YY')
114 db ('AA') ; Integer expression - outputs single byte
115 db %('BB') ; List, containing a string
116 db ?
117 db 6 dup (33)
118 db 6 dup (33, 34)
119 db 6 dup (33, 34), 35
120 db 7 dup (99)
121 db 7 dup dword (?, word ?, ?)
122 dw byte (?,44)
123 dw 3 dup (0xcc, 4 dup byte ('PQR'), ?), 0xabcd
124 dd 16 dup (0xaaaa, ?, 0xbbbbbb)
125 dd 64 dup (?)
126 db `\u263a` ; UTF-8 smiley face
127 db `\xe2\x98\xba` ; UTF-8 smiley face
128 db 0E2h, 098h, 0BAh ; UTF-8 smiley face
129 buffer: resb 64 ; reserve 64 bytes
130 wordvar: resw 1 ; reserve a word
131 buffer: db 64 dup (?) ; reserve 64 bytes
132 wordvar: dw ? ; reserve a word
133
134 incbin "file.dat" ; include the whole file
135 incbin "file.dat",1024 ; skip the first 1024 bytes
136 incbin "file.dat",1024,512 ; skip the first 1024, and
137 ; actually include at most 512
138
139 message db 'hello, world'
140 msglen equ $-message
141
142 buffer: db 'hello, world'
143 times 64-$+buffer db ' '
144
145 bndstx [rbx*1+rax+0x3], bnd0 ; GAS - '*1' indecates an index reg
146 VDIVPS zmm4, zmm5, dword [rbx]{1to16} ; single-precision float
147 VDIVPS zmm4, zmm5, zword [rbx] ; packed 512 bit memory
148
149 mov ax,200 ; decimal
150 mov ax,0200 ; still decimal
151 mov ax,0200d ; explicitly decimal
152 mov ax,0d200 ; also decimal
153 mov ax,0c8h ; hex
154 mov ax,$0c8 ; hex again: the 0 is required
155 mov ax,0xc8 ; hex yet again
156 mov ax,0hc8 ; still hex
157 mov ax,310q ; octal
158 mov ax,310o ; octal again
159 mov ax,0o310 ; octal yet again
160 mov ax,0q310 ; octal yet again
161 mov ax,11001000b ; binary
162 mov ax,1100_1000b ; same binary constant
163 mov ax,1100_1000y ; same binary constant once more
164 mov ax,0b1100_1000 ; same binary constant yet again
165 mov ax,0y1100_1000 ; same binary constant yet again
166
167 %define u(x) __?utf16?__(x)
168 %define w(x) __?utf32?__(x)
169
170 dw u('C:\WINDOWS'), 0 ; Pathname in UTF-16
171 dd w(`A + B = \u206a`), 0 ; String in UTF-32
172
173 db -0.2 ; "Quarter precision"
174 dw -0.5 ; IEEE 754r/SSE5 half precision
175 dd 1.2 ; an easy one
176 dd 1.222_222_222 ; underscores are permitted
177 dd 0x1p+2 ; 1.0x2^2 = 4.0
178 dq 0x1p+32 ; 1.0x2^32 = 4 294 967 296.0
179 dq 1.e10 ; 10 000 000 000.0
180 dq 1.e+10 ; synonymous with 1.e10
181 dq 1.e-10 ; 0.000 000 000 1
182 dt 3.141592653589793238462 ; pi
183 do 1.e+4000 ; IEEE 754r quad precision
184
185 mov rax,__?float64?__(3.141592653589793238462)
186 mov rax,0x400921fb54442d18
187
188 %define Inf __?Infinity?__
189 %define NaN __?QNaN?__
190
191 dq +1.5, -Inf, NaN ; Double-precision constants
192 dt 12_345_678_901_245_678p
193 dt -12_345_678_901_245_678p
194 dt +0p33
195 dt 33p
196
197 dq b * (a // b) + (a %% b)
198
199 call (seg procedure):procedure
200 call weird_seg:(procedure wrt weird_seg)
201
202 push dword 33
203 push strict dword 33
204
205 times (label-$) db 0
206
207 label: db 'Where am I?'
208 times (label-$+1) db 0
209 label: db 'NOW where am I?'
210
211 label1 ; some code
212
213 .loop
214 ; some more code
215
216 jne .loop
217 ret
218
219 label1: ; a non-local label
220 .local: ; this is really label1.local
221 ..@foo: ; this is a special symbol
222 label2: ; another non-local label
223 .local: ; this is really label2.local
224
225 jmp ..@foo ; this will jump three lines up
226
227
228 %define THIS_VERY_LONG_MACRO_NAME_IS_DEFINED_TO \
229 THIS_VALUE
230
231 %define ctrl 0x1F &
232 %define param(a,b) ((a)+(a)*(b))
233
234 mov byte [param(2,ebx)], ctrl 'D'
235 mov byte [(2)+(2)*(ebx)], 0x1F & 'D'
236
237 %define a(x) 1+b(x)
238 %define b(x) 2*x
239
240 mov ax,a(8)
241
242 %define foo (a,b) ; no arguments, (a,b) is the expansion
243 %define bar(a,b) ; two arguments, empty expansion
244
245 %define ereg(foo,) e %+ foo
246 mov eax,ereg(dx,cx)
247
248 %define xyzzy(=expr,&val) expr, str
249 %define plugh(x) xyzzy(x,x)
250 db plugh(3+5), `\0` ; Expands to: db 8, "3+5", `\0`
251
252 mov ax,Foo%[__?BITS?__] ; The Foo value
253 %xdefine Bar Quux ; Expands due to %xdefine
254 %define Bar %[Quux] ; Expands due to %[...]
255
256
257
258 %define BDASTART 400h ; Start of BIOS data area
259
260 struc tBIOSDA ; its structure
261 .COM1addr RESW 1
262 .COM2addr RESW 1
263 ; ..and so on
264 endstruc
265
266 mov ax,BDASTART + tBIOSDA.COM1addr
267 mov bx,BDASTART + tBIOSDA.COM2addr
268 ; Macro to access BIOS variables by their names (from tBDA):
269
270 %idefine Foo mov %?,%??
271
272 %idefine keyword $%?
273
274 %idefine pause $%? ; Hide the PAUSE instruction
275 %define foo bar
276 %undef foo
277
278 mov eax, foo
279 %assign i i+1
280 %defstr test TEST
281 %define test 'TEST'
282 %defstr PATH %!PATH ; The operating system PATH variable
283
284 %deftok test 'TEST'
285
286 %define test TEST
287
288 %define greedy(a,b,c+) a + 66 %, b * 3 %, c
289
290 db greedy(1,2) ; db 1 + 66, 2 * 3
291 db greedy(1,2,3) ; db 1 + 66, 2 * 3, 3
292 db greedy(1,2,3,4) ; db 1 + 66, 2 * 3, 3, 4
293 db greedy(1,2,3,4,5) ; db 1 + 66, 2 * 3, 3, 4, 5
294
295 %macro silly 2
296 %2: db %1
297 %endmacro
298
299 silly 'a', letter_a ; letter_a: db 'a'
300 silly 'ab', string_ab ; string_ab: db 'ab'
301 silly {13,10}, crlf ; crlf: db 13,10
302 %pragma preproc sane_empty_expansion
303
304 %macro mpar 1-*
305 db %{3:5}
306 db %{-1:-3}
307 %endmacro
308
309 mpar 1,2,3,4,5,6
310
311 %macro die 0-1 "Painful program death has occurred."
312
313 writefile 2,%1
314 mov ax,0x4c01
315 int 0x21
316
317 %endmacro
318
319 %macro multipush 1-*
320
321 %rep %0
322 push %1
323 %rotate 1
324 %endrep
325
326 j%-1 %%skip
327 ret
328 %%skip:
329
330 %endmacro
331
332
333 %macro foo 1.nolist
334
335 %endmacro
336
337 %macro pushparam 1
338
339 %ifidni %1,ip
340 call %%label
341 %%label:
342 %else
343 push %1
344 %endif
345
346 %endmacro
347
348 %assign i 0
349 %rep 64
350 inc word [table+2*i]
351 %assign i i+1
352 %endrep
353
354
355 fibonacci:
356 %assign i 0
357 %assign j 1
358 %rep 100
359 %if j > 65535
360 %exitrep
361 %endif
362 dw j
363 %assign k j+i
364 %assign i j
365 %assign j k
366 %endrep
367
368 fib_number equ ($-fibonacci)/2
369
370
371 %include "macros.mac"
372
373
374 %ifndef MACROS_MAC
375 %define MACROS_MAC
376 ; now define some macros
377 %endif
378
379 %pathsearch MyFoo "foo.bin"
380
381 %imacro incbin 1-2+ 0
382 %pathsearch dep %1
383 %depend dep
384 incbin dep,%2
385 %endmacro
386 %use altreg
387 %use 'altreg'
388
389 %push foobar
390
391 %macro repeat 0
392
393 %push repeat
394 %$begin:
395
396 %endmacro
397
398 %macro until 1
399
400 j%-1 %$begin
401 %pop
402
403 %endmacro
404
405 %define %$localmac 3
406
407 %macro else 0
408
409 %ifctx if
410 %repl else
411 jmp %$ifend
412 %$ifnot:
413 %else
414 %error "expected `if' before `else'"
415 %endif
416
417 %endmacro
418
419 %macro endif 0
420
421 %ifctx if
422 %$ifnot:
423 %pop
424 %elifctx else
425 %$ifend:
426 %pop
427 %else
428 %error "expected `if' or `else' before `endif'"
429 %endif
430
431 %endmacro
432
433 some_function:
434 %push mycontext ; save the current context
435 %stacksize small ; tell NASM to use bp
436 %assign %$localsize 0 ; see text for explanation
437 %local old_ax:word, old_dx:word
438
439 enter %$localsize,0 ; see text for explanation
440 mov [old_ax],ax ; swap ax & bx
441 mov cx,[old_dx]
442 leave ; restore old bp
443 ret ;
444
445 %pop ; restore original context
446
447
448 %ifdef F1
449 ; do some setup
450 %elifdef F2
451 ; do some different setup
452 %else
453 %error "Neither F1 nor F2 was defined."
454 %endif
455 %ifdef F1
456 ; do some setup
457 %elifdef F2
458 ; do some different setup
459 %else
460 %warning "Neither F1 nor F2 was defined, assuming F1."
461 %define F1
462 %endif%if foo > 64
463 %assign foo_over foo-64
464 %error foo is foo_over bytes too large
465 %endif
466
467 db __?NASM_VER?__
468
469 struc mytype
470 mt_long: resd 1
471 .str: resb 32
472 endstruc
473
474 %use altreg
475
476
477 %macro writefile 2+
478
479 [section .data]
480
481 %%str: db %2
482 %%endstr:
483
484 __?SECT?__
485
486 mov dx,%%str
487 mov cx,%%endstr-%%str
488 mov bx,%1
489 mov ah,0x40
490 int 0x21
491
492 %endmacro
493
494 DEFAULT BND
495 call foo ; BND will be prefixed
496 nobnd call foo ; BND will NOT be prefixed
497
498 global _main
499 _main:
500 global hashlookup:function, hashtable:data
501
502 common intvar 4
503 static foo
504 foo:
505 ; codes
506
507 %pragma macho lprefix L_
508 ; The most common conventions
509 %pragma output gprefix _
510 %pragma output lprefix L_
511 ; ELF uses a different convention
512 %pragma elf gprefix ; empty
513 %pragma elf lprefix .L
514
515 section .pdata rdata align=4
516 dd main wrt ..imagebase
517 dd main_end wrt ..imagebase
518 dd xmain wrt ..imagebase
519 section .xdata rdata align=8
520 xmain: db 9,0,0,0
521 dd handler wrt ..imagebase
522 section .drectve info
523 db '/defaultlib:user32.lib /defaultlib:msvcrt.lib '