Skip to content

Commit 4b33636

Browse files
committed
add assembly article
1 parent 510de89 commit 4b33636

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

docs/c&cpp/assembly.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: assembly
3+
tags:
4+
- assembly
5+
- c&cpp
6+
createTime: 2025/02/28 19:25:24
7+
permalink: /article/tr4ayjno/
8+
---
9+
# visual studio 2022 编写汇编设置
10+
+ 创建一个空 windows 程序空项目
11+
+ 右键项目选择 生成依赖项 -> 生成自定义 -> 选择 masm
12+
13+
![](../images/c&cpp/assembly/1.png)
14+
15+
![](../images/c&cpp/assembly/2.png)
16+
17+
+ 新建一个空的 cpp 文件,后缀自己换成 .asm
18+
+ 右键项目选择 属性 -> 链接器 -> 系统 -> 子系统 -> 控制台 (/SUBSYSTEM:CONSOLE)
19+
+ 右键项目选择 属性 -> 链接器 -> 系统 -> 高级 -> 入口点(自己填 main)
20+
+ 选中生成的文件右键属性 -> 常规 -> 项类型(选择 Microsoft Macro Assembler)
21+
+ 选中 x64 直接运行
22+
23+
以下是汇编代码版的 Hello World
24+
25+
```plain
26+
EXTERN GetStdHandle : PROC
27+
EXTERN WriteFile : PROC
28+
EXTERN ExitProcess : PROC
29+
.DATA?
30+
hFile QWORD ?
31+
BytesWritten DWORD ?
32+
33+
.DATA
34+
hello BYTE 'Hello world!', 13, 10
35+
36+
.CODE
37+
main PROC
38+
; https://blogs.msdn.microsoft.com/oldnewthing/20160623-00/?p=93735
39+
sub rsp, 40 ; Shadow space (4 * 8) & 1 parameter (8 bytes)
40+
; https://docs.microsoft.com/en-us/cpp/build/stack-allocation
41+
and spl, -16 ; Align to 16
42+
43+
; https://msdn.microsoft.com/library/windows/desktop/ms683231.aspx
44+
mov ecx, -11 ; DWORD nStdHandle = STD_OUTPUT_HANDLE
45+
call GetStdHandle ; Call WinApi
46+
mov hFile, rax ; Save returned handle
47+
48+
; https://msdn.microsoft.com/library/windows/desktop/aa365747.aspx
49+
mov rcx, hFile ; HANDLE hFile (here: Stdout)
50+
lea rdx, hello ; LPCVOID lpBuffer
51+
lea r9, BytesWritten ; LPDWORD lpNumberOfBytesWritten
52+
mov r8d, LENGTHOF hello ; DWORD nNumberOfBytesToWrite
53+
mov qword ptr [rsp+32], 0 ; LPOVERLAPPED lpOverlapped = NULL
54+
call WriteFile ; Call WinAPI
55+
56+
exit:
57+
; https://msdn.microsoft.com/library/windows/desktop/ms682658.aspx
58+
xor ecx, ecx ; Set RCX to null for return value
59+
call ExitProcess ; Call WinAPI to exit
60+
main ENDP
61+
62+
END
63+
```
64+

docs/images/c&cpp/assembly/1.png

101 KB
Loading

docs/images/c&cpp/assembly/2.png

46.3 KB
Loading

0 commit comments

Comments
 (0)