我正在通过将简单的 C 代码转换为汇编代码来练习我在汇编中学到的东西。
这是C中的代码
int square_me (int val)
{
return (val* val)
}
这是我的代码转换为程序集(我声明了 val 并将其初始化为 4)
val dw 4 ; declaration and initialization of the val variable (in main)
push val ; push val onto the stack so that you still have a copy of the original value of val incase i'll be needing it in some methods or functions (in main)
call square_me ; calling the function square_me, (in main)
push EIP ; pushing the value of EIP onto the stack so the code knows where to go back after the function
push EBP ; creating the stack frame for the function
mov EBP, ESP ; same with the one above
push val ; save the value of val so that ill have a copy of the original value of val in case I made some changes to it
mul val, val ; multiply to val to itself, and save it to val
mov eax, val ; move the value of val to eax
pop val ; pop the original value of val from the stack
mov ESP, EBP ; to restore the stack frame
pop EBP ; same with the one above
leave
ret ; return to the caller
但是当我看文档中写的答案时,它与我的相差甚远,这是他如何将其转换为汇编的
Push EBP
mov EBP, ESP
mov EAX, DWORD PTR [EBP + 8]
XOR EDX, EDX
mov EBX, EAX
MUL EBX
MOV ESP, EBP
POP EBP
Ret
问题 1:我是否正确地将上面看到的 C 代码转换为汇编?
问题2:这是为了什么
mov EAX, DWORD PTR [EBP + 8]
问题3:他为什么需要这样做?在该声明之后没有使用 EDX,那有什么意义呢?
异或 EDX,E
任何的想法?
谢谢!