官术网_书友最值得收藏!

Setting up the Assembly project

Unfortunately, Visual Studio, by default, has no template for Assembly language projects, therefore, we have to create one ourselves:

  1. Launch Visual Studio and create an empty solution, as shown in the following screenshot:
Creating blank VS2017 solution

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.

  1. 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:
Adding a new project to the solution

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

Creating an empty project
  1. 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.
  1. 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:

Setting the target subsystem
  1. The next step is to tell Visual Studio that this is an Assembly language project:
Opening the "Build Customizations" window
  1. 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):
Setting proper targets
  1. We are now ready to add the first Assembly source file:
Adding a new 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.

  1. 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:

Sample output

By now, we have a working setup for Assembly development on Windows.

主站蜘蛛池模板: 南京市| 邵阳县| 江孜县| 辽中县| 舟曲县| 桂阳县| 安达市| 商南县| 朔州市| 滁州市| 堆龙德庆县| 墨脱县| 运城市| 余江县| 丹寨县| 民勤县| 隆尧县| 保德县| 东港市| 兰溪市| 米易县| 江达县| 乳源| 海阳市| 峡江县| 梅州市| 宜州市| 崇阳县| 柘城县| 马鞍山市| 法库县| 巩留县| 金川县| 新干县| 哈密市| 新化县| 肥乡县| 东安县| 云浮市| 农安县| 仁怀市|