Programming Fibonacci on a breadboard computer

Ben Eater · Beginner ·📐 ML Fundamentals ·11y ago

Key Takeaways

Programming a breadboard computer to calculate Fibonacci numbers using Assembly Language and machine code, demonstrating low-level programming concepts and computer architecture.

Full Transcript

okay so I got this compiled program from running a the g c compiler on my on my MacBook and this this Assembly Language will work on my MacBook but it won't work on this computer that I built because this computer has its own Assembly Language that that I sort of invented um and so what we would need to do is we would need to find a c compiler that would compile this program for this computer but um since I haven't written one it doesn't exist so instead what we can do is actually um kind of handwrite the Assembly Language program ourself and so that's what I've done here and so this this program is is basically the same thing as as this but it's written in the Assembly Language for for this computer um and so if we want to kind of look at it just really quickly um zoom in a little bit here help us see what's going on but if we look at our uh our C program again basically um what we're doing here in these first two lines is the x equals 0 actually we're doing the Y equals 0 here there's a little bit of an optimization that I needed to do because the uh the the memory on this program this this or excuse on this computer this computer only has 16 bytes of memory which is not very much memory at all um and so because we needed three bytes left over here at the end for the variables x y and z um I had to do a little bit of an optimization here to make the rest of the program fit and so the first two lines is setting y equal equal to one let me just cross that out this is y equals 1 is what this is doing so it's loading one into the a register and it's storing that in the E which is y uh at memory location e which is y um and then it's uh this is sort of uh the the x equals equals zero except we're not really saving it um and that's just to kind of save me that one line of code um so this is sort of x equals z and then we output um so the so we're kind of doing these a little bit out of order but doesn't matter um and then the print f is is sort of my my output here and then we do the add uh and we're adding uh let's see we're adding e so we're adding y to X so X is in the register we're adding y so this is kind of uh you can think of this line as being the Z well this line here actually these two lines are the zal X + Y because this store a is storing the the result of this in F which is address location f is z so this is the zal X+ y um and then these two lines here are the xals Y uh cuz we're loading and uh from E and then storing to D so e to D is y to X so xal Y and then here we're doing the uh y equals z and is the same thing we're going from F to e f is uh Z and address e is is is um variable y um and then at the end here we we load a again um and this is just or we load uh the value of x into the a register and that's that way when we loop back we output the value of x and this is kind of again the sort of optimization I did to um rather than storing it up here and then loading it again that would take two extra bytes um so this is down here um and then the jump carry so what this is saying is that if the result of the last addition which is up here uh had a had a carry bit which means that uh you know this is an 8-bit computer so if the result of that addition was uh took up nine bits um and you know because one bit had to be carried then go to the top and that's kind of a way of of detecting whether uh X is greater uh than uh than 255 because if it carried into a nth bit then you know something that takes you know more than eight bits to to to store is going to be greater than 255 so if that's the case then jump all the way to address Z which then resets the yal 0 xal 0 otherwise it falls down here and we jump uh we only jump up to address three which is the output so again sort of going back to the printf um so you can see this this program is is basically um an assembly another assembly of or another compilation I guess if you want to think of it that way um we've compiled this this C program uh but instead we've compiled it for uh this this homebuilt computer here now the next step um now that we have this Assembly Language is is to convert this to um to machine code um and so I've done that here and uh slide this over so you can see everything um and so what done here is is this is the same program that we just walked through and what I've done is just converted everything to Binary uh because when we program the computer we're going to be using these switches and we have to enter these things in in binary on these switches which is very cumbersome but fortunately it's a short program um and so basically the addresses here are just uh 0 through uh C heximal which is 0 through 1 1 0 0 in binary um and then each of these op codes here each of these instructions has a corresponding 4 bit binary value and so I've just substituted those in here um and then most of these um instructions have some sort of parameter that goes with them um and then that just is just converted to Binary over here and so this is what we'll we'll go ahead and enter um and so let me just kind of slide this over a little bit so we can get it right next to here and zoom in so we can see what's going on and so to program this I'm going to stop the clock real quickly and uh just clear everything out of the computer and then we'll switch it over to programming mode and so in programming mode what I can do is I can set the address so this is uh all zeros and then I can set down here the data that I want to program in so one one one 00 0 1 and then once I have that programmed in I'll just push this button to program it and then I can move on to the next value so this is memory location one and it's going to be turn a little off it's going to be 0 1 0 0 1 one one zero and I can program that and I'll go ahead and program the rest of this and through the magic of video editing we can speed this up for you and there we go so I think we're all programmed here so we can go ahead and shift that back into run mode and I will uh zoom out here so you can see what's going on and get this here think you can see everything there um and so now uh we can reset the computer and uh try running this and it looks like it is working yeah here we go it's counting through all the Fibonacci numbers and it looks like there might be a bug it looks like it's missing the last one the 233 so there's probably probably a little bug with the jump logic maybe um oh yeah because I'm outputting first and then adding um okay well maybe I'll fix that a little bit later but uh it's close enough we're getting Fibonacci numbers

