Help with binary (loops?)

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.
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Help with binary (loops?)

Post 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?
Attachments
VGA_TEST.CPP
The source file.
(36.33 KiB) Downloaded 238 times
ym
Posts: 8
Joined: Thu Mar 24, 2011 2:05 am

Re: Help with binary (loops?)

Post by ym »

maybe your screen base address wrong.

BTS, your exe may include some dos functions maybe occurs some crash.
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Help with binary (loops?)

Post 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?
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Help with binary (loops?)

Post 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
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Re: Help with binary (loops?)

Post by abraker95 »

But how come it works good as an exe, but as a disaster as a binary?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Help with binary (loops?)

Post by Solar »

Does it really matter? Broken code can work by accident, but good code doesn't break.
Every good solution is obvious once you've found it.
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Re: Help with binary (loops?)

Post 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?
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: Help with binary (loops?)

Post 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.
"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 ]
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Re: Help with binary (loops?)

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Help with binary (loops?)

Post 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.
Every good solution is obvious once you've found it.
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Re: Help with binary (loops?)

Post 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)
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: Help with binary (loops?)

Post by Combuster »

They already exist. There is even a link at the very top of this page :wink:
"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 ]
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Re: Help with binary (loops?)

Post 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.
abraker95
Member
Member
Posts: 36
Joined: Mon Feb 14, 2011 7:39 am

Re: Help with binary (loops?)

Post 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 :(
User avatar
JackScott
Member
Member
Posts: 1032
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Mastodon: https://aus.social/@jackscottau
GitHub: https://github.com/JackScottAU
Contact:

Re: Help with binary (loops?)

Post by JackScott »

A very small nut to get started with: http://wiki.osdev.org/Barebones

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