Page 2 of 2
Re: Assembly and C++ working together
Posted: Sat Nov 07, 2009 5:05 pm
by gravaera
@OP: I think JamesM is referring to the original C++ article on the Wiki Main Page; not the one on C++ - ASM linkage written recently. The article in question was there long before you asked this question
.
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
Re: Assembly and C++ working together
Posted: Sun Nov 08, 2009 5:24 am
by chibicitiberiu
gravaera wrote:C++ - ASM linkage
Yep, that's what I was referring to.
I haven't found the information about C++ and ASM linkage in the C++ article, but if it is there, then I missed it. But I know I asked about exceptions even though it was in the article, I confess.
And I don't want any conflicts with anyone...
Thanks for help everyone.
Re: Assembly and C++ working together
Posted: Sun Nov 08, 2009 4:30 pm
by neon
It isnt hard to get ASM and C++ working in the same project. Just assemble the ASM files into object files and link them with your linker. Declare the ASM routines as extern in a C header file and call them from your C or C++ source.
The only thing you have to watch out for is symbolic names. The linker only sees the symbolic names in the object files of the routines that you would like to call. This usually is only an issue when calling C routines from assembly though. Calling ASM routines from assembly is not a big problem.
My suggestion is to try getting it working. If you have problems, feel free to post what you have done and the issue. You already know what to do, so just try it.
Re: Assembly and C++ working together
Posted: Mon Nov 09, 2009 2:19 am
by chibicitiberiu
Now the question is how to pass arguments to an ASM function?
To return something, I write it in eax.
Re: Assembly and C++ working together
Posted: Mon Nov 09, 2009 6:30 am
by Firestryke31
A quick and simple way to access parameters in the x86 ABI:
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
Re: Assembly and C++ working together
Posted: Mon Nov 09, 2009 6:34 am
by ru2aqare
Firestryke31 wrote:A quick and simple way to access parameters in the x86 ABI:
That would be
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
Or you can do without ebp.
Code: Select all
mov eax, [esp + 4 ] ;; (first param)
mov ecx, [esp + 8] ;; (second param)
mov edx, [esp + 12] ;; and so on.
ret
Re: Assembly and C++ working together
Posted: Mon Nov 09, 2009 6:44 am
by qw
chibicitiberiu wrote:Now the question is how to pass arguments to an ASM function?
To return something, I write it in eax.
What you are asking for are
calling conventions.
Roel
Re: Assembly and C++ working together
Posted: Tue Nov 10, 2009 9:25 am
by Firestryke31
ru2aqare wrote:Firestryke31 wrote:A quick and simple way to access parameters in the x86 ABI:
That would be
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
Or you can do without ebp.
Code: Select all
mov eax, [esp + 4 ] ;; (first param)
mov ecx, [esp + 8] ;; (second param)
mov edx, [esp + 12] ;; and so on.
ret
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:
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
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).
Re: Assembly and C++ working together
Posted: Tue Nov 10, 2009 10:00 am
by ru2aqare
Firestryke31 wrote:originally from had a lot more that was needed. Now that I know that, there's yet another way to do it:
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
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).
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.
Re: Assembly and C++ working together
Posted: Tue Nov 10, 2009 1:38 pm
by chibicitiberiu
Thanks very much, that will be really helpful...