x64 Assembly Tutorial 1 : Getting into x64 ASM from C++
VS2017中64位汇编设置
1) 新建一个Visual C++类型的空项目;
2)右键新建的空项目,选择“生成依赖项”---> “生成自定义”,勾选 “masm”,如下图所示:
3) 在项目"源文件"右键选择添加C++源文件,为文件命名时,选择.asm后缀,即可开始在该asm文件中填写汇编代码了。用该方法添加code.asm文件,编写如下汇编代码:
.code GetValueFromASM proc mov rax, 1234 ret GetValueFromASM endp end
注意,必须先作MASM设置,然后添加asm汇编源文件,否则会报错。
4) 在项目中添加main.cpp, 代码如下:
#include <iostream> using namespace std; extern "C" int GetValueFromASM(); /* // 32bits asm code, NOT work for x64 asm int GetValueFromASM32bits { _asm { mov eax, 32 } } */ int main() { cout << "Value is: " << GetValueFromASM() << endl; int id; cin >> id; }
5) 编译运行程序!
x64 Assembly Integer Data Types - x64汇编中的数据长度
Type | bits | Bytes | C++ Type | unsigned or signed | Other |
---|---|---|---|---|---|
Bit | 1
|
- | bit | - | - |
byte | 8
|
1 | char | yes | - |
word | 16
|
2 | short | yes | - |
dword (double word) | 32
|
4 | int | yes | - |
qword (quad word) | 64
|
8 | long long | yes | - |
xmmword | 128
|
16 | - | - | SSE support |
ymmword | 256
|
32 | - | - | AVX support |
x64 Register Set
rax : eax, ax, ah, al
rbx : ebx, bx, bh, bl
rcx : ecx, cx, ch, cl
rdx : edx, dx, dh, dl
rsi : esi, si
rdi : edi, di
rbp : ebp, bp
rsp : esp, sp
rip: eip, ip
x64 new general purpose register:
r8, r9, r10, r11, r12, r13, r14, r15
each of them can be used as:
r8: r8d, r8w, r8b
NO ds, es or ss in x64 register set!