寄存器(内存访问)


title:寄存器(内存访问)
date:
tags:
categories: IT

description:

寄存器(内存访问)


本文摘自《汇编语言》,作者王爽

DS和[address]

8086CPU中有一个DS寄存器,通常用来存放要访问数据的段地址。

move al,[0]

这里使用move指令将一个内存单元中的内容送入一个寄存器。[0]表示内存单元的偏移地址。只有偏移地址是不能定位一个内存单元的,那么内存的段地址是多少呢?指令执行时,CPU自动取ds中的数据为内存单元的段地址。
有一点需要注意的是,8086CPU不支持直接将数据送到段寄存器。所以move ds,1000H这条指令是非法的。应该这样:

move bx,1000H
move ds,bx

至于为什么8086CPU不支持直接将数据送到段寄存器?这属于8086CPU硬件设计的问题,只需要知道这一点就够了。

move,add,sub指令总结

move 寄存器,数据
move 寄存器,寄存器
move 寄存器,内存单元
move 内存单元,寄存器
move 段寄存器,寄存器
move 寄存器,段寄存器
move 内存单元,段寄存器
move 段寄存器,内存单元

add与sub指令与move一样,只是不能对段寄存器进行操作。因为段寄存器不能像通用寄存器那样进行运算,这是硬件限制。而move指令不涉及运算,只是传送指令。

8086CPU的push和pop都是以字为单位进行的。

两个问题

例如,我们可以把10000H-1000FH这段内存当做栈使用,但是至少有两个问题。
1. CPU如何知道10000H-1000FH这段空间被当做栈使用?
2. 在进行push,pop操作的时候,必须知道栈顶单元,可是,如何知道?

8086CPU中,有两个寄存器,段寄存器SS和寄存器SP。栈顶地址存放在SS中,偏移地址存放在SP中。任意时刻,SS:SP指向栈顶元素。

push与pop功能描述

这样,我们就可以完整的描述push和pop的功能了,如push ax

  1. SP=SP-2,SS:SP指向当前栈顶前面的单元,以当前栈顶前面的单元为新的栈顶。
  2. 将ax中的内容送入SS:SP指向内存单元处,SS:SP指向新栈顶。

pop执行过程与push刚好相反,pop ax

  1. 将SS:SP指向的内存单元处的数据送入ax
  2. SP=SP+2,SS:SP指向当前栈顶下面的单元,以当前栈顶下面的单元为新的栈顶。

push和pop都可以操作寄存器,段寄存器和内存单元。

push 寄存器
push 段寄存器
push 内存单元

栈顶超界

8086帮助我们解决栈顶超界问题,自己使用时注意。既要防止入栈太多导致超界,也要防止栈空的时候出栈导致的超界。

栈为什么逆向生长

这段内容摘自《Reverse Engineering for Beginners》,作者:Dennis Yurichev

The reason that the stack grows backward is probably historical. When the computers were big and occupied a whole room, it was easy to divide memory into two parts, one for the heap and one for the stack. Of course, it was unknown how big the heap and the stack would be during program execution, sothis solution was the simplest possible.
In [D. M. Ritchie and K. Thompson, The UNIX Time Sharing System, (1974)]we can read:

The user-core part of an image is divided into three logical segments.
The program text segment begins at location 0 in the virtual address
space. During execution, this segment is write-protected and a single
copy of it is shared among all processes executing the same program.
At the first 8K byte boundary above the program text segment in the
virtual address space begins a nonshared, writable data segment, the
size of which may be extended by a system call. Starting at the
highest address in the virtual address space is a stack segment, which
automatically grows downward as the hardware’s stack pointer
fluctuates.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值