pacman128 / pcasm

PC Assembly language book
160 stars 34 forks source link

Why LE is necessary #13

Closed GuichiZhao closed 11 months ago

GuichiZhao commented 11 months ago

Firstly I understand the difference between lev and mov in terms of the result they can achieve, put in simply

mov eax ebp  ;put the value in ebp register into eax register
lea eax [ebp] ;same as above, and they are equivalent 

However

mov eax,ebp+8 ;invalid register set size
lea eax,[ebp+8] ;calculate in sum of ebp value and 8, then assign it to eax

So what mov eax,ebp+8 is illegal while lea eax,[ebp+8] is OK? My book says

The value that MOV stores into EAX must be computed by the assembler (that is, it must in the end be a constant)

But it make no sense to me! What is mean by CONSTANT? The obvious understanding it that the CONSTANT should be calculate by assembler/linker before the program run, BUT, think about mov eax,[ebp+8] is a LEGAL instruction, The assembler/linker has no way of knowing the value of [ebp+8](*(ebp+8) as C lingo) before the program run!

https://stackoverflow.com/questions/77416613/why-leaload-effective-address-is-necessary

pacman128 commented 11 months ago

This could be clearer. For a label, you can do mov eax, LABEL where LABEL is effectively a constant. However, you can't do mov eax, ebp+8 since ebp+8 is not a constant; however, you can do lea eax, [ebp+8] to achieve the desired result. Otherwise one could do: mov eax, ebp add ebp,8 But this is not as simple as the single lea instruction.

However, issues are not a place to ask questions like this. An e-mail would be more appropriate.