CONCEPT OF MOV AX,CS and MOV DS,AX
Asked Answered
S

3

6

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. How they are implemented in this program?

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

What do these two instructions actually do?

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

(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

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

Staffman answered 19/3, 2011 at 19:12 Comment(2)
#3820199Oto
When a DOS program starts, segment registers DS and ES are pointing to 256 bytes long structure called PSP. You (the programmer) need to set DS to point to you data segment (which is identical with code segment in TINY memory model). That is why DS is loaded from CS. BTW this simple program does not use any memory variables, so it could get by without using DS at all.Spectroscopy
C
12

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
Cinderellacindi answered 19/3, 2011 at 20:54 Comment(0)
B
8

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.

Bostick answered 19/3, 2011 at 19:24 Comment(2)
Ya. I am a newbie to assembly. Can you explain the things in Detail.(If you didnt mind)Staffman
@Muthu: sounds like you ought to just read some standard resources on the subject. SO answers are great for answering specific well-defined questions, but not if you need everything explained in detail. Don't ask others to do your studying for youSherrilsherrill
G
1

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

Grinnell answered 19/3, 2011 at 19:15 Comment(4)
Ya, can you explain me about the Meaning of MOV AX,CS and MOV DS,AXStaffman
I can understand MOV DS,CS is NotAllowed. But can you tell me about the concept of MOV AX,CSStaffman
@Muthu: The "concept" is simply "copy the value of the cs register` into the ds register". That's all mov does. Which part of it is not clear?Sherrilsherrill
@MuthuGanapathyNathan The MOV instruction cannot be used to load the CS register. Attempting to do so results in an invalid opcode exception (#UD). To load the CS register, use the far JMP, CALL, or RET instruction.Vicar

© 2022 - 2024 — McMap. All rights reserved.