Assembly and C++ working together
- gravaera
- 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
@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
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.
-
- Member
- Posts: 68
- Joined: Thu May 28, 2009 11:46 pm
Re: Assembly and C++ working together
Yep, that's what I was referring to.gravaera wrote:C++ - ASM linkage
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
Currently working on the Lux Operating System
Re: Assembly and C++ working together
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.
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();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
-
- Member
- Posts: 68
- Joined: Thu May 28, 2009 11:46 pm
Re: Assembly and C++ working together
Now the question is how to pass arguments to an ASM function?
To return something, I write it in eax.
To return something, I write it in eax.
Tibi,
Currently working on the Lux Operating System
Currently working on the Lux Operating System
- Firestryke31
- 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
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?
Re: Assembly and C++ working together
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
Re: Assembly and C++ working together
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.
Roel
- Firestryke31
- 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
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
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?
Re: Assembly and C++ working together
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
-
- Member
- Posts: 68
- Joined: Thu May 28, 2009 11:46 pm
Re: Assembly and C++ working together
Thanks very much, that will be really helpful...
Tibi,
Currently working on the Lux Operating System
Currently working on the Lux Operating System