
Getting started with bare-metal assembly - a7b3fa
https://johv.dk/blog/bare-metal-assembly-tutorial.html
======
rixrax
I think one great way to do this is to get a Commodore 64 emulator (or Atari
2600 etc) and start writing and learning 6502 assy. Arguably its one of the
last instruction sets that was designed with humans writing code in assembly
(and not in some higher level language) making it excellent learning language.
You can readily run your code in emulators and for not too much $$$ you can
pick a real hardware from EBay to really run it on HW.

And once You think you’ve run hw to its limits there are plentiful demos and
examples around to blow your mind watching what people have been able to do
with these machines (often in obscure ways).

~~~
pizza234
I've always been skeptical of using retro machines to learn low-level
programming.

While the processors are simple, making non-trivial programs is hard, because
the machines as a whole have lot of limitations, making programming very
intricate, compared to more modern systems (say, 16-bit 80x86, but I guess
even Amiga and so on).

If the target it challenge for the sake of challenge, then nothing makes those
machines special, I mean, one can code directly in machine code if that's the
intention :-)

~~~
saagarjha
I had to teach an class recently that required introducing students to
assembler. I designed an _extremely_ RISC architecture (we’re talking mov into
pc for control flow, r31 is “suggested” for use as a stack pointer) but gave
it just enough that they could write more advanced instructions in assembler
macros, which they designed themselves. I think it worked out well!

~~~
gumby
Finally an application for MIX!

~~~
saagarjha
Well, kinda. The architecture is minimal but I had a strong focus on
orthogonality, a linear address space, and clean instruction decoding; so much
so that I named it REGULAR ;) The ISA ([https://github.com/regular-
vm/specification](https://github.com/regular-vm/specification)) was
specifically selected so that traditional control flow could be created with
the use of only one temporary register (some take a bit of work; conditional
branches decompose to branch islands for example).

~~~
gumby
Yeah I was being tongue in cheek. Your approach sounds great for encouraging
people to think about and find solutions to specific issues.

MIX was, in theory, supposed really to be the opposite: so universal/generic
that you'd ignore the language and focus on the lesson. Similar to the
motivation for using Scheme in SICP. Obviously times have changed :-).

Sort of the difference between putting on eyeglasses to see the world better
(MIX) or putting on eyeglasses to learn about how lenses work.

------
heinrichhartman
Does anyone know how to circumvent UEFI?

When the CPU starts, it will start reading instructions from a hard-coded
address on the memory bus / EPROM somewhere, right? How can I directly control
these bytes?

I don't want some proprietary firmware sit between me and the CPU.

If it's not possible on hardware because "secure boot", or whatever, this
should at least be possible in emulators like QEMU.

Does anyone know how to do that? ... or clear up my misconceptions? :)

