### Embed Windows Resources with fasm2 Source: https://context7.com/tgrysztar/fasm2/llms.txt This example demonstrates how to embed various Windows resources, such as icons, bitmaps, menus, dialogs, and version information, directly into PE executables using fasm2's resource macros. It includes setting up version information for an application. ```assembly format PE64 NX GUI 5.0 entry start include 'win64a.inc' section '.text' code readable executable start: sub rsp,28h invoke MessageBoxA, 0, 'Application with resources', 'fasm2', MB_OK invoke ExitProcess, 0 section '.idata' import data readable library kernel,'KERNEL32.DLL', user,'USER32.DLL' import kernel, ExitProcess,'ExitProcess' import user, MessageBoxA,'MessageBoxA' section '.rsrc' resource data readable directory RT_VERSION,versions resource versions, 1,LANG_NEUTRAL,version versioninfo version,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN, LANG_ENGLISH+SUBLANG_DEFAULT,0, 'FileDescription','fasm2 Example Application', 'FileVersion','1.0.0.0', 'ProductName','fasm2 Demo', 'ProductVersion','1.0', 'LegalCopyright','Public Domain' ``` -------------------------------- ### Create Windows PE64 GUI Application Source: https://context7.com/tgrysztar/fasm2/llms.txt Generates a Windows graphical application using the `format PE64 GUI` directive. Optional parameters like `NX` for DEP compatibility and `DLL` for dynamic libraries can be included. This example demonstrates calling `MessageBoxA` and `ExitProcess`. ```assembly format PE64 NX GUI 5.0 entry start include 'win64a.inc' section '.data' data readable writeable _title db 'Hello World',0 _message db 'Welcome to fasm2!',0 section '.text' code readable executable start: sub rsp,28h xor ecx,ecx lea rdx,[_message] lea r8,[_title] xor r9d,r9d call [MessageBoxA] xor ecx,ecx call [ExitProcess] section '.idata' import data readable library kernel,'KERNEL32.DLL', user,'USER32.DLL' import kernel, ExitProcess,'ExitProcess' import user, MessageBoxA,'MessageBoxA' ``` -------------------------------- ### Generate ELF Object Files for Linking with fasm2 Source: https://context7.com/tgrysztar/fasm2/llms.txt This example demonstrates how to create relocatable ELF object files using the `format ELF64` directive without specifying `executable`. These files are compatible with standard linkers like `ld` or `gcc` for further linking into executables. It includes a simple `main` function and external declaration for `printf`. ```assembly format ELF64 section '.text' executable public main extrn printf main: push rbp mov rbp,rsp lea rdi,[format_string] mov esi,42 xor eax,eax ; No vector arguments call printf xor eax,eax ; Return 0 pop rbp ret section '.rodata' format_string db 'The answer is %d',10,0 ``` -------------------------------- ### Generate Linux ELF64 Executable Source: https://context7.com/tgrysztar/fasm2/llms.txt Creates a native Linux ELF64 executable using the `format ELF64 executable` directive. The optional brand parameter (e.g., 3 for Linux) and `at` for base address can be specified. This example uses `sys_write` and `sys_exit` system calls. ```assembly format ELF64 executable 3 entry start segment readable executable start: ; sys_write(1, message, length) mov edi,1 ; stdout lea rsi,[message] mov edx,14 ; length mov eax,1 ; sys_write syscall ; sys_exit(0) xor edi,edi mov eax,60 ; sys_exit syscall segment readable message db 'Hello, Linux!',10 ``` -------------------------------- ### Define Procedures with proc/endp Source: https://context7.com/tgrysztar/fasm2/llms.txt Defines procedures using the `proc` and `endp` macros, which automate prologue/epilogue generation, parameter handling, and local variable management. Supports stdcall and fastcall conventions. This example defines a `calculate_sum` procedure. ```assembly format PE64 NX GUI 5.0 entry start include 'win64a.inc' section '.text' code readable executable proc calculate_sum uses rbx rsi rdi, a, b, c local result:qword mov rax,[a] add rax,[b] add rax,[c] mov [result],rax ; Return value in rax mov rax,[result] ret endp start: sub rsp,28h fastcall calculate_sum, 10, 20, 30 ; rax now contains 60 xor ecx,ecx call [ExitProcess] section '.idata' import data readable library kernel,'KERNEL32.DLL' import kernel, ExitProcess,'ExitProcess' ``` -------------------------------- ### Define Data Structures with struct/ends Source: https://context7.com/tgrysztar/fasm2/llms.txt Creates data structures using the `struct` and `ends` macros, which handle size calculation, field access, and initialization. Structures can be nested and have default values. This example defines `POINT`, `RECT`, and `WINDOW` structures. ```assembly format PE64 console entry start include 'win64a.inc' struct POINT x dd ? y dd ? ends struct RECT left dd ? top dd ? right dd ? bottom dd ? ends struct WINDOW position POINT size POINT title dq ? visible db ? ends section '.data' data readable writeable ; Initialize structure with values mywindow WINDOW position:<100,200>, size:<640,480>, title:_title, visible:1 myrect RECT 0,0,800,600 _title db 'My Window',0 section '.text' code readable executable start: sub rsp,28h ; Access structure fields mov eax,[mywindow.position.x] ; eax = 100 mov ebx,[mywindow.size.y] ; ebx = 480 mov ecx,[myrect.right] ; ecx = 800 xor ecx,ecx call [ExitProcess] section '.idata' import data readable library kernel,'KERNEL32.DLL' import kernel, ExitProcess,'ExitProcess' ``` -------------------------------- ### Utilize AVX-512 Instructions with fasm2 Source: https://context7.com/tgrysztar/fasm2/llms.txt This example showcases the use of modern x86 instruction sets, specifically AVX-512, within fasm2. It demonstrates enabling instruction sets using the `use` directive and performing vectorized operations like broadcasting, loading, addition, multiplication, and square root with masking. ```assembly use AMD64, XSAVE, AVX512F format PE64 NX GUI 5.0 entry start section '.data' data readable writeable x dq 3.14159265389 align 64 vector_a dq 8 dup (1.0) vector_b dq 8 dup (2.0) result dq 8 dup (?) section '.text' code readable executable start: ; Check for AVX-512 support mov eax,1 cpuid and ecx,18000000h cmp ecx,18000000h jne no_avx512 ; AVX-512 operations vbroadcastsd zmm0, [x] ; Broadcast scalar to all elements vmovapd zmm1, [vector_a] ; Load 512-bit vector vmovapd zmm2, [vector_b] ; Masked operations with k registers mov al, 10101010b kmovw k1, eax vaddpd zmm3{k1}, zmm1, zmm2 ; Add with mask vmulpd zmm4, zmm0, zmm3 ; Multiply vsqrtpd zmm5, zmm4 ; Square root vmovapd [result], zmm5 ; Store result xor ecx,ecx call [ExitProcess] no_avx512: mov ecx,1 call [ExitProcess] section '.idata' import data readable library kernel,'KERNEL32.DLL' import kernel, ExitProcess,'ExitProcess' ``` -------------------------------- ### Create Boot Sector Raw Binary Output with fasm2 Source: https://context7.com/tgrysztar/fasm2/llms.txt This snippet demonstrates how to use the `format binary` directive to create raw binary output suitable for boot sectors. It also utilizes the `org` directive to set the origin address and includes basic assembly code for setting video mode and displaying a message. ```assembly ; Boot sector example (TetrOS-style) format binary as 'img' org 7C00h use16 ; Boot code xor ax,ax mov ss,ax mov sp,7C00h mov ds,ax mov es,ax ; Set video mode 13h (320x200 256 colors) mov al,13h int 10h ; Display message mov si,message mov ah,0Eh print_loop: lodsb test al,al jz done int 10h jmp print_loop done: jmp $ ; Infinite loop message db 'Hello from boot sector!',0 ; Pad to 510 bytes and add boot signature rb 510 - ($ - $$) dw 0AA55h ``` -------------------------------- ### MachO64 Executable Creation for macOS Source: https://context7.com/tgrysztar/fasm2/llms.txt This snippet demonstrates how to create a MachO64 executable for macOS using flat assembler 2. It includes system calls for writing a message to standard output and exiting the program. The code defines segments for text and data, and utilizes specific system call numbers and arguments for macOS. ```asm format MachO64 executable entry start segment '__TEXT' readable executable section '__text' start: ; sys_write(1, message, 13) mov rax,0x2000004 ; sys_write mov rdi,1 ; stdout lea rsi,[message] mov rdx,13 syscall ; sys_exit(0) mov rax,0x2000001 ; sys_exit xor rdi,rdi syscall segment '__DATA' readable writeable section '__data' message db 'Hello macOS!',10 ``` -------------------------------- ### Windows API Import Macros in Assembly Source: https://context7.com/tgrysztar/fasm2/llms.txt Demonstrates the use of 'library' and 'import' macros in fasm to generate PE import tables for Windows API functions. It handles imports by name or ordinal and automatically eliminates unused imports. ```asm format PE64 NX GUI 5.0 entry start include 'win64a.inc' section '.data' data readable writeable wc WNDCLASS style:0, lpfnWndProc:DefWindowProc, lpszClassName:_class _class db 'MyWindowClass',0 _title db 'fasm2 Window',0 msg MSG section '.text' code readable executable start: sub rsp,28h invoke GetModuleHandle,0 mov [wc.hInstance],rax invoke LoadCursor,0,IDC_ARROW mov [wc.hCursor],rax invoke RegisterClass,wc invoke CreateWindowEx,0,_class,_title,\ WS_VISIBLE+WS_OVERLAPPEDWINDOW,\ 100,100,640,480,NULL,NULL,[wc.hInstance],NULL msg_loop: invoke GetMessage,addr msg,NULL,0,0 test eax,eax jz exit invoke TranslateMessage,addr msg invoke DispatchMessage,addr msg jmp msg_loop exit: invoke ExitProcess,[msg.wParam] section '.idata' import data readable library kernel,'KERNEL32.DLL',\ user,'USER32.DLL' import kernel,\ GetModuleHandle,'GetModuleHandleA',\ ExitProcess,'ExitProcess' import user,\ RegisterClass,'RegisterClassA',\ CreateWindowEx,'CreateWindowExA',\ DefWindowProc,'DefWindowProcA',\ GetMessage,'GetMessageA',\ TranslateMessage,'TranslateMessage',\ DispatchMessage,'DispatchMessageA',\ LoadCursor,'LoadCursorA' ``` -------------------------------- ### Implement Global String Pool with GLOBSTR in fasm2 Source: https://context7.com/tgrysztar/fasm2/llms.txt This snippet illustrates the use of the `GLOBSTR` macro to collect string data into a single location, simplifying inline string usage. It shows how to define a string pool and use a custom `GLOB` macro to reference strings from this pool, enabling reuse of identical strings. ```assembly format PE64 NX GUI 5.0 entry start include 'win64a.inc' include 'globstr.inc' GLOBSTR.reuse := 1 ; Reuse identical strings section '.data' data readable writeable GLOBSTR.here ; String pool location section '.text' code readable executable ; Define inline macro for global strings inlinemacro GLOB(value) local data data GLOBSTR value,0 return equ data end inlinemacro start: sub rsp,28h ; Use GLOB() for inline string references lea rdx,[GLOB('Hello, World!')] invoke MessageBoxA, 0, rdx, GLOB('Greeting'), MB_OK lea rdx,[GLOB('Goodbye!')] invoke MessageBoxA, 0, rdx, GLOB('Farewell'), MB_OK invoke ExitProcess, 0 section '.idata' import data readable library kernel,'KERNEL32.DLL', user,'USER32.DLL' import kernel, ExitProcess,'ExitProcess' import user, MessageBoxA,'MessageBoxA' ``` -------------------------------- ### Declare Windows PE64 Console Output Format Source: https://context7.com/tgrysztar/fasm2/llms.txt Specifies the output format for a Windows PE64 console application. The `format` directive must precede any data output and configures the binary structure, entry points, and section layouts. It requires an `entry` point and defines sections for code and imported data. ```assembly format PE64 console entry start section '.text' code readable executable start: sub rsp,8 mov ecx,0 call [ExitProcess] section '.idata' import data readable library kernel,'KERNEL32.DLL' import kernel, ExitProcess,'ExitProcess' ``` -------------------------------- ### Invoke and Fastcall Macros in Assembly Source: https://context7.com/tgrysztar/fasm2/llms.txt Explains the 'invoke' and 'fastcall' macros in fasm for function calls. 'invoke' uses import table pointers with automatic argument handling, while 'fastcall' allows direct calls with support for inline strings, addresses, and floating-point registers. ```asm format PE64 NX GUI 5.0 entry start include 'win64a.inc' section '.data' data readable writeable value dq 3.14159 result dq ? section '.text' code readable executable proc my_function, param1, param2 mov rax,[param1] add rax,[param2] ret endp start: sub rsp,28h ; invoke calls through import pointer invoke MessageBoxA, 0, 'Hello from invoke!', 'Title', MB_OK ; fastcall with various argument types fastcall my_function, 100, 200 ; rax = 300 ; addr passes address of variable invoke GetModuleHandle, 0 ; Inline string argument invoke MessageBoxA, 0, 'Direct string', 'Note', MB_ICONINFORMATION invoke ExitProcess, 0 section '.idata' import data readable library kernel,'KERNEL32.DLL', user,'USER32.DLL' import kernel, GetModuleHandle,'GetModuleHandleA', ExitProcess,'ExitProcess' import user, MessageBoxA,'MessageBoxA' ``` -------------------------------- ### Alignment Directive in Assembly Source: https://context7.com/tgrysztar/fasm2/llms.txt Explains the 'align' directive in fasm for aligning code or data to specified boundaries. It supports custom fill values and can enforce specific offsets relative to alignment boundaries, crucial for performance and data integrity. ```asm format PE64 console entry start section '.text' code readable executable start: nop align 16 ; Align to 16-byte boundary hot_loop: ; Performance-critical code aligned for cache xor eax,eax ret align 4 ; Align to 4-byte boundary data_table: dd 1,2,3,4 align 16, 0x90 ; Align with NOP padding another_function: ret align 8 | 4 ; Align to 8, offset by 4 section '.data' data readable writeable align 64 ; Cache line alignment critical_data dq 8 dup (0) ``` -------------------------------- ### Data Definition Directives in Assembly Source: https://context7.com/tgrysztar/fasm2/llms.txt Details fasm's data definition directives: 'db' (byte), 'dw' (word), 'dd' (dword), 'dq' (qword), and 'dt' (tenbyte). It also covers the 'dup' operator for repetition and '?' for uninitialized space. ```asm format binary as 'bin' ; Byte definitions bytes_data: db 0x90, 0x90, 0x90 ; Three NOP bytes db 'Hello',0 ; String with null terminator db 10 dup (0) ; 10 zero bytes db 5 dup ('AB') ; "ABABABABAB" ; Word definitions (16-bit) words_data: dw 0x1234, 0x5678 dw 100 dup (?) ; 100 uninitialized words ; Dword definitions (32-bit) dwords_data: dd 12345678h dd 1.0, 2.5, 3.14159 ; 32-bit floats dd RVA some_label ; Relative virtual address ; Qword definitions (64-bit) qwords_data: dq 0x123456789ABCDEF0 dq 3.141592653589793 ; 64-bit double dq 1000 dup (0FFFFFFFFFFFFFFFFh) ; Tenbyte (80-bit extended precision) tenbytes_data: dt 3.14159265358979323846 ; 80-bit extended float some_label: db 0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.