Question

I have the next code:

mov  ax,@data
mov  ds,ax

Why I can not write just like this?

mov ds,@data

All source:

   .MODEL small
   .STACK 100h
   .DATA
   HelloMessage DB 'Hello, world',13,10,'$'
   .CODE 
   .startup
   mov  ax,@data
   mov  ds,ax
   mov  ah,9
   mov  dx,OFFSET HelloMessage
   int  21h
   mov  ah,4ch
   int  21h
   END

Thank you!

Was it helpful?

Solution

You can't, because the instruction set doesn't contain an instruction to do that. It is just one of the many idiosyncrasies of the x86.

These kind of restrictions are fairly normal for assembly languages. Most architectures contain some registers that are treated specially (for example the processor status word), though usually fewer than the x86 architecture.

The reason to not provide an instruction for all possible moves is to reduce the size of the instruction set, so that an instruction takes less memory. Overall it is more efficient to do moves that are rarely needed in two steps.

OTHER TIPS

General purpose register as the 'ax' is designed to hold the 16 bit number pointing to the data (in your case the string inside the DATA)

So if you try to directly pass the data to the special register (ds or data segment here) it will not work correctly as it does not know to accept data that way. So we first get that 'number' or the point in memory location where data starts & pass that point to ds register.

I'm no expert but this is how I understand this constraint to work.

The Segment registers are used to control which segment of memory is used by the register instructions, as such the last thing you want is to load a segment register (DS in this case which is the Data Segment register) from a memory location. The act of modifying DS could cause the memory location being read to change during the process of updating DS, i.e. the first bits/byte loaded into DS now cause it to be pointing to another segment before the remainder has been read. It's safer to read the value into the Accumulator (AX) or another general purpose register, so now the value is in the processor when it's loaded into the segment register, so no chance of the value getting corrupted during the load.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top