Kernel base-register and memory Addressing (by quqi99)

Source: Internet
Author: User

Zhang Hua posted: 2016-03-01Copyright Notice: Can be reproduced arbitrarily, please be sure to use hyperlinks in the form of the original source of the article and the author's information and this copyright notice(http://blog.csdn.net/quqi99)


compilation

Assembler compiler assembler Compile the target code binaries (Nasm-f elf-g-F stabs *.asm), connector Linker (Ld-o Bin_file *.O) In addition to combining the target code into a single block, Also make sure that function calls outside the module can point to the correct memory reference (the connector must establish an index, the symbol table, which stores each named item in each of the target modules it connects to, and holds some information about which name or symbol points to the inside of the module).
The immediate number, built into the inside of the machine instruction, is not stored in the register, nor is it stored in memory outside of an instruction.
1, the register hits parentheses to represent memory data in the memory address of the register. Cases:
MOV eax,[ebx+16].
2, in the assembly, the variable name represents the address, not the data. Cases:
MSG: "Hello World"
MOV ecx,msg #复制Msg地址到ecx寄存器 instead of data
MOV edx,[msg] #复制数据 instead of address
Msglen:equ $-msg #$ represents end, length = end position minus start position

Real mode and segment

8080 has 16 address lines (register is 16 bits, 2 of 16 square =64k), 8086 has 20 address lines (2 of 20 =1m=16x64k, but its register is still 16 bits), in order to be compatible with 8080,intel still let the program only use 64K byte segment in 1M, This is called the real mode of memory. 8088, 8086, 80286 of the registers are still 16 bits, it is to use two 16-bit registers to address the 20-bit, a 16-bit register to hold the segment address, a 16-bit register to hold the segment offset. The segment can start at any of the 16-based segment addresses in the 1M, and then up to a limit of 64K. At the beginning of the No. 0019 H byte of the 0001:0019=0001h segment, we know that a segment may start at any of the 1M bytes in real memory at any one of 16 bytes, so 0001:0019 equals 0000:0029 and equals 0002:0009.

Segment Register

This kind of real mode, a segment only 64K size, a program is not enough, so generally by dividing CS, DS, SS, ES and other segments of the way to enlarge size.
8088, 8086, 80286 have 4 segment ships for storing segment addresses, 80386 added 2.
CS (Code Segment): a snippet for storing machine instructions.
DS (Data Segment): A segment for storing variables and other data. There may be a lot of data segments, but the CPU can only use one at a time.
SS (Stack Segment): Stack segment, a single program can have only one stack
ES (Extra Segment): Additional segment registers (combined with DS use mean that you can access up to two data segments at the same time) to specify an alternate segment for a location in memory.
FS and GS: is the clone of ES, the command is arranged according to Es F,g, only exists in 80386 (32 bits) and later x86 CPU.

Push-y instructions in the Assembly
Push AX
push [AX]
PUSHF #将16位寄存器标志值压入堆栈
PUSHFD #将EFLAGS寄存器的全部32位值压入堆栈
Pusha #将8个通用寄存器压入堆栈

Universal Registers

In the 32-bit world, general-purpose registers are divided into three general classes: 16-bit general-purpose registers, 32-bit expansion registers, and 8-bit half-registers (in fact, 16-bit and 8-bit registers are only a single area within the 32-bit register).
There are 8 16-bit general-purpose registers: The Ax,bx,cx,dx,bp,si,di and SP are used to hold 16-bit or less bits. In real mode it can be used in conjunction with the segment register so that a full 20-bit address is specified:
Ss:sp
Ss:bp
Es:di
Ds:si
Cs:bx
Later, when the x86 architecture expanded to 32-bit, prefixed with the original name E (EAX, EBX, ECX, EDX, EBP, ESI, EDI and ESP, the low 16 bits can still be used as the old-fashioned non-prefixed e-name, but unfortunately, the Register's high 16-bit does not have its own names at all).
64-bit system, preceded by the original name prefix R (RAX, RBX, RCX, RBP, RSI, RDI, RSP). Additional 8 new 64-bit registers (R8 to R15) are added. The x86-64 also adds 8 128-bit SSE registers to the 8 homogeneous registers of IA-32, with a total of 16 SSE registers.

Half Register

The above 4 general register (EAX, EBX, ECX, EDX) low 16 bits (AX, BX, CX, DX) are divided into 8-bit half-register, high post plus H, low post plus l, such as: in BX has BH and BL two half registers, and so on.

instruction Pointer Register

16-bit called IP, 32-bit called EIP, holds the current code snippet (a program may contain more than one code snippet) the next part of the execution of the machine instruction offset address. CS and IP together, save the next command to execute the full address (in real mode, CS with the IP to bring 20-bit address, CS set by the operating system, IP can track 64K memory segments, in protected mode, 32-bit system IP can track 4G memory segments). The IP register is the only register that cannot be read in and written out, and it can only be moved using the jump command.

Flag Register

The 16-bit called flags,32 is called EFlags, each one has a special meaning, there are separate names, such as CF, DF, of and so on.

Protection Mode

Real mode because addressing only 64K is not enough, so there is the concept of paragraph. However, in a 32-bit system, you can address 4G of memory space without the need for fragmentation. But the traditional segment registers still exist, but you cannot read or write them, and are completely left to the operating system to do. The operating system has a virtual address space can be very large, but the 32-bit system has the largest physical addressing space is only 4G, the operating system from the virtual address space to find a 4G of memory space as memory addressing, then the section register is set by the operating system as the base address of this virtual space. So, Linux has no real mode "legacy issues" to deal with, and since the first appearance in 1992, it has been running in protected mode. Only the BIOS needs to run in real mode (Linux provides soft interrupt 80H to access the BIOS). If multiple programs access a piece of memory at the same time, can cause confusion, such as DOS is a single-tasking program without this problem, more than one program through the driver access (drive to isolate the program to a block of memory access) also does not have this problem, protection mode can allow multiple programs to run at the same time.

64-bit long mode

X86-64 defines three general modes: Real mode, protected mode, and long mode. The real mode is compatible mode compatible with 16-bit systems, Protected mode is also a compatibility mode compatible with 32-bit system, long mode. Because 64-bit addresses are too large (1 billion GB), today's x86-64 CPUs generally only support 48-bit virtual memory and 40-bit physical memory addresses (1000GB).

Interrupt vector Table-

All interrupts are actually described by bitmaps, and each bitmap is similar to a register pointing to a memory address offset vector (that is, the interrupt Service program address, a vector consisting of 4 bytes, a total of 256 vectors , which are written to the interrupt vector table by Linux to the correct address ) , also called the Interrupt vector table. x86 has a soft interrupt instruction 80H dedicated to the operating system to find the interrupt program (Protected mode is forbidden):

Move eax, 4 #要调用的中断号

Move ebx,1 # The parameters of the interrupt are passed into the register, not the stack

int 80H #进行软中断调用, then Linux will find the interrupt service address in the interrupt vector via the interrupt number in the EAX

Next_order #下一条指令 that the next instruction is pressed into the stack before the interrupt on the line is executed , and the interrupt returns to know that it continues to execute from here.


Kernel base-register and memory Addressing (by quqi99)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.