Warning, /frameworks/syntax-highlighting/autotests/input/highlight.asm-nasm is written in an unsupported language. File is not indexed.
0001 ; Example file for nasm.xml kate syntax file 0002 ; compile with `nasm example.asm -f elf -o example.o` 0003 ; and link with 'gcc example.o -o example` 0004 ; Public domain 0005 ; kate: hl Intel x86 (NASM); 0006 0007 section .data 0008 0009 hello dd 'Hello World', 0x0A, 0h 0010 printf_param dd '%s', 0q 0011 0012 section .text 0013 0014 extern printf 0015 0016 global main 0017 main: 0018 push ebp 0019 mov ebp, esp 0020 0021 push hello 0022 push printf_param 0023 call printf 0024 0025 mov eax, 0b 0026 leave 0027 ret 0028 0029 0030 NULL EQU 0 ; Constants 0031 STD_OUTPUT_HANDLE EQU -11 0032 0033 extern GetStdHandle ; Import external symbols 0034 0035 global Start ; Export symbols. The entry point 0036 0037 section .data ; Initialized data segment 0038 Message db "Console Message 64", 0Dh, 0Ah 0039 MessageLength EQU $-Message ; Address of this line ($) - address of Message 0040 0041 section .bss ; Uninitialized data segment 0042 alignb 8 0043 Written resq 1 0044 0045 section .text ; Code segment 0046 Start: 0047 sub RSP, 8 ; Align the stack to a multiple of 16 bytes 0048 0049 mov ECX, STD_OUTPUT_HANDLE 0050 call GetStdHandle 0051 mov qword [REL StandardHandle], RAX 0052 0053 sub RSP, 32 + 8 + 8 ; Shadow space + 5th parameter + align stack 0054 ; to a multiple of 16 bytes 0055 mov RCX, qword [REL StandardHandle] ; 1st parameter 0056 lea RDX, [REL Message] ; 2nd parameter 0057 mov R8, MessageLength ; 3rd parameter 0058 mov qword [RSP + 4 * 8], NULL ; 5th parameter 0059 ; Message Box, 64 bit. V1.02 0060 .DisplayMessageBox: 0061 xor ECX, ECX ; 1st parameter 0062 lea RDX, [REL MessageBoxText] ; 2nd parameter 0063 lea R8, [REL MessageBoxCaption] ; 3rd parameter 0064 mov R9D, MB_YESNO | MB_DEFBUTTON2 ; 4th parameter. 2 constants ORed together 0065 call MessageBoxA 0066 0067 cmp RAX, IDNO ; Check the return value for "No" 0068 je .DisplayMessageBox 0069 0070 extern _GetStdHandle@4 ; Import external symbols 0071 Start: 0072 call _WriteFile@20 0073 0074 section .data ; Initialized data segment 0075 Static1Colour dd 0F0F0F0h, 0076 Edit2 resq 1 0077 0078 %define Screen.Width RBP - 160 ; 4 bytes 0079 %define ClientArea RBP - 152 ; RECT structure. 16 bytes 0080 0081 mov dword [wc.cbSize], 80 ; [RBP - 136] 0082 mov dword [wc.style], CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW ; [RBP - 132] 0083 mov qword [wc.lpfnWndProc], RAX ; [RBP - 128] 0084 mov qword [wc.hIcon], RAX ; [RBP - 104] 0085 mov dword [RSP + 4 * 8], EAX ; X position, now centred 0086 0087 cmp qword [uMsg], WM_CLOSE ; [RBP + 24] 0088 je WMCLOSE 0089 call DestroyWindow ; Send a WM_DESTROY message 0090 jmp Return.WM_Processed 0091 mov EDX, 0604060h 0092 call SetBkColor 0093 mov EDX, 0005000h 0094 0095 db 0x55 ; just the byte 0x55 0096 db 0x55,0x56,0x57 ; three bytes in succession 0097 db 'a',0x55 ; character constants are OK 0098 db 'hello',13,10,'$' ; so are string constants 0099 dw 0x1234 ; 0x34 0x12 0100 dw 'a' ; 0x61 0x00 (it's just a number) 0101 dw 'ab' ; 0x61 0x62 (character constant) 0102 dw 'abc' ; 0x61 0x62 0x63 0x00 (string) 0103 dd 0x12345678 ; 0x78 0x56 0x34 0x12 0104 dd 1.234567e20 ; floating-point constant 0105 dq 0x123456789abcdef0 ; eight byte constant 0106 dq 1.234567e20 ; double-precision float 0107 dt 1.234567e20 ; extended-precision float 0108 0109 db 33 0110 db (44) ; Integer expression 0111 ; db (44,55) ; Invalid - error 0112 db %(44,55) 0113 db %('XX','YY') 0114 db ('AA') ; Integer expression - outputs single byte 0115 db %('BB') ; List, containing a string 0116 db ? 0117 db 6 dup (33) 0118 db 6 dup (33, 34) 0119 db 6 dup (33, 34), 35 0120 db 7 dup (99) 0121 db 7 dup dword (?, word ?, ?) 0122 dw byte (?,44) 0123 dw 3 dup (0xcc, 4 dup byte ('PQR'), ?), 0xabcd 0124 dd 16 dup (0xaaaa, ?, 0xbbbbbb) 0125 dd 64 dup (?) 0126 db `\u263a` ; UTF-8 smiley face 0127 db `\xe2\x98\xba` ; UTF-8 smiley face 0128 db 0E2h, 098h, 0BAh ; UTF-8 smiley face 0129 buffer: resb 64 ; reserve 64 bytes 0130 wordvar: resw 1 ; reserve a word 0131 buffer: db 64 dup (?) ; reserve 64 bytes 0132 wordvar: dw ? ; reserve a word 0133 0134 incbin "file.dat" ; include the whole file 0135 incbin "file.dat",1024 ; skip the first 1024 bytes 0136 incbin "file.dat",1024,512 ; skip the first 1024, and 0137 ; actually include at most 512 0138 0139 message db 'hello, world' 0140 msglen equ $-message 0141 0142 buffer: db 'hello, world' 0143 times 64-$+buffer db ' ' 0144 0145 bndstx [rbx*1+rax+0x3], bnd0 ; GAS - '*1' indecates an index reg 0146 VDIVPS zmm4, zmm5, dword [rbx]{1to16} ; single-precision float 0147 VDIVPS zmm4, zmm5, zword [rbx] ; packed 512 bit memory 0148 0149 mov ax,200 ; decimal 0150 mov ax,0200 ; still decimal 0151 mov ax,0200d ; explicitly decimal 0152 mov ax,0d200 ; also decimal 0153 mov ax,0c8h ; hex 0154 mov ax,$0c8 ; hex again: the 0 is required 0155 mov ax,0xc8 ; hex yet again 0156 mov ax,0hc8 ; still hex 0157 mov ax,310q ; octal 0158 mov ax,310o ; octal again 0159 mov ax,0o310 ; octal yet again 0160 mov ax,0q310 ; octal yet again 0161 mov ax,11001000b ; binary 0162 mov ax,1100_1000b ; same binary constant 0163 mov ax,1100_1000y ; same binary constant once more 0164 mov ax,0b1100_1000 ; same binary constant yet again 0165 mov ax,0y1100_1000 ; same binary constant yet again 0166 0167 %define u(x) __?utf16?__(x) 0168 %define w(x) __?utf32?__(x) 0169 0170 dw u('C:\WINDOWS'), 0 ; Pathname in UTF-16 0171 dd w(`A + B = \u206a`), 0 ; String in UTF-32 0172 0173 db -0.2 ; "Quarter precision" 0174 dw -0.5 ; IEEE 754r/SSE5 half precision 0175 dd 1.2 ; an easy one 0176 dd 1.222_222_222 ; underscores are permitted 0177 dd 0x1p+2 ; 1.0x2^2 = 4.0 0178 dq 0x1p+32 ; 1.0x2^32 = 4 294 967 296.0 0179 dq 1.e10 ; 10 000 000 000.0 0180 dq 1.e+10 ; synonymous with 1.e10 0181 dq 1.e-10 ; 0.000 000 000 1 0182 dt 3.141592653589793238462 ; pi 0183 do 1.e+4000 ; IEEE 754r quad precision 0184 0185 mov rax,__?float64?__(3.141592653589793238462) 0186 mov rax,0x400921fb54442d18 0187 0188 %define Inf __?Infinity?__ 0189 %define NaN __?QNaN?__ 0190 0191 dq +1.5, -Inf, NaN ; Double-precision constants 0192 dt 12_345_678_901_245_678p 0193 dt -12_345_678_901_245_678p 0194 dt +0p33 0195 dt 33p 0196 0197 dq b * (a // b) + (a %% b) 0198 0199 call (seg procedure):procedure 0200 call weird_seg:(procedure wrt weird_seg) 0201 0202 push dword 33 0203 push strict dword 33 0204 0205 times (label-$) db 0 0206 0207 label: db 'Where am I?' 0208 times (label-$+1) db 0 0209 label: db 'NOW where am I?' 0210 0211 label1 ; some code 0212 0213 .loop 0214 ; some more code 0215 0216 jne .loop 0217 ret 0218 0219 label1: ; a non-local label 0220 .local: ; this is really label1.local 0221 ..@foo: ; this is a special symbol 0222 label2: ; another non-local label 0223 .local: ; this is really label2.local 0224 0225 jmp ..@foo ; this will jump three lines up 0226 0227 0228 %define THIS_VERY_LONG_MACRO_NAME_IS_DEFINED_TO \ 0229 THIS_VALUE 0230 0231 %define ctrl 0x1F & 0232 %define param(a,b) ((a)+(a)*(b)) 0233 0234 mov byte [param(2,ebx)], ctrl 'D' 0235 mov byte [(2)+(2)*(ebx)], 0x1F & 'D' 0236 0237 %define a(x) 1+b(x) 0238 %define b(x) 2*x 0239 0240 mov ax,a(8) 0241 0242 %define foo (a,b) ; no arguments, (a,b) is the expansion 0243 %define bar(a,b) ; two arguments, empty expansion 0244 0245 %define ereg(foo,) e %+ foo 0246 mov eax,ereg(dx,cx) 0247 0248 %define xyzzy(=expr,&val) expr, str 0249 %define plugh(x) xyzzy(x,x) 0250 db plugh(3+5), `\0` ; Expands to: db 8, "3+5", `\0` 0251 0252 mov ax,Foo%[__?BITS?__] ; The Foo value 0253 %xdefine Bar Quux ; Expands due to %xdefine 0254 %define Bar %[Quux] ; Expands due to %[...] 0255 0256 0257 0258 %define BDASTART 400h ; Start of BIOS data area 0259 0260 struc tBIOSDA ; its structure 0261 .COM1addr RESW 1 0262 .COM2addr RESW 1 0263 ; ..and so on 0264 endstruc 0265 0266 mov ax,BDASTART + tBIOSDA.COM1addr 0267 mov bx,BDASTART + tBIOSDA.COM2addr 0268 ; Macro to access BIOS variables by their names (from tBDA): 0269 0270 %idefine Foo mov %?,%?? 0271 0272 %idefine keyword $%? 0273 0274 %idefine pause $%? ; Hide the PAUSE instruction 0275 %define foo bar 0276 %undef foo 0277 0278 mov eax, foo 0279 %assign i i+1 0280 %defstr test TEST 0281 %define test 'TEST' 0282 %defstr PATH %!PATH ; The operating system PATH variable 0283 0284 %deftok test 'TEST' 0285 0286 %define test TEST 0287 0288 %define greedy(a,b,c+) a + 66 %, b * 3 %, c 0289 0290 db greedy(1,2) ; db 1 + 66, 2 * 3 0291 db greedy(1,2,3) ; db 1 + 66, 2 * 3, 3 0292 db greedy(1,2,3,4) ; db 1 + 66, 2 * 3, 3, 4 0293 db greedy(1,2,3,4,5) ; db 1 + 66, 2 * 3, 3, 4, 5 0294 0295 %macro silly 2 0296 %2: db %1 0297 %endmacro 0298 0299 silly 'a', letter_a ; letter_a: db 'a' 0300 silly 'ab', string_ab ; string_ab: db 'ab' 0301 silly {13,10}, crlf ; crlf: db 13,10 0302 %pragma preproc sane_empty_expansion 0303 0304 %macro mpar 1-* 0305 db %{3:5} 0306 db %{-1:-3} 0307 %endmacro 0308 0309 mpar 1,2,3,4,5,6 0310 0311 %macro die 0-1 "Painful program death has occurred." 0312 0313 writefile 2,%1 0314 mov ax,0x4c01 0315 int 0x21 0316 0317 %endmacro 0318 0319 %macro multipush 1-* 0320 0321 %rep %0 0322 push %1 0323 %rotate 1 0324 %endrep 0325 0326 j%-1 %%skip 0327 ret 0328 %%skip: 0329 0330 %endmacro 0331 0332 0333 %macro foo 1.nolist 0334 0335 %endmacro 0336 0337 %macro pushparam 1 0338 0339 %ifidni %1,ip 0340 call %%label 0341 %%label: 0342 %else 0343 push %1 0344 %endif 0345 0346 %endmacro 0347 0348 %assign i 0 0349 %rep 64 0350 inc word [table+2*i] 0351 %assign i i+1 0352 %endrep 0353 0354 0355 fibonacci: 0356 %assign i 0 0357 %assign j 1 0358 %rep 100 0359 %if j > 65535 0360 %exitrep 0361 %endif 0362 dw j 0363 %assign k j+i 0364 %assign i j 0365 %assign j k 0366 %endrep 0367 0368 fib_number equ ($-fibonacci)/2 0369 0370 0371 %include "macros.mac" 0372 0373 0374 %ifndef MACROS_MAC 0375 %define MACROS_MAC 0376 ; now define some macros 0377 %endif 0378 0379 %pathsearch MyFoo "foo.bin" 0380 0381 %imacro incbin 1-2+ 0 0382 %pathsearch dep %1 0383 %depend dep 0384 incbin dep,%2 0385 %endmacro 0386 %use altreg 0387 %use 'altreg' 0388 0389 %push foobar 0390 0391 %macro repeat 0 0392 0393 %push repeat 0394 %$begin: 0395 0396 %endmacro 0397 0398 %macro until 1 0399 0400 j%-1 %$begin 0401 %pop 0402 0403 %endmacro 0404 0405 %define %$localmac 3 0406 0407 %macro else 0 0408 0409 %ifctx if 0410 %repl else 0411 jmp %$ifend 0412 %$ifnot: 0413 %else 0414 %error "expected `if' before `else'" 0415 %endif 0416 0417 %endmacro 0418 0419 %macro endif 0 0420 0421 %ifctx if 0422 %$ifnot: 0423 %pop 0424 %elifctx else 0425 %$ifend: 0426 %pop 0427 %else 0428 %error "expected `if' or `else' before `endif'" 0429 %endif 0430 0431 %endmacro 0432 0433 some_function: 0434 %push mycontext ; save the current context 0435 %stacksize small ; tell NASM to use bp 0436 %assign %$localsize 0 ; see text for explanation 0437 %local old_ax:word, old_dx:word 0438 0439 enter %$localsize,0 ; see text for explanation 0440 mov [old_ax],ax ; swap ax & bx 0441 mov cx,[old_dx] 0442 leave ; restore old bp 0443 ret ; 0444 0445 %pop ; restore original context 0446 0447 0448 %ifdef F1 0449 ; do some setup 0450 %elifdef F2 0451 ; do some different setup 0452 %else 0453 %error "Neither F1 nor F2 was defined." 0454 %endif 0455 %ifdef F1 0456 ; do some setup 0457 %elifdef F2 0458 ; do some different setup 0459 %else 0460 %warning "Neither F1 nor F2 was defined, assuming F1." 0461 %define F1 0462 %endif%if foo > 64 0463 %assign foo_over foo-64 0464 %error foo is foo_over bytes too large 0465 %endif 0466 0467 db __?NASM_VER?__ 0468 0469 struc mytype 0470 mt_long: resd 1 0471 .str: resb 32 0472 endstruc 0473 0474 %use altreg 0475 0476 0477 %macro writefile 2+ 0478 0479 [section .data] 0480 0481 %%str: db %2 0482 %%endstr: 0483 0484 __?SECT?__ 0485 0486 mov dx,%%str 0487 mov cx,%%endstr-%%str 0488 mov bx,%1 0489 mov ah,0x40 0490 int 0x21 0491 0492 %endmacro 0493 0494 DEFAULT BND 0495 call foo ; BND will be prefixed 0496 nobnd call foo ; BND will NOT be prefixed 0497 0498 global _main 0499 _main: 0500 global hashlookup:function, hashtable:data 0501 0502 common intvar 4 0503 static foo 0504 foo: 0505 ; codes 0506 0507 %pragma macho lprefix L_ 0508 ; The most common conventions 0509 %pragma output gprefix _ 0510 %pragma output lprefix L_ 0511 ; ELF uses a different convention 0512 %pragma elf gprefix ; empty 0513 %pragma elf lprefix .L 0514 0515 section .pdata rdata align=4 0516 dd main wrt ..imagebase 0517 dd main_end wrt ..imagebase 0518 dd xmain wrt ..imagebase 0519 section .xdata rdata align=8 0520 xmain: db 9,0,0,0 0521 dd handler wrt ..imagebase 0522 section .drectve info 0523 db '/defaultlib:user32.lib /defaultlib:msvcrt.lib '