i am having some fun with asm here
in witch ways i can store local variables? and the faster one?
ps: esp is the global stack address right?
ps: ebx and ebp ?
i am having some fun with asm here
in witch ways i can store local variables? and the faster one?
ps: esp is the global stack address right?
ps: ebx and ebp ?
From brazil (:
Pascal pownz!
Here's an example
This is ofcourse a pretty naive and unoptimized code sample, but it goes to show how to write assemblerCode:push ebp //Push the base pointer mov ebp, esp //Set the base pointer to the current stack pointer add esp, -4 //Subtract 4 from the stack. Equivalent of PUSH DWORD 0. This will hold the local variable. [EBP-4] and [ESP] now points to the local variable lea ecx, [eax*4+1024] //ecx := eax*4+1024 mov [ebp-4], ecx //store ecx in the local variable mov eax, [ebp-4] //store the local variable in result mov esp, ebp //restore the old stack pointer pop ebp //pop the old base pointer ret
The equivalent pascal code:
[pascal]
function GetIndex(i: integer): integer;
var x: integer;
begin
x := i*4+1024;
result := x;
end;
[/pascal]
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
weird :shock:
ebp and esp need to be restored on the function end?
what about ebx?
From brazil (:
Pascal pownz!
ebx is a general purpose register. It's not different from eax, ecx, edx, esi or edi
'
You would probably have to read up on the calling convention you use. This is most likely fastcall, which is the standard in delphi and freepascal.
This states that the parameters gets passed left to right like this:
eax,edx,ecx,ebp+4,ebp+8,ebp+12,ebp+16, and so on
You are free to do whatever with ebx in the function, but if you modify any of the others and they are not used to pass parameters in, then you must restore them to the value they were before you altered them
This you must do, to any register, except for eax, which is the result register
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
interesting :}, thanks
From brazil (:
Pascal pownz!
Actually EBX, ESP and EBP values must be always preserved. Also you're free to change and not restore contents of EAX, EDX and ECX even if they were not used to pass parameters into the function. That's the case with all normal calling conventions (fastcall, stdcall, cdecl) as far as I know. Or maybe I do not understand your post correctly.Originally Posted by JSoftware
Actually EBX, ESP and EBP values must be always preserved. Also you're free to change and not restore contents of EAX, EDX and ECX even if they were not used to pass parameters into the function. That's the case with all normal calling conventions (fastcall, stdcall, cdecl) as far as I know. Or maybe I do not understand your post correctly.Originally Posted by JSoftware
EDIT: How to delete my own double-post? I can't seem to find a buttom for that.
hi arthur,
note:Code:push ebp mov ebp, esp add esp, FFFFFFFCh leave ret
this is basically esp := esp -4Code:add esp, FFFFFFFCh
reason for being -4 is because a cardinal is 4 bytes.Code:FFFFFFFCh = -4
also the leave is basically to restore the stack for the procedure exit.
ret is to return to the line that called the procedure
this is equive something along the lines of.
also a quick note, passing an argument is different to storing a local variable.Code:procedure Blah; var xxx: Cardinal; begin //do nothing. end;
this whole proc would take 8 bytes, it is the smallest and quickest way todo it.
hope this helps.
-MM
Bookmarks