Trying to deal with low-level programming. Set the task - to write a "Hello World" program, write it to a USB flash drive, reboot, start the program from a USB flash drive ( without starting the OS ).

  1. Can this be done in C or C ++?

  2. How to make it clear to the BIOS that it should be loaded into memory, executed?

  3. What should be the program? Executable .com file? .bin file? I heard it should be a 16-bit application.

  4. Are there any restrictions in the name of the program?

  5. A little off topic: Should I use MASM? Which assembler is now most popular for x86?

Thank you in advance!

  • I am not at all knowledgeable in this area, but I do not think it is possible. Question 2, the fact of the matter is that the BIOS can say this in no way - it refers at the start to the boot sector, and not to external devices. That is, it will be necessary to manually call the program at startup. And, probably, from the BIOS, you can only load the OS, and this is too low level to write with a light hand. - AivanF.
  • AivanF, How is it manually? So after all the boot sector and on the flash drive is (or not?) - Arthur Klochko
  • Yes, but in a lower priority. This one still needs to somehow adjust the bios. - AivanF.
  • Your question is too extensive, please shorten it leaving only the most important. - Cerbo
  • AivanF, and, I understand what you are saying. So this is not a problem at all) - Arthur Klochko

4 answers 4

It is possible and in C / C ++, but in a very truncated form - almost no libraries. Only the BIOS functions ( int 13h ) are at your service (Ie, your "Hello, world" should not be displayed either using puts or printf , much less in cout ). No main as an input point (unless you change the runtime loader accordingly). Those. only some basic things (like arithmetic :)) that are compiled into .obj , and linked with a bootloader in an assembler. Once, for the sake of interest, I shortened Turbo C ++ to such a state - nothing even worked out :)

I used TASM in due time - perfectly suited for such purposes.

This code starts working in real mode, so only 16 bits, and (what about PC?), It is loaded into memory at address 0000:7С00 and starts to run from there. It is loaded from the MBR, so if it is something more or less large, then it should immediately count and load the rest into memory. Consider that the DOS com-file, only no org 100h - what is received, is simply put into a very specific :) memory - both on the disk and in RAM, and control is transferred to the first byte.

Accordingly, what is the name of the program here ... The name (file) is already a file system, and here it does not smell like it.

My advice: a virtual machine with a virtual floppy disk drive :), loading from such a virtual floppy disk. The first sector is yours. This will allow you to do what you want without risking anything, and it is very easy to write your creation to a file without having to worry about writing to specific sectors on a disk / flash drive (and you can easily mess things up).

    1. Yes.
    2. Write it to the MBR flash drive, and in the BIOS, choose to boot from USB over priority than from the hard drive.
    3. The easiest way is to get the assembler listing and enter the code using HexEdit directly into the MBR disk.
    4. Try not to use other brands in the name.
    5. Worth it. Great assembler.
    • About HexEdit I will start, I will understand. And what about C \ ++? - Arthur Klochko
    • @ArturKlochko No HexEdit is needed, it is easier to make a program to fill in the MBR or use ready-made utilities. If from under linux, so generally dd if=file of=/dev/device bs=446 count=1 yes, by the way, the code should be no more than 446 bytes, otherwise it will not fit and will have to be perverted. For hello world of course for the eyes is enough. Collect com-file, without org 100h (aka bin). If you make the C compiler compile without libraries and a com file, then of course you can - Mike
    • Mike, and how to write from under Windows? DMDE? What software is needed then? - Arthur Klochko
    • And what is the use of other brands? He is not going to sell his healoworld) - Schullz
    • Well, what if he wants a program called Microsoft Hello. - Vanyamba Electronics

    If you want to study low-level programming - start with microcontrollers, everything is sharpened for this. I recommend starting with avr - there are many good articles on asm and s are very convenient development environments and emulators. This is if you x86 is not critical.

    • Yes, I know about microcontrollers, I just want to do something more impressive first. Thank! - Arthur Klochko
    • easyelectronics.ru/category/avr-uchebnyj-kurs there are good articles on the subject of MK if you decide. There, too, HW can be written - as a rule, this is the output in the uart lines. - Artem Tikhonovich
    • Thank you, I am just picking up the material for training. - Arthur Klochko

    Yes, and practical benefits after studying low-level programming MK will still be more. Programming x86 or MK will differ little if you write in C (C ++ is not very common among M) from the point of view of learning. If of course you don’t have to write some exotic tasks for 86 platforms.