Original Description

If you want to learn more about how this computer works or how I built it, check out https://eater.net/8bit Support me on Patreon: https://www.patreon.com/beneater
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Ben Eater · Ben Eater · 16 of 60

1 KA 60 Minutes Sep 2013 rerun (10x speed)
KA 60 Minutes Sep 2013 rerun (10x speed)
Ben Eater
2 Frame formats | Networking tutorial (6 of 13)
Frame formats | Networking tutorial (6 of 13)
Ben Eater
3 TCP: Transmission control protocol | Networking tutorial (12 of 13)
TCP: Transmission control protocol | Networking tutorial (12 of 13)
Ben Eater
4 Clock synchronization and Manchester coding | Networking tutorial (3 of 13)
Clock synchronization and Manchester coding | Networking tutorial (3 of 13)
Ben Eater
5 TCP connection walkthrough | Networking tutorial (13 of 13)
TCP connection walkthrough | Networking tutorial (13 of 13)
Ben Eater
6 Lower layers of the OSI model | Networking tutorial (7 of 13)
Lower layers of the OSI model | Networking tutorial (7 of 13)
Ben Eater
7 Hop-by-hop routing | Networking tutorial (11 of 13)
Hop-by-hop routing | Networking tutorial (11 of 13)
Ben Eater
8 Sending digital information over a wire | Networking tutorial (1 of 13)
Sending digital information over a wire | Networking tutorial (1 of 13)
Ben Eater
9 ARP: Mapping between IP and Ethernet | Networking tutorial (9 of 13)
ARP: Mapping between IP and Ethernet | Networking tutorial (9 of 13)
Ben Eater
10 Analyzing actual Ethernet encoding | Networking tutorial (4 of 13)
Analyzing actual Ethernet encoding | Networking tutorial (4 of 13)
Ben Eater
11 Intro to fiber optics and RF encoding | Networking tutorial (2 of 13)
Intro to fiber optics and RF encoding | Networking tutorial (2 of 13)
Ben Eater
12 The Internet Protocol | Networking tutorial (8 of 13)
The Internet Protocol | Networking tutorial (8 of 13)
Ben Eater
13 Looking at ARP and ping packets | Networking tutorial (10 of 13)
Looking at ARP and ping packets | Networking tutorial (10 of 13)
Ben Eater
14 The importance of framing | Networking tutorial (5 of 13)
The importance of framing | Networking tutorial (5 of 13)
Ben Eater
15 Programming my 8-bit breadboard computer
Programming my 8-bit breadboard computer
Ben Eater
Programming Fibonacci on a breadboard computer
Programming Fibonacci on a breadboard computer
Ben Eater
17 Connecting to a mystery signal | Digital electronics (4 of 10)
Connecting to a mystery signal | Digital electronics (4 of 10)
Ben Eater
18 Using a transistor to solve our problem | Digital electronics (8 of 10)
Using a transistor to solve our problem | Digital electronics (8 of 10)
Ben Eater
19 Inverting the signal with a transistor | Digital electronics (9 of 10)
Inverting the signal with a transistor | Digital electronics (9 of 10)
Ben Eater
20 8-bit computer update
8-bit computer update
Ben Eater
21 Bus architecture and how register transfers work - 8 bit register - Part 1
Bus architecture and how register transfers work - 8 bit register - Part 1
Ben Eater
22 RAM module build - part 2
RAM module build - part 2
Ben Eater
23 Using an EEPROM to replace combinational logic
Using an EEPROM to replace combinational logic
Ben Eater
24 Build an Arduino EEPROM programmer
Build an Arduino EEPROM programmer
Ben Eater
25 Build an 8-bit decimal display for our 8-bit computer
Build an 8-bit decimal display for our 8-bit computer
Ben Eater
26 8-bit CPU control logic: Part 2
8-bit CPU control logic: Part 2
Ben Eater
27 Reprogramming CPU microcode with an Arduino
Reprogramming CPU microcode with an Arduino
Ben Eater
28 Update and PODCAST ANNOUNCEMENT!
Update and PODCAST ANNOUNCEMENT!
Ben Eater
29 The case against Net Neutrality?
The case against Net Neutrality?
Ben Eater
30 Making a computer Turing complete
Making a computer Turing complete
Ben Eater
31 CPU flags register
CPU flags register
Ben Eater
32 Conditional jump instructions
Conditional jump instructions
Ben Eater
33 “Hello, world” from scratch on a 6502 — Part 1
“Hello, world” from scratch on a 6502 — Part 1
Ben Eater
34 What is a stack and how does it work? — 6502 part 5
What is a stack and how does it work? — 6502 part 5
Ben Eater
35 RAM and bus timing — 6502 part 6
RAM and bus timing — 6502 part 6
Ben Eater
36 Subroutine calls, now with RAM — 6502 part 7
Subroutine calls, now with RAM — 6502 part 7
Ben Eater
37 Why build an entire computer on breadboards?
Why build an entire computer on breadboards?
Ben Eater
38 How assembly language loops work
How assembly language loops work
Ben Eater
39 Binary to decimal can’t be that hard, right?
Binary to decimal can’t be that hard, right?
Ben Eater
40 Hardware interrupts
Hardware interrupts
Ben Eater
41 What is error correction? Hamming codes in hardware
What is error correction? Hamming codes in hardware
Ben Eater
42 Installing the world’s worst video card
Installing the world’s worst video card
Ben Eater
43 World's worst video card gets better?
World's worst video card gets better?
Ben Eater
44 Breadboarding tips
Breadboarding tips
Ben Eater
45 So how does a PS/2 keyboard interface work?
So how does a PS/2 keyboard interface work?
Ben Eater
46 Keyboard interface hardware
Keyboard interface hardware
Ben Eater
47 Keyboard interface software
Keyboard interface software
Ben Eater
48 How does a USB keyboard work?
How does a USB keyboard work?
Ben Eater
49 How does USB device discovery work?
How does USB device discovery work?
Ben Eater
50 How does n-key rollover work?
How does n-key rollover work?
Ben Eater
51 SPI: The serial peripheral interface
SPI: The serial peripheral interface
Ben Eater
52 Why was Facebook down for five hours?
Why was Facebook down for five hours?
Ben Eater
53 How do hardware timers work?
How do hardware timers work?
Ben Eater
54 The RS-232 protocol
The RS-232 protocol
Ben Eater
55 Hacking a weird TV censoring device
Hacking a weird TV censoring device
Ben Eater
56 Let's build a voltage multiplier!
Let's build a voltage multiplier!
Ben Eater
57 6502 serial interface
6502 serial interface
Ben Eater
58 RS232 interface with the 6551 UART
RS232 interface with the 6551 UART
Ben Eater
59 Fixing a hardware bug in software (65C51 UART)
Fixing a hardware bug in software (65C51 UART)
Ben Eater
60 Running Apple 1 software on a breadboard computer (Wozmon)
Running Apple 1 software on a breadboard computer (Wozmon)
Ben Eater

