为什么不能执行mov [eax],[ebx] [英] Why cannot do mov [eax], [ebx]

查看:631
本文介绍了为什么不能执行mov [eax],[ebx]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样做:

mov eax, ebx

这:

mov [eax], ebx

甚至是这个:

mov eax, [ebx]

但不是这个(错误C2415):

mov [eax], [ebx]

只是wtf ...为什么? 它与* ptr1 = * ptr2相同,为什么我可以将一个寄存器复制到另一个寄存器,为什么要通过寄存器中的地址将值复制到另一个寄存器中,或者将寄存器的值复制到另一个寄存器中存储的地址...

但是它不能将一个地址上的值复制到另一个地址上.为什么?

解决方案

您不能执行mov [eax],[ebx],因为这暗示了一条机器指令,该指令将允许您指定两个内存操作数. x86指令集主要是围绕机器指令设计的,这些指令使您可以指定一个寄存器和一个内存操作数,因为这非常有用,并且它不会影响取回寄存器然后分步放入内存的操作的性能./p>

与所有复杂的指令集体系结构一样,有一些原则(例如上述内容),然后该规则的例外也有其他用途.

您可能对分别生效的MOVB/MOVW/MOVD指令感兴趣:

 MOV byte[edi], byte[esi]; add edi,1; add esi,1
 MOV word[edi], word[esi]; add edi,2; add esi,2
 MOV dword[edi], dword[esi]; add edi,4, add esi,4

(不,您不必选择寄存器;这些寄存器硬性使用ESI和EDI).就其本身而言,这不是很有趣(除了完全按照您想要的方式进行:),但是可以使用REP前缀重复此操作以构造块移动.而且,芯片设计人员非常努力地使这种程序段移动运行得非常快.

[对于64位x86,有一个在qword上运行的MOVQ].

如果要编写汇编代码,那么在编写大量代码之前仔细阅读指令集描述是很值得的.

I can do this:

mov eax, ebx

And this:

mov [eax], ebx

And even this:

mov eax, [ebx]

But not this (Error C2415):

mov [eax], [ebx]

Just wtf... Why? It the same as *ptr1 = *ptr2, why am I able to copy one register to another, copy value by addres in register to another register or copy value of register to address stored in another register...

But it cannot copy value on one address to another address. Why?

解决方案

You can't do mov [eax], [ebx] because that implies a machine instruction that will let you specify two memory operands. The x86 instruction set is largely designed around machine instructions that let you specify one register and one memory operand, because that is pretty useful, and it doesn't affect performance of operations that fetch to register and then put to memory in separate steps.

As with all complicated instruction set architectures, there are principles (e.g., the above) and then there are exceptions to the rule that serve some other purpose.

You might be interested in the MOVB/MOVW/MOVD instruction(s), which are in effect (respectively):

 MOV byte[edi], byte[esi]; add edi,1; add esi,1
 MOV word[edi], word[esi]; add edi,2; add esi,2
 MOV dword[edi], dword[esi]; add edi,4, add esi,4

(No, you don't get to choose the registers; these are hardwired to use ESI and EDI). By itself this isn't very interesting (other than doing exactly what you want :), but it can be repeated using an REP prefix to consttruction a block move. And the chip designers work very hard to make this kind of block move run extremely fast.

[For 64 bit x86, there's a MOVQ that operates on qwords].

If you are going to write assembly code, it is well worth your trouble to read the instruction set description carefully before you write much code.

这篇关于为什么不能执行mov [eax],[ebx]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