I have tried to check out the code the compiler produces, and found the cause why the code crashes:
New functioning code:
function __SkrUtilCheckInRange(Low,High,Val : integer) : boolean; assembler;
asm
cmp ecx,edx // check if Val > High
ja @@1 // return with FALSE
cmp ecx, eax // check if Val < Low
jb @@1 // return with FALSE
mov eax, 1 // return with TRUE
leave
ret
@@1: xor eax, eax
end;
The compiler produces the following code:
00552240 55 push ebp
00552241 89e5 mov ebp,esp
00552243 83ec04 sub esp,0x4
00552246 39d1 cmp ecx,edx
00552248 770b ja 0x552255 <__SKRUTILCHECKINRANGE+21>
0055224A 39c1 cmp ecx,eax
0055224C 7207 jb 0x552255 <__SKRUTILCHECKINRANGE+21>
0055224E b801000000 mov eax,0x1
00552253 c9 leave
00552254 c3 ret
00552255 31c0 xor eax,eax
00552257 c9 leave
00552258 c3 ret
Since the compiler produces the additional code:
push ebp
mov ebp,esp
sub esp,0x4
it needs to restore the esp and get back the epb and that is what the leave instruction does - analog code is :
mov esp, ebp
pop ebp
Why is this additional code added? it seems uneccessary. If an additional begin-end is used it produces a longer code:
skrutilities.pas:37 begin
00552240 55 push ebp
00552241 89e5 mov ebp,esp
00552243 83ec10 sub esp,0x10
00552246 8945fc mov DWORD PTR [ebp-0x4],eax
00552249 8955f8 mov DWORD PTR [ebp-0x8],edx
0055224C 894df4 mov DWORD PTR [ebp-0xc],ecx
0055224F 39d1 cmp ecx,edx
00552251 770b ja 0x55225e <__SKRUTILCHECKINRANGE+30>
00552253 39c1 cmp ecx,eax
00552255 7207 jb 0x55225e <__SKRUTILCHECKINRANGE+30>
00552257 b801000000 mov eax,0x1
0055225C c9 leave
0055225D c3 ret
0055225E 31c0 xor eax,eax
00552260 c9 leave
00552261 c3 ret
I tried to use delphi mode, this did not change the result.
I personally try to make effective assembly code and as such adding unecessary code is not a good idea.
By the way: it is possible to set the assmbly window in debugger to Intel notation rather than the A&T notation!
Tormods