Hi Berserk,
If you want your questions answered, you may try a different approach to asking, you are being a bit rude.
You must at least try to solve your problems yourself, I and most people on this forum are willing to help, however some people will just ignore you if you ask like this.
How much programming experience do you have? I think that you should have at least a year of experience where you have been spending a few hours a day programming. You need to understand the concepts of programming and problem solving before starting an OS, it really depends on how much time you have to learn programming.
With OS programming ALL beginners will need to ask many questions, however some of the questions you are asking are very easy to get answers to from a simple google search. There is nothing wrong with asking questions, but it can be very annoying when someone demands code, especially when that person shows no desire to find the information themselves, everybody, myself included, starts out demanding code from others until they realise that they are no getting as much help as others, find the thread about asking questions, it is on this forum somewhere, and read it, it will help you.
OK, now for some answers :
C Arguments are pushed onto the stack in reverse order, so a C function declared like this
void sumof(int a, int b);
and called like this
int x = sumof(5, 10);
would have asm code like this
Code: Select all
PUSH DWORD 10
PUSH DWORD 5
CALL _sumof
ADD esp, 8
MOV DWORD [x], EAX
Assigning to x is not as simple as this, but you will not need to worry about this because C will generate this code for you.
OK, First we push the arguments in reverse order, it is important to use the DWORD size type so that args smaller than a DWORD (like 5 or 10) are zero extended, then we call the function which would have been declared as external to our asm file like this
extern _sumof or [extern _sumof]
after this by C conventions the caller must clean the stack, so we simply add 8 (4 bytes per arg) to esp to allow the stack to overwrite these values. All arguments regardless of type are DWORDs (4 bytes) in 32bit PMode.
OK using the above example the sumof function would be written like this in an asm file, you will normally only be writing functions in asm and calling them from C so the above info is not needed but included for you info.
Code: Select all
_sumof:
push ebp
mov ebp, esp
mov eax, DWORD [ebp + 8]
add eax, DWORD [ebp + 12]
pop ebp
ret
So what is this you ask:
First we push the base pointer to save it, when we use the CALL instruction it automatically saves the return address on the stack (this only applies to a flat memory, where CS has unrestricted access to all parts of memory, more than 4 bytes is pushed otherwise), so when we get to the function CALL has pushed 4 bytes, then we push 4 bytes so our arguments start 8 bytes into the stack, so we copy the stack pointer into the base pointer and use this as a reference to the arguments, this is called a stack frame, so 8 bytes into the stack is our first arg, so ebp + 8 = arg1, and add 4 bytes for every arg, so ebp + 12 = arg 2. At the end we simply pop ebp to restore the base pointer. EAX, AX, AL are used for return values, EAX for 4 byte return, AX for 2 bytes and AL for 1.
I am sorry but I cannot help you with your other questions you will have to hope that someone else is willing to help.
I hope that I have helped.
If anyone knows of an error in this please say so as I have done this without referring to any docs.