loading and running programs from turbo c++

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

loading and running programs from turbo c++

Post by renovatio »

Hello, renovatio is an operating system written in asm and turbo c++. Up to now it has a CLI where you enter a file name and it tells you if it exists or not. Now i would like to load and run programs. Loading is very easy with int 13h. Running is very easy (from assembly) but from turbo c++?

i was thinking to do this:

Code: Select all

// 2000h:0h = the program

void run_prg(void)
{
   prepare_segments();
   asm {
      jmp 2000h:0h //this gives me an error : "Expression syntax". The syntax is wrong but why?
   }
}
thanks for helping me...
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Re: loading and running programs from turbo c++

Post by codemastersnake »

Turboc++ is good for 16 bit real mode OS, But I think you should shift to gcc and ld.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: loading and running programs from turbo c++

Post by inflater »

Yeah, another helpful answer. "Switch to gcc". Only a "Linus Torvalds slave" aka the fanatic open-source linux developer would say that. :roll:

As for the code, try this:

jmp 2000h:0 or jmp 2000:0 or jmp far 2000h:0 or

Code: Select all

label kam; (this is a expression from pascal but it should exist something in c++ alike)
jmp dword ptr cs:kam
jmp get_away
kam:
dw 2000h
dw 0
get_away:
If at 0x2000:0 is present a binary-format executable, it should load. If it's EXE, you need to look at the exe specs (or any good boot sector code) to get CS:IP from the exe header. :)

If you want to return from your program, I suggest replacing the jmp opcode with "call". That way, if you will have your stack alright, you can return from your program and resume execution in your OS.

And people, stop posting your stupid linux propaganda. You're post-hunting and spamming the whole forum. :roll:

Regards
inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: loading and running programs from turbo c++

Post by 01000101 »

I have never used TurboC++ and it's assembler syntaxes but maybe it dislikes the dual 'h' suffixes in that statement, try the '0x' prefix as an addition to the afore mentioned.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: loading and running programs from turbo c++

Post by inflater »

I don't think that turbo c++ will accept the prefix "0x" because it's from Borland and e.g. borland pascal (or turbo pascal) didn't like "0x" too. Correct me if I'm wrong with the C++ part.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: loading and running programs from turbo c++

Post by renovatio »

i tried changing the "0x" and "h" and the different combinations but i didn't work. anyone know the tasm syntax.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: loading and running programs from turbo c++

Post by inflater »

Code: Select all

push 2000h
push 0
retf
for "jmp 2000h:0", and

Code: Select all

push cs
push return_here (or push offset return_here)
push 2000h
push 0
retf
return_here:
... 
for "call far 2000h:0".

That should do the trick. All what JMP does is: pop the two words off the stack and jump to them. CALL does the same as JMP, but plus, it will put the return CS:IP to the stack. In this method, I've "simulated" the JMP and CALL FAR opcodes.

Hope this helps.

Regards
inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: loading and running programs from turbo c++

Post by renovatio »

i tried all you suggested but it doesn't work... thanks anyway
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: loading and running programs from turbo c++

Post by inflater »

Does the above code compile? Try debugging with bochs debugger: do a loop before the push retf opcodes, then trace the CS:IP if it's really coming to 0x2000:0 and if there is your application. If it comes to 0x2000:0 and it still doesn't work, something is wrong with your code.

Regards
inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: loading and running programs from turbo c++

Post by renovatio »

Inflater: the compiler tells me the syntax is wrong... but i can't realize why...
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: loading and running programs from turbo c++

Post by 01000101 »

can you post the 'exact' code, without any post-pasted comments.
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: loading and running programs from turbo c++

Post by renovatio »

yes of course... here is the code... i also wanted to say that the get_addr function, the load_dir, and the load_prg function worked perfectly... my problem is in "jmp 2000h:0h" (error: Expression Syntax). Thanks for helping me...

renova~1.cpp

Code: Select all

#include <screen.h>
#include <keyboa~1.h>
#include <disk.h>

#include <screen.h>
#include <keyboa~1.h>
#include <disk.h>

void main(void)
{
   unsigned char file[10];
   unsigned char resu;
   for (; ; )
   {
      put_str("? ");
      get_str(9, file);
      put_str("\n\r");
      load_dir();
      resu = get_addr(file);
      if (resu == 1)
      {
	 load_prg();
	 asm jmp 2000h:0h
	 put_str("\n\r");
      }
   }
}
disk.h

Code: Select all

#include <string.h>

struct file
{
   unsigned char name[10];
   unsigned char size;
   unsigned char sector;
   unsigned char track;
   unsigned char head;
};

struct file direntries[37];

unsigned char size;
unsigned char sector;
unsigned char track;
unsigned char head;

void load_dir(void)
{
   asm {
      mov ah, 2h
      mov al, 1
      lea bx, direntries
      mov cl, 1
      mov ch, 0
      mov dl, 0
      mov dh, 0
      int 13h
   }
}

unsigned char get_addr(unsigned char *file)
{
   unsigned char curr;
   unsigned char resu;
   for (curr = 0; curr < 14; curr++)
   {
      resu = strcmp(direntries[curr].name, file);
      if (resu == 0)
      {
	 size = direntries[curr].size;
	 sector = direntries[curr].sector;
	 track = direntries[curr].track;
	 head = direntries[curr].head;
	 return 1;
      }
   }
   return 0;
}

void load_prg(void)
{
   asm {
      mov ah, 2h
      mov al, [size]
      mov cl, [sector]
      mov ch, [track]
      mov dl, 0
      mov dh, [head]

      push es
      mov bx, 2000h
      mov es, bx
      mov bx, 0h

      int 13h
      pop es
   }
}
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: loading and running programs from turbo c++

Post by Combuster »

for starters, i would put all assembly within curly braces

so instead of

Code: Select all

asm jmp 2000h:0h
try

Code: Select all

asm {
    jmp 2000h:0h
}
then try aforementioned alternatives if that doesn't work. (including those braces!)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: loading and running programs from turbo c++

Post by renovatio »

i tried the following:

Code: Select all


asm jmp 2000h:0h

asm {
   jmp 2000h:0h
}

jmp 2000h:0

jmp 0x2000:0

jmp 0x2000:0x0

User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: loading and running programs from turbo c++

Post by Combuster »

read again: include those braces :roll:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply