Hello everyone,
I am using Cygwin under windows vista and programming in nasm and gcc (c, c++).
I wrote successfully an bootsector, enabled the A20 gate and switched to Pmode
with a GDT. That is all working. But now my question: Some functions I want to
write in assembler, assemble it with nasm to an elf32-file and using the linker ld to
link it with the other files. IMO first the arguments from right to left where pushed
on the stack (Every Arg 4 bytes aligned). then the address to return is pushed and at
least ebp will be pushed on the stack. Then there is a jump to the function. So if I
programm a function in asm, the initially things are coded in the calling function, right?
But what I must do to do a return successfully? I mean, what does the calling function
and what I have to do. Have I only to do pop ebp and then make a return? Or Have I to
free the parameteres too?
The Stack of my function I must free myself, that's clear. ^^
TIA Sebihepp
Calling Asm functions from C, how to?
-
- Member
- Posts: 195
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: Calling Asm functions from C, how to?
Hi,
The calling function is responsible for popping all arguments, so yes, once you have popped EBP, all you need is a RET. You may be interested in searching for the System V ABI which has more information. Looks like you are nearly there, though!
Cheers,
Adam
The calling function is responsible for popping all arguments, so yes, once you have popped EBP, all you need is a RET. You may be interested in searching for the System V ABI which has more information. Looks like you are nearly there, though!
Cheers,
Adam
-
- Member
- Posts: 195
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: Calling Asm functions from C, how to?
Hi,
thanks for your fast answer. What did you mean by "System V ABI"? And why I am nearly there?
I now research and development on low level for quite some time and am happy that I found this
forum. On the first frew weeks I had problems getting the information I needed. I look for the
tutorials on lowlevel.brainsware.org, where I get my basics. Then I found some other sites about
low level programming. And now I plan to programm.. well, not really an OS... I only want
a programm which can be executed on PCs without needing anything else like windows etc.
I want support for floppys, cd-roms and harddisks (for the beginning only FAT),
a keyboard driver, a console style display driver and my own API wich provides some useful functions
like requesting the system time and so on. All the modules provides functions for an easy access to the
hardware and will be stored in extra files. The kernel is the connection between them and just have
a little driver for floppys, cd-roms and harddisks inside to load all modules. It will be only a single threaded
"mini OS" without multithreading. Oh and you should be able to execute programms as well.
First I plan the design. This means the needed classes, the interfaces etc. Then I programm quickly
a little BootLoader, who loads some sectors and the GDT, enable A20Gate and switch to PMode, and
starting the Kernel in c. There I set the IDT. Next I think I should implement a disk-driver. Therefore
I must know about the Interrupt Assignment of the IDT and how to set the PIC. The next thing is
the DMA and then finally the diskdriver. About everything else I will think later. About tipps I would
be very happy. ^^
greetings Sebihepp
thanks for your fast answer. What did you mean by "System V ABI"? And why I am nearly there?
I now research and development on low level for quite some time and am happy that I found this
forum. On the first frew weeks I had problems getting the information I needed. I look for the
tutorials on lowlevel.brainsware.org, where I get my basics. Then I found some other sites about
low level programming. And now I plan to programm.. well, not really an OS... I only want
a programm which can be executed on PCs without needing anything else like windows etc.
I want support for floppys, cd-roms and harddisks (for the beginning only FAT),
a keyboard driver, a console style display driver and my own API wich provides some useful functions
like requesting the system time and so on. All the modules provides functions for an easy access to the
hardware and will be stored in extra files. The kernel is the connection between them and just have
a little driver for floppys, cd-roms and harddisks inside to load all modules. It will be only a single threaded
"mini OS" without multithreading. Oh and you should be able to execute programms as well.
First I plan the design. This means the needed classes, the interfaces etc. Then I programm quickly
a little BootLoader, who loads some sectors and the GDT, enable A20Gate and switch to PMode, and
starting the Kernel in c. There I set the IDT. Next I think I should implement a disk-driver. Therefore
I must know about the Interrupt Assignment of the IDT and how to set the PIC. The next thing is
the DMA and then finally the diskdriver. About everything else I will think later. About tipps I would
be very happy. ^^
greetings Sebihepp
Re: Calling Asm functions from C, how to?
First Google hit for "System V ABI". The application binary interface gives information about everything you could possibly want to know about how binaries are expected to behave - from which registers are "scratch" registers within a function (they do not have to be preserved), to calling and return sequences. The second Google hit is a 64 bit addendum.sebihepp wrote:What did you mean by "System V ABI"?
The reason I said that is that it sounds like you pretty much know the theory for linking C and ASM code already.And why I am nearly there?
Cheers,
Adam
-
- Member
- Posts: 195
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: Calling Asm functions from C, how to?
Wow, I am very proud that I sound nearly like an expert.
I already know very much about programming, because it interessts me. But there
are also things I didn't know. One is the gernerating of an COM (Comunication Object Model,
not COM-Port) DLL under Windows. At time I didn't want to read 8 articles about programming
this, because I have many other things to do. In most cases I doesn't programm because of
the result. Instead I love to optimize and writing things who other people believe it
would be impossible.^^ But I am not a superman or something like that. I am not perfect, like
everyone else on the world. So I wrote a little game in Windows and DX8 not because
of the game itself (okay, I like my game and I wrote it al little bit because of the game itself ^^).
Instead my game was on the top 5 of a so called "Reader-Contest" in the magazin "GameStar"
in Germany.
I am going for lunch. Read you later.
Sebihepp
I already know very much about programming, because it interessts me. But there
are also things I didn't know. One is the gernerating of an COM (Comunication Object Model,
not COM-Port) DLL under Windows. At time I didn't want to read 8 articles about programming
this, because I have many other things to do. In most cases I doesn't programm because of
the result. Instead I love to optimize and writing things who other people believe it
would be impossible.^^ But I am not a superman or something like that. I am not perfect, like
everyone else on the world. So I wrote a little game in Windows and DX8 not because
of the game itself (okay, I like my game and I wrote it al little bit because of the game itself ^^).
Instead my game was on the top 5 of a so called "Reader-Contest" in the magazin "GameStar"
in Germany.
I am going for lunch. Read you later.
Sebihepp
Re: Calling Asm functions from C, how to?
Cool, other than the chose of language, this is a simular layout to my OS .sebihepp wrote:Hi,
thanks for your fast answer. What did you mean by "System V ABI"? And why I am nearly there?
I now research and development on low level for quite some time and am happy that I found this
forum. On the first frew weeks I had problems getting the information I needed. I look for the
tutorials on lowlevel.brainsware.org, where I get my basics. Then I found some other sites about
low level programming. And now I plan to programm.. well, not really an OS... I only want
a programm which can be executed on PCs without needing anything else like windows etc.
I want support for floppys, cd-roms and harddisks (for the beginning only FAT),
a keyboard driver, a console style display driver and my own API wich provides some useful functions
like requesting the system time and so on. All the modules provides functions for an easy access to the
hardware and will be stored in extra files. The kernel is the connection between them and just have
a little driver for floppys, cd-roms and harddisks inside to load all modules. It will be only a single threaded
"mini OS" without multithreading. Oh and you should be able to execute programms as well.
First I plan the design. This means the needed classes, the interfaces etc. Then I programm quickly
a little BootLoader, who loads some sectors and the GDT, enable A20Gate and switch to PMode, and
starting the Kernel in c. There I set the IDT. Next I think I should implement a disk-driver. Therefore
I must know about the Interrupt Assignment of the IDT and how to set the PIC. The next thing is
the DMA and then finally the diskdriver. About everything else I will think later. About tipps I would
be very happy. ^^
greetings Sebihepp
-
- Member
- Posts: 195
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: Calling Asm functions from C, how to?
@Dex:
Really? Wich language did you use?
Sebihepp
Really? Wich language did you use?
Sebihepp
Re: Calling Asm functions from C, how to?
Fully ASM (fasm) http://www.dex4u.com/