Assembler mov issue

前端 未结 3 1489
庸人自扰
庸人自扰 2021-01-25 08:24

I have the next code:

mov  ax,@data
mov  ds,ax

Why I can not write just like this?

mov ds,@data

All source:

相关标签:
3条回答
  • 2021-01-25 08:44

    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.

    0 讨论(0)
  • 2021-01-25 08:52

    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.

    0 讨论(0)
  • 2021-01-25 08:56

    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.

    0 讨论(0)
提交回复
热议问题