Assembly language--------Wang Shuang Chapter 2


The CPU is composed of arithmetic units, controllers, and various registers. The CPU communicates with the devices on the motherboard through the external bus, and the CPU communicates between various periods within the CPU through the internal bus.
Simply put in the CPU:

  • Arithmetic unit for information processing
  • Registers are used to store various information
  • The controller controls various devices to work
  • The internal bus connects various devices and transfers data between them
  • Programmers can control the CPU by changing the contents of various registers

General register

All registers in the 8086CPU are 16 bits and can store two bytes (1 word) of data. The four registers AX, BX, CX, and DX are generally used to store general data, so they are called general-purpose registers. Because the register used by the previous generation of 8086CPU is 8-bit, in order to ensure compatibility, the sixteen-bit register can be divided into two 8-bit registers for use.
such as

  • AX can be divided into AH and AL;
  • BX can be divided into BH and BL;
  • CX can be divided into CH and CL;
  • DX can be divided into DH and DL;Insert picture description here
    For example, this is to divide AX into two 8-bit registers for use, where the upper 8 bits are represented by AH, and the lower 8 bits are represented by AL.

Word storage in registers

For compatibility considerations, 8086CPU can process contiguous size data-bytes or words at one time.

  • Byte: marked as byte, a byte consists of 8 bites (bits), which can be stored in an 8-bit register
  • Word: denoted as word, a word consists of two bytes, which can be stored in a 16-bit register. A word can be divided into two bytes, high byte and low byte.

A few assembly instructions

Assembly instructions Control the operations done by the CPU Use high-level language grammar description
mov ax,18 Send 18 into register AX AX=18
mov ah,78 Send 78 into the register ah AH=78
add ax,8 Put the sum of the value in ax and the value of 8 into the register ax AX=AX+8
mov ax,bx Put the value of bx in the register into the register ax AX = BX
add ax,bx Put the result of adding the value in register bx and register ax into ax AX=AX+BX

It is worth noting that the case is not distinguished in assembly language, so MOV AX,18 is no different from mov ax,18.

A few things to note

  • If the result of the addition of an 8-bit register is placed in an 8-bit register, if the addition of these two numbers produces a carry, the 8-bit register will only store the low 8-bit byte.
  • It is also required that the number of bits of the two memories is the same during data transfer or calculation.

Physical address

We know that when the CPU accesses the memory unit, it needs to give the address of the memory unit, because the storage space of the memory unit is linear and one-dimensional, and each storage unit has a unique address, which is called the physical address.
8086 is a 16-bit CPU. What are the characteristics of a 16-bit CPU?

16-bit CPU

  • The arithmetic unit can process up to 16 bits of data at a time
  • The maximum width of the register is 16 bits
  • The path between the register and the arithmetic unit is 16 bits

8086CPU gives the method of physical address

The address bus of the 8086CPU is 20 bits, so it can transmit 20-bit addresses to reach 1MB addressing capability, but the 8086CPU is a 16-bit structure, and can only transmit 16-bit data at a time. If the address is simply sent out from the inside, it can only send out a 16-bit address, and the addressing capability shown is only 64KB. 8086CPU uses a method of combining two 16-bit addresses internally to form a 20-bit physical address.
Insert picture description here
When 8086CPU wants to read and write memory:

  1. Related components in the CPU provide two 16-bit addresses, one is called the segment address, and the other is called the offset address;
  2. The segment address and offset address are passed into a component called address adder through the internal bus;
  3. The address adder combines two 16-bit addresses into a 20-bit physical address;
  4. The address adder sends the 20-bit physical address to the input and output control circuit through the internal bus
  5. The input and output control circuit sends the 20-bit physical address to the address bus;
  6. The 20-bit physical address is transferred to the memory by the address bus
    . The algorithm used by the address adder isPhysical address = segment address * 16 + offset address

Physical address = segment address * 16 + the essence of offset address

Next, I will explain the essence of this equation. In fact, we can artificially divide the storage space into several small storage segments. The segment address *16 can actually be called the base address. The base address is our artificially divided storage segment. The starting address and the offset address are just the number of bits moved under the base address. So according to our definition, the revelation address of our artificially divided segment must be a multiple of 16, and because the offset address is 16 bits, the length of a segment is up to 64KB.

Segment register

The memories of CS, DS, and SS are also called segment registers. These registers are used to store segment addresses.

CS and IP

The CS segment register is used to provide the segment address, the IP register is used to store the offset address, and the address adder in the CPU synthesizes the physical address with the value in the CS and IP register.
The steps for the CPU to execute instructions:

  1. After the 8086CPU home appliance is started or reset, CS and IP are set to FFFFH and 0000H respectively, so the storage unit of the FFFF0H address is the first instruction executed by the 8086CPU.
  2. Read instructions from the memory unit pointed to by CS:IP, and the read instructions enter the instruction buffer;
  3. ip = ip+the length of the instruction read, which points to the next instruction;
  4. Execute the instruction, go to step 1, and repeat the process.
    In assembly language, mov cannot be used to input values ​​to CS and IP, so 8086CPU has prepared jmp instructions to modify the values ​​of CS and IP.
    The jmp instruction has the following two uses. The
    jmp segment address: Offset address This usage will directly modify the value in CS and the value in IP.
    jmp. The function of a legal register instruction is to modify the ip with the value in the register. For
    example, jmp ax is equivalent in meaning For mov ip, ax.
    Next, I will introduce the use of the following debug

debug

What is debug? Debug is a debugging tool for real-mode programs. You can use it to view the contents of various registers in the CPU, memory conditions, and track program operation at the machine code level.
Our commonly used debug function

  • With the debug R command, we can view the contents of various registers in the cpu
  • With the debug D command, we can view the contents of the memory
  • We can rewrite the contents of the memory with the debug E command
  • With the debug u command, we can view the assembly instructions translated into the machine code in the memory
  • With the debug T command, we can execute the assembly instructions pointed to by CS:IP in turn
  • With Debug's A command, we can write a machine instruction in the memory in the format of an assembly instruction.

R command

r register name, you can directly modify the contents of the register, such as rcs, you can modify the contents of the cs segment register

D command

The d command will directly list the contents of 128 bytes starting from the specified address
. The format of the D command is relatively large. If the d command is used directly, the content at the address preset by debug will be directly viewed.
If you use the d-segment address: the offset address will list the contents of 128 bytes starting from the content of the storage unit pointed to by the physical address.
If you use the d-segment address: the start offset address and the end offset address, then only List the contents of the storage unit pointing to the address from the beginning to the contents of the storage unit pointing to the address at the end.

E command

We can modify the data starting from the pointed address through the e-segment address: offset address data data data,
or we can modify the data by asking questions through the e-segment address: offset address. We can complete the modification of the data by entering the value and pressing the space. If you don't want to modify it, just press the ENTER key to exit the modification of the data.

u command

Through u segment address: offset address, you can view the assembly instructions starting from the pointed address

t command

Directly use t to run the command pointed to by cs: ip

a command

Using a segment address: offset address, we can write the program through assembly instructions from the point to the address, and directly press enter after the address is displayed to exit the input.

Guess you like

Origin blog.csdn.net/weixin_47617598/article/details/114989625