What is a stack and how does it work? - 6502 part 5

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024
  • Schematics, datasheets, kits, and more at eater.net/6502
    Part 1: • “Hello, world” from sc...
    Part 2: • How do CPUs read machi...
    Part 3: • Assembly language vs. ...
    Part 4: • Connecting an LCD to o...
    Part 5: This video!
    Part 6: • RAM and bus timing - 6...
    Part 7: • Subroutine calls, now ...
    Support these videos on Patreon: / beneater or eater.net/support for other ways to support.
    ------------------
    Social media:
    Website: www.eater.net
    Twitter: / ben_eater
    Patreon: / beneater
    Reddit: / beneater
    Special thanks to these supporters for making this video possible:
    Adam Lininger, Adrien Friggeri, Alex Catchpole, Andrew R. Whalley, Anthony Cuccia, Armin Brauns, Austin Grant, BakerStaunch, Beau-James Erion, Ben Dyson, Ben Kamen, Ben Williams, Bradley Pirtle, Bryan Brickman, Carlos Ambrozak, Christopher Blackmon, Clayton Parker Coleman, Daniel Tang, Dave Walter, David Boardman, David H. Friedman, David House, David Turner, Dean Winger, Debilu Krastas, Dmitry Guyvoronsky, Dušan Dželebdžić, Dzevad Trumic, Eric Brummer, Eric Busalacchi, Eric Dynowski, Eric Twilegar, Erik Broeders, Eugene Bulkin, Foaly, fxshlein, HaykH, Ian Tait, Ingo Eble, Ivan Sorokin, Jason DeStefano, JavaXP, Jay Binks, Jayne Gabriele, Jeremy A., Jeremy Wise, Jimmy Campbell, Joel Jakobsson, Joel Messerli, Joel Miller, Joern Heidenreich, Jon Dugan, Jordan Scales, Joshua King, Justin Duch, Kefen, Kent Collins, Lambda GPU Workstations, Lucas Nestor, Maksym Zavershynskyi, Manne Moquist, Marcus Classon, Mats Fredriksson, Matt Alexander, Matthäus Pawelczyk, Michael, Michael Burke, Michael Garland, Michael Tedder, Miguel Ríos, Nathan Wachholz, Nicholas Counts, Nicholas Moresco, Örn Arnarson, Paul Pluzhnikov, Paul Randal, Philip Hofstetter, Randy True, Ric King, Richard Wells, Rob Bruno, Robert Blackshaw, Robert Butler, Ross, Sachin Chitale, Sam Rose, Samuel E Joseph, Scott, Sergey Ten, Sever Banesiu, SonOfSofaman, Stefan Nesinger, Stefanus Du Toit, Stephen Riley, Stephen Smithstone, Steve Jones, Steve Gorman, Steven Pequeno, Thomas Ballinger, Tom Burns, Vladimir Kanazir, Warren Miller, xisente, Ziggy L

