GhaSShee


Stack Machine


# A behaviour of a stack given this piece of code: ~~~ swap: push ebp ; back up the base pointer, mov ebp, esp ; push the context of the registers on the stack push eax push ebx push ecx push edx mov eax, [ebp+8] ; address of the first parameter mov ebx, [ebp+12] ; address of the second parameter mov dl, [eax] mov cl, [ebx] mov [eax], cl mov [ebx], dl ; restore the context of the registers from the stack pop edx pop ecx pop ebx pop eax ; restore the ebp pop ebp ret ~~~ When the function is called, the stack looks like: ******************************* * * * +-------------+ * * | Parameter 2 | * * +-------------+ * * | Parameter 1 | * * +-------------+ * * | Return Addr | <-- esp * * +-------------+ * * * ******************************* then after the "stack frame" is set up: ************************************** * * * +-------------+ * * | Parameter 2 | <-- [ebp + 12] * * +-------------+ * * | Parameter 1 | <-- [ebp + 8] * * +-------------+ * * | Return Addr | * * +-------------+ * * | saved ebp | <-- ebp * * +-------------+ <-- esp * * * ************************************** Now the context is saved: ************************************** * * * +-------------+ * * | Parameter 2 | <-- [ebp + 12] * * +-------------+ * * | Parameter 1 | <-- [ebp + 8] * * +-------------+ * * | Return Addr | * * +-------------+ * * | saved ebp | <-- ebp * * +-------------+ * * | saved eax | * * +-------------+ * * | saved ebx | * * +-------------+ * * | saved ecx | * * +-------------+ * * | saved edx | <-- esp * * +-------------+ * * * **************************************