7

I am new to x86 assembly and I am trying to understand the code in this document : http://www.cs.cmu.edu/~410-s07/p4/p4-boot.pdf page 3 :

movw $0x1234, %ax
movw %ax, %ds
movw $0x5678, %bx
# The following instruction is the same as "movw $0x1337, (%bx)".
movw $0x1337, %ds:(%bx) # Places 0x1337 into memory word 0x179b8.
# Segment Base: %ds << 4: 12340
# Offset: %bx: + 5678
# -------
# Linear Address: 179b8

But I am not understanding the command :

movw $0x1337, %ds:(%bx) # Places 0x1337 into memory word 0x179b8.

Why concatenating %ds with (%bx) is the same as ((%ds << 4) | %bx) ?

As I am in real mode (16 bits), the concatenation shouldn't be %ds << 8 ? instead of %ds << 4?

And why the parenthesis is just around %bx? And not around the whole structure like : movw $0x1337, (%ds:%bx) ?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Lilás
  • 1,111
  • 1
  • 16
  • 29
  • 1
    Possible duplicate of [What are the segment and offset in real mode memory addressing?](http://stackoverflow.com/questions/4119504/what-are-the-segment-and-offset-in-real-mode-memory-addressing) NASM version: http://stackoverflow.com/questions/3819699/what-does-ds40207a-mean-in-assembly – Ciro Santilli OurBigBook.com Nov 07 '15 at 09:10

1 Answers1

9

In real mode, the segment register is used to provide a 20-bit address. In this case, the data segment register ds provides the 'high' 16 bits of the address: (0x1234 << 4 = 0x12340), and the offset in the segment is given by: 0x5678, to yield: 0x179b8.

The data segment register is implicit, so it's not necessary to use: ds:(%bx). If you were using another segment register, like es, it would need to be explicit.

I hope I've understood your question. As to why it's not written as (%ds:%bx), that's really just a syntactic decision that you're stuck with.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • Hi, thanks for your answer! If it was not in real mode, it would be like ((0x1234 << 16) | 0x5678) ? – Lilás Sep 11 '13 at 09:19
  • @Lilás - No. [Protected mode](http://en.wikipedia.org/wiki/X86_memory_segmentation#Protected_mode) is much more complicated, with paging introduced in the 386. There's plenty of documentation and code (e.g., kernel source code) out there. And then there are x86-64 addressing modes, like 'long mode'. – Brett Hale Sep 11 '13 at 09:30