Original Hello World in "B" Programming Language - Computerphile
9:09

Original Hello World in "B" Programming Language - Computerphile

Computerphile 17.04.2026 98 641 просмотров 3 815 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
B is the forerunner to C - but seemed lost - Angelo Papenhoff decided to change it and brought it back from the brink! Here he tries to recreate Brian Kernighan's original 'Hello World' with a few wrinkles! Angelo's B compiler is here: https://github.com/aap/b Angelo's emulators: https://github.com/aap/blincolnlights There's a type in the end of the video, it should of course read "Obsolescence" Computerphile is supported by Jane Street. Learn more about them (and exciting career opportunities) at: https://jane-st.co/computerphile This video was filmed and edited by Sean Riley. Computerphile is a sister project to Brady Haran's Numberphile. More at https://www.bradyharanblog.com

Оглавление (2 сегментов)

Segment 1 (00:00 - 05:00)

C is one of the best known programming languages probably. Became famous due to Unix, but B is sort of an obscure historic detail that people don't really know about. And that is because the compiler has probably been lost as these it doesn't seem to be around anymore. Um so uh I wanted to play with this language um because the first hello world program was written in B in Brian Kernighan's tutorial on the B language. And I was very frustrated that the that there was no way to execute B code. So, yeah, I fixed that. Was there an A? There was no A, no. B was actually uh probably called B because it was a simplified version of BCPL, which was an earlier language. Um and somebody uh maybe Dennis Ritchie uh said that B was like BCPL filtered through Ken Thompson's brain. Um and also it had to run on a PDP-7 at the time, which was a very small machine and was very constrained. Um but this is about the PDP-11 version that I wrote. But it's actually a fairly um portable language. So, it runs on a number of systems um and we're going to see a little bit of that. So, let's just boot uh the PDP-11 first in this emulator here. Uh this is version 4 Unix, which uh was found uh just last year, so 2000 uh 25. And it's been read and I got it to boot and uh I made B run on it. So, we boot from the arcade disk and type the kernel name Unix. And I will log in as myself. And here we are. Let's see, I have some files here. And hello B is a little hello world program that I wrote. Unfortunately, not the same as Brian Kernighan's tutorial hello world program because that was written for an for a different machine that had a different word length and um unfortunately, it was dependent on that. So, I wrote my own version, but to just highlight how B is different from C, uh I wrote this put string function here. So, we can see the differences a little bit. This looks just like early C, main, put string, and a string. We have the asterisk for an escape character in B still. Um but that's just cosmetics. One interesting feature of B is that it only has a single data type, just the machine word. And that is the only machine dependent part of the language really. Everything else is really portable because it's uh compiled to a to an interpreted code or a threaded code, which people may be familiar from the fourth programming language. Um uh and the reason was is that the compiler is very short uh because it had to fit into a small memory uh amount of memory. And it just couldn't be very intelligent and just generating this uh this code for a stack machine uh was the reasonable thing to do. And that also makes it very portable. If we start here uh auto, people know this from C++ nowadays because they repurposed the keyword. But originally, it came from B and it just declares variables on the stack uh automatic variables. There's no int, no char, uh no pointers because we just have memory uh we just have machine words to work with. We just declare two variables, I and C. I is our counter that goes through the string and C is the character that we want to print out. You will notice that I have used a while loop because B didn't have for yet. So, we have to use while. And because there is no character or byte data type in B, there is actually a function called char, which extracts one character out of a string. So, it's just the base address of the string and the index. Like just goes character by character. So, we get that compare against the end of the string marker and until that is the case, we just print the character on screen. So, to see that in action, let's call the B command and give it hello. This is just a shell script actually. Calls [snorts] two programs and now we get an a. out executable. And there we go, hello world. A little bit about the implementation and how this works, the threaded code. Uh it's two programs actually, the B compiler, which takes uh B source code and produces an intermediate code. That intermediate code is an invention by me because I don't know the format of the original intermediate code. There was intermediate code, but I don't know exactly what it was. Needs to be in Ken Thompson's brain for that. Yeah, uh I

Segment 2 (05:00 - 09:00)

did ask Ken about it, but the details he didn't remember. So, uh this is what the intermediate code looks like and if we now run the assembler, the B assembler on it. hello. i, hello. s. Uh that now produces something that can be assembled by the Unix assembler. So, this is what it what the program comes out to be. And you will notice that this is not machine code. This one is, but most of it is just the threaded code that the compiler generates and it's doing just a very straightforward thing. And yeah, there you can see the way that B used to work on the PDP-11. The compiler does generate the original code from the binaries that have survived. So, there are a few B binaries. We have a standard library in B and the threaded code implementation, but no compiler, no large amounts of source code. Um so, the biggest uh that I that we probably have is I hope I have this here. Yep. Is probably the B compiler itself. So, you can see the the the Uh this is what B looks like. It's based on the earliest C compiler that has survived. And it's kind of morphed backwards in time again to generate B code and so on. I'm going to So, what you do that you take that early C compiler and you go right, okay, let me go through that and edit it so it works with B or It's not I didn't really edit it. It was more for inspiration. Like I took the structure, variable names, like where it made sense to retain it. The compiler is set up is constructed a bit differently from the C compiler, but you can still see that it used to be a B compiler. So, you can see that uh the B compiler is just isn't even a thousand lines of code of B. And the B assembler is even shorter. So, yeah, you can write a compiler in like about thousand lines of B code uh that compiles itself. — Yeah. And did you do that from looking at B programs that you had code for? Um yeah, so the things that were left over that had survived were um yeah, like object code that you would link into B programs, like for instance, printf and printn. They are in the standard library um and that had survived and there was source code for those functions in the documentation. So, you kind of can match what the original source code should generate. And yeah, that way I was able to reason my way through how the compiler must have worked. And yeah, again, it produces the original code for all the code I've tried on it. And one of the early bigger B programs was Yacc, the compiler compiler, yet another compiler from Steve Johnson. Uh that early B version has not survived. There is some early C code, but um yeah. So, yeah, that's uh that's kind of what I can say about B. And there are the some of this stuff on GitHub. Can people go and have a look at it and play with it and Yes, I actually have a guide for uh how to install it on these old Unix machines. It's a bit chaotic right now still. I have a bunch of ports uh to like to RISC-V, even to MIPS and X86, uh PDP-11, PDP-10 even on ITS. Uh so, it is portable. And it Yeah, it's a very fun tiny language. You can write a compiler probably in something like awk or any simple language. Uh like it's a fun project you can do. Uh and yeah, it's a very sweet short language. We're a loosely organized group of computer hobbyists. We kind of met each other at vintage computer festivals. And all of us we are sucked into the rabbit hole of pre-micro micro processor computing and we consider themselves them not to be um replicas, but more like computer

Другие видео автора — Computerphile

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник