Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

assembly - What "MOV AX, [BX]" actually does?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is rather confusing, so I'm not sure if I understood it correctly.

What MOV AX, [BX] actually does?

The instruction ...

  • ... adds the value of the BX register to the base address of the segment specified by the DS register
    It depends on the operating mode of the CPU ("real mode" or "protected mode") how the base address is calculated from the DS register.
  • ... reads one byte from the memory at the address represented by that sum
  • ... writes that byte to the low byte of AX (this means: AL)
  • ... reads the next byte from the memory
  • ... writes that byte to the high byte of AX (this means: AH)

Let's say that BX contains the value 456h and the segment specified by DS has the base address 23000h. Then the address is 23000h+456h = 23456h. Let's say the memory at address 23456h contains the value 0CDh and the memory at address 23457h contains the value 0ABh, then AX will contain the value 0ABCDh after the operation.

When code started, address of BX always does 078BH. What is the reason of this situation?

I don't know about emu8086.

On a real computer, the values of the registers depend on the operating system of the computer. Under my Ubuntu Linux version, BX has the value 0 when the program starts.

Otherwise, There is a question like "Write assembly code which generates 8B07H machine code". I guess that this question solves like that MOV AX, [BX] but [BX] is 078BH, is not 8B07H.

8B07H is a 16-bit number, but the memory of x86 systems is organized in bytes (8-bit units). So the first question is: What does 8B07H mean in this case?

It might mean that the first byte shall be 08Bh and the second byte shall be 07h.

In this case the instruction MOV [AX], BX would do the job, not MOV AX, [BX]:

This instruction does more or less the opposite of MOV AX, [BX]: It writes data to memory. This time, the data in the register BX is written to the memory specified by the registers DS and AX.

Because on x86 systems the low byte of some data is stored in the first byte and the high byte is stored in the second byte, the value 1234h would be stored as two bytes:34h in the first byte and 12h in the second one.

The same is true for 78Bh: 8Bh is stored in the first byte and 07h is stored in the second one.

EDIT

The question is "Write a assembly code which generates 8B07H value.".
...
I searched this question on The Internet and solving is MOV AX, [BX].

In RAM memory, you can only store numbers. To store the letter "A", the number 65 is stored (as an example).

Assembler instructions are also stored as numbers.

The instruction MOV AX, [BX] is stored as the two numbers 8Bh and 07h. However, ...

So, after assembly program executed, finally 8B07H value should be generated in a register(AX, BX etc.).

... this has nothing to do with what the program is doing:

The instruction is stored as 8Bh and 07h, just like the word main in a Java program is stored as the letters m, a, i and n.

This means that the memory already contains the values 8Bh and 07h before the program is started.

What is the reason of this situation?

Every inventor of a new CPU type has to define the meanings of the numbers. For ARM CPUs (this is what the new Apple PCs or the Raspberry Pi use) in 16-bit mode, 8Bh and 07h means lsls r3, r1, #30 which is a completely different operation than mov ax, [bx].

You have to look into the documentation for the corresponding CPU to know what exactly 8Bh and 07h means.

In the Intel 8086 documentation, you'll find a table that contains the following line (on page 4-31):

1st byte |2nd byte   |more bytes|meaning
---------+-----------+----------+---------------
   ...   |     ...   |   ...    |  ...
   8B    |mod reg r/m|  disp    | MOV reg16, r/m
   ...   |     ...   |   ...    |  ...

The value 07h is 00 000 111 (binary). This is the second byte (mod reg r/m) so you look up some other tables (in the 8086 documentation, these tables are on page 4-20):

The combination of mod=00 and r/m=111 means: r/m = [BX]
reg=000 means: reg16 = AX

So the instruction 8Bh, 07h is MOV AX, [BX].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...