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
827 views
in Technique[技术] by (71.8m points)

assembly - How to store a two-register mul result into memory

So let's say that I've got a result of mul in dx:ax, how can I save it to dword [ebx]??

I have the same problem with double words : edx (high half) and eax (low half) to two dwords pointed to by [ebx].

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

As said by commenters Michael and Ped7g already, you have use offsets. Let me explain:

x86 stores numbers in little-endian format, meaning that the lowest-order bytes are stored first in memory. A little example: Assuming you have the value 0x12345678 in eax, and you execute this instruction:

mov [addr], eax

...then the memory at address addr will look like this:

78 56 34 12

In your example, you have a value in dx:ax which is a shorthand for "have a value's upper 16 bits in dx and its lower 16 bits in ax". Assuming the value is, again, 0x12345678, so you have 0x1234 in dx and 0x5678 in ax, then you need two move instructions:

mov [addr], ax   // Memory now looks like this: 78 56
mov [addr+2], dx // Memory now looks like this: 78 56 34 12

The +2 comes from the fact that ax is a 16-bit register, i.e. it uses up two bytes when stored in memory, so since you want to put dx right after it, you need to increase the address by 2.

Same thing for 64-bit values in edx and eax, with an offset 4. Let's assume you have the value 0x1234567890ABCDEF split to 0x12345678 in edx and 0x90ABCDEF in eax, then it would look like this:

mov [addr], eax   // Memory now looks like this: EF CD AB 90
mov [addr+4], edx // Memory now looks like this: EF CD AB 90 78 56 34 12

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

...