Page 1 of 3

Help with binary (loops?)

Posted: Thu Mar 31, 2011 3:34 pm
by abraker95
I've made a exe file with Turbo C++ 3.0 and then used exe2bin. I tried to run on boot and it kept going off from the memory location at where the program is. The exe is fine though, the words come up to the screen. Any help?

Re: Help with binary (loops?)

Posted: Thu Mar 31, 2011 7:03 pm
by ym
maybe your screen base address wrong.

BTS, your exe may include some dos functions maybe occurs some crash.

Re: Help with binary (loops?)

Posted: Thu Mar 31, 2011 11:09 pm
by Chandra
Are you sure CS:EIP is pointing to where this code is loaded? You've to manually setup your CS:EIP to pointer to this location. I don't think it is possible to do that while compiling with Turbo C++.

By the way, do you've a functional bootloader?

Re: Help with binary (loops?)

Posted: Fri Apr 01, 2011 2:17 am
by JamesM
Hi,

Quick code review:
  • Your loop to count the number of characters in the string is compeletely broken - it will only exit if the string ends with a '/'. Otherwise it will keep going off the end of the string.
  • Your chained "if"s would be better rewritten as a switch statement.
  • Your font writing code assumes that it is writing on a pre-blanked background, which is not necessarily true as there is no blanking code in the draw_text function.
  • The string that you send to draw_text is, uh, interesting - it ends with the two characters "/" and "0" - why don't you just rely on the NULL-termination you get for free when defining a C-string?
The reason your program dives off into unknown territory is that the string is not ended by ['/' 0] as your check looks for but ['/' '0'] (note one is the numeric value 0 and the other is the ascii character code for '0', which is 48).

Better solution? Change your check to:

Code: Select all

while(string[i]) {
    i++;
}
Cheers,

James

Re: Help with binary (loops?)

Posted: Mon Apr 04, 2011 6:08 am
by abraker95
But how come it works good as an exe, but as a disaster as a binary?

Re: Help with binary (loops?)

Posted: Mon Apr 04, 2011 6:21 am
by Solar
Does it really matter? Broken code can work by accident, but good code doesn't break.

Re: Help with binary (loops?)

Posted: Mon Apr 04, 2011 12:02 pm
by abraker95
Huh :?:

The problem is that it doesn't work as a binary; how else should to bootloader read it?
I tried to debug it using emu8086, but I still could figure out why it escaped the program.

Can somebody use compile the code with Turbo C, use exe2bin, and try to boot it to see if I did anything wrong in the process?

Re: Help with binary (loops?)

Posted: Mon Apr 04, 2011 12:20 pm
by Combuster
There's more than a single problem here:
- Your drawing code would even fail basic programming courses for programming language abuse. Do you even have the required knowledge?
- You don't use the official way of far pointer creation (use the MK_FP macro)
- You include the DOS header
- You build a DOS executable, not a platform independent executable
- You change an EXE into a binary file. The result is still something that requires DOS, as well as rather specific compiler settings.
- Emu8086 is old and never was worth the price with all the obvious hacks (it does not even have a bios), Turbo C is also old and nonstandard.

Please, learn proper C, use a sane compiler, and learn how your tools work before trying to make them do something they were not intended to do.

Re: Help with binary (loops?)

Posted: Mon Apr 04, 2011 3:00 pm
by abraker95
Yes master! :wink:


Your drawing code would even fail basic programming courses for programming language abuse. Do you even have the required knowledge?
There might be a way to simlify it, or make a better design, but as long as it works, I'm ok with it; making it work is more important than making it perfect. :D Although I did really get the abuse part :?

You don't use the official way of far pointer creation (use the MK_FP macro)
What I like is creating stuff on my own. MK_FP might do or have advantages over what I'm using, but I'd prefer me doing my own pointers.

You include the DOS header
I had suspition by looking at the DOS header.

You build a DOS executable, not a platform independent executable
You change an EXE into a binary file. The result is still something that requires DOS, as well as rather specific compiler settings.
I use Borland or Mingw, how do I set those specific compiler settings?

Please, learn proper C, use a sane compiler
What would be your suggestion of proper C? Unfortunatly TC 3.1 is the only 16 bit compiler I could find, and it allows me do ring 0 opperations/simulations splendidly!

Learn how your tools work before trying to make them do something they were not intended to do.
Any links to walkthroughs/tutorials? I've been through most of them, none of them stating how to make an OS using C, and if they do they stated it poorly.

Re: Help with binary (loops?)

Posted: Tue Apr 05, 2011 1:37 am
by Solar
abraker95 wrote:
Combuster wrote:Do you even have the required knowledge?
...as long as it works, I'm ok with it; making it work is more important than making it perfect.
That answers the question in the negative, it seems.
Although I did really get the abuse part :?
There are canonically accepted ways of "making it work". Deviating from them should only be done for a very good reason. Deviating from them without a good reason is a strong indication of a person not having the experience required to "get things done" regarding OS development.

Your way of terminating a string with "/0" is such a deviation from the canonical way, with a suspicion of a huge misunderstanding of your textbooks lingering beneath it. (C strings are terminated by '\0', and there's a world of difference between the two.)

Not only did you apparently not consider that including a header from any operating system ("dos.h") might not be a good idea if you're attempting to write your own OS. Your code in draw_text() is also highly inefficient, which does not bode well for further attempts at OS development. Not because OS code needs to be highly efficient, but because it points at a lack of experience in C coding.

All in all, it seems like you should try your hand at a couple of user-space projects before you attempt developing your own OS.

Re: Help with binary (loops?)

Posted: Tue Apr 05, 2011 6:13 am
by abraker95
How about a tutorial on how to:
Correctly compile the source file, and what header to and not to use (no need to mention dos.h twice)

Re: Help with binary (loops?)

Posted: Tue Apr 05, 2011 6:26 am
by Combuster
They already exist. There is even a link at the very top of this page :wink:

Re: Help with binary (loops?)

Posted: Tue Apr 05, 2011 7:07 am
by abraker95
I've been through most of them, none of them stating how to make an OS using C, and if they do they stated it poorly.

Re: Help with binary (loops?)

Posted: Tue Apr 05, 2011 2:55 pm
by abraker95
I've always noticed a pattern: I start out slow and poor, but with given support and materials I take off faster than a bullet. In this case I just need a few examples of how people started out with there OS (how they compiled and booted their first 10 - 50 lines of code). I learn better from working examples. I also would like staight foward stuff; People know how to do it yet they still send me to tutorial (back to square 1).

Opening a nutshell might be a VERY big struggle, but with a nutcracker or another reasonable tool you can easily get to the good stuff inside... in this case I'm trying to do it with bare hands, with everybody turning their back on me :(

Re: Help with binary (loops?)

Posted: Tue Apr 05, 2011 3:32 pm
by JackScott
A very small nut to get started with: http://wiki.osdev.org/Barebones

The nutcracker: http://wiki.osdev.org/GCC_Cross-Compiler