If you use call instead of jmp, ret can be used..Legend wrote:But how do you return from such a jmp?.
Drivers! Main Problem for OS dev!
Re: Drivers! Main Problem for OS dev!
Re: Drivers! Main Problem for OS dev!
Then I just could use C functions call, with the register parameter passing found in better compilers and get that call without a table lookup (or a table lookup when using vtables, but that is C++ then). Would boil down to the same speed.
*post*
Re: Drivers! Main Problem for OS dev!
You can "return" from a FAR jump to everywhere. That's why I use a FAR jump instead of the normally used call to start a program. This omits uncalculatable growing of the stack ( with a lot of consequences for OS-programming) and allows further return-branches in case of an error or something else. But You can "return" from a jump similar returning from a call, doing this:
mov eax,cx ; selector of return label
mov edx,return ; offset-adress of return label
jmp FAR [fs:ebx] ; Adress of FAR label in fs:ebx
return: ; The offset-adress to return
After that jump the return label can be stored in a variable local to the program, where the program counter now does its job. This program needs to store the selector in ds too, before it is changed fitting to the program.
The descriptors need to be defined in an order, which is always the same: program below data OR program above data. Then a program can find its program-data-descriptor in the GDT at every time: programselector +8 OR programselector -8.
I posted already an example for indexed adressing, which is a mighty task-switch. You can define the indices anywhere in a program, even in other programs, if the table with the pointers has a known base-adress.
This could be provided doing this:
adress 0 jmp NEAR start
nop
nop
nop
adress 8.........base adress of variables
.......................
start: first adress of program
This scheme is well known from bootsectors. In this case You need to know the selector too - i.e. 10h for the kernel
I now want to post an other example, which is an alldays duty. Imagine, that You want to write a scanline to the refresh-memory. This will be done by different procedures (or "functions") depending on the video-mode You selected during an installation. If You are a C-programmer, You have to code:
write_a_scanline(){
case: videomode=12; function1()
......a lot of other cases
}
I saw this in the "svga-lib", used in linux.
But You need not ask for the videomode every time, You want to write a scanline! You know this after initialisation. If You are an assembler-programmer, You need not store a value for the use in a case-selection. You can do this:
videomode_offset: DD xx ; Initialisation writes adress of selected procedure
videomode_selector: DD xx
...................
call FAR [videomode_offset] ; jump indexed to the adress,stored in videomode_offset, which could be everywhere in the same or any other segment.
If You change the videomode during runtime, You can change the adress of the according procedure, which writes the scanline, with only two mov-commands.
mov eax,cx ; selector of return label
mov edx,return ; offset-adress of return label
jmp FAR [fs:ebx] ; Adress of FAR label in fs:ebx
return: ; The offset-adress to return
After that jump the return label can be stored in a variable local to the program, where the program counter now does its job. This program needs to store the selector in ds too, before it is changed fitting to the program.
The descriptors need to be defined in an order, which is always the same: program below data OR program above data. Then a program can find its program-data-descriptor in the GDT at every time: programselector +8 OR programselector -8.
I posted already an example for indexed adressing, which is a mighty task-switch. You can define the indices anywhere in a program, even in other programs, if the table with the pointers has a known base-adress.
This could be provided doing this:
adress 0 jmp NEAR start
nop
nop
nop
adress 8.........base adress of variables
.......................
start: first adress of program
This scheme is well known from bootsectors. In this case You need to know the selector too - i.e. 10h for the kernel
I now want to post an other example, which is an alldays duty. Imagine, that You want to write a scanline to the refresh-memory. This will be done by different procedures (or "functions") depending on the video-mode You selected during an installation. If You are a C-programmer, You have to code:
write_a_scanline(){
case: videomode=12; function1()
......a lot of other cases
}
I saw this in the "svga-lib", used in linux.
But You need not ask for the videomode every time, You want to write a scanline! You know this after initialisation. If You are an assembler-programmer, You need not store a value for the use in a case-selection. You can do this:
videomode_offset: DD xx ; Initialisation writes adress of selected procedure
videomode_selector: DD xx
...................
call FAR [videomode_offset] ; jump indexed to the adress,stored in videomode_offset, which could be everywhere in the same or any other segment.
If You change the videomode during runtime, You can change the adress of the according procedure, which writes the scanline, with only two mov-commands.
Re: Drivers! Main Problem for OS dev!
You store the return address manually? I see, I don't need to do this.
Great, in that case I can just have the function calling the scanline drawing routine in C have calling a specific scanline filler routine. You used just a worse algorithm for the C solution, that is no real comparision at all. The best solution at the moment in this area is neither C nor raw asm, it is this: http://softwire.sourceforge.net/
Great, in that case I can just have the function calling the scanline drawing routine in C have calling a specific scanline filler routine. You used just a worse algorithm for the C solution, that is no real comparision at all. The best solution at the moment in this area is neither C nor raw asm, it is this: http://softwire.sourceforge.net/
*post*
-
rexlunae
- Member

