[HN Gopher] Bare metal printf - C standard library without OS
       ___________________________________________________________________
        
       Bare metal printf - C standard library without OS
        
       Author : todsacerdoti
       Score  : 38 points
       Date   : 2025-04-26 21:32 UTC (1 hours ago)
        
 (HTM) web link (popovicu.com)
 (TXT) w3m dump (popovicu.com)
        
       | eqvinox wrote:
       | I was very confused by the title, expected someone writing their
       | own printf -- i.e. the part that parses the format string, grabs
       | varargs, converts numbers, lines up strings, etc.
       | 
       | I'd have called it "Bare metal puts()" or "Bare metal write()" or
       | something along those lines instead.
       | 
       | (FWIW, FreeBSD's printf() is quite easy to pluck out of its
       | surrounding libc infrastructure and adapt/customize.)
        
       | sylware wrote:
       | I am coding RISC-V assembly (which I run on x86_64 with a mini-
       | interpreter) but I am careful to avoid the usage of the pseudo-
       | instructions and the registers aliases (no compressed instruction
       | ofc). I have a little tool to generate constant loading code,
       | one-liner (semi-colon separated instructions).
       | 
       | And as a pre-processor I use a simple C preprocessor (I don't
       | want to tie the code to the pre-processor of a specific
       | assembler): I did that for x86_64 assembly, and I could assemble
       | with gas, nasm and fasmng(fasm2) transparently.
        
       | ChuckMcM wrote:
       | I was feeling a bit like the Petunia and thought "Oh no, not
       | again." :-) One of the annoyances of embedded programming can be
       | having the wheel re-invented a zillion times. I was pleased to
       | see that the author was just describing good software
       | architecture that creates portable code on top of an environment
       | specific library.
       | 
       | For doing 'bare metal' embedded work in C you need the crt0 which
       | is the weirdly named C startup code that satisfies the assumption
       | the C compiler made when it compiled your code. And a set of
       | primitives to do what the i/o drivers of an operating system
       | would have been doing for you. And voila, your C program runs on
       | 'bare metal.'
       | 
       | Another good topic associated with this is setting up hooks to
       | make STDIN and STDOUT work for your particular setup, so that
       | when you type printf() it just automagically works.
       | 
       | This will also then introduce you to the concept of a basic
       | input/output system or BIOS which exports those primitives. Then
       | you can take that code in flash/eprom and load a binary
       | compilation into memory and start it and now you've got a monitor
       | or a primitive one application at a time OS like CP/M or DOS.
       | 
       | Its a fun road for students who really want to understand
       | computer _systems_ to go down.
        
         | marssaxman wrote:
         | This was my attempt at a minimal bare-metal C environment:
         | 
         | https://github.com/marssaxman/startc
        
       ___________________________________________________________________
       (page generated 2025-04-26 23:00 UTC)