Bona Fide OS Developer
View unanswered posts | View active topics It is currently Mon Jun 24, 2019 4:04 am



Post new topic Reply to topic  [ 67 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 7  Next
 How do i get my kernel to load binary programs? 
Author Message
Post Re: How do i get my kernel to load binary programs?
i should add malloc allocates memory in the code segment :!: :roll: .


Tue Oct 06, 2009 12:23 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
the intricacis behind malloc has much to do with what you want... I got something to work... it took me much longer then I expected... but it works at least for very simple and friendly .COM files... :D

This is the C file, I did it under Turbo C++ 3.0 which is free
Code:
#include <stdio.h>
#include <alloc.h>
#include <mem.h>

#define OFFSET(p)  ((unsigned short int *)(&p))[0]
#define SEGMENT(p) ((unsigned short int *)(&p))[1]

int main() {
   FILE *f;
   char far *buff;
   char far *prog;
   int len;
   int skew;

   // for some reason TC won't let me allocate
   // much more then 60000
   buff = (char far *) farmalloc(60000);
   if (buff == NULL) exit(0);
   skew = 256 - OFFSET(buff);
   prog = buff + skew;


   f = fopen("TEST.COM", "rb");
   fseek(f, 0, SEEK_END);
   len = ftell(f);
   fseek(f, 0, SEEK_SET);
   fread(prog, 1, len, f);
   fclose(f);

   asm { CALL prog; };

   return 0;
}


and here is the source for TEST.COM assembled using Tasm, I wanted to use Fasm but it wasn't installed on the PC...

Code:
.MODEL tiny
.CODE 
ORG 0100h
mystart:
  MOV AX, 013h
  INT 010h
  RETF
END mystart


Tue Oct 06, 2009 2:17 pm
Profile
Post Re: How do i get my kernel to load binary programs?
a few issues
first that test program is so simple it would have worked even if the data is misaligned
the only way to test it is if you test one with procedures
second i cannot allocate that much memory (p.s. the max malloc is 65535 in turbo C++)
third i do not have farmalloc


Tue Oct 06, 2009 3:32 pm
Post Re: How do i get my kernel to load binary programs?
i changed it how it may work but now i got errors
Quote:
Compiling NODOS.CPP:
•Warning NODOS.CPP 153: Parameter 'xp' is never used
Error NODOS.CPP 504: Undefined symbol 'SEEK_END'
Error NODOS.CPP 506: Undefined symbol 'SEEK_SET'


Tue Oct 06, 2009 3:38 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
SEEK_END means you are seeking from the end of the file, I used it to find the length of the file
SEEK_SET means you are seeking from the beginning of the file...
I think you have this ___len() function to do that job

in real mode there are these concepts of memory models and the behaviour of pointers change according to the memory model... farmalloc() is just to make sure I got a far pointer in return... I am using standard far word pointers 16bit segment with a 16bit offset...

and well my dilema was that Turbo C++ reported that I had over 500KB available yet I couldn't allocate a memory block larger then 60K, I don't know why :|


Tue Oct 06, 2009 3:46 pm
Profile

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
Also I almost forgot, in COM programs, both data and code share the same segment... the only missalignment issue with COMs is that they expect to start at 0100h

I'll give you a more complicated COM program later on to show you...


Tue Oct 06, 2009 4:02 pm
Profile
Post Re: How do i get my kernel to load binary programs?
thing is is my kernel is built in the tiny memory model
and your code put the program right on top of the kernel (hex:4000:100)
the kernel is at offset 100 itself because it is a com file itself


Tue Oct 06, 2009 4:02 pm
Post Re: How do i get my kernel to load binary programs?
it crashes bad


Tue Oct 06, 2009 5:35 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
what crashes bad??

AND I've spent the whole day trying to find a compiler that does not issue INT calls to DOS... even when the program is as simple as

Code:
void main(void) {
   return;
}

I still get an EXE with a bunch of code... I found out that Turbo Pascal 3.01 and below compile .COM files but they also make a bunch of calls to DOS even when its a program that does nothing...

Do you know of a compiler that doesn't do this????


Tue Oct 06, 2009 8:32 pm
Profile
Post Re: How do i get my kernel to load binary programs?
none
i dont use the libraries
i wrote the whole library myself (look at the map file)
Code:
 Address         Publics by Name

 0000:0712       absread(int,int,int,void near*)
 0000:076D       abswrite(int,int,int,void near*)
 0000:0472       clock_hook()
 0000:0A62       createfile(char near*,int)
 0000:0357       farstrcpy(char far*,char near*)
 0000:0C53       fclose(jfile near*)
 0000:0DF6       feof(jfile near*)
 0000:09E2       findfile(char near*,int)
 0000:0BAA       fopen(char near*,char near*)
 0000:0CEB       fread(void near*,long,long,jfile near*)
 0000:03B4       free(void near*)
 0000:0E19       fseek(jfile near*,int,char)
 0000:0E5E       ftell(jfile near*)
 0000:0EA0       fwrite(void near*,int,int,jfile near*)
 0000:0F3A       gets(char near*)
 0000:0819       getstring(char near*,char near*,char,int)
 0000:0D6B       getvector(unsigned char)
 0000:0526       itoa(int,char near*,int)
 0000:0F86       kernel_execute(char near*)
 0000:0297       kernel_print(char near*)
 0000:0247       keyready()
 0000:0981       li(char near*,char)
 0000:099B       locate(char near*,char near*,int near*)
 0000:06D1       malloc(int)
 0000:0B39       mgen(char near*)
 0000:0150       rand()
 0000:0CB1       readbyte(jfile near*)
 0000:025A       read_keyboard()
 0000:0AFA       refresh(jfile near*)
 0000:07C8       seperate(char near*,__string near*)
 0000:0C6E       setbyte(jfile near*,unsigned char)
 0000:0D43       setvector(unsigned char,(far*)(),...)
 0000:058A       sprintf(char near*,const char near*,...)
 0000:0851       str2int(char near*)
 0000:037F       strcat(char near*,char near*)
 0000:032D       strcpy(char near*,char near*)
 0000:019F       strlen(char near*)
 0000:03F3       strlwr(char near*)
 0000:01F9       strncat(char near*,char near*,int)
 0000:02BD       strncmp(char near*,char near*,int)
 0000:042D       strncmpi(char near*,char near*,int)
 0000:01BD       strncpy(char near*,char near*,int)
 0000:04C9       strrev(char near*)
 0000:03B9       strupr(char near*)
 0000:0D8F       timer(unsigned int)
 0000:0112       ub()
 0000:0E70       _k_panic(char near*,int)
 0000:0275       _video_clearscreen()
 0000:0288       _video_movecursor(char,char)
 0000:0267       _video_putchar(char)
 0000:0DC9       ____len(jfile near*)
 0000:04BB       ____ticker()
 0000:0110       DGROUP@
 0000:10C9       _MAIN
 0000:115A       _OLD_CLOCK

it crashes bad with your loading code
because the child overwrites the kernel


Tue Oct 06, 2009 8:49 pm
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 67 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 7  Next


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.