Compilation Notes
processor registerregister
To learn assembly language, you must first understand two things: registers and the memory model.
Let's look at the registers first.CPU
itself is only responsible for arithmetic, not for storing data. Data is generally stored in memory.CPU
You go to memory to read and write data when you want to use it. However, theCPU
The computing speed is much higher than the read/write speed of the memoryAnd to avoid being slowed down.CPU
Both come with a first level cache and a second level cache. Basically.CPU
Cache can be thought of as memory with faster read and write speeds.
But.CPU
The cache is still not fast enough, plus the address of the data inside the cache is not fixed, theCPU
Addressing every read and write also slows things down. Therefore, in addition to caching, theCPU
It also comes with its own register (register
), which is used to store the most commonly used data. In other words.Those data that are most frequently read and written (such as loop variables) are placed inside the registers, theCPU
Priority is given to reading and writing to registers, which in turn exchange data with memory.
Memory Model.Heap
-
During program operation, for dynamic memory occupation requests (e.g., new object creation, or the use of the
malloc
command), the system will set aside a portion of that pre-allocated memory for the user, as specified by the rules from theStarting address to divide(In fact, the starting address will have astatic data(which is ignored here). For example, if the user asks for 10 bytes of memory, then from the start address0x1000
Start assigning him, all the way down to the address0x100A
, if another 22 bytes are requested, then it is allocated to the0x1020
。 -
This partitioning of memory areas due to user-initiated requests is called the
Heap
(heap). It starts at the start address and goes fromLow (address) to high (address) growth。\(Heap\) An important feature of theDoes not disappear automatically, must be released manually, or recycled by the garbage collection mechanism
Memory Model.Stack
-
apart from
Heap
Other than that, the memory footprint is calledStack
(Stack). In a nutshell.Stack
attributable toThe area of memory temporarily occupied by a function。 -
The system starts executing
main
function, a frame is created for it in memory (frame
), allmain
internal variables (e.g.a
cap (a poem)b
) are saved inside this frame.main
At the end of the function's execution, the frame is recycled, releasing all internal variables and taking up no more space. -
Calling a function from within a function
process()
The system also creates a new frame for the function when it is called. Generally speaking, there are as many frames as there are layers of the call stack. -
When the function finishes running, its frame is recycled and the system returns to the place where the function was interrupted to continue the flow of the previous frame. Through this mechanism, functions can be called at different levels, and each level can use its own local variables.
-
Stack
is allocated from the end address of the memory region.From high status to status distribution
CPU
directives
utilizationg++ -S
orgcc -S
, availableC/C++
Generate the corresponding assembly code
#include<>
void hello()
{
int a=1,b=2;
a=a+b;
printf("hello world\n");
}
int main()
{
int a=1,b=2;
a=a+b;
hello() ;
return 0;
}
.LC0:
.string "hello world"
hello():
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 1
mov DWORD PTR [rbp-8], 2
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
mov edi, OFFSET FLAT:.LC0
call puts
nop
leave
ret
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 1
mov DWORD PTR [rbp-8], 2
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
call hello()
mov eax, 0
leave
ret
push
directives
push rbp
push
beCPU
Instruction.rbp
is the operator of the instruction. ACPU
Instructions can have zero to more than one operator
processor register
IA-32
There are 10 32-bit and 6 16-bit processor registers in the architecture. There are three types of registers.
-
general-purpose register
-
data register
-
pointer register
-
index register
-
-
control register
-
segment register
command system
80x86 addressing method
-
Addressing: Finds the address of the operand.
-
Addressing method: the way to find the operand
80x86
command format
- Command mnemonic +operand1,+operand2,+operand3
- The number of operands of an instruction can be 0, 1, 2, or 3.
How to determine the value of the offset address
- Ten ways to relate to the data, the
- immediate addressing
- register addressing
- direct addressing
- indirect register addressing
- relative register addressing
- base address variable address seeking (BVAS)
- relative base address variable addressing
- proportional variable-address addressing
- base proportional variable address (BPRVA)
- relative base-address proportional variable-address addressing
- The 4 types of addressing associated with transferring addresses
- in-segment direct addressing
- Indirect addressing within a segment
- intersegmental direct addressing
- indirect addressing between segments
addressing method
Immediate addressing mode
MOV AX, 5
MOV AX, 05H
hexadecimal number05H
Move to General Purpose RegisterAX
first (of multiple parts)
Register addressing mode (no EA)
- The operand intended by the instruction is already stored in a register or the target number is stored in a register.
MOV AX , BX
- Registers and their symbolic names that can be referenced in the instruction.
8-bit registers | AH、AL、BH、BL、CH、CL、DH、DL |
8-bit registers | AX, BX, CX, DX, SI, DI, SP, BP and segment registers, etc. |
8-bit registers | EAX、EBX、ECX、EDX、ESI、EDI、ESP、EBP。 |
-
DST and SRC have the same word length
-
CS cannot be changed with the MOV instruction
direct addressing method
-
The operand of the instruction is stored in memory, and the effective address of the operand is given directly in the instruction.
-
Physical address =\((DS)\times16d+EA\)
-
MOV AX , [2000H]
-
MOV AX, VALUE
-
MOV AX , [VALUE]
-
-
Physical address =\((ES)\times16d+EA\)
-
MOV AX , ES:[2000H]
-
MOV AX, ES:VALUE
-
MOV AX , ES:[VALUE]
-
Normally, operands are stored in data segments, so their physical addresses will be formed directly from the valid addresses in the data segment registers DS and instructions, but if segments are used beyond prefixes, then operands can be stored in other segments. For example.
MOV ES:[1000H], AX
-
Immediate Addressing:1234H
-
Direct Addressing;[1234H]
-
Register indirect addressing mode → EA base address/variable address
-
MOV AX
, [BX] orMOV AX,ES:[BX]
Register Relative Addressing Mode → EA = Base Address/Variable Address + Displacement
-
MOV AX,COUNT[SI]
maybeMOV AX,3000H[SI]
-
MOV AX,[COUNT+SI]
maybeMOV AX,[3000H+SI]
-
MOV AX,ES:COUNT[SI]
maybeMOV AX,ES:[COUNT+SI]
-
The operand is in memory and its effective address is aThe base address registers (
BX
、BP
) or variable address registers (SI
、DI
The sum of the contents of the instruction and the 8-bit/16-bit offset in the instruction。
base address variable address method (BVAM)→EA=
Base + variable site
-
The operand is in memory and its effective address isA base address register (BX, BP) and theA variable-address register (
SI
、DI
) of the sum of the contents of the。-
MOV AX,[BX][SI]
maybeMOV AX,[BX+SI]
-
MOV AX,ES:[BX][SI]
maybeMOV AX,ES:[BX+SI]
-
Relative base-address-variable-address addressing mode → EA = Base Address + Variable Address + Displacement
-
MOV AX,COUNT[BX][SI]
maybeMOV AX,-46H[BX][SI]
-
MOV AX,COUNT[BX+SI]
maybeMOV AX,0246H[BX+SI]
-
MOV AX,[COUNT+BX+SI]
maybeMOV AX,[-56H+BX+SI]
-
MOV AX,ES:COUNT[BX][SI]
maybeMOV AX,ES:[0246H+BX+SI]
Relative proportional variable address addressing → EA = variable address × scale factor + displacement
-
MOV EBX,[EAX][EDX*8]
maybeMOV EBX,
-
[EAX+EDX\*8]
maybeMOV EBX,[ESP][EAX*2]
-
MOV EAX,TABLE[EBP][EDI*4]
maybeMOV EAX,[TABLE+EBP+EDI\*4]
8-bit/16-bit/32-bit displacement
Note 1: The scale factor can only be used with 32-bit variable address registers: EAX, EBX, ECX, EDX, EBP, ESI,
EDI coupling; and the scale factor can only be 1, 2, 4, or 8;
Note 2: 8), 9), and 10) can only be 32-bit addressed, not 16-bit addressed;