procedure DoSomethingCritical(var Lock: integer);

var
  Temp: integer;

begin
{ Initialize lock }
  Lock := -1;
{ Enter Lock }
  repeat
    Inc(Lock);
    Temp := Lock;
    if Temp > 0 then
      Dec(Lock);
  until not (Temp > 0);
{ Perform operations }
{ Leave Lock }
  Dec(Lock);
end;

procedure AsmDoSomethingCritical(var Lock: integer);
asm
{ Initialize lock }
  lock mov dword ptr[eax],$FFFFFFFF
{ Enter Lock }
@spin:
  lock inc dword ptr[eax]
  mov edx,[eax]
  test edx,edx
  jng @skipdec
  lock dec dword ptr[eax]
@skipdec:
  test edx,edx
  jg @spin
{ Perform operations }
{ Leave Lock }
  lock dec dword ptr[eax]
end;