Комментарии • 911

  • @thef1dude501
    @thef1dude501 4 года назад +2054

    Dude, your videos are something else, the amount of effort you put into them is unbelievable

    • @VACatholic
      @VACatholic 4 года назад +12

      Agree completely. Might I recommend vim macros for the replacing of code, though? :)

    • @cjay2
      @cjay2 4 года назад +2

      Agree completely.

    • @lifesworn9413
      @lifesworn9413 4 года назад +16

      @@VACatholic We won't hear the pleasent sound of the keyboard.

    • @twashing2006
      @twashing2006 4 года назад +5

      +1 Ben Eater's a beast.

    • @Zzznmop
      @Zzznmop 4 года назад +5

      ndirish Might I recommend you realize he is using his time to explain something else to all of us, though? :)

  • @MaxTheDragon
    @MaxTheDragon 4 года назад +212

    If you're adding RAM, I'd like to suggest 640k of it. That'll be enough for everyone.

    • @timh.6872
      @timh.6872 4 года назад +26

      Unfortunately we don't have enough address space unless we do some fun segmentation/swapping to work with >16 bit pointers. 16 bits is 65536 addresses, or 64K, not 640K.

    • @MaxTheDragon
      @MaxTheDragon 4 года назад +20

      @@timh.6872 I know. It was a joke. ;-)

    • @andrevc85
      @andrevc85 4 года назад +35

      Bill gates reference !

    • @zulqarnayeenschaftler9323
      @zulqarnayeenschaftler9323 4 года назад

      @@MaxTheDragon yo

    • @zulqarnayeenschaftler9323
      @zulqarnayeenschaftler9323 4 года назад +1

      @@andrevc85 xD

  • @rotteegher39
    @rotteegher39 8 месяцев назад +1

    I couldn't understand what the hell stack is until I watched these video series. Thank you.

  • @ddstutorials6048
    @ddstutorials6048 3 года назад

    Thank you for taking the time to share your vast and hard earned knowledge. Your approach makes difficult concepts easier to understand.

  • @alyctro8158
    @alyctro8158 4 года назад

    It's so mesmerizing, even though it's boring because i have no idea what i just watched.But that's not your fault. You've made a good video.

    • @massimookissed1023
      @massimookissed1023 4 года назад

      This might help slightly
      ruclips.net/video/lNa9bQRPMB8/видео.html
      (8mins)

  • @amitk79
    @amitk79 3 года назад

    Thanks for these videos!! God Bless you!!

  • @bennysh
    @bennysh 4 года назад +1

    First time I actually understand what "stack overflow" means!

  • @MrAndrewtux
    @MrAndrewtux 2 года назад

    Unbelievable value

  • @bassman87
    @bassman87 4 года назад

    in my assembly language class we had to recursively create a maze. We would recursively follow a sub-routine for each move until we hit a wall, then the program had to work backwards to return to the closest opening and start again. needless to say we were pushing a lot of return addresses to the stack.

  • @qwerty1423a
    @qwerty1423a 10 месяцев назад

    good job ben

  • @uamanager
    @uamanager 4 года назад

    Waiting for new video with RAM.
    BTW are you planning to record new videos about video card?

  • @shiewhun1772
    @shiewhun1772 2 года назад

    Oh. That's what StackOverflow means

  • @Stripes2711
    @Stripes2711 Год назад

    Can you just tell it which address to return to, instead of using a stack? Also, good explanations.👍

  • @joaquinortiz279
    @joaquinortiz279 4 года назад +777

    I want to let you know that you are one of the reasons why I as an electrical engineer student chose the specialization of computer engineering

    • @pesto12601
      @pesto12601 4 года назад +34

      I enrolled in computer engineering back in 1988... left the program in 1990 and switched to programming because of a) terrible part-time "teachers" who were full time engineers but couldn't explain b) tired of everyone in the class getting 40's on exams and then getting 70's thx to a curve. I felt like I was learning ANYTHING related to actual computer engineering nor could I understand what was going on.
      Fast forward to 2020 and I just learned more in a 25 minute video than in an entire semester of college! The internet and access to info has changed learning... if I had these tools, I probably would have stuck with the program all those years ago! Thank you for doing what you do.... very clear explanation and well presented!

    • @zulqarnayeenschaftler9323
      @zulqarnayeenschaftler9323 4 года назад

      Can we use another eeprom as our ram?

    • @TimothyGrindall
      @TimothyGrindall 4 года назад +4

      @@zulqarnayeenschaftler9323 No, because ROM is read only memory. Can't write to it.

    • @thepawday
      @thepawday 2 года назад +1

      Okay, but how do we programed the first ROM?

    • @jimmysyar889
      @jimmysyar889 2 года назад

      @@zulqarnayeenschaftler9323 theoreticlly

  • @HughWilliams1
    @HughWilliams1 4 года назад +1907

    Ben Eater is like Bob Ross for computer engineers.

    • @Breakfast_of_Champions
      @Breakfast_of_Champions 4 года назад +17

      engineers or 80s kids did this after school 😋

    • @ianhamilton350
      @ianhamilton350 4 года назад +73

      "There are no errors, just happy stack overflows"

    • @markjones5973
      @markjones5973 4 года назад +34

      "We're going to insert this happy little RAM chip over here!"

    • @gingerkurmudgeon4838
      @gingerkurmudgeon4838 4 года назад +6

      The most perfect description!

    • @urugulu1656
      @urugulu1656 4 года назад +1

      just that ben is still alive.

  • @carterisonline
    @carterisonline 4 года назад +883

    now THIS is full stack programming

  • @RealNovgorod
    @RealNovgorod 4 года назад +561

    "Please insert RAM to continue and press ENTER"

    • @dav1dsm1th
      @dav1dsm1th 4 года назад +45

      or just download some...

    • @davidflores909
      @davidflores909 4 года назад +17

      **Presses F**

    • @DaNiel-wj8qk
      @DaNiel-wj8qk 4 года назад +11

      or just PRESS PLAY ON TAPE

    • @carlos2004
      @carlos2004 4 года назад +7

      @@dav1dsm1th first he would have to build a wifi module.

    • @Kitulous
      @Kitulous 4 года назад +6

      @@carlos2004 HEY THAT'S A GREAT IDEA! I'd like to watch him build a Wi-Fi module, maybe a bare bones but a working one.

  • @UpcycleElectronics
    @UpcycleElectronics 4 года назад +265

    Awesome breakdown. The time spent on the highlight boxes was definitely worthwhile. I'm watching this in the morning before I'm 100% focused. Without the boxes I would probably shrug it off as something to rewatch later while trying to replicate it. With the highlight boxes I followed completely. Thanks! I know something apparently small like this requires an enormous amount of editing effort.

  • @mumiemonstret
    @mumiemonstret 4 года назад +231

    This is top-notch pedagogy. How you kept the mystery of the malfunction alive as long as possible and acted surprised about it!

    • @TheGuyThatEveryoneIgnores
      @TheGuyThatEveryoneIgnores 4 года назад +55

      Having grown up programming the 6502 (I am watching these videos for nostalgia), I knew where this was headed as soon as he typed the JSR instruction. However, he played it quite convincingly. I guess he wants to show people how to work through problems and not get discouraged. This makes him not just a good teacher, but a good mentor as well.

    • @MadScientist267
      @MadScientist267 3 года назад +8

      Don't over-encourage the surprise... That's how you get cheesy action lab videos

    • @knghtbrd
      @knghtbrd 2 года назад +3

      @@MadScientist267 I think Ben Heck called it "regrettable acting". 🤣 It's fair enough to go ahead and do it wrong and have people who figured it out going "ooh ooh ooh!" like they did when they were kids in school though…! 😉

    • @thewiirocks
      @thewiirocks 2 года назад +5

      @@TheGuyThatEveryoneIgnores I had much the same reaction. I'm watching thinking, "isn't he going to need a stack for that?" He played it so straight that I was kind of convinced he was going to pull a clever trick out of his hat. But nope. Just a "well that didn't work". Kind of satisfying to know _why_ it didn't work, though.

    • @apveening
      @apveening Год назад

      I had forgotten about the return stack, but a very good explanation.

  • @ahmedbendz1875
    @ahmedbendz1875 4 года назад +413

    A newbie interne programmer : real man programs in "C"
    Ben eater : hold my 6502 please

    • @MrPnew1
      @MrPnew1 4 года назад +1

      Gold :)

    • @humm535
      @humm535 4 года назад +5

      Real Programmers don’t use Pascal, also they use FORTRAN for calculation, FORTRAN for string processing, FORTRAN for artificial intelligence. But of course they have no problem writing in machine code or just fixing already compiled programs.
      see also “Real Programmers Don’t Use Pascal”

    • @DarthZackTheFirstI
      @DarthZackTheFirstI 4 года назад

      @Lenny69 シ i just imagined a great painting to it, shame i cant draw it that way XD

    • @argadargad9128
      @argadargad9128 4 года назад +2

      Well when you see an intern position where they want you to know IoT WiFi BLE Embedded Linux, you kinda become overwhelmed and don't learn asm.

    • @keiyakins
      @keiyakins 4 года назад +12

      @PolySakenearly ROMs were hand made by little old ladies who had been knitting for decades and could thus handle working with wire at tiny scales. No joke.

  • @planetdilien2932
    @planetdilien2932 4 года назад +272

    20:09 stack overflow, I wonder were I’ve heard That before

    • @hacker-7214
      @hacker-7214 4 года назад +3

      Its trash

    • @lexibigcheese
      @lexibigcheese 4 года назад +21

      many places e.g. stackoverflow.

    • @paulstelian97
      @paulstelian97 4 года назад +15

      The website took its name from the concept, not vice versa.

    • @lexibigcheese
      @lexibigcheese 4 года назад +40

      @@paulstelian97 I think most of us figured that out already, there is a chance that you whooshed.

    • @paulstelian97
      @paulstelian97 4 года назад +7

      @@lexibigcheese You'd be surprised how many I've seen that don't know that.

  • @paulstelian97
    @paulstelian97 4 года назад +212

    14:36
    "Our computer doesn't actually have any RAM in it"
    Never thought I'd ever hear that.

    • @markjones5973
      @markjones5973 4 года назад +43

      After he fixed up the code and plugged the ROM chip in, I started yelling "No no no no!! Not gonna work!" at the screen like it's a damn football game! LOL.

    • @paulstelian97
      @paulstelian97 4 года назад +18

      @@markjones5973 lmao, yeah I didn't actually consider that the system didn't have RAM yet, but it took me rather little to figure it out after the fact

    • @joshuawalker7054
      @joshuawalker7054 4 года назад +5

      Well, it has memory, it's just read only...

    • @noether9447
      @noether9447 4 года назад +3

      @@joshuawalker7054 well it is an EEPROM so not really read only. Also I think that the EEPROM can be used instead of a RAM here?

    • @RealCadde
      @RealCadde 4 года назад +8

      @@noether9447 Except you really don't want to do that.

  • @secular8887
    @secular8887 4 года назад +218

    no one ever showed this.....while teaching these concepts.....
    these videos are out off the world

    • @nkusters
      @nkusters 4 года назад +13

      Yup, Ben is on a whole nother level.

    • @lexus4tw
      @lexus4tw 4 года назад +6

      I learned this in university, but yes not many people go in detail

    • @MikehMike01
      @MikehMike01 3 года назад +1

      I learned all of this in my CS program

  • @richardlighthouse5328
    @richardlighthouse5328 4 года назад +44

    Great! Now I have to wait another month for Ben to put ram chip into 6502 computer.

    • @MichaelRebar
      @MichaelRebar 4 года назад +1

      Yeah, while I enjoy the 30 minute primer, I would have appreciated a warning that this was purely concept/theory and that we'd have to wait for the next vid to get to the actual RAM installation. I pulled out and setup all my kit to follow along with this video for...well...nothing, as everything I needed to learn this time was in the video. No real hands-on needed. I would have released this video AND the RAM install video at the same time...even if that meant withholding this one just a couple weeks longer.

    • @WarrenGarabrandt
      @WarrenGarabrandt 4 года назад +11

      You could try to put it in yourself. Just check the data sheet for that ram to see which pins do what, and see if you can figure out how it goes together. That type of independent exercise is enormously educational. Even if you fail, it's just a teachable moment.
      When Ben comes back with the next video, his way might be a little different than your way, but there are many different ways to connect these together (logic processing of the address lines I mean).
      Just make sure you are careful with your output enable line and r/w line so that your ROM, RAM, and CPU never have their outputs enabled at the same time on the data buss. You will burn out something if you do that.

  • @mousefad3673
    @mousefad3673 4 года назад +123

    How I wish I'd had access to these videos back when I was trying to understand 68000 assembler on the Atari ST. Before the Internet, and with crappy tech sections in the local library I had one pretty opaque book as a resource, and I really struggled.
    These videos are absolutely brilliant - thank you for putting them out there. I have no doubt that in 10 years time some really accomplished engineers will owe a lot of their success to you.

    • @DarthZackTheFirstI
      @DarthZackTheFirstI 4 года назад +1

      if we had that back then we could have programmed some nes game (kinda same chip afaik) ;D . wish i had a library that had tech books XD .

    • @1over137
      @1over137 4 года назад +6

      The other issue back when I was trying to learn this was the SDK for the Commodore Amiga was £130 (in 1987!) and so completely out of reach. As soon as you wanted to do something beyond print to the CLI you didn't have the includes, offsets, vectors to call routines you needed. There were some "sandbox" environments but they weren't worth much.
      By the time I got to an OS with and open stack it was a Celeron CPU with a modern OS (Linux) and a lot harder to understand and code for.
      I found the arduinno (or other MCU) is a nice little place to learn assembler, though a little different than a 6502/68000, it still small and simple enough and not bogged down in the details of the OS.
      It's really heplful to see Ben apply this to a real CPU on a BB.

    • @umutk5614
      @umutk5614 4 года назад +3

      That is exactly what i thought. I wish we had this videos back in Amiga times 🙄

    • @kaitlyn__L
      @kaitlyn__L Год назад

      @@DarthZackTheFirstI NES uses the 6510, a 6502 variant, while OP talks about a 68k (Amiga, Mac, ST, Mega Drive) :) so indeed _these videos_ could help get started with an NES. But the core concepts can also help to understand any machine-specific assembly, you just have to go look at the data sheet to find specific behaviour (ie the Z80, used in the Game about, initialises quite differently to the 6502)

  • @msmith2961
    @msmith2961 4 года назад +10

    The 'now why didn't this work as expected' autopsy is the best way to get a deep understanding of a subject. I learn more in each video than I did in 2 years of studying Computer Science.

  • @notmuch_23
    @notmuch_23 4 года назад +52

    I always wondered what the "stack" part of "stack overflow" was, this was very educational. Thank you!

    • @jonathanb3899
      @jonathanb3899 2 года назад +6

      You know what's crazy? I always wondered what the "overflow" part was!
      I understood it as a consequence for running out of stack, but this is a whole new level of understanding

  • @MakeItWithJim
    @MakeItWithJim 4 года назад +19

    23:18 I don't think you drew it upside down - its a stack so things get piled on top of each other to build a stack!
    Really insightful video - thank you

  • @mariuszdullak1399
    @mariuszdullak1399 3 года назад +12

    I am software engineer with MSc and over 15 years of professional experience. 6502 Hello world was like click bait for me, since I just recently started playing with Arduino and ESP32.
    I would like to send BIG thank you to you Ben Eater. I should have learned that during my MSc, but it was not part of it.
    I also need to say, I am admiring when you have printed data sheet (instead of displaying PDF), but the thing that makes me smile every time I see it in your videos is the paper with all the addresses, codes, etc. written manually. It is not just that you use paper to show that off (actually SP pointer as a little arrow "forced" me to write this comment). You actually did handwrite all of the 0's, 1's and other HEXes. It is not printed it is hand written. WOW !!!

  • @MozartJunior22
    @MozartJunior22 4 года назад +45

    Every time you upload another video in the series I realize I completely forgot everything you showed in the previous videos

    • @benjamindragon598
      @benjamindragon598 4 года назад +5

      Could be the spacing out of videos. Just go back and binge watch

    • @maximyarmolik1726
      @maximyarmolik1726 4 года назад +2

      @@GerardMenvussa XD

    • @HomeofLawboy
      @HomeofLawboy 4 года назад +2

      You know what time it is then
      _Rewind Time_

    • @BertGrink
      @BertGrink 4 года назад

      @@GerardMenvussa Well played, Sir or Madam, well played. :D

    • @MozartJunior22
      @MozartJunior22 4 года назад

      @@benjamindragon598 Well sure but each video is 30 minutes and ain't nobody got time for that

  • @toddw9021
    @toddw9021 4 года назад +31

    I kept wondering why the stack pointer started at 0124 and was about to look it up but of course I should have known that Ben would cover it (21:02). These are some of the most informative videos on these topics that I've ever seen. A+

    • @vuurniacsquarewave5091
      @vuurniacsquarewave5091 3 года назад +3

      It's probably the result of some random leftover current, or capacitance, that S started with that value.

  • @redpillcommando
    @redpillcommando 2 года назад +10

    This is mind boggling. I was a contractor at Apple back in the eighties. I remember one of the engineering teams at Apple getting a fancy HP buss analyser that cost thousands of dollars. They were doing just about the same thing you are doing with a ten dollar Arduino mega. We live in an age of digital wonders.

  • @frankeke8541
    @frankeke8541 4 года назад +31

    My fav series on youtube

  • @plshalpme173
    @plshalpme173 4 года назад +27

    you are a blessing, thank you for existing.

  • @rogerfar
    @rogerfar 4 года назад +109

    Interesting, I always thought the stack was part of the CPU, like the stack pointer register is too. In modern CPU's the stack is still part of your internal RAM?

    • @toto123456ish
      @toto123456ish 4 года назад +81

      Ironically, stackoverflow has an answer for you. stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap. So the stack is in RAM but due to the cache system in modern CPUs, it's probably stored inside the CPU cache most of the time.

    • @Selicre
      @Selicre 4 года назад +16

      Not only is it in RAM, you can manipulate it from inside your own program. This is how green threads work.

    • @markjones5973
      @markjones5973 4 года назад +33

      Yep. In modern computers, the stack pointer can be adjusted to point anywhere in RAM. Multitasking OSes create a stack for every process, in fact. And that RAM can be accessed like any other parts of RAM, and even moved around or shifted out to long term storage if memory gets low, and shifted back in when needed.

    • @lexus4tw
      @lexus4tw 4 года назад +12

      The stack will always be a part of the ram, in multi thread/process operating systems like windows each thread or process gets its own stack and heap

    • @BertGrink
      @BertGrink 4 года назад +9

      @@Selicre Great care should be taken if you "mess around" with the stack, though; an "unbalanced" stack is a recipe for disaster.

  • @piotraniola5393
    @piotraniola5393 4 года назад +14

    Isn't it weird the processor stores the return address on stack BETWEEN reading the lower and higher byte of the jump address?

    • @kallewirsch2263
      @kallewirsch2263 4 года назад +1

      It is also weird, that it actually stores the address of the lower byte of the jsr address.
      I would have expected to see a 0x800F on the stack as this is the address of the next instruction.

    • @piotraniola5393
      @piotraniola5393 4 года назад +4

      @@kallewirsch2263 that's actually because the processor increments the PC before each instruction. so after fetching the return address and before running the next instruction it will increment the program counter and thus resume in the correct place

    • @kallewirsch2263
      @kallewirsch2263 4 года назад +4

      @@piotraniola5393
      I thought that too (That the Von Neumann cycle would start with incrementing the PC) and I did a check.
      It is indeed an exception for the jsr and ret instructions.
      You can see it with the jump instructions. In the opcode the target address is coded without subtracting 1.
      This was the point when I became unsure and I used the "last resort" - google. It is indeed a well known exception for jsr and ret instructions. jsr pushes the address of the last address byte and ret compensates for that by adding 1 before assigning to the PC.
      Which begs the question: how does it work with interrupts. Well, a reti (return from interrupt) does not add 1 to the address poped from the stack. :-)
      I can only guess: The designer had some troubles in the jsr instruction to get his hands onto the address of the next instruction before performing the call by assigning the target address to the PC. Thats why he used the last known good value for PC he had, which was the address from where to load the low byte of the target address.
      But this is only a guess. I am not very familier with the inner workings of a 6502. I always prefered a Z80

    • @Brian_Of_Melbourne
      @Brian_Of_Melbourne 4 года назад +1

      Piotr, the timing of the storing of the return address on the stack within the execution of the JSR is about selecting the time when the internal mechanisms have the correct value (of the PC register) able to be saved before it gets re-written by the JSR's target address.

  • @K-o-R
    @K-o-R 4 года назад +9

    Please add a ZIF socket for the ROM chip while you're adding RAM :)

    • @BertGrink
      @BertGrink 4 года назад

      That seems like a very good idea.

    • @pev_
      @pev_ 4 года назад

      Yeah it is somewhat irritating to have to pry and then push back a large chip in a breadboard.

  • @richfiles
    @richfiles 4 года назад +18

    What I wouldn't have given to have had access to tutorials like this 30 years ago. I understand more in 20-30 minutes on RUclips than I did in an entire lifetime of tinkering, education, and life experience.

  • @ghosthuntergr
    @ghosthuntergr 4 года назад +27

    Ben is the best instructor ever! Also my kits have arrived and it is perfect. Thank you!

  • @dmsaintrain
    @dmsaintrain 4 года назад +5

    Very nice. I've known this stuff for decades, but listening to you is like hearing a live performance of a favorite tune. Thanks.

  • @jafizzle95
    @jafizzle95 4 года назад +4

    I've heard the word Stack Overflow for years and years and somehow I've never once wondered what that actually meant. Now I understand sort of.

  • @dohdohbryan
    @dohdohbryan 4 года назад +7

    I'm second year computer engineering, and these videos affirm that I am doing what I love!

  • @XxUltimateGodzXx
    @XxUltimateGodzXx 2 года назад +3

    This man is debugging by tapping into the data pins of a CPU and I had to deal with teammates in college who complained that debugging was too hard to do.
    This video is SUPER satisfying and makes me happy that I have a debugger in so many IDE's.
    Been meaning to get into code like this, been having a lot of fun in C on my Arduino and for fun in Linux.

  • @cristiconstantin452
    @cristiconstantin452 4 года назад +6

    Ben's effort is unbelievable. His videos are breathtaking and I cannot recommend them enough.

  • @hyperion2858
    @hyperion2858 4 года назад +15

    I love how you‘re producing high quality, often also relatively long, videos instead of this 10 minutes clickbait ad-spam stuff which can be found all over youtube.
    Your content is extremely interesting (though complicated xD) and unique to your channel.
    Keep up the good work!

  • @mattiviljanen8109
    @mattiviljanen8109 4 года назад +3

    It's surprising how close "Hello, world!" is to "stack overflow" and "initializing stack pointer"...

  • @ristomatti
    @ristomatti 4 года назад +2

    Amazing video! I'm a software developer by trade and it's been fascinating to gain better understanding of the inner workings of computers.
    Tip: Press single quote (') twice to jump to last edit location.

  • @clumsyjester459
    @clumsyjester459 4 года назад +4

    So, after seeing the GPU videos, my only remaining wishes would be a multi-core CPU and a self-built FPGA.
    Your work is truly amazing.

  • @Vermilicious
    @Vermilicious 4 года назад +1

    With a computer science degree, this isn't new to me, but I didn't know about the 6502 and how similar it is to "good old" x86. The basics are the same. What was a bit frustrating for me during my studies, was that I knew roughly how it all related to the electronics, but I didn't know how to bridge electronics with microprocessors in practice. Modern processors are made in laboratories, and the other hardware is also much too advanced for a hobbyist. Thanks to videos like these, that show how easy it can be with a processor like 6502, ROM and RAM chips, that seemingly impossible barrier is no longer impossible. I can sit down and make my own computer, just like that, and that just feels awesome. Hopefully I'll find the time to do just that.

    • @melkiorwiseman5234
      @melkiorwiseman5234 4 года назад

      Microcontrollers are a good way to get into hobby computing. If you don't want to go too deeply into the hardware then you can use one of the Ardunio computers, but it's also perfectly possible to build something simple with a PIC chip.
      Not so long ago, I programmed a 8-pin PIC to act as a white noise generator using one of its pins as the output and using a 32-bit pseudo-random number generator adapted from a software implemented shift register. That was fun.
      One of the best bits is that because of the voltage tolerance, I can run it from a single lithium-ion rechargeable cell. I only needed to add a speaker to get the output, but with a buffer transistor I get more volume since it acts like an amplifier. Exactly four components in the end: PIC, transistor, speaker and battery, and I now have something I could use to make white noise to sleep by.

  • @pinicius
    @pinicius 4 года назад +7

    I know everything on this video and I still love watching

    • @MrTridac
      @MrTridac 4 года назад +2

      Same here. I'm currently writing assembly code for a C64, but I can't help smiling and nodding as he explains.

  • @jackdaniels8898
    @jackdaniels8898 4 года назад +4

    When I was in high school my dad bought me an AIM65 single board 6502 computer. Watching these videos brings back so many good memories of when I first started into computers.

  • @sorek__
    @sorek__ 4 года назад +3

    Worst part about your videos is that once they end you end up with emptiness and you have to wait a month for a new one :(
    Great work, thank you for that!

  • @xc68hc705kj1
    @xc68hc705kj1 3 года назад +3

    I'm an embedded systems design engineer and University professor for digital systems, I recognize the awesome job you do with your videos, I recommend your videos to my students due to the way you explain the topics, your videos keep teaching and never start boring, that's an important fact, and you always contrast the practical way of things, keep up the good work!

  • @vbkfivn
    @vbkfivn 4 года назад +19

    Quick question: how did you know the address for the subroutine lcd_instruction was 805d?

    • @mfaizsyahmi
      @mfaizsyahmi 4 года назад +7

      the compiler takes care of that

    • @davidmcgill1000
      @davidmcgill1000 4 года назад +2

      Can find where it compiled to at 3:23

    • @BenEater
      @BenEater  4 года назад +28

      At that point in the video I didn't know it was exactly 805d. But I knew the program started at 8000, and was ~130 bytes long, so 805d seemed like a reasonable address for the subroutine.
      That said, I could have looked through the assembled binary to figure it out. For a larger program that wouldn't be very fun, but the assembler can also probably generate a symbol table, though I'm not sure off the top of my head how to get it to do that.

    • @jco2641
      @jco2641 4 года назад +10

      For vasm it looks to be -L . It shows what it parsed from the source, what op codes it generated, and the address it put them at. Then at the end it has a table of the addresses of the labels in the program.

    • @xedover
      @xedover 4 года назад +1

      you could just count the bytes in each line of code from the beginning (8000) to the subroutine, being careful to note which instructions are only one byte long and which ones are two or more bytes long, and the number of bytes for the data parameters.
      But that's super tedious. As the others have said, let the assembler do it for you, looking at the symbol table.

  • @Invisible_0011
    @Invisible_0011 4 года назад +2

    #OMG😎😎😎......Your new video experience is just like a new released Movie for me.....Thanks for this amazing Video😍😍😍😍🤗🤗🤗

  • @toncho1986
    @toncho1986 4 года назад +4

    Ben, please... never stop making videos. You are awesome

  • @wlorenz65
    @wlorenz65 4 года назад +2

    Having the stack pointer decrement instead of increment when pushing data prevents superfast variable-length stringbuilders on the stack. And mixing user data with program return adresses opens a can of security issues. But if processor vendors wanna stick to it ... well, that's no longer my problem. Fiveteen years of programming is enough.

    • @Brian_Of_Melbourne
      @Brian_Of_Melbourne 4 года назад

      True enough for the processors of this century, but not relevant to the 6502.

    • @michaelburns8073
      @michaelburns8073 4 года назад +2

      The 6502 was never really designed to make PC's (or more to the point, multi-user operating systems) with, IIRC, it was made to control embedded equipment. As such, you would not have to worry about the issues that you brought up. There were two things that made it popular for the microcomputer crowd: One -- it was cheap. Two -- it was easy to put together a computer because you didn't need a whole lot of support logic to build a basic PC, you would treat I/O , data and instructions all the same -- a memory access. I figure that's the main reason Ben chose this CPU for these videos, and in fact, it's still used quite a bit in embedded systems.

  • @Oli1974
    @Oli1974 4 года назад +4

    Being (or having been, back in the days) a fairly experienced 6502 assembly programmer, I have always thought the stack were inside the processor. It is amazing to learn everything anew even a level of abstraction less - or two. Keep up the amazing work!

    • @kaitlyn__L
      @kaitlyn__L Год назад

      That’s not surprising, reserving some RAM for the stack was one of the 6502’s (and 6501 before it got pulled for patent infringement) cost cutting measures against the 6800. 6800 had a lot more registers too! Since programmers were accustomed to separate stacks, the machine code still acts as if it’s an onboard location. Indeed some latter interface chips had enough RAM for the stack so it could operate even without proper system RAM, which no doubt further pushed that perception!

  • @rayroulstone3565
    @rayroulstone3565 4 года назад +2

    Fantastic Video Series. I grew up in the 80 computer boom and never quite understood assembler code. You explain it so well, really wish my computer science teacher explained it like this. I'm getting the urge to pull my BBC out of storage and play around with 6502 assembler.

  • @AshtonSnapp
    @AshtonSnapp 4 года назад +4

    6502 Part 5: Optimization and O Crap Need RAM

  • @jorgesaxon3781
    @jorgesaxon3781 2 года назад +2

    "We got to be careful because the stack is only 256 bytes"
    Me: Say it! Say it!
    "So if we keep pushing stop on the stack we may cause a stack overflow"
    Me: WHOOOO

    • @hf2138
      @hf2138 2 года назад

      256 bits or bytes?

  • @unclerojelio6320
    @unclerojelio6320 4 года назад +3

    Thanks, I guess I assumed that the stack was part of the cpu. Learned something new.

  • @ropersonline
    @ropersonline 3 года назад +1

    19:12... 19:59... 20:43... I think I've just understood why stack overflows are such a bitch to troubleshoot, because the problem is likely to make itself felt WAY later than when the actual overflow occurred.

  • @levyroth
    @levyroth 4 года назад +3

    I'm dying to see the next episode already. Your videos compressed about 2 years of university lectures in a few hours. This is what multiple lecturers couldn't coherently explain to us in class. Insane how all of a sudden everything fell into place in my mind.

  • @o9guy
    @o9guy 4 месяца назад +1

    I appreciate the fact that Ben knows this wouldn't work but did it anyway so we could learn more. Learning by failure is a great way to challenge our troubleshooting.

  • @vision2k890
    @vision2k890 4 года назад +4

    Now this is the content I want to see on RUclips!

  • @DarthZackTheFirstI
    @DarthZackTheFirstI 4 года назад +1

    if you do this with the benEater EEProm Arduino Programmer (...and rebuild it for the 256 eeprom used in this video) you might want to use this pastebin.com/GrVpEeCp . its a python programm i wrote to rewrite the hexdump copy to eeprom values like he has in "dataByte [] {0x56, 0xff ....} . basicly copy the hexdump values with strg+mouse to mark them and then rightlick copy. paste it into a file called data.txt. important! after the last value of a number write a ! and save the file with it. this is used to terminate the read. otherwise it might go into an endless loop. first it copies the value into newData.txt without empty spaces and then into a file called dataNew.txt which should have all the values in hex format for the arduino progam. you just need to copy them straight into the arduino program of the arduino eeprom writer. meaning to create a variable like dataByte, whateverrr you might call it then. adjust the write sequence a bit to the new variable and it should store it on the eeprom. worked for me (open a terminal and write "python hexData.py" to start it btw), after many fails... XD . i thought i might share if you only use an eeprom programmer for this it might save some bucks.

  • @JohnSmith-one
    @JohnSmith-one 4 года назад +12

    The best explanation of stack overflow that I've ever heard. Thanks, Ben)

  • @flinkiklug6666
    @flinkiklug6666 Год назад +2

    If you remove the jumper from the Rom so you can chance the values in the Rom you can use it as Ram? I like this series soo much. I am ill in the bed ant want to lerne something. Thank you

    • @EndOfLineTech
      @EndOfLineTech Год назад

      Are you asking if Electrically Erasable Programmable Read-only Memory can be written to as Write memory? If so… idk man… seems like a difficult question. I’m not sure anyone here can help you.

  • @benjamindragon598
    @benjamindragon598 4 года назад +12

    I love seeing how adding the flags made the size of the file smaller and how using recursion in your computation can shrink the program while keeping the same result. It would maybe be cool to see some stuff about brainfuck lambda calculus or turing complete languages that are minimal in their construction but they can achieve the same results as something more "static". I know you're more of a digital electronics channel though. Just a thought love your videos!

    • @lexus4tw
      @lexus4tw 4 года назад +13

      That's not recursion, just function calls

    • @RealCadde
      @RealCadde 4 года назад

      Recursion is when a subroutine calls itself. Very much possible but you'd have to make sure you have enough stack to do so.
      In this case, you can recurse 128 times assuming nothing else gets pushed on the stack.

    • @clonkex
      @clonkex 4 года назад +1

      Adding flags? What flags?

  • @mysterythecat971
    @mysterythecat971 4 года назад +3

    I wish this was covered when I was in tech school, instead of here is your micro controller. make it do X.

  •  4 года назад +5

    there is something extremely relaxing about his videos, I do not know what it is

    • @BertGrink
      @BertGrink 4 года назад +2

      You are right; i would put it down to his calm voice, and the thoroughness with which he explains how things work.

    • @SoftBreadSoft
      @SoftBreadSoft 4 года назад +1

      Agree with Berty.
      As a fairly experienced (modern c/c++) programmer but comparatively inexperienced in electronics, he explains everything in the right level of information and depth for me.
      I'm not building a computer like this (yet), but has helped me understand electronics, computers, and programming at a lower level than I had.

    • @SoftBreadSoft
      @SoftBreadSoft 4 года назад +1

      And understanding something the first time its explained is very relaxing :^)

  • @georgetodorov4242
    @georgetodorov4242 4 года назад +1

    Your videos are awesome! I would like to ask you are you planning to make an FPGA tutorial series? It would be very, very interesting! Greetings from Bulgaria!

  • @markjones5973
    @markjones5973 4 года назад +9

    I've been really looking forward to this video. I wanted to see how you explained the stack to the layperson. I think the stack, with the JSR and RTS instructions are the neatest concepts of the 6502, or any processor for that matter.
    I wrote a 6502 emulator a while back. Coding the stack related functions was some of the most fun parts of the sub-project.

    • @kneesnap1041
      @kneesnap1041 4 года назад

      Ooh. Is it open source? I'd be interested in checking it out.

    • @markjones5973
      @markjones5973 4 года назад

      @@kneesnap1041 Not at the moment. I want to put it on Git Hub. I just haven't gotten around to it.

  • @CR33D404
    @CR33D404 4 месяца назад +1

    and now i get how a stack work! but i have a question, what makes this an 8bit system? is it cuz it has an 8bit data bus so it process only 8bits per clock? but it has a 16 bit address bus and i always thought that a 64bit system is called so because it can address 2**64 byte, so that would make a 16bit data bus system able to address 2 bytes at the same time instead of one? it sounds wrong to me cuz i thought that a memory address should point to one byte? or is it because it has only 8bit register?

  • @katemoon7476
    @katemoon7476 4 года назад +8

    0:00 was the moment I anxiously have been waiting a month for. I really love the story-arc presentation style. You tell us there is a problem and then keep the tension on until the moment of resolution. You have successfully figured out a way of teaching computer science in a story format.

  • @daaara
    @daaara 3 года назад +1

    At 9:41 I had this crazy moment when I realized the call stack (which, as a python/JS programmer I think of as this abstract thing that exists somewhere in virtual memory) is a literal physical object in this system - that is missing - and as a result the poor microprocessor has dementia and doesn't know where to come back to. I've never been able to get into low level systems before, but your guides make it approachable and easy to follow. Thanks!

  • @nkusters
    @nkusters 4 года назад +9

    I'm not even 15 minutes into this video, but it's so amazing... I love the stuff you make Ben. I want to do some of these projects so bad. I will have to free up some time in the future to do so.

  • @amogus7
    @amogus7 4 года назад +1

    He: jsr
    6502: RAM not found. Please put RAM at adresses $0100 - $01FF and reset.

  • @thevinstigator2511
    @thevinstigator2511 4 года назад +3

    Who is Stack and why does he always overflow?

  • @flain283
    @flain283 4 года назад +1

    I've done a lot of higher level programming, but only recently i've taken up lower level programming. I assume when you say the CPU is doing these extra things to store the return location (or trying too by pushing to the bus). That it's actually the assembler compiler doing that when it converts the assembly into machine code? Which is why it all worked without the subroutine, since there was no jump statement the assembly would of compiled into working machine code.
    I guess my question is - is the subroutine functionality a part of the assembler compiler feature set or the CPU itself? It seems to me probably the compiler? I just ask because you were referring to actual CPU documentation.

    • @massimookissed1023
      @massimookissed1023 4 года назад

      No, the subroutine call is actually a hardware-level instruction in the 6502 & Z80a.
      ·Put the Stack Pointer on the address bus, write ½ the return address, (current value of Program Counter)
      ·Decrement the Stack Pointer,
      ·Put the new Stack Pointer on the address bus, write the other ½ of the return address,
      ·Decrement the Stack Pointer,
      ·Load the subroutine address into the Program Counter.

  • @pev_
    @pev_ 4 года назад +3

    The Z80 has a 16 bit SP so the stack can be anywhere and any length. And "anywhere" of course means 64 kB because these 8-bit CPUs only have 16-bit addressing for memory. It is interesting to see the differences between the Z80 (which I know very well) and the 6502, two 8-bit processors of the same era. The 6502 can be classified as a RISC and therefore much more limited in the programmer's view but also slightly faster per instruction, and the Z80 a CISC.

  • @AntonioDellaRovere
    @AntonioDellaRovere 4 года назад +1

    Hey Ben! I just found your series about the 6502. Really great! I don't know if you already thought about it or somebody else already suggested it. You could clock or reset the 6502 via GPIOs of the arduino and extend the arduino code to read commands from the terminal and easily: reset by hitting 'r', single step 's', run 'g' or even set breakpoints 'b 8002' by just comparing the address bus with that address on the arduino..

  • @paparansen
    @paparansen 4 года назад +4

    why is "jump to subroutine" -> jsr
    and "return from subroutine" -> rts and not rsr ?

    • @gmfCoding
      @gmfCoding 4 года назад +3

      *Acronym characters*
      *Jump*
      To
      *Sub*
      *Routine*
      *Re*
      *Turn*
      From
      *Subroutine*

    • @Mostlyharmless1985
      @Mostlyharmless1985 4 года назад +5

      because the return instructions in the 68xx series start with RT

    • @paparansen
      @paparansen 4 года назад +1

      @@Mostlyharmless1985 aahhh ok, thanks :)
      btw i am not a coder, just wanted to know.

    • @Garbaz
      @Garbaz 4 года назад +2

      Because there are other jump instructions, so "emphasising" that it's to a subroutine makes sense. But there only is one return instruction so there is no reason to emphasise that it's to a subroutine. In some assembly languages, the return instruction is even just called "ret".

    • @BertGrink
      @BertGrink 4 года назад +1

      @oH well,lord! The Z80 uses the mnemonic RETI for the RETurn from Interrupt instruction

  • @iuer4643
    @iuer4643 4 года назад +1

    man schools are going 90% virtual, 10% non-virtual will be for exams

  • @skilz8098
    @skilz8098 4 года назад +3

    You could have added a buzzer to the processor... this way when the processor starts and detects that there is no ram, you end up getting 3 consecutive beeps... Oh the joys of making a BIOS or CMOS...

  • @chriskaprys
    @chriskaprys Год назад +1

    Is any kind of memory - stored bits for later retrieving- considered a ‘stack’? Or does it just refer to memory inside a cpu? I’m curious because at the end you said we’ll add ram which will give this computer a stack.

  • @jonathanmarler5808
    @jonathanmarler5808 4 года назад +3

    Forgot to download the ram, rookie mistake

  • @Sergeeeek
    @Sergeeeek 3 года назад +2

    Never understood why stack overflow could lead to arbitrary code execution until now. Thank you for a clear explanation!

  • @TREmreprogaming
    @TREmreprogaming 4 года назад +4

    20:08 stackoverflow 😆 nice.

  • @ropersonline
    @ropersonline 3 года назад +1

    That S icon flashing up at 9:37 is SnagIt, in case someone else was wondering.
    There also was a similar-looking C icon that flashed up in an earlier video, which was for Camtasia.
    It is interesting to see what tools people are using.

  • @bigpitu
    @bigpitu 4 года назад +4

    I love you channel, seriously.

  • @pauldeane8369
    @pauldeane8369 4 года назад +2

    This is great! I've recently started a "Genius Hour" project where I am hoping to learn and create a simple program in 6502 assembler. Very helpful!

  • @themirlabs
    @themirlabs 3 года назад +1

    im someone whos been working on his own game in his free time. Now i know what a stack overflow means thanks to this. i mean i understood in my code i had created an infi loop by accident but i never got why it was called a stack overflow before.

  • @yousefali995
    @yousefali995 Год назад +1

    You need two things to become a computer engineer:
    1- Internet access
    2- Ben Eater videos

  • @digimon916
    @digimon916 4 года назад +2

    I took a class, "integrated circuits with computer applications" and this is the sort of stuff we did but on a very very fundamental level and ive been trying to find a channel just like this. Thanks for your work.

  • @BerenES
    @BerenES 4 года назад +1

    Now I understand what stack overflow means, after 4 years coding!

  • @spandanroy8380
    @spandanroy8380 Год назад +1

    I just realised that using the ardiono is like a debugger and leds are like print statements. I don't know if anyone else got this feeling.

  • @yusdanjd
    @yusdanjd Год назад +1

    2:08 “assuming they’re correct”, I felt that in my soul.

  • @xtdycxtfuv9353
    @xtdycxtfuv9353 4 года назад +1

    yes lord bed bless us with ur divine knowledge like a priest would baptise a baby