当前位置:网站首页>Fundamentals of assembly language: register and addressing mode

Fundamentals of assembly language: register and addressing mode

2022-06-13 07:05:00 hustlei


1. register

With 16 position 8086 The architecture mainly describes the name and purpose of the register , And in 16 Bit basis 32 position 、64 Bit register .

Register classification

  • General registers
    • Data register (AX,BX,CX,DX)
    • Pointer register (SP,BP)
    • Index register (SI,DI)
  • Segment register (CS,DS,SS,ES)
  • Control register
    • Instruction pointer register (IP)
    • Flag register (FLAG)

16 Bit 8086 The processor consists of 14 A register .32 Bit and 64 Bit x86,x64 The architecture expands the registers respectively .

1.1 General registers

General purpose registers are divided into three categories , But in fact, for the relatively new cpu These registers have similar functions ,RISC General purpose registers with equal architecture are directly numbered , There is no subdivision function .
Of course, it is customary to use different registers for different purposes , This is also ABI An important part of . For example, it usually defaults to AX Is the return value of the function , Some systems give priority to SI,DI As a function parameter .

1.1.1 Data register

AX、BX、CX、DX Generally used to store data , So it is called data register .

  • AX(Accumulator Register) : Accumulation register , Mainly used to input / Output and large-scale instruction operation . It is often used in four operations , Function return value, etc .
  • BX(Base Register): Base register , Commonly used in relative addressing ( Base address + The offset , See the following chapters in this article ) Store base address in ( Basic access address ). It is also commonly used in four operations .
  • CX(Count Register): Count register ,CX Registers are counted in cycles during iterative operations .
  • DX(Data Register): Data register , It's also used to input / Output operation . Also with AX Use with registers , Used for multiplication and division involving large numbers .

modern CPU Of AX,BX,CX,DX Several registers have no difference in function , The most common use is to store operands , Function parameters, operation results and other information .
For example, the return value of a function is usually saved in by default AX in .

8 Bit register

AX,BX,CX,DX stay 8086 In Chinese, it means 16 Bit register , The high and low bits of each register can be used as 8 Bit register access .

  • AX Registers can be divided into two separate 8 Bit AH and AL register
  • BX Registers can be divided into two separate 8 Bit BH and BL register
  • CX Registers can be divided into two separate 8 Bit CH and CL register
  • DX Registers can be divided into two separate 8 Bit DH and DL register

Register names are usually case insensitive , In assembly language ax,bx,cx,dx and AX,BX,CX,DX identical .

x32 The architecture

x32 Architecture , The general-purpose registers are all in 16 On the basis of bit version, it is extended to 32 Bit version , Add... To the name E As a prefix .

  • EAX( Low position is AX): General registers
  • EBX( Low position is BX): General registers
  • ECX( Low position is CX): General registers
  • EDX( Low position is DX): General registers

For compatibility 16 Bit mode program , visit AX,BX,CX,DX It's equivalent to visiting 32 Bit register low 16 position ,32 High bit register 16 Bits are not individually accessible .

x64 The architecture

stay x64 Architecture , General purpose registers are extended to 64 Bit version , The name has also been upgraded .

  • RAX, RBX, RCX, RDX
  • R8-R15:x64 Architecture introduces 8 New general purpose registers

For compatibility 32 Bit mode program , Using the above name is still accessible , It's equivalent to visiting 64 Bit register low 32 position . High position 32 Bit mode is not accessible .

1.1.2 Pointer register (Pointer Register)

  • SP(Stack Pointer): Stack pointer , Top pointer of stack , Only the top of the stack can be accessed .
  • BP(Base Pointer): Base pointer , Offset on stack register , Used to locate variables on the stack . It can directly access the data in the stack .

The pointer register cannot be divided into 8 Bit register . As a general register , It can also store the operands and results of arithmetic and logic operations .

  • x32 The name of pointer register under the framework is ESP,EBP. yes 32 Bit general register . The lower order can be used SP,BP visit .
  • x64 The name of pointer register under the framework is RSP,RBP. yes 64 Bit general register . The lower order can be used ESP,EBP visit .

1.1.3 Indexes ( Address ) register

  • SI(Source Index): Source index register . Index register . Commonly used in index addressing ( Base address + Address , See the following chapters in this article ) Store index in , Also commonly used to copy source strings .
  • DI(Destination Index): Destination index register . Target index register . Commonly used in index addressing ( Base address + Address , See the following chapters in this article ) Store index in . It is also commonly used to copy to the target string .

SI and DI Function and BX identical , Can be used for indirect addressing . It is mainly used to store the offset of the storage unit in the segment . however SI、DI Cannot be divided into 8 Bit register .

