Assembly language (Wang Shuang) notes Chapter III

third chapter

From the perspective of learning access memory register

3.1 of words in memory storage

The CPU, the register 16 stores a word, high-order eight bits of high byte and low order eight bits of low byte in memory, since the memory cell stores a one byte, so the two successive address memory means to store low order byte stored in low address unit, high byte stored in high address unit, as shown in FIG

Here Insert Picture Description

0,1 two memory cells as a word starting at address 0 of the storage unit 4E20H, 1,2 regarded as the start address two memory units 1 word storage unit 124EH, we start address word of N N address word referred to as unit cells, such as font data stored in the second address word units is 0012H, data byte 2 is stored in address location 12H, distinction font data and a data type bYTE

3.2 DS and [address]

A CPU write to the memory cell when the first address of the memory cell is given in 8086CPU, the memory address by the segment address and offset address components, the DS register is typically used to store the data segment address to be accessed, such as a read 10000H content fetch unit, can be performed with the following program segment

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

[] Described the operation target is a memory unit, [...] ... indicate the offset address, CPU instruction execution segment address automatically removed from ds obtained while 8086CPU does not support the data directly into the segment register operation, it is necessary additional registers to transfer, the data al to be fed into the memory unit 10000H

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

3.3 word transmission

Because 8086CPU structure 16 is disposable can transmit 16-bit data, i.e., a word

mov bx,1000H
mov ds,bx
mov cx,2266H
mov ax,[0] ;1000:0处的字型数据送入ax
mov [0],cx

Here Insert Picture Description

After the run ax = 1123H, 1000: low font data of 0 to 8 bits 66H, 22H for the higher 8 bits

3.4 mov, add, sub command

Mov instruction now known to form

mov registers, data mov ax, 8

mov register, register mov ax, bx

mov register, memory location mov ax, [0]

mov memory units, registers mov [0], ax

mov segment registers, register mov ds, ax

mov registers, segment registers mov ax, ds

mov memory unit, the segment registers mov [0], ds

mov segment registers, memory cells mov ds, [0]
is not mov memory unit, the memory unit

add instruction

add register, data

add register, register

add register, memory location

add memory units, registers

sub instruction

sub registers, data

sub register, register

sub register, memory location

sub memory units, registers

and add sub segment register can not perform operations such as "add ds, ax"

3.5 Data segment

The memory cell as defined 123B0H ~ 123B9H data segment, the data now to be accumulated in this data segment in the first three units, as follows

mov ax,123BH
mov ds,ax ;输入数据段的段地址
mov al,0
add al,[0]
add al,[1]
add al,[2]

If the font data

mov ax,123BH
mov ds,ax ;输入数据段的段地址
mov ax,0
add ax,[0]
add ax,[2]
add ax,[4]

3.6 Stack

Particularity: LIFO

3.7 stack mechanism provided by the CPU

In 8086CPU based programming, a section of memory may be used as a stack, for the basic instruction PUSH and POP, push ax ax represents the data register into the stack, POP ax represents data extracted from the stack into the ax, 8086CPU the pushing and popping operations are word units carried

mov ax,0123H
push ax
mov bx,2266H
push bx
mov cx,1122H
push cx
pop ax
pop bx
pop cx

Execution code below shows the above

Here Insert Picture Description

8086CPU, there is a segment register SS and SP registers, stack segment address is stored in the SS, the offset address is stored in the SP, at any time, SS: SP points to the top element, when the push and pop instructions instruction execution, from SS and SP stack address obtained

push ax done by performing the following two steps of

1, SP = SP-2, as the new top of the stack

2, the contents of the ax fed SS: SP at the new memory location pointed to

When the stack is empty, no element in the stack, the top element does not exist, all the SS: SP stack can only point below the bottommost unit cell, the offset address of the unit as the offset address word units bottommost +2, 10000H ~ 1000FH assumed to take this space when the stack segment, the bottom-most word units as the address 1000: 000E, so the stack empty SP = 0010H

Pop ax performed as follows

1, the SS: SP data memory unit at a point in the fed ax

2、SP=SP+2,以当前栈顶下面的单元为新的栈顶

要注意原栈顶元素仍然存在,但已经不在栈中,当再次执行push等入栈指令后,将被新的数据覆盖

3.8 栈顶超界的问题

当栈满时再使用push,栈空时再用pop,都将发生栈顶超界问题,栈空间之外的空间里可能存放了具有其他用途的数据代码,超界时很可能将其改写,引发不少错误,且8086CPU不保证对栈的操作不会超界,也不知道我们安排的栈空间有多大,它只考虑当前栈顶在何处、当前要执行的指令是哪一条

我们要根据可能用到的最大栈空间来安排栈的大小

3.9 push、pop指令

push、pop还有如下指令

push 段寄存器

pop 段寄存器

push 内存单元 ;注意操作以字为单位,将一个内存字单元处的字入栈

pop 内存单元

将10000H~1000FH这段空间当作栈,初始状态栈是空的,将ax、bx、ds中的数据入栈

mov ax,1000H
mov ss,ax	;注意不能直接向段寄存器送入数据

mov sp,0010H	;注意栈空时栈顶偏移地址的设置
		    ;编程中要自己注意栈的大小

push ax
push bx
push ds

将寄存器清零可用

sub ax,ax

将10000H~1000FH这段空间当作栈,初始状态栈是空的,设置ax=001AH,bx=001BH,利用栈交换ax和bx中的数据

mov ax,1000H
mov ss,ax
mov sp,0010H

mov ax,001AH
mov bx,001BH

push ax
push bx

pop ax
pop bx

注意push、pop等栈操作命令,修改的只是SP,即栈顶的变化范围最大为0~FFFFH

用栈来暂存以后需要恢复的寄存器的内容时,出栈的顺序要和入栈的顺序相反

3.10 栈段

如果将10000H~1FFFFH这段空间当作栈段,初始状态栈是空的,此时SS=1000H,SP=?

当栈中只有一个元素时,SP=FFFEH,栈为空就相当于唯一的元素出栈,SP=SP+2,SP=0,当有元素入栈时,SP=SP-2,SP=FFFEH

一个栈段最大的容量为64KB

我们可以将一段内存定义为一个段,用一个段地址指示段,用偏移地址访问段内的单元,这是我们自己的安排,一段内存既可以是代码的存储空间,又是数据的存储空间,还可以是栈空间,这关键在于是CS:IP还是SS:SP还是DS的指向

编程,使10000H~1000FH中的8个字逆序复制到20000H-2000FH

Here Insert Picture Description

方法一、右边的看作一个栈段

mov ax 1000H
mov ds,ax

mov ax,2000H
mov ss,ax
mov sp,10H

push [0]
push [2]
push [4]
push [6]
push [8]
push [A]
push [C]
push [E]

方法二、左边的看作一个栈段

mov ax,2000H
mov ds,ax

mov ax,1000H
mov ss,ax
mov sp,0

pop [E]
pop [C]
pop [A]
pop [8]
pop [6]
pop [4]
pop [2]
pop [0]
Published 84 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43569916/article/details/104304299