~~~
kryptiskt
Below UEFI is the BIOS, or the firmware formerly known as the BIOS. There is a
project to make an open source firmware for PCs:
[https://www.coreboot.org](https://www.coreboot.org). It works on a selection
of newish motherboards.

You can't really start completely from scratch in an understandable way on
Intel platforms, and it's iffy on ARM. Because setting up the DRAM requires
some insane magic, where it doesn't really help if you can see the source.

~~~
izacus
> Below UEFI is the BIOS, or the firmware formerly known as the BIOS. There is
> a project to make an open source firmware for PCs:
> [https://www.coreboot.org](https://www.coreboot.org). It works on a
> selection of newish motherboards.

This simply isn't true - while UEFI firmwares do offer BIOS emulation, there's
no "BIOS" underneath them on most modern boards.

~~~
XMPPwocky
They might mean the "meta-bootloader" which brings up all those UEFI capsules-
afaik Intel's boot ROM doesn't, for example, parse PE headers.

------
0xff00ffee
Not exactly bare metal when you've got UEFI in there.

In the embedded world, Bare Metal means you control the first byte executed by
the CPU when it comes out of POST, and aren't using any kind of operating
system or proxy loader in between. But it gets kinda fuzzy, because RToS is
still "bare metal" and you have full access to the source-code.

~~~
waynecochran
That's not really bare metal either. You should be directly manipulating the
electrons flowing through the material :)

~~~
chungus_khan
Nah, electrons aren't quite full bare metal either. Mechanical computing gives
you real bare metal!

~~~
waynecochran
Can't have metal w/o electrons. :)

------
saagarjha
If you want to go down a different rabbit hole than being "bare-metal" while
still being in UEFI, take a look at EFI Byte Code: it's a strange and arcane
little virtual machine that you can use to write cross-platform UEFI drivers.
Here's a simple emulator for it:
[https://github.com/yabits/ebcvm](https://github.com/yabits/ebcvm)

------
kahlonel
I recommend anyone just starting with bare-metal assembly to get an STM32F0
board and write assembly programs for it. I'm just gonna plug my super small
toy RTOS I wrote for Cortex-M0 a while ago.

[https://github.com/kahlonel/rt0s](https://github.com/kahlonel/rt0s)

~~~
0xff00ffee
CubeMX is the best config manager I've ever used, the clocking interface alone
should be mandatory for all embedded companies.

I have still yet to really need an RTOS, even for BLE.

------
newswasboring
Just an aside, but it always felt weird to me that we call is bare-metal while
the actual code runs on mostly semiconductors.

I am very glad that in my bachelor program our microcontrollers class actually
made us hand enter hex codes in a kit. It got tedious after a while (maybe it
should have been for only a few weeks not the whole semester), but it gave me
a weird sense of being one with the machine. And it has as awesome ice breaker
when talking with older programmers. For some of them I am the only one of my
age that they have met who has ever done this. Another thing is it helped me
sort of see the flow of it and encouraged optimization.

(I don't want to give too much credit to my college, they did it not as some
great pedagogical trick but to save money and laziness)

~~~
tyingq
I think it might come from auto body shop terminology. We would remove layers
of paint or rust and get down to the "bare metal" for prep work. That "down to
the bare metal" phrase was very commonly used.

That is to say "no layers" or "right on the substrate".

Just a guess though.

~~~
newswasboring
> That is to say "no layers" or "right on the substrate".

Ok, I feel very pedantic saying this, but the substrate is the thing that is
mostly semi conductors :P.

~~~
pjc50
This "silicon is not a metal" pedantry is a whole new layer of tedium. But if
you want to take it that way: the data is mostly carried in the _metal_ layers
of aluminium, and the substrate is .. a substrate that's not doing very much.

~~~
newswasboring
If you really want to be pedantic then the data is processed and stored in
flip flops and stored as charge in semi conductor dielectrics.

But don't take this so seriously, I work in semiconductor manufacturing and
even I don't take it that seriously.

------
cptnapalm
A couple of years ago, I was learning PDP-11 assembly on 2.11 BSD and enjoying
it, but then the old textbook got to the point of system calls. I couldn't get
anything working properly, so eventually I found something else to do. I did
very much like it though.

Also, TIS-100 from Zachtronics (the assembly language game you never asked
for!) I think made assembly type programming less intimidating.

------
lonelygirl15a
There's a bunch of people doing bare-metal work on Ben Eater's two projects
right now:

[https://eater.net/](https://eater.net/)

------
fmakunbound
It used to be way easier. Something like this:

    
    
        debug hn.com
    
        a 100
        mov dx, 200
        mov ah, 9
        int 21
        mov ax, 4c00
        int 21
    
        a 200
        db "Hello, World$"
        
        w 300
        q
    

Replace 100 with 0 and write it to the first sector of a disk and you had a
bootable program (BIOS interrupts only, of course).

Edit: Geezus. It's just an example of how accessible getting something running
in assembly language was compared to all the qemu, UEFI stuff in the article.

~~~
0xff00ffee
int 21h uses ...

A BIOS CALL. :)

Still not bare metal.

:)

(I'm totally gatekeeping for laughs: write your own BIOS you noob!)

~~~
non-entity
On a random, but interesting note though, manuals from some of the early IBM
PC's have the BIOs source listing in them

~~~
0xff00ffee
I had a giant pink book of PC BIOS that was my bible from 1988 to 1994 before
the internet took off. I can't for the life of me remember who published it,
but it had everything you needed to know about PC BIOS and an old IBM bios
source listing. It was over 600 pages.

It look liked this 1200 page book, but this isn't it (also a book I used):

[https://www.amazon.com/PC-Interrupts-Programmers-
Reference-T...](https://www.amazon.com/PC-Interrupts-Programmers-Reference-
Third-Party/dp/0201624850)

I also had the Apple ][+ ROM listing in one of those spiral-bound books from
apple circa 1980 but that too has been lost, sadly.

~~~
spc476
Sounds like the _The Programmer 's PC Sourcebook_ (which is on my shelf, and
yes, one could say it has a pink cover).

~~~
0xff00ffee
YES! That is exactly it. I wish I saved mine for nostalgia.

------
winrid
If this interests you I suggest Michael Abrash's Graphics Programming Black
Book.

I have a copy I'd sell, although you can find it for free, legally, online.
It's about a thousand pages....

~~~
SkyMarshal
Posted to HN multiple times:
[https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...](https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=Graphics%20black%20book&sort=byPopularity&type=story)

------
greyhair
You can explore the same world easily in AVR, ARM M0/4, or PIC. And really be
running on bare metal.

------
mlang23
Does anyone know if qemu and OVMF can be made to work in plain text mode? I
just tried but qemu wants _GTK_ by default, and when I pass -curses I get a
message that the screen is in 640 x 480 graphics mode. Is UEFI really that
complex that it doesnt allow for plain console access?

~~~
a7b3fa
Try '-nographic'. It can be a bit wonky IME, but good enough for the basics.

~~~
toast0
If you do this (or -serial mon:stdio and leave the VGA output), you can do
console I/O via com1, and it works pretty well. As a bonus, this is viable on
real hardware too, although most consumer level motherboards don't do serial
consoles :(

------
DoofusOfDeath
At first I thought this would be a story about DIY metalwork projects during
quarantine.

------
liquidify
I always wanted to do this stuff, but classes just never taught it. I think
some of the ECE students got some classes which taught some assembly, but not
us lowly CS people.

~~~
coldpie
Well, assembly doesn't have much to do with computer science. The fact that
aspiring programmers are taking CS degrees is the root of this problem =/

~~~
therealcamino
It used to be standard to teach a Computer Organization course, which would
involve some assembly programming, even in a liberal arts CS program.

------
commandlinefan
FYI if you're trying to follow along - it appears that QEMU doesn't (for some
reason) run on a headless workstation. You'll have to have X installed.

~~~
AntiRush
You can run qemu without the graphical UI with `-nographic` (serial port
output to the console) or `-curses` which can show the vga text output on your
console.

~~~
commandlinefan
Ah - thanks. I wish I'd known that before I installed the full X distribution.

------
ngcc_hk
Any instruction to run this under macOS. Qemu can run of course. But ovmf.fd
(or install from edk2) seems no clear instruction under macOS.

------
classified
Is there another level of assembly than bare-metal?

------
underthensun
Love playing with Assembly and bare-metal :)

------
djmips
Why are assembly related topics so popular on Hacker News? I don't see pursuit
of this topic happening in my anecdata of programmers I interact with in day
to day life.

~~~
rvz
> Why are assembly related topics so popular on Hacker News?

Because there are some engineers perhaps at ARM, Apple, Microsoft, Intel or
Google who deal with compilers or operating systems and UEFI booting devices
that you're probably currently running or using to either build software or
even reply to this thread, who still take interest in reading about blog posts
like this.

> I don't see pursuit of this topic happening in my anecdata of programmers I
> interact with in day to day life.

Maybe not for you, but it's the reason why you are able to post your message
here and browse the web or build software faster thanks to the engineers who
did this sort of programming. Sure, general end-users shouldn't care but it's
gives me confidence that there are some engineers out there who understand
some OS internals to 'make' things happen even at FAANMG companies, rather
than doing generic web apps all day long.

~~~
abnercoimbre
Came here to say just that, but to also mention I know some web developers who
love these topics and are ahead of the game compared to their colleagues.

P.S. And if a low-level programmer goes into web, stuff like this [0] happens
:-)

[0] [https://nira.app/](https://nira.app/)

------
Koshkin
I am not sure if using heavily microcoded CPU instructions qualifies as "bare
metal programming."

~~~
commandlinefan
risque-metal programming maybe?

~~~
blendo
Touche

