I've been reverse engineering the EnterCriticalSection
function on Windows 10 and found this interesting spin-loop:
It goes:
lbl_loop:
mov ecx, [rsp+60h]
mov ecx, [rsp+60h]
mov ecx, [rsp+60h]
pause
mov ecx, [rsp+60h]
inc ecx
mov [rsp+60h], ecx
cmp ecx, eax
jb lbl_loop
So my question is - what is the purpose of reading 4 times from [rsp+60h]
and then writing back into it from a loop?
Why couldn't they just do:
lbl_loop:
pause
inc ecx
cmp ecx, eax
jb lbl_loop
mov [rsp+60h], ecx
PS. Note this is a production build of Windows 10. And the rest of the EnterCriticalSection
function is optimized. So this is not a debugging build.