Home > front end >  Can the LEA instruction mimic every MOV instruction?
Can the LEA instruction mimic every MOV instruction?

Time:01-16

Recently came across the M/o/Vfuscator

A complete single-instruction C compiler which compiles programs into "mov" instructions, and only "mov" instructions. Arithmetic, comparisons, jumps, function calls, and everything else a program needs are all performed through mov operations; there is no self-modifying code, no transport-triggered calculation, and no other form of non-mov cheating.

Question

I'm curious if every MOV instruction can be replaced with an equivalent LEA?

For example

 mov     rax, 10
 mov     rbx, rax
 ...

can be replaced with

 lea     rax, [10]
 lea     rbx, [rax]
 ...

Update

Oops, totally forgot that LEA cannot do a pointer dereference. For example

.data
     ten dq 10

.code

main proc

    mov     rax, offset ten
    mov     rbx, [rax]  ; <--- dereference, move 10 into rbx
     
    lea     rax, [ten]
                        ; <--- no dereference equivalent using lea
    ret
main endp

end

CodePudding user response:

what you have just written are identical, but lea is most often used to do some address calculations inside square braces [], it can multiply 2 numbers add third number to those etc. in some places I've come across lea instruction doing just some math (and it is for that). when you have struct address for example and want to retrieve a member of it located, say offset 8 bytes away from offset you do lea rax, [rbx 8], rbx being the address of struct. mov can be used to load addresses (just like lea), but values also. mov rax, [rbx] is equivalent to dereferencing a pointer pointed held in rbx. often times you'll see mov rax, [rbx 8*4] this is how you load the value of int array (assuming it takes 4 bytes to store ints on your system), equivalent in C would be array[8]. So to wrap up lea and mov could be used interchangeably when loading addresses in registers, but lea can't do pointer dereference, you need mov for that, or when storing values at memory addresses lea won't help, you need mov for those operations too. Note about doing math in square braces [] in lea, the thing is you have much more flexibility in terms of math with lea. Here is how to do math with mov when accessing memory to not break rules. link.

  • Related