This video demonstrates how to program a breadboard computer to calculate Fibonacci numbers using Assembly Language and machine code, covering low-level programming concepts and computer architecture. The instructor writes the Assembly Language program, converts it to machine code, and programs the computer using binary code.

Key Takeaways
  1. Write an Assembly Language program for the breadboard computer
  2. Convert the Assembly Language program to machine code
  3. Program the computer using binary code in programming mode
  4. Switch to run mode and test the program
  5. Debug the program to fix any errors or bugs
💡 Low-level programming requires a deep understanding of computer architecture and binary representation, and debugging is an essential part of the programming process.

Related AI Lessons

The Beginner Python Mistake That 17 Tutorials Never Explained
Learn how a simple Python concept can transform your programming skills and avoid a common beginner mistake
Medium · Programming
The Beginner Python Mistake That 17 Tutorials Never Explained
Learn how a simple Python concept can transform your programming skills and avoid a common beginner mistake
Medium · Python
Train Neural Networks without Draining your Pocket: Distributed Training in TensorFlow using…
Learn to train neural networks efficiently using distributed training in TensorFlow without excessive costs
Medium · AI
Train Neural Networks without Draining your Pocket: Distributed Training in TensorFlow using…
Learn to train neural networks efficiently using TensorFlow's distributed training, saving time and resources
Medium · Deep Learning
Up next
Learn Deep Learning by Hand (Beginner's Guide - Part 1)
Thu Vu
Watch →