The role of brackets[] in assembly and the difference between lea and mov instructions
Let's summarize it now: it involves lea instructions, mov instructions, []
1.lea instruction:
For registers: the second operand is that the register must be added [], otherwise an error will be reported. Here, the lea will take the value of [register], such as:
mov eax,2
lea ebx,[eax]; after execution ebx=2
mov ebx,eax; equivalent to the previous sentence
lea ebx,eax; compiler error: error A2070: invalid instruction operators
For variables, adding or not adding [] is the same effect. They all take the address of the variable, which is equivalent to a pointer
like:
num dword 2
lea ebx,num
lea eax,[num]; eax is the address of num, such as eax=4206598, which varies according to the program. At this time, ebx==eax
2.mov command:
For variables
num dword 2
mov eax,2
mov ebx,num
mov ecx,[num]; after executing ebx==ecx==2
To registers
mov ebx,eax;ebx==2
mov ecx,[eax]; may report an error, because the translation into assembly here is mov ecx, DS:[eax]
In general, the difference between adding brackets or not [] is:
Lea has no effect on variables and is to get the address. For registers, it is illegal to get the value when adding [], and the second operand does not add [].
The mov has no effect on variables and takes the value. For registers, it is the address when adding [], and the second operand does not add [] to the value.
Another point here by the way, I read some tutorials that mov instructions do not support mov ebx, [eax+2*eax...what messy expressions] In the final analysis, for mov, when the second operand is a register, [] is added, it is addressed...
ps: the role of brackets in assembly[]
The role of [] in assembly is not just a simple C language. Pointers are similar, but can be divided into two situations. For variables, [var] and var have the same function.
But there is a difference for registers. [eax] is the address of eax (here is a pointer in C language). Eax without brackets is the variable value in C language. If there are any errors in understanding, I hope to correct it.
.386 .model flat, stdcall option casemap :none include include include include includelib includelib includelib .data buffer byte "%d",0 num dword 12 .data? lpszSize db 50 dup(?) .CODE START: lea ebx,[num] mov eax,[ebx] mov eax,num mov eax,[num] invoke wsprintf,offset lpszSize,offset buffer,eax invoke MessageBox,NULL, offset lpszSize, offset lpszSize,MB_OK invoke ExitProcess,0 end START
Summarize
The above is the function of brackets [] in the assembly language and the difference between lea and mov instructions introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!