
How to run a program without an operating system? Ask Question How do you run a program all by itself without an operating system running? Can you create assembly programs that the computer can load and run at startup, e.g. boot the computer from a flash drive and it runs the program that is on the cpu? assembly x86 operating-system bootloader osdev edited yesterday Peter Cordes 116k 16 176 302 asked Feb 26 '14 at 22:13 user2320609 461 3 5 6 2 On which architecture? x86? ARM? – Kissiel Feb 26 '14 at 22:14 I was speaking generaly, but most likely x86 or x64 – user2320609 Feb 26 '14 at 22:19 yes that is exactly how processors boot. doesnt have to be assembly, C is often used with a little bit of asm for a bootstrap and perhaps some other support. – old_timer Feb 26 '14 at 22:26 13 Think of it: if there was no such capability, how would the OS itself start and run? :) – Seva Alekseyev Dec 4 '15 at 19:21 2 Answers How do you run a program all by itself without an operating system running? You place your binary code to a place where processor looks for after rebooting (e.g. address 0 on ARM). Can you create assembly programs that the computer can load and run at startup ( e.g. boot the computer from a flash drive and it runs the program that is on the drive)? General answer to the question: it can be done. It's often reffered to as "bare metal programming". To read from flash drive, you want to know what's USB, and you want to have some driver to work with this USB. The program on this drive would also have to be in some particular format. On some particular filesystem... This is something that usually boot loaders do. Many ARM boards let you do some of those things. Some have boot loader to help you with basic setup. Here you may find great tutorial of how to do basic operating system on Raspberry PI. Edit: This article, and the whole wiki.osdev.org will anwer most of your questions http://wiki.osdev.org/Introduction Also, if you don't want to experiment directly on hardware, you can run it as a virtual machine using hypervisors like qemu. See how to run "hello world" directly on virtualized ARM hardware here. edited Feb 27 '14 at 5:19 Isa A 849 9 27 answered Feb 26 '14 at 22:23 Kissiel 1,115 1 8 9 Runnable examples Technically, a program that runs without an OS, is an OS. So let's see how to create and run some minuscule hello world OSes. The code of all examples below and more is present on this GitHub repo. Boot sector On x86, the simplest and lowest level thing you can do is to create a Master Boot Sector (MBR), which is a type of boot sector, and then install it to a disk. Here we create one with a single printf call: printf '\364%509s\125\252' > main.img sudo apt-get install qemu-system-x86 qemu-system-x86_64 -hda main.img Outcome: Tested on Ubuntu 18.04, QEMU 2.11.1. main.img contains the following: \364 in octal == 0xf4 in hex: the encoding for a hlt instruction, which tells the CPU to stop working. Therefore our program will not do anything: only start and stop. We use octal because \x hex numbers are not specified by POSIX. We could obtain this encoding easily with: echo hlt > a.asm nasm -f bin a.asm hd a but the 0xf4 encoding is also documented on the Intel manual of course. %509s produce 509 spaces. Needed to fill in the file until byte 510. \125\252 in octal == 0x55 followed by 0xaa : magic bytes required by the hardware. They must be bytes 511 and 512. If not present, the hardware will not treat this as a bootable disk. Note that even without doing anything, a few characters are already printed on the screen. Those are printed by the firmware, and serve to identify the system. Run on real hardware Emulators are fun, but hardware is the real deal. Note however that this is dangerous, and you could wipe your disk by mistake: only do this on old machines that don't contain critical data! Or even better, devboards such as the Raspberry Pi, see the ARM example below. For a typical laptop, you have to do something like: Burn the image to an USB stick (will destroy your data!): sudo dd if=main.img of=/dev/sdX plug the USB on a computer turn it on tell it to boot from the USB. This means making the firmware pick USB before hard disk. If that is not the default behavior of your machine, keep hitting Enter, F12, ESC or other such weird keys after power-on until you get a boot menu where you can select to boot from the USB. It is often possible to configure the search order in those menus. For example, on my old Lenovo Thinkpad T430, UEFI BIOS 1.16, I can see: Hello world Now that we have made a minimal program, let's move to a hello world. The obvious question is: how to do IO? A few options: ask the firmware, e.g. BIOS or UEFI, to do if for us VGA: special memory region that gets printed to the screen if written to. Can be used on Protected mode. write a driver and talk directly to the display hardware. This is the "proper" way to do it: more powerful, but more complex. serial port. This is a very simple standardized protocol that sends and retrieves characters from a host terminal. Source. It is unfortunately not exposed on most modern laptops, but is the common way to go for development boards, see the ARM examples below. This is really a shame, since such interfaces are really useful to debug the Linux kernel for example. use debug features of chips. ARM calls theirs semihosting for example. On real hardware, it requires some extra hardware and software support, but on emulators it can be a free convenient option. Example. Here we will do a BIOS example as it is simpler on x86. But note that it is not the most robust method. main.S .code16 mov $msg, %si mov $0x0e, %ah loop: lodsb or %al, %al jz halt int $0x10 jmp loop halt: hlt msg: .asciz "hello world" link.ld SECTIONS { . = 0x7c00; .text : { __start = .; *(.text) . = 0x1FE; SHORT(0xAA55) } } Assemble and link with: gcc -c -g -o main.o main.S ld --oformat binary -o main.img -T linker.ld main.o Outcome: Tested on: Lenovo Thinkpad T430, UEFI BIOS 1.16. Disk generated on an Ubuntu 18.04 host. Besides the standard userland assembly instructions, we have: .code16 : tells GAS to output 16-bit code cli : disable software interrupts. Those could make the processor start running again after the hlt int $0x10 : does a BIOS call. This is what prints the characters one by one. The important link flags are: --oformat binary : output raw binary assembly code, don't warp it inside an ELF file as is the case for regular userland executables. Cooler x86 bare metal programs Here are a few cooler baremetal setups that I've achieved: multicore: What does multicore assembly language look like? paging: How does x86 paging work? Use C instead of assembly Since C compiles to assembly, using C without the standard library is pretty simple, you basically just need: a linker script to put things in memory at the right place flags that tell GCC not to use the standard library a tiny assembly entry point that sets required C state for main , notably: the stack zero out BSS TODO: link so some x86 example on GitHub. Here is an ARM one I've created. Things get more fun if you want to use the standard library however, since we don't have the Linux kernel, which implements much of the C standard library functionality through POSIX. A few possibilities, without going to a full-blown OS like Linux, include: Newlib Detailed example at: https://electronics.stackexchange.com/questions/223929/c-standard- libraries-on-bare-metal/223931 In Newlib, you have to implement the syscalls yourself, but you get a very minimal system, and it is very easy to implement them. For example, you could redirect printf to the UART or ARM systems, or implement exit() with semihosting. embedded operating systems like FreeRTOS and Zephyr. Such operating systems typically allows you to turn off pre-emptive scheduling, therefore giving you full control over the runtime of the program. They can be seen as a sort of pre-implemented Newlib. ARM In ARM, the general ideas are the same. I have uploaded: a few simple QEMU baremetal examples here on GitHub. The prompt.c example takes input from your host terminal and gives back output all through the simulated UART: enter a character got: a new alloc of 1 bytes at address 0x0x4000a1c0 enter a character got: b new alloc of 2 bytes at address 0x0x4000a1c0 enter a character See also: How to make bare metal ARM programs and run them on QEMU? a fully automated Raspberry Pi blinker setup at: https://github.com/cirosantilli/raspberry-pi- bare-metal-blinker See also: How to run a C program with no OS on the Raspberry Pi? For the Raspberry Pi, https://github.com/dwelch67/raspberrypi looks like the most popular tutorial available today.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages1 Page
-
File Size-