- Posts: 134
- Joined: Sun Oct 24, 2004 11:00 pm
- Location: North Dakota, where the buffalo roam
Re: Drivers! Main Problem for OS dev!
In what way will using call to start a program EVER cause the stack to grow "uncalculatable"ly? Each program is started once, so whereever you pass control to it, you have a single potential return address to store.frizzz wrote:You can "return" from a FAR jump to everywhere. That's why I use a FAR jump instead of the normally used call to start a program. This omits uncalculatable growing of the stack ( with a lot of consequences for OS-programming)
Re: Drivers! Main Problem for OS dev!
rexlunae wrote:
If You think of the lexical levels, normally used ( a function calls a function which calls a function....), You can see, that I'm right.
Legend found it to be silly, that I store a return adress manually....
But You did not see, what this is good for! If You don't want to "return" to the adress stored on the stack ( i.e. an error occured),
You need to pop that adress "manually" or You'd go to nirwana - and in C You can't pop the stack...
My example was meant for the purpose of branching out of a procedure to anywhere without the need of restoring the stack.
The link, You gave us, lead me to one of those efforts to make C/C++ better, using assembler. This is an argument for using assembler - and not for using C !
But all this efforts are only the half way to a solution...
As I only wanted to give a scetch of a case-selection, I did not write it in C ... I think, everybody knows how to write this. The problem, I wanted to show is, that in every "higher" language ( that's why I used a scetch), there is a case selection needed again and again, when the case could be selected already. (In my example from linux there are some 100000 case-selections done per second, only to write a scanline in the right manner!)
I want to show You the problem in an other way, so that you can see how bad it is to use a "higher" language:
If you set any "mode" in a system or a program using such a language, you need to to this using a value, which represents this mode. If you want to find a fitting branch due to that mode, you need a case-selection, where certain branches are derivated from values, which represent those branches. This means, that you finally need the adress of a label to branch to - but in higher languages you can't use such an adress, because you can't do indexed jumping or calling! You need to write case selections again and again - "manually"!
Using assembler you have the choice to do that case-selection only once. Then the result is not a value representing the mode, which needs to be compared again and again, but the right adress to branch to, which You store instead of that mode-value and use in an indexed jump or call.
You should know, that normal C-programs call a lot of functions, which are part of a library - these are "programs" too ( You do not program Your own printf(...)). In the NASM-docs You can find an example, how the normal C-function-call is done. You will see, that there is a lot of data stacked. Often the local variables too, because a C-function can call itself.Each program is started once, so whereever you pass control to it, you have a single potential return address to store.
If You think of the lexical levels, normally used ( a function calls a function which calls a function....), You can see, that I'm right.
Legend found it to be silly, that I store a return adress manually....
But You did not see, what this is good for! If You don't want to "return" to the adress stored on the stack ( i.e. an error occured),
You need to pop that adress "manually" or You'd go to nirwana - and in C You can't pop the stack...
My example was meant for the purpose of branching out of a procedure to anywhere without the need of restoring the stack.
The link, You gave us, lead me to one of those efforts to make C/C++ better, using assembler. This is an argument for using assembler - and not for using C !
But all this efforts are only the half way to a solution...
As I only wanted to give a scetch of a case-selection, I did not write it in C ... I think, everybody knows how to write this. The problem, I wanted to show is, that in every "higher" language ( that's why I used a scetch), there is a case selection needed again and again, when the case could be selected already. (In my example from linux there are some 100000 case-selections done per second, only to write a scanline in the right manner!)
I want to show You the problem in an other way, so that you can see how bad it is to use a "higher" language:
If you set any "mode" in a system or a program using such a language, you need to to this using a value, which represents this mode. If you want to find a fitting branch due to that mode, you need a case-selection, where certain branches are derivated from values, which represent those branches. This means, that you finally need the adress of a label to branch to - but in higher languages you can't use such an adress, because you can't do indexed jumping or calling! You need to write case selections again and again - "manually"!
Using assembler you have the choice to do that case-selection only once. Then the result is not a value representing the mode, which needs to be compared again and again, but the right adress to branch to, which You store instead of that mode-value and use in an indexed jump or call.
Last edited by frizzz on Thu Apr 28, 2005 11:00 pm, edited 1 time in total.
Re: Drivers! Main Problem for OS dev!
A function calling a function calling a function is exactly what everyone codes, there is only one time when the stack usage grows uncontrollable and that is when a recursive function is buggy.
If I have such a severe error in a) kernel mode - then probably I need to shut down the system.
b) A user program that called a kernel function and the others processes can continue then I need to kill that program.
c) If in user mode a severe error that I don't want to continue at all - then I need to kill the app.
In all other cases, for normal errors, I just return for example an error code.
In C I can't pop the stack in the case - and especially - I don't need to!
And you can do indexed calling using an array of function pointers, if it is this what you mean ...
If I have such a severe error in a) kernel mode - then probably I need to shut down the system.
b) A user program that called a kernel function and the others processes can continue then I need to kill that program.
c) If in user mode a severe error that I don't want to continue at all - then I need to kill the app.
In all other cases, for normal errors, I just return for example an error code.
In C I can't pop the stack in the case - and especially - I don't need to!
And you can do indexed calling using an array of function pointers, if it is this what you mean ...
*post*
Re: Drivers! Main Problem for OS dev!
ReactOS aims to be able to use Windows Drivers (and run windows software), its open source so perhaps you can take a look at it, perhaps it might help


