
You should check it out. It has a lot of useful information on C++ kernel development.
EDIT: and really, you should try not to get witty with the mods. Believe it or not, they're experienced and know what they're talking about
-All the best
gravaera
Yep, that's what I was referring to.gravaera wrote:C++ - ASM linkage
Code: Select all
push ebp
mov ebp, esp
;; Save certain registers (x86 C ABI says which but I can't recall ATM)
mov eax, [ebp + 8 ] ;; (first param)
mov ebx, [ebp + 12] ;; (second param)
;; third param would be + 16 and so on
;; Do stuff here
;; restore the saved registers
pop ebp
ret
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
That would beFirestryke31 wrote:A quick and simple way to access parameters in the x86 ABI:
Code: Select all
push ebp
mov ebp, esp
push esi
push edi
push ebx
mov eax, [ebp + 8 ] ;; (first param)
mov ebx, [ebp + 12] ;; (second param)
;; third param would be + 16 and so on
;; Do stuff here
pop ebx
pop edi
pop esi
pop ebp
ret
Code: Select all
mov eax, [esp + 4 ] ;; (first param)
mov ecx, [esp + 8] ;; (second param)
mov edx, [esp + 12] ;; and so on.
ret
What you are asking for are calling conventions.chibicitiberiu wrote:Now the question is how to pass arguments to an ASM function?
To return something, I write it in eax.
Ah, yes. Thank you, I knew it was something simple, but I couldn't remember what. The code I wrote that that was originally from had a lot more that was needed. Now that I know that, there's yet another way to do it:ru2aqare wrote:That would beFirestryke31 wrote:A quick and simple way to access parameters in the x86 ABI:Or you can do without ebp.Code: Select all
push ebp mov ebp, esp push esi push edi push ebx mov eax, [ebp + 8 ] ;; (first param) mov ebx, [ebp + 12] ;; (second param) ;; third param would be + 16 and so on ;; Do stuff here pop ebx pop edi pop esi pop ebp ret
Code: Select all
mov eax, [esp + 4 ] ;; (first param) mov ecx, [esp + 8] ;; (second param) mov edx, [esp + 12] ;; and so on. ret
Code: Select all
push esi
push edi
push ebx
push ebp
mov ebp, esp
mov eax, [ebp + 20] ;; (first param)
mov ebx, [ebp + 24] ;; (second param)
;; third param would be + 28 and so on
;; Do stuff here
mov esp, ebp
pop ebp
pop ebx
pop edi
pop esi
ret
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
I think on the contrary, it doesn't make the stack look cleaner; plus if you suddenly decide you need to save two registers instead of three, you have to update all the [ebp+N] offsets. Saving the registers after the stack frame has been built saves you from having to update the offsets. But other than this, there is no difference really.Firestryke31 wrote:originally from had a lot more that was needed. Now that I know that, there's yet another way to do it:Makes stack cleanup a lot easier since local variables are all instantly removed on the "mov esp, ebp" instruction. Though your method is easier if you're not going to be doing anything that needs local variables (i.e. a simple memcpy).Code: Select all
push esi push edi push ebx push ebp mov ebp, esp mov eax, [ebp + 20] ;; (first param) mov ebx, [ebp + 24] ;; (second param) ;; third param would be + 28 and so on ;; Do stuff here mov esp, ebp pop ebp pop ebx pop edi pop esi ret