====== Programming in Assembler for x64 ======
In this section, we will show some examples of programs written purely in assembler or in connection with other programming languages, including C++ and C#. We assume that the reader is familiar with the coursebook, instructions and directives used to write the assembler programs. We will describe the use of the integrated development environment (Visual Studio) and methods to assemble programs with the command line only.We will also show how to create the static and dynamic library written in assembler for use in assembler or in other compilers.
===== Introduction to the x64 Assembler programming in MASM - Microsoft Visual Studio Community Edition =====
In the following chapter, we explain how to write, assemble, link, and execute assembly-language programs for x64 processors. We assume that the reader is familiar with the most important processor instructions and MASM directives.
To write a program in assembler, it is convenient to use a Visual Studio IDE: either commercial or free (Community Edition). The following section presents the installation of Visual Studio Community Edition on Windows.
Visual Studio Community, Professional, and Enterprise are other products than Visual Studio Code. Here, we do not use Visual Studio Code!
==== Installing VS Community ====
Installation requires the following simple steps:
- Download installer executable from: [[https://visualstudio.microsoft.com/free-developer-offers/]] - **Mind to choose Community Edition (purple), not Code (blue)!** as in figure {{ref>vscommunityinstall1}}.
- Run the installer, let it download and install.
- Configure components - a minimal set of development platforms for our laboratory exercises requires:
- full install of C++ development platform in native code for Windows (remember to click additional components as in the figure {{ref>vscommunityinstall2}}),
- default install of the C# development platform in managed code for Windows.
This scenario concerns the implementation of a command-line Windows x64 application written in pure Assembler.
Assembling, debugging, disassembly window, register view, memory view - data section,
TO BE DONE
===== Standalone assembly =====
It is possible to use command-line MASM tools to assemble, link, and create libraries written in assembly language. You can use any editor to create the assembler source code and translate it into machine code. The tools required are integral elements of the Visual Studio Community installation, installed with the option "Desktop development with C++". For the default VS installation, you can find them in the following folder (it can change due to different version numbers).
C:\Program Files\Microsoft Visual Studio\18\Community\VS\Tools\MSVC\14.50.35717\bin\Hostx64\x64
To use statically included Windows libraries, you need lib files. The essential library is kernel32.lib, but for other Windows functions, you will also need some additional libraries. All are available in the following folder (it can change due to different version numbers).
C:\Program FIles (x86)\Windows Kits\10\Lib\10.0.26100.0\um\x64
For assembling the source file, the ML64.exe program is used. This program has many options, which you can see executing:
ML64.exe /?
After assembling, ML64 can call the linker automatically. An exemplary MASM execution command to assemble and link the file named source.asm can look like this:
ml64 /Fl /Zi /Zd source.asm /link /entry:main
The options used explanation:
* /Fl - generate listing file. MASM will output the source.lst file with the report on the assembling process.
* /Zi - add symbolic debug info. MASM will add to the object file names of symbols defined in the program. It will allow debuggers to name user-defined symbols during debugging.
* /Zd - add line number debug info. MASM will add to the object file source code line numbers.
* /link - MASM will call the linker.
* /entry:main - option for the linker, which informs about the entry point of the program.
If you prefer another name than "main" as the entry point for your console program, you will need to specify the type of the system for the resulting code. For a console application, you need to add /SUBSYSTEM:CONSOLE.
The easiest way is to put all required files in the same folder on the disk. This is not the case for more complex projects, so file names should be preceded by their full paths.