Assembly language--------Wang Shuang Chapter 3

Word storage in memory

In the CPU, we use 16-bit memory to store a word. The size of a word is two bytes, which is 16 bits. But the memory unit is a byte unit, so a word needs two memory units to store. The low byte unit of this word is placed in the low address unit, and the high byte unit is placed in the high address unit. We call the word unit whose address is N abbreviated as N address word unit.

DS and [address]

When the CPU wants to read a memory unit, it must know the physical address of the memory unit. We generally use the segment address and offset address to mark the memory unit. There is a DS register in the 8086CPU, which is usually used to store the segment address of the data to be accessed. For example, if we want to access the content of unit 10000H, we can use the following assembly instructions.

mov bx,10000H
mov ds bx
mov al,[0]

8086CPU does not allow to put data directly into the segment register, so we need a transfer register to transfer the contents of the register to the segment register. The first instruction transfers 1000H into bx, and the second instruction transfers the data 1000H in bx into the ds segment register.
mov register [offset address], the second instruction gives the offset address, but we know that the physical address cannot be located by the offset address alone. The 8086CPU uses the data in ds as the default segment address. Unless we display the specified segment address.

mov, add, sub instructions

The mov add sub command cannot be used to change the value in the segment register.

Stack

A stack is a special structural model. A stack is equivalent to a box. We usually put things into the box from the bottom to the bottom, and when we take things, we start from the top. The stack is similar to this structure, first-in-last-out structure, this structure is also called: LIFO (last in first out).
The 8086 provides related instructions to access the memory space in a stack. The two basic commands provided by the 8086 are PUSH (Push into Stack) and POP ( Pull Out) . Push ax means sending the contents of the register ax into the stack, and pop ax means sending the data taken out of the stack into ax. The stacking and popping operations of the 8086CPU are all carried out in units of words. The following figure describes the process of an instruction execution. Insert picture description here
We use a section of memory as a stack. The CPU executes push and pop instructions to access this space according to the stack's first-in-last-out rule. But the problem is how does the CPU know which memory space is processed by the stack's rules?
In the 8086CPU, the SS and SP registers specify which space is the stack. SS stores the segment address of the top space of the stack, and SP stores the offset address. SS:SP jointly specifies the top element of the stack.
The execution of push ax is completed by the following two steps.

  1. SP=SP-2, SS:SP points to the unit before the current stack top, and the unit before the current stack top element is the new stack top.
  2. Send the content in ax to the memory unit pointed to by SS:SP, and SS:SP now points to the top of the new stack.

The implementation of pop ax is completed by the following two parts.
1. Send the top element of the stack pointed to by ss:sp into the ax register.
2sp = sp +2.SS: SP points to the unit below the current stack top, and the unit below the current stack top is the new stack top.
Let's talk about which memory unit SS:SP should point to when the stack space is empty. If the lowest word unit of the stack is 1000:E. SP should be 0010H. So when the stack is empty, SS:SP should point to the unit below the bottom unit of the stack.

The problem of stack top out of bounds

In fact, the CPU memory space is the same, and there is no regulation of which memory space is the stack at all, but we artificially stipulate which memory space is accessed according to the rules of the stack. We need to consider the problem of the stack top exceeding the bounds. We want to know whether the written instructions will cause the stack top to exceed the bounds, and specify which space is the stack space and what is the size of the stack space.
We can use pop and push commands to modify the value in the segment register.
In debug mode, the D command, E command, A command, U command, can use the segment register to represent the segment address of the memory unit. For example, e ds: 0 fold means starting to modify the value from the storage unit whose physical address is ds*16 + 0.

Guess you like

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