当前位置: 动力学知识库 > 问答 > 编程问答 >

assembly - CONCEPT OF MOV AX,CS and MOV DS,AX

问题描述:

can someone please explain the functions of the three instructions.....

> ORG 1000H

> MOV AX,CS

> MOV DS,AX

I know what is code , data , extra segments in theory.

But,

1.How they are implementing in these program?

2.Why they move the entire segment into another?(MOV AX,CS;MOV DS,AX;)

(excuse me if question is absurd) what is the real concept of these 2 instructions...

/* The below given Assembly program of

8086 Works well.

I can Understand the meaning of every

instruction in this code,

except the highlighted 3 instructions.

*/

The code accepts the input till 0 is hit!

Code:

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

网友答案:

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).

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.

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.

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

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
网友答案:

ORG 1000H tells the assembler that the code that follows should be placed at offset 1000H in the code image.

The other two instructions copy CS to DS. It is not copying the segment itself, just updating the pointer to the data segment. For a small program (<64K), static data (string literals in the source, indirect jump tables) may be placed together in the same segment with the code. The segment base pointer needs to be loaded in DS before accessing the static data. The loader (the part of the OS that reads the program from disk to memory and starts it running) has to set CS so that it can run the program, but may not set DS, so the program copies CS to DS when it starts.

The two instruction sequence is needed because "MOV DS, CS" is not a legal 8086 instruction.

网友答案:

you can't do

MOV DS, CS

it's an invalid operation (masm 32: error A2070: invalid instruction operands).

MOV AX, CS
MOV DS, AX

These 2 instruction perform the same as mov ds, cs (which is invalid). This way the assembler is happy and doesn't complain. But I can't tell you why the programmer wants the data segment to be the same as the code segment

分享给朋友:
您可能感兴趣的文章:
随机阅读: