Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:multiasm:papc:chapter_6_9 [2025/12/14 17:12] – [Programming in Assembler for Windows] pczekalskien:multiasm:papc:chapter_6_9 [2025/12/14 17:58] (current) – [Pure Assembler Applications for Windows] pczekalski
Line 6: Line 6:
 ===== Programming in Assembler for Windows ===== ===== Programming in Assembler for Windows =====
 Windows OS has historically supported unmanaged code written primarily in C++. This kind of code runs directly on the CPU, but divergence in hardware platforms, such as the introduction of ARM-core-based platforms running Windows, causes incompatibility issues. Since the introduction of the .NET framework, Windows has provided developers with a safer way to execute their code, called "managed code". The difference is that managed code, typically written in C#, is executed by a .NET framework interpreter rather than being compiled into machine code, as unmanaged code is. The use of managed code brings multiple advantages for developers, including automated memory management and code isolation from the operating system. This, however, raises several challenges when integrating managed code and assembly code. In any case, the integration model is common: the assembler implements functions (usually stateless) that are later called from the high-level language and return data to it. Windows OS has historically supported unmanaged code written primarily in C++. This kind of code runs directly on the CPU, but divergence in hardware platforms, such as the introduction of ARM-core-based platforms running Windows, causes incompatibility issues. Since the introduction of the .NET framework, Windows has provided developers with a safer way to execute their code, called "managed code". The difference is that managed code, typically written in C#, is executed by a .NET framework interpreter rather than being compiled into machine code, as unmanaged code is. The use of managed code brings multiple advantages for developers, including automated memory management and code isolation from the operating system. This, however, raises several challenges when integrating managed code and assembly code. In any case, the integration model is common: the assembler implements functions (usually stateless) that are later called from the high-level language and return data to it.
-There are significant differences between x86 (32-bit) and x64 (64-bit) code, mostly in the scope of integration methods. As we're at a very low level of programming, there are no shorts, and all program flow from higher-level code to assembler code and the opposite must follow strict calling conventions.  +There are significant differences between x86 (32-bit) and x64 (64-bit) code, mostly in the scope of integration methods. As we're at a very low level of programming, there are no shorts, and all program flow from higher-level code to assembler code and the opposite must follow strict calling conventions. Details are presented in chapter [[en:multiasm:papc:chapter_6_8]].
- +
-<todo @ktokarz>Mamy gdzieś opisane metody wywoływania funkcji? Pewnie takMożesz dorzucić tu zdanie i link do innego rozdziału?</todo>+
  
 <note tip>Code written in assembler and compiled to machine code is always an unmanaged one!</note> <note tip>Code written in assembler and compiled to machine code is always an unmanaged one!</note>
 +
 +==== Pure Assembler Applications for Windows ====
 +It is possible to write an application for Windows solely in assembler. While the reason to do it is doubtful, some hints presented below, such as calling system functions, may be helpful.
 +Calls to the Windows system functions is possible via classical ''call'', and require explicit declaration of the functions as external, and linking ''kernel32.lib'' and ''user32.lib''. Use of ''legacy_stdio_definitions.lib'' and ''legacy_stdio_wide_specifiers.lib'' may be helpful when using advanced ''stdio'' functions and enumerations.\\
 +A common approach to development is to start with a stub command-line C++ application and manually convert it to assembler requirements. Visual Studio Community ([[https://visualstudio.microsoft.com/vs/community/]]) is a free version and the first choice for developing apps written in pure assembler, for Windows OSes. A great feature of such an integrated IDE is that, properly configured, it enables debugging of assembler code, even in high-level integration scenarios.
 +
 +A template of the typical pure assembler, command-line application for Windows is as follows:
 +<code asm>
 +...
 +.code 
 +hello_world_asm PROC
 +push rbp ; save frame pointer
 +mov rbp, rsp ; fix stack pointer
 +sub rsp, 8 * (4 + 2)
 +
 +.... ; here comes your code
 +
 +
 +mov rsp, rbp
 +pop rbp
 +ret
 +hello_world_asm ENDP
 +END
 +
 +</code>
 +
 +The name ''hello_world_asm'' must be specified to the compiler as the so-called entry point.
 +
 +Calling system functions, such as the system message box, requires understanding the arguments passed to them. As there is no direct assembler help, documentation of the Windows system API for C++ is helpful.
 +Code below presents the necessary components of the assembler app to call system functions (library includes are configured on the project level):
 +<code adm>
 +.data
 +STD_INPUT_HANDLE = -10
 +STD_OUTPUT_HANDLE = -11
 +STD_ERROR_HANDLE = -12
 +
 +handler dq 0
 +hello_msg db "Hello world", 0
 +info_msg  db "Info", 0
 +...
 +includelib          kernel32.lib 
 +includelib          user32.lib 
 +EXTERN MessageBoxA: PROC
 +...
 +
 + WINUSERAPI int WINAPI MessageBoxA(
 +;  RCX =>  _In_opt_ HWND hWnd,
 +;  RDX =>  _In_opt_ LPCSTR lpText,
 +;  R8  =>  _In_opt_ LPCSTR lpCaption,
 +;  R9  =>  _In_ UINT uType);
 +mov rcx, handler
 +mov rdx, offset hello_msg
 +mov r8,  offset info_msg
 +mov r9,  0 ; 0 is MB_OK
 +and rsp, not 8 
 +call MessageBoxA
 +...
 +</code>
 +The majority of standard library functions accept ASCII strings and must be terminated with a 0 byte (0 is a value), so they do not require passing the string length.
 +The ''and rsp, not 8'' instruction causes stack alignment to be required before application leave or before following system function calls.
  
 ==== Dynamic memory management considerations ==== ==== Dynamic memory management considerations ====
en/multiasm/papc/chapter_6_9.1765732376.txt.gz · Last modified: 2025/12/14 17:12 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0