Assembly and C++ working together

Programming, for all ages and all languages.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Assembly and C++ working together

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Assembly and C++ working together

Post 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.
Tibi,
Currently working on the Lux Operating System
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Assembly and C++ working together

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Assembly and C++ working together

Post by chibicitiberiu »

Now the question is how to pass arguments to an ASM function?

To return something, I write it in eax.
Tibi,
Currently working on the Lux Operating System
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Assembly and C++ working together

Post 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
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Assembly and C++ working together

Post 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
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Assembly and C++ working together

Post 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
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Assembly and C++ working together

Post 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).
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Assembly and C++ working together

Post 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.
chibicitiberiu
Member
Member
Posts: 68
Joined: Thu May 28, 2009 11:46 pm

Re: Assembly and C++ working together

Post by chibicitiberiu »

Thanks very much, that will be really helpful...
Tibi,
Currently working on the Lux Operating System
Post Reply