LEA & MOV instruction comparision

不羁岁月 提交于 2019-12-20 03:48:07

问题


Instruction 1:

LEA DX, MESSAGE ; Move the address of MESSAGE in register DX

Instruction 2:

MOV DX, OFFSET MESSAGE ; Move the address of MESSAGE in register DX

Questions:

  • Are the above instructions equal? They seem to work similarly, but I have just started programming assembly so I can not say.
  • If both of them are similar, then which one of above is the better way to do the above task?

Note: I have already read this question


回答1:


On my 32-bit system, the instructions match opcodes like this:

8d 15 c8 90 04 08       lea    0x80490c8,%edx
ba c8 90 04 08          mov    $0x80490c8,%edx

So you use a whole extra byte when the code is loaded into memory if you use lea.

I found a reference to AMD chips at one point having lower latency for lea than for mov, but only by one clock cycle (and this will be immaterial if the data is not in L1 cache). I am not sure if that result holds for recent processors.

I have found lea useful when trying to add an offset to a base address like this:

lea message(,%esi,2), %ecx  # put address of message + 2 x ESI in ECX

whereas I can't do this:

mov $message(,%esi,2), %ecx  # fails on bad syntax

and this produces the wrong result:

mov message(,%esi,2), %ecx  # puts _content_ at address, not address, into ECX 

at least in my assembler (GNU as).



来源:https://stackoverflow.com/questions/27263749/lea-mov-instruction-comparision

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!