11 ms·
Getting started with bare-metal assembly
- djmips 6y agoWhy 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.
- yellowapple 6y agoIntellectual curiosity?
- Chris2048 6y ago> programmers I interact with in day to day life What kind of dev are you, and what kind of dev do you usually interact with?
- vzidex 6y agoAgreed, I think it's somewhat industry-dependent. I work with FPGAs and while we don't deal with assembly on a regular basis I'm confident everyone on my team would be able to develop or debug it.
- stevekemp 6y agoPartly curiousity, also I think there's a lot of nostalgia. Back when I got started with computers it was 1982, and the available programming languages were BASIC and Z80 assembly language. A lot of people growing up around that time did assembly language coding, because they needed the speed, or because (like me) they wanted to hack games for either infinite lives, or to ease copying. Of course assembly language is still used in some areas, it used to be necessary for optimization a lot more than today due to evolving general purpose hardware. But it is still useful for embedded and microcontroller programming. I don't often touch assembly these days because I have a need for it, but do it for fun because it reminds me of simpler times and fewer abstractions. e.g. a recent project: https://github.com/skx/math-compiler https://github.com/skx/math-compiler (Of course these days with microcode, and complex processors, it's not really bare-metal anymore. Not like it used to be. But try telling kids today that, and they won't believe you !)
- commandlinefan 6y ago> Partly curiousity, also I think there's a lot of nostalgia. I think it's deeper than that. I started coding professionally just before the internet took off. Before that I didn't have much interest in computer networks and seemingly overnight I was dealing with a huge new vocabulary of unfamiliar terminology like "port", "peer", "client/server", "protocol", "router", "bridge", "firewall". This wasn't just academic arcana either: whenever I had a problem, the network types would start asking me to check to see if the "firewall" was blocking my "client" from accessing the "port". I found myself able to muddle through by following their instructions, but I felt like a foreigner in a foreign land whenever anything didn't work the way it was "supposed to". Web programming documentation wasn't helpful in navigating this territory; it assumed you either knew all this stuff of somebody else was taking care of it for you. One day I decided to start from "the beginning" and read TCP/IP illustrated and within a few chapters it all came together and I found myself able to troubleshoot problems that sometimes even the networking "experts" couldn't resolve. Learning and understanding assembler is like that: once you understand EXACTLY how a computer works, there's no mystery in whether you coded it wrong or you didn't understand what to expect.
- newswasboring 6y agoAnswered in the very first sentence of the article > Seeing a program you wrote running directly on the bare metal is deeply satisfying to anyone who enjoys writing software. This is it. I have been on this site for over a decade now and most the obscure stuff is popular here because it satisfies this criteria for some people. Its actually different for different people. For some its bare metal programming, for some its obscure languages like K, and for even others its some libraries.
- throw1234651234 6y agoMost people don't do this in real life.
- commandlinefan 6y agosadly.
- rvz 6y ago> 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.
- christiansakai 6y agoI'm just your generic line fullstack web engineer. I'm still interested in these kinds of stuffs.
- abnercoimbre 6y agoCame 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/
- ngneer 6y agoFor me, the desire to better understand the innards.
- NwtnsMthd 6y agoSome of us still write assembly now and then, even if it is for embedded systems and not x86. I found this interesting!
- pjmlp 6y agoAnyone doing compilers, graphics, video or audio codecs, GPGPU, kernel development or OS drivers needs to know Assembly. Maybe you don't do any of this stuff, but there are plenty of engineers that do this daily.
- banachtarski 6y agoWhy are webdev 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. Oh right, not everyone is in my field.
- omniscient_oce 6y agoDid you coin the term 'anecdata'. I really like it.
- notadog 6y agoI doubt that they did - its been a term since at least the 1980s https://www.lexico.com/definition/anecdata https://www.lexico.com/definition/anecdata
- nandkeypull 6y agoKnowledge of assembly is also extremely important for computer security related topics such as reverse engineering, malware analysis, and exploit development.
- heinrichhartman 6y agoDoes 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? :)
- deleted 6y ago[deleted]
- kryptiskt 6y agoBelow 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.
- deleted 6y ago[deleted]
- wolfgke 6y ago> Below UEFI is the BIOS, or the firmware formerly known as the BIOS. Source? As far as I am aware, UEFI is a replacement for BIOS.
- Rusky 6y agoI believe they're using BIOS in a more general sense. You're right that UEFI replaces the old BIOS APIs that bootloaders used, but there's still firmware (e.g. coreboot) below or part of its newer APIs.
- izacus 6y ago> 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.
- 0xff00ffee 6y agoNot 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 6y agoThat's not really bare metal either. You should be directly manipulating the electrons flowing through the material :)
- chungus_khan 6y agoNah, electrons aren't quite full bare metal either. Mechanical computing gives you real bare metal!
- waynecochran 6y agoCan't have metal w/o electrons. :)
- newswasboring 6y agoJust 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)
- caspper69 6y agoMy first actual programming class was on an Intel 8085 kit, and I have to say that hand-assembling on pre-printed carbon sheets was pretty cool. It has served me well in my career to have started at that level.
- alharith 6y agoIt would serve everyone well to start at that level. I write enterprise CRUD webapps, and learning these things helped me tremendously. Unfortunately we now have a culture that views this type of knowledge acquisition as "gatekeeping"
- prox 6y agoHow so? Why would that be gatekeeping?
- terminalcommand 6y agoMaybe that is so, because teaching people assembly allows them to pull magic tricks on companies. An example I can think is Apple banning all dynamic code generation and execution on ios apps.
- 6y ago
- Koshkin 6y agoI am not sure if using heavily microcoded CPU instructions qualifies as "bare metal programming."
- commandlinefan 6y agorisque-metal programming maybe?
- blendo 6y agoTouche
- rixrax 6y agoI 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 6y agoI'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 6y agoI 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!
- perl4ever 6y agoOne homework problem I had in a computer architecture class circa the late 90s was, here's a hypothetical computer, now implement this instruction using the given microcode definition. I really enjoyed it because I was able to do it in much fewer micro-ops than the instructor expected.
- kahlonel 6y agoI 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 6y agoCubeMX 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.
- fmakunbound 6y agoIt 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 6y agoint 21h uses ... A BIOS CALL. :) Still not bare metal. :) (I'm totally gatekeeping for laughs: write your own BIOS you noob!)
- non-entity 6y agoOn a random, but interesting note though, manuals from some of the early IBM PC's have the BIOs source listing in them
- homarp 6y agoe.g. https://archive.org/details/bitsavers_ibmpcpc602renceAug81_17295874/page/n189/mode/2up https://archive.org/details/bitsavers_ibmpcpc602renceAug81_1... https://sites.google.com/site/pcdosretro/ibmpcbios https://sites.google.com/site/pcdosretro/ibmpcbios has the source code for various models typed.
- 0xff00ffee 6y agoI 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-Third-Party/dp/0201624850 https://www.amazon.com/PC-Interrupts-Programmers-Reference-T... 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.
- mlang23 6y agoDoes 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 6y agoTry '-nographic'. It can be a bit wonky IME, but good enough for the basics.
- toast0 6y agoIf 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 :(
- mlang23 6y agoThanks, -nographic did the trick.
- saagarjha 6y agoIf 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
- winrid 6y agoIf 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 6y agoPosted to HN multiple times: https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=Graphics%20black%20book&sort=byPopularity&type=story https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...
- DoofusOfDeath 6y agoAt first I thought this would be a story about DIY metalwork projects during quarantine.
- underthensun 6y agoLove playing with Assembly and bare-metal :)
- liquidify 6y agoI 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 6y agoWell, 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 =/
- pjmlp 6y agoSure it does, the problem here is the existence of CS degrees that don't teach programming properly.
- therealcamino 6y agoIt used to be standard to teach a Computer Organization course, which would involve some assembly programming, even in a liberal arts CS program.
- commandlinefan 6y agoNo kidding? When did you go to school? When I went back for my master's degree in 2005, they made me take a "leveling" undergraduate x86 assembler course because my 90's-era S/360 (yes, in the 90's) was too out-of-date, but they definitely required at least one full assembler course back then.
- lonelygirl15a 6y agoThere's a bunch of people doing bare-metal work on Ben Eater's two projects right now: https://eater.net/ https://eater.net/
- commandlinefan 6y agoFYI 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 6y agoYou 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 6y agoAh - thanks. I wish I'd known that before I installed the full X distribution.
- greyhair 6y agoYou can explore the same world easily in AVR, ARM M0/4, or PIC. And really be running on bare metal.
- cptnapalm 6y agoA 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.
- classified 6y agoIs there another level of assembly than bare-metal?
- ngcc_hk 6y agoAny instruction to run this under macOS. Qemu can run of course. But ovmf.fd (or install from edk2) seems no clear instruction under macOS.