MOV AX,CS和MOV DS,AX的概念 [英] CONCEPT OF MOV AX,CS and MOV DS,AX

查看:1978
本文介绍了MOV AX,CS和MOV DS,AX的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释这三个说明的功能吗?

Can someone please explain the functions of these three instructions?

  ORG 1000H 
  MOV AX,CS
  MOV DS,AX

我知道理论上的代码,数据和多余的段,但是:

I know what the code, data, and extra segments are in theory, but:

  1. 在此程序中如何实现它们?

  1. How they are implemented in this program?

为什么整个段都移到另一个段? (MOV AX,CSMOV DS,AX)

Why is the entire segment moved into another? (MOV AX,CS and MOV DS,AX)

这两个指令实际上是做什么的??

What do these two instructions actually do?

除了突出显示的3条指令外,我还能理解该代码中所有其他指令的含义.

I can understand the meaning of every other instruction in this code, except for the highlighted 3 instructions.

(程序运行正常.它接受输入直到被击中0为止-有一个mov ah,01hint 21h,然后将al'0'进行比较,如果al'0',则它将跳转到last,否则跳转到back.)

(The program works fine. It accepts input till 0 is hit -- there's a mov ah,01h and an int 21h, then it compares al to '0' and if al is '0', it jumps to last, otherwise it jumps into back.)

    ASSUME CS:CODE        
    CODE SEGMENT 
    ORG 1000H
    MOV AX,CS
    MOV DS,AX
BACK:
    MOV AH,01H
    INT 21H
    CMP AL,'0'
    JZ LAST
    JMP BACK
LAST:
    MOV AX,4C00H
    INT 21H
    CODE ENDS

    END

(编者注:.com程序以偏移量100h加载,所有段寄存器设置为彼此相等.org 1000h可能是org 100h的错字,因为它看起来像是.com程序.该程序不会中断,因为它不使用任何绝对地址,而仅使用相对跳转.)

(Editor's note: .com programs are loaded at offset 100h, with all segment registers set equal to each other. org 1000h is likely a typo for org 100h because this looks like a .com program. This program doesn't break because it doesn't use any absolute addresses, only relative jumps.)

推荐答案

要真正解释这个概念,我们必须支持分段的基本概念以及x86如何使用它们(在实模式下).

To really explain the concept, we have to back up to the basic idea of segments, and how the x86 uses them (in real mode).

8086具有20位寻址,但只有16位寄存器.要生成20位地址,它将段与偏移量组合在一起.段必须位于段寄存器(CS,DS,ES或SS)中.然后,您生成一个偏移量(作为立即值或另一个或两个寄存器的内容.

The 8086 has 20-bit addressing, but only 16-bit registers. To generate 20-bit addresses, it combines a segment with an offset. The segment has to be in a segment register (CS, DS, ES, or SS). You then generate an offset (as an immediate value, or the contents of another register or two.

因此,为了生成地址,将16位段寄存器左移四位,然后在该寄存器中添加其他寄存器中的16位偏移量,并将合并的总数实际用作地址.大多数指令在其上附加了默认段-pushpop,并且与bp相关的任何内容都将使用ss.跳转等使用cs.某些字符串指令es(例如scans)和某些使用两个段-例如,movsd将数据从[ds:si]复制到[es:di].其他大多数指令都使用ds.您还可以使用段替代来明确指定一个地址,例如es:bx.

So, to generate an address, a 16-bit segment register is shifted left four bits, and then a 16-bit offset in some other register is added to that, and the combined total is actually used as the address. Most instructions have a default segment attached to them -- push, pop and anything relative to bp will use ss. Jumps and such use cs. Some of the string instructions es (e.g., scans) and some use use two segments -- for example, movsd copies data from [ds:si] to [es:di]. Most other instructions use ds. You can also use segment overrides to explicitly specify an address like es:bx.

无论如何,在可以有意义地使用段寄存器之前,首先必须将您关心的数据的地址(前16位)装入该寄存器.典型的小模型"程序将以类似以下内容的开头:

In any case, before you can make any meaningful use of a segment register, you first have to load it with the (top 16 bits of) the address of the data you care about. A typical "small model" program will start with something like:

mov ax, @Data
mov ds, ax

在微型模型中,您对数据和代码使用相同的段.为了确保它指向正确的段,您想从CS中获取16位并将其复制到DS.正如其他许多人提到的那样,没有指令将CS直接移到DS.这个问题提到了一种可能性.另一个常见的是:

In tiny model, you use the same segment for the data and the code. To make sure it's referring to the correct segment, you want to get the 16 bits from CS and copy it to DS. As a number of others have mentioned, there's no instruction to move CS directly to DS. The question mentions one possibility; another common one is:

push cs
pop ds

这篇关于MOV AX,CS和MOV DS,AX的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