- Mastering Assembly Programming
- Alexey Lyashko
- 699字
- 2021-08-20 10:23:30
Setting up the Assembly project
Unfortunately, Visual Studio, by default, has no template for Assembly language projects, therefore, we have to create one ourselves:
- Launch Visual Studio and create an empty solution, as shown in the following screenshot:

Look at the bottom-right part of the Start Page window, where you will see the option to create a blank solution. If there is no such option, click on More project templates... and select Blank Solution from there.
- Once the solution has been created for us, we may add a new project. Right-click on the name of the solution and go to Add | New Project:

As Visual Studio has no built-in template for an Assembly project, we will add an empty C++ project to our solution:

- Choose a name for the project and click on OK. There are a two more things we have to do before we can add source files. To be more precise, we can add sources and then take care of these two things, as the order does not really matter. Just keep in mind that we will not be able to build (or, correctly build) our project before we take care of these.
- The first thing to take care of is setting the subsystem for the project; otherwise, the linker will not know what kind of executable to generate.
Right-click on the project name in the Solution Explorer tab and go to Properties. In the project properties window, we go to Configuration Properties | Linker | System and select Windows (/SUBSYSTEM:WINDOWS) under SubSystem:

- The next step is to tell Visual Studio that this is an Assembly language project:

- Right-click on the project name and go to Build Dependencies in the context menu, click on Build Customizations…, and from the build customizations window, select masm(.targets, .props):

- We are now ready to add the first Assembly source file:

Unfortunately, Visual Studio does not seem to be prepared for Assembly projects and, therefore, has no built-in template for Assembly files. So, we right-click on Source Files in the Solution Explorer, select New Item under Add, and since there is no template for the Assembly source file, we select C++ File (.cpp), but set the name of the file with the .asm extension. We click on Add, and voila! Our first Assembly source file is shown in the IDE.
- Just for fun, let's add some code:
.686
.model flat, stdcall
; this is a comment
; Imported functions
ExitProcess proto uExitCode:DWORD
MessageBoxA proto hWnd:DWORD, lpText:DWORD, lpCaption:DWORD,
uType:DWORD
; Here we tell assembler what to put into data section
.data
msg db 'Hello from Assembly!', 0
ti db 'Hello message', 0
; and here what to put into code section
.code
; This is our entry point
main PROC
push 0 ; Prepare the value to return to the
; operating system
push offset msg ; Pass pointer to MessageBox's text to
; the show_message() function
push offset ti ; Pass pointer to MessageBox's title to
; the show_message() function
call show_message ; Call it
call ExitProcess ; and return to the operating system
main ENDP
; This function's prototype would be:
; void show_message(char* title, char* message);
show_message PROC
push ebp
mov ebp, esp
push eax
push 0 ; uType
mov eax, [dword ptr ebp + 8]
push eax ; lpCaption
mov eax, [dword ptr ebp + 12]
push eax ; lpText
push 0 ; hWnd
call MessageBoxA ; call MessageBox()
pop eax
mov esp, ebp
pop ebp
ret 4 * 2 ; Return and clean the stack
show_message ENDP
END main
Do not worry if the code does not "speak" to you yet; we will begin to get acquainted with the instructions and program structure in Chapter 3, Intel Instruction Set Architecture (ISA).
Right now, let's build the project and run it. The code does not do much, as it simply displays a message box and terminates the program:

By now, we have a working setup for Assembly development on Windows.
- Learn to Create WordPress Themes by Building 5 Projects
- 兩周自制腳本語言
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- Mastering Ubuntu Server
- Data Analysis with Stata
- 網絡爬蟲原理與實踐:基于C#語言
- Mastering Apache Spark 2.x(Second Edition)
- Lighttpd源碼分析
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Android系統下Java編程詳解
- Mastering Concurrency in Python
- Maven for Eclipse
- SAP Web Dynpro for ABAP開發技術詳解:基礎應用
- SQL Server 2014數據庫設計與開發教程(微課版)
- Practical Linux Security Cookbook