汇编语言靠近底层, 需要对处理器原理(堆栈,寄存器,中断,内存模型)以及x86指令集有一定了解.

寄存器

实模式下(real mode)

CS (code segment)
代码段寄存器, 指向CPU当前代码段在内存中的基地址
IP
指令指针寄存器, 表示CPU当前指令在代码段中的偏移位置
所以 CS + IP 指向当前指令地址
所以PC寄存器的作用?

DS
数据段寄存器
ES
附加段寄存器
SS
栈段寄存器
FS GS
无特殊说明

ESP
栈顶指针寄存器

EBP
基址寄存器,用于基址寻址

通用寄存器
eax, ebx, ecx, edx, esi, edi, r8~r12

EAX
常用于存储函数返回值

CX
计数寄存器,是一些指令的隐式操作数,比如 REP:重复CX次。

8086 只有16bit的寄存器,但是提供了20bit的地址总线,所以8086实际上可以访问1MB的内存空间,
具体而言,8086将CS段寄存器中的值左移4bit,然后再加上IP中的偏移量,这样就构成了20bit的地址

1MB的内存最多可以划分为2^16个段,每个段16B,或16个段,每段大小64KB
段的划分是自由的,只要不超过段的最大大小(IP寄存器的容量),你想怎么分就怎么分!

保护模式(protected mode)

CS (code segment selector)
存储代码段描述符,低2位表示CPL(current privilege level)
所以根据不同代码段的低位即可区分正在执行代码的特权级.

DS (data segment selector)

指令集

X86
x86 is an instruction set, the compatible instruction set of 8086, 286, 386 when there was a personal computer.
x86-64
x86-64 sometimes abbreviated as “ x64 “, which is a kind of 64-bit microprocessor architecture and its corresponding instruction set, and is also an extension of Intel x86 architecture. “X86-64” was designed by AMD in 1999. AMD first publicized the 64-bit set to expand to IA-32, called x86-64 (later renamed AMD64). Later, it was also adopted by Intel, and Intel now calls it “ Intel 64 “.
AMD64
The AMD64 architecture adds 64-bit registers to IA-32 and is compatible with early 16-bit and 32-bit software. It can easily convert existing x86-targeted compilers to the AMD64 version.
Since AMD64 and Intel64 are basically the same, many software and hardware products use a vocabulary that does not favor either side to indicate that they are compatible with both architectures at the same time. For this purpose, AMD’s original name for this CPU architecture-
“X86-64” is used from time to time, and there is a variant “x86_64”. Other companies such as Microsoft and Sun use “x64” as an abbreviation for “x86-64” in marketing materials.
Many operating systems and products, especially those that introduced “x86-64” support before Intel entered this market, use “AMD64” or “amd64” to refer to both AMD64 and Intel64.
IA-64
Intel has previously used its own 64-bit IA-64 technology on Itanium processors. Although Intel 64 is also 64-bit, the two are not compatible, that is, IA-64 software cannot run directly on Intel 64. The x86-64 used by Intel 64 is an extension of the IA-32 instruction set, while IA-64 is another independent architecture without any shadow of IA-32. Although IA-64 can run IA-32 instructions through simulation, the instructions need to be converted before they can run on IA-64, causing its speed to slow down. Since x86-64 is derived from IA-32, the performance of running IA-32 and 64-bit programs is more than adequate.
A 32-bit operating system, with a 32-bit CPU, the addressing space is 2^32 to the power. It is calculated to be 4294967296 bytes, which is 4G. A 32-bit system supports a maximum of 4G space. If you want a 32-bit system to support more than 4G space, It is necessary to use PAE’s special kernel to access it in a special way, but the system efficiency will be lower than that of the native 64-bit system. Of course, sometimes some Linux can only recognize more than 3G, because it is said to be a flaw in the original design. No detailed explanation here.
With a 64-bit operating system and a 64-bit CPU, the addressing space is 2^64, which is calculated to be 18446744073709551616 bytes. Converting 4G to G is too big. Anyway, if N is more than G, it will last a lifetime. Of course, this is the theoretical maximum memory support for a 64-bit system. In fact, no one can use such a large amount of memory.
Therefore, we have to download a 64 operating system, and the most frequently seen operating system with 64 is ia64/amd64. How do they distinguish, which CPU belongs to AMD64, which belongs to IA64?
In fact, many people literally think that AMD64 is aimed at AMD CPUs, and IA64 is aimed at INTEL CPUs, which is actually wrong. I also thought that at first, but it is not:
The Intel 64-bit CPUs you buy on the market belong to the amd64 category. This architecture should be called x86_64, so whether it is AMD’s 64-bit CPU or INTEL’s 64-bit CPU, it belongs to the AMD64 category.
And IA64 refers to Intel Itanium series CPU, not X86 architecture. ia64 is mainly used on servers, not the desktops we usually use. Usually these CPUs are very expensive, and related memory and hard drives are also very expensive!
Therefore, in the future, your PC’s CPU is 64-bit (Intel, also AMD), if it is not an Itanium CPU, you can download the AMD64 OS and install it.
At present, there are three main types of Intel-compatible processors on the market that can achieve 64-bit computing:
1) Intel IA64, based on Itanium 2 processor, is not compatible with 32-bit applications, and the version of the software is called xxx for Itanium. 2) Intel EM64T, based on Xeon DP “Nocona” and MP processors, is compatible with 32-bit applications, and the version of the software is called xxx for x86-64.

3) AMD AMD64, based on Opteron processor, compatible with 32-bit applications, the version of the software is called xxx for x86-64.

规则

两种不同的格式

intel

AT&T

立即数的表示 (x86 汇编 这里是intel 写法)

在汇编指令中,默认的数字表示是10进制
比如

  1. mov ax, 10

在这里,10代表十进制10

  1. mov ax, 0x10

而这里,0x代表16进制0x10 (没有人知道为什么会用0x表示16进制,好像突然开始,大家就这么用了)

当然,二进制也是可以的

  1. move ax, 1010B

末尾+B表示二进制

BIOS 中断 DOS中断

直接和硬件相关 (实模式使用)
没有虚拟化, 直接操作内存
在操作系统内无法调用BIOS中断,取而代之,通过系统调用来做这件事情,INT 80

Data movement instruction

函数调用

GCC风格内联汇编