1

Are we able to move a memory operand to a segment register in these ways using MOV instruction in assembly(x86) language ?

1.

MOV DS,[BX]

2.

MOV DS,[6401H]

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mohammad mahed
  • 511
  • 2
  • 6
  • 11

2 Answers2

4

Yes, both addressing modes are valid.

You've got this question tagged both Masm and Nasm. They're not the same, y'know! To convince Masm you want a memory reference, you may need to do mov ds, ds:[6401h] - strange, I know, but that's the syntax of the assembler - or was, the last time I used Masm (long time ago!). The redundant ds: is optimized away in Masm, Nasm would emit it. If Fasm won't do this, Fasm is broken (which I doubt! Tomasz is a genius!)... just tried it with Fasm - works fine!

Incidentally, 32-bit addresses do involve a segment register - the OS sets 'em up, and it is rare to use 'em in "userland" code, but they're still there! (64-bit code, no - but I'm less sure of that).

Frank Kotler
  • 1,462
  • 1
  • 8
  • 4
  • I checked it too: ./FASM.EXE bar.asm flat assembler version 1.70 (1484627 kilobytes memory) bar.asm [1]: mov ds, ds:[6401h] error: invalid operand. – Aki Suihkonen Nov 17 '12 at 16:25
  • 1
    Fasm, like Nasm, wants the segment override inside the brackets. Try `mov ds, [ds:6401h]` (the `ds:` is redundant - like Masm but unlike Nasm, Fasm removes it). – Frank Kotler Nov 17 '12 at 16:48
  • I dont' seem the get it... objdump -Mi8086 decodes this as `mov 0x6401,%ds`, which is unsupported. – Aki Suihkonen Nov 17 '12 at 17:02
  • AT&T syntax wants "$" to indicate an immediate operand. `mov 0x6401, %ds` is memory at that address. `mov $0x6401, %ds` would be unsupported. Gotta love that AT&T! :) – Frank Kotler Nov 17 '12 at 18:57
  • The canonical for MASM syntax treating `[1234]` as an immediate, not a memory operand, is [Confusing brackets in MASM32](https://stackoverflow.com/q/25129743) – Peter Cordes May 01 '22 at 02:31
0

Dude - nobody even uses the DS register this century :)!

I'd strongly encourage you to learn 32-bit assembler. If you have access to Linux, this is an excellent resource:

To answer your question - I believe "No". You typically load DS from the AX register (although you can certainly use any of the other three general-purpose registers).

To be absolutely sure, you should look it up in the Intel reference manual (you should be able to find it on Google).

PS:

When I say "32-bit", I hasten to add that anything you learn for x86-32 is directly applicable to x86-64. But much (most?) of the stuff you learn for 16-bit DOS is not applicable to any contemporary (read: virtual memory/linear address space) system.

IMHO...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 2
    Note that `FS` and `GS` are still used for things such as thread-local storage and access to kernel structures (see the `SWAPGS` instruction). – Jester Nov 17 '12 at 15:59
  • Agreed with your opinion that learning segmentation, especially real-mode, is a waste of time until you already know assembly and are curious about retro-computing and how 8086 real-mode was designed. But your facts are wrong; `MOV Sreg,r/m16` doesn't place any restriction on the addressing-mode you can use with the source. – Peter Cordes May 01 '22 at 02:16
  • `MOV DS,[6401H]` is a valid instruction in NASM, where it means to load from memory. It's not a valid instruction in MASM, where you'd expect it to mean that but MASM is dumb so you'd actually have to write `MOV DS, ds:[6401H]` or `MOV DS, word ptr [6401H]`. [Confusing brackets in MASM32](https://stackoverflow.com/q/25129743). – Peter Cordes May 01 '22 at 02:19
  • See also [Why are these DOS console drivers wasting precious bytes?](https://retrocomputing.stackexchange.com/q/24173) for discussion of not using `mov sreg, [absolute]` being a missed optimization in MS-DOS, probably because of Microsoft programmers having the same misconception that you could only move from to an Sreg from a general-purpose register. Nobody on that Q&A claimed it wouldn't run on 8086, and I don't see any reason why some addressing mode would be rejected. – Peter Cordes May 01 '22 at 02:19