SI and DI Two are often used together , Perform string copy and other operations . Some systems give priority to SI,DI As a function parameter .

  • x32 The name of pointer register under the framework is ESI,EDI. The lower addresses correspond to SI and DI.
  • x64 The name of pointer register under the framework is RSI,RDI. The lower addresses correspond to ESI and EDI.

1.2 Segment register

stay 16 In the framework ,16 Pointers to bits can only access 64k Of memory , To access more memory , Use segment registers and pointers to access memory addresses .(x32,x64 Segment registers are used + Access memory by pointer address ).

x86 The architecture cpu, All memory addresses are Segment address + The offset Way to quote .cpu The physical address is internally calculated according to the segment address and offset address .
16 Bit system Physical address = Segment address *16+ offset Usually writing Segment register : The offset .

  • CS(Code Segment): Code segment register .CS:IP Indicates the memory address of the code to be executed .
  • DS(Data Segment): Segment register .DS: Offset Is the memory address of the data .
  • SS(Stack Segment): Stack segment register .SS:SP Is the top memory address .
  • ES(Extra Segment): Extended segment register . Extended segment register .

After the computer starts, it will automatically find the address CS:IP The instruction code of the address runs , After operation IP Automatically offsets the next instruction .
The default segment register for the data address is DS. The stack default segment register is SS.

x32 and x64 The architecture segment register name has not changed , But two registers are extended .

  • FS(Extra Segment): Segment register . Extended segment register .
  • GS(Extra Segment): Segment register . Extended segment register .

1.3 Control register

  • IP(Instruction Pointer): Instruction pointer . Is the offset of the next instruction to be executed in the code segment . namely CS:IP Execute the next command to execute .

  • 32 position CPU Extend the instruction pointer to 32 position , And write it down EIP.EIP It's low 16 Bit and IP The same effect .

  • 64 position CPU Extend the instruction pointer to 64 position , And write it down RIP.RIP It's low 16 Bit and EIP The same effect .

In a system with prefetch function , The next instruction to be executed is usually prefetched into the instruction queue , Unless there is a transfer .

2. Register addressing

  • cpu The instructions executed have only one addressing mode , namely CS:IP.(x32 The address is CS:EIP,x64 The address is CS:RIP
  • There are seven ways to address data, that is, the operands of instructions .
    • Address immediately : Count now
    • Register addressing : Number in register
    • Memory addressing ( Find the data according to the segment address and the given address )
      • Direct addressing : Specify the address directly
      • Indirect addressing : Address specified in register
      • Relative addressing : Address in register + Offset
      • Addressing : Address in register + Address in index register
      • Relative index addressing : Address in register + Address in index register + Offset

2.1 Count now

Constants are actually placed directly in the code , When loading instructions , Auto find .

mov AX,80h   ; 80h Is called an immediate number 

2.2 Register addressing

Data is stored in registers , Call directly with the register name .

mov AX,BX

Register addressing is very fast . The data is in the register , So there is no segment address .

2.3 Memory addressing

2.3.1 Direct addressing ( Hard coded address in instruction )

Operands are stored in memory , The address is given directly in the instruction .

mov AX,[123H]

The segment register of the data segment defaults to DS.

If you want to specify access to data in other segments , It can be written explicitly in the form of segment prefix in the instruction .

mov BX,ES:[123H]

2.3.2 Indirect addressing ( Address of a pointer )

The operands are in memory , Address use SI、DI、BX and BP One of the registers specifies .

mov BX,[di]
mov AX,[si]

Address use SI、DI and BX When specified , The default segment register is DS;
Address use BP To specify , The default segment register is SS( namely : stack segment ).

2.3.3 Relative addressing ( Base address + The offset )

The operands are in memory , The address is a base register (BX、BP) Or index register (SI、DI) The sum of the contents of and an offset .

mov BX,[si+100H]

If the address is SI、DI、BX Equal plus offset calculation , The default segment register is DS;
If the address is BP Add offset calculation , The default segment register is SS.

2.3.4 Addressing ( Base address + Address )

The operands are in memory , The address is a base register (BX、BP) And an index register (SI、DI) The sum of the contents of .

mov BX,[BX+SI]
  • The base register is BX, The default segment register is DS.
  • The base register is BP, The default segment register is SS.

2.3.5 Relative index addressing ( Base address + Address + The offset )

The operands are in memory , The address is a base register (BX、BP) Value 、 An index register (SI、DI) The sum of the value of and an offset .

mov AX,[BX+si+200H]   ;mov AX,200h[BX][si] perhaps mov AX,200h[si][BX] That's right. , Various writing methods 
  • The base register is BX, The default segment register is DS.
  • The base register is BP, The default segment register is SS.


Continuously updated and revised .

Please do not reprint without permission .

原网站

版权声明
本文为[hustlei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130657151707.html