Bitwise Logic - 6502 Assembly Crash Course 04

Поділитися
Вставка
  • Опубліковано 28 вер 2024

КОМЕНТАРІ • 80

  • @NesHacker
    @NesHacker  2 роки тому +62

    This was a lot of fun but pretty tough to make, let me know if it helped you understand bitwise logic. Also, stay to the end of the video for a programming challenge!

    • @CartoonFinder
      @CartoonFinder 2 роки тому +5

      Amazing video as always

    • @JoshCano
      @JoshCano 2 роки тому

      Thank you so much for these videos! I really enjoyed them!

    • @steveHoweisno1
      @steveHoweisno1 2 роки тому +2

      Great vid but I have a problem from a memory complexity pov. You attempt to save storing 8 bytes for 8 bools and instead stuff them in a single byte. The problem is if you want to isolate a single bit you need a literal like #01000000 which bloats your code by 1 byte wherever you use it. So the overall memory overhead could even be worse if you have lots of literals like this. Any thoughts? You could store a bitmask for each bit you want but then the mem complexity is 9 bytes! Then again, if the point is not to reduce mem overhead but improve computational complexity ie number of instructions then that’s fine.

    • @alexandrohdez3982
      @alexandrohdez3982 Рік тому

      Yesssss ....

    • @BillAnt
      @BillAnt Рік тому

      ​@@steveHoweisno1 - While it's true that bit masks use extra bytes, you can perform an ASL/LSR, ROL/ROR, INC/DEC on a bit mask without requiring extra bytes.

  • @jwalshmorrissey
    @jwalshmorrissey 2 роки тому +27

    This series is fantastic. The production is top-notch, and you clearly either have a background in education or a strong aptitude in that area. (My opinion as a former college professor.) I considered leaving the feedback that each video in this series could be literally twice as long, not because you don’t explain adequately, but simply because the pace is extremely fast. It’s easy to follow along superficially, but too quick to really assimilate the information for most people, especially the novices you’re addressing. But now I think that the use case is such that the viewer can simply pause and rewind as needed. That’s what I’m doing as I delve into the 6502.
    This channel should be MUCH bigger. Keep up the excellent work. You exude intelligence and competence, and I’m sure you’ll reach a broader audience.

    • @yt_n-c0de-r
      @yt_n-c0de-r 2 роки тому +1

      Yeah, his way of teaching is very clear & clean. Going into didactics myself (hopefully), I like his style and progression.

    • @mrmadmaxalot
      @mrmadmaxalot Рік тому +2

      As for pacing, I would say he nailed it. One of the benefits of YT and video style lectures that you don't get with 'in person' lectures is that you can pause and go back to see again. This allows the videos to move at a quicker pace than traditional lectures so as not to lose the 'students' more familiar with the topic, but also provide a means for those new to the subject to move at their own pace.

  • @SoulcatcherLucario
    @SoulcatcherLucario 2 роки тому +6

    this series has been invaluable as someone who although is a software dev by trade, only has experience in very _high_ level languages. i had my mouth open the entire time because of how well you explained this.
    i really hope you continue making these videos, because this combined with easy 6502, i feel much more safe dealing with assembly.

  • @kirby0louise
    @kirby0louise 2 роки тому +55

    LDA $40
    EOR #%00001111 '(or just a 1 wherever you want to flip)
    STA $40
    On a side note, it's interesting that EOR is used in favor of XOR for the exclusive or mnemonic. Perhaps the 6502's designers were fans of winnie the pooh?

    • @NesHacker
      @NesHacker  2 роки тому +17

      Indeed, I think I’ve only every seen it as XOR elsewhere.

    • @kirby0louise
      @kirby0louise 2 роки тому +10

      @@NesHacker So I did a quick investigation, and it's also denoted "EOR" on the M68k, but "XOR" in x86, so I guess it must have just been whatever the personal preference of the CPU designer who was writing the manual 40+ years ago

    • @eniolotero8808
      @eniolotero8808 2 роки тому +1

      6 months late lol. Anyway, I managed to came across the solution before jumpint into the comments!

  • @AT-zr9tv
    @AT-zr9tv 2 роки тому +10

    Great video, super easy to follow and exciting to see the direct use in gameplay.
    Regarding your exercise: say we want to only flip bit0, the exclusive OR against bitmask %0000 0001 will flip bit 0 and leave all 7 other bits unchanged.

  • @Favourites669
    @Favourites669 2 роки тому +6

    Excellent stuff man. The videos keep getting better, more interesting and even easier to follow!

  • @madcorndog
    @madcorndog Рік тому +4

    We need more tutorials! These are the only actually watchable tutorials for NES programming I found. I learn programming languages best with youtube tutorials and I would love to learn how to make a fully fledged nes game.

    • @kythrathesuntamer9715
      @kythrathesuntamer9715 Рік тому +1

      I feel the same. These must of taken him a lot of work to make but they're the best series on here on it.

    • @madcorndog
      @madcorndog Рік тому

      @@kythrathesuntamer9715 yeah, every other tutorial is really low quality and stops at the first episode.

  • @kamara4700
    @kamara4700 2 роки тому +4

    I've really enjoyed the videos and learned a lot. Please keep going and I'm sure your channel will grow in no time

  • @Wineman3383
    @Wineman3383 2 роки тому +2

    Smoke, smoke is coming out of my ears!!!.
    Last time I made a program was over 26 years ago.
    That was on an ancient computer used in a class in the Navy on programming.
    First program I wrote was a diagnostic program to test for shorts that the instructor would place on a random pin on the main board.
    The peripheral was these awful dinged out monochrome screens that were impossible to read.
    Yeah programming I could do but for only the basic stuff for fixing things in the field.
    Love your videos man thanks for what you are doing.

  • @mysteryemblem8806
    @mysteryemblem8806 2 роки тому +3

    Enjoyed the content of the video. I'm not making a game, but I like to learn how a hardware, or a computer work behind the scene.

  • @kythrathesuntamer9715
    @kythrathesuntamer9715 Рік тому

    You're actually producing some of the most helpful content i've run across on UA-cam, I jsut wish you would keep going. I

  • @Mireneye
    @Mireneye 2 роки тому +3

    Hey! Thanks for the crash-course. I'm still making my way through the Tutorials.
    I found a .PDF viewer for VSCode, so I downloaded the Instruction set page as a .pdf and put it in amongst the project files. So I have easy access to it. This way I can cross reference in any examples.
    I hope this helps someone else. As I'm just starting out, I can see this being helpful.
    Cheers!

    • @jengelenm
      @jengelenm Рік тому

      This might help me too getting started, thanks!

  • @AsciiKing
    @AsciiKing 2 роки тому +1

    I'm really enjoying this series. I had to pause the video to tell you that loud music makes it extremely difficult for me to focus on what you are saying. My brain actively listens to the music. This was a very difficult video for me. Also, at 2:02, you say 'four two-bit numbers' when you meant, 'two four-bit numbers'.

  • @ebanavorio
    @ebanavorio Рік тому

    XOR 1
    This series is just magnificent

  • @Momuzzz
    @Momuzzz 7 місяців тому

    Excellent video!

  • @lean.drocalil
    @lean.drocalil 2 роки тому

    Excellent content, as usual!! 👏👏

  • @DaNewMusicChannel
    @DaNewMusicChannel 2 роки тому +2

    Please do a video about how sound chip works!

    • @NesHacker
      @NesHacker  2 роки тому +3

      Planning on it, but its gonna be a tough one xD

  • @HamsterImWinterschlaf
    @HamsterImWinterschlaf 2 роки тому +2

    I learned assembler in university 20 yrs ago. We did some basic stuff to read values from pins, show digits on LCDs, stuff like that, more hardware oriented.... Now 20yrs later I am working as a Senior Architect with full access to collaboration tools, ticket systems, SCMs etc...
    I totally understand the paths they needed to walk to squeeze all the logic in this limited memory but I always wonder how they documented and handled this in large scale projects like games. Would be awesome to have a chat with some of those developers from back in the days.

    • @NesHacker
      @NesHacker  2 роки тому +2

      I agree, it always seems kinda amazing to me what folks did with limited tooling back in the day :)

  • @mzxrules
    @mzxrules 6 місяців тому

    The one thing this video is definitely missing is bitwise negation. It's not a proper 6502 opcode but it's important operation that any decent assembler should have for simplifying the case where you want to zero a specific bit. In DASM at least, if you precede an assembler time constant with ~, it will flip all 0 bits to 1s, and 1s to 0. This allows you to write code in a way that expresses just the bits you intend to modify:
    ; Set up constant values to represent item acquired flags
    ItemA = #%0001
    ItemB = #%0010
    ; Let's give items A, B
    lda ItemsVar ; %0...0000
    ora #[ItemA + ItemB]
    sta ItemsVar ; %0...0011
    ; Some gameplay happens
    ; Let's take away ItemB
    lda ItemsVar ; %0...0011
    and #~ItemB ; Because of the bitwise negation operator, the constant %0...0001 becomes %1...1101, which will zero just that specific bit
    sta ItemsVar ; %0...0001

  • @msmalik681
    @msmalik681 Рік тому

    You would need a manual to keep track of all the bits. I would like to make a game only using bitwise operations.

  • @spacecomplex7180
    @spacecomplex7180 2 роки тому +4

    Hey NesHacker, even in danger of posting something horribly wrong with my little knowledge of asm (what i hope to mitigate and improve in the future), that's what i quickly put together in Windows Editor before going to bed ;)
    {RAM}
    [$24] 1 ; 1=add powerup, 0=remove powerup
    {ROM}
    [$20] 11001010 ; Bitmask for Powerups
    .
    .
    .
    LDA $20 ; load bitmask
    LDX $24 ; load ram value, add or remove a powerup?
    CPX #%1 ; compare with 1
    BEQ + ; branch to or-instruction if 1
    AND #%11111011 ; remove powerup
    BRA ++ ; branch to store instruction (BRA = branch always!?)
    * ORA #%00000100 ; add powerup
    * STA $20 ; write new powerup byte-value to memory address
    .
    also, i'm pretty sure there are more efficient ways to do this, that i just don't know (yet).
    Thanks and props for your videos :)

    • @NesHacker
      @NesHacker  2 роки тому +1

      It’s an interesting way of doing it. To make it simpler consider using the EOR instruction I introduced in the video ;)

    • @spacecomplex7180
      @spacecomplex7180 2 роки тому

      @@NesHacker of course, i was thinking too hard to make it seperate algorithms for adding / removing, eor would be the one solution, my bad :) yeah logic algebra, hadn't done it for too long now.

  • @BGDMusic
    @BGDMusic Рік тому +1

    and i thought i knew 6502 pretty well...

  • @sdfmnb2314
    @sdfmnb2314 11 днів тому

    NOT gate:
    LDA $00
    ;$00 can be anywhere in the data
    EOR #%1111111
    ;flips all bits, change the 1s to any bit you want to flip
    STA $00
    ;once again, $00 could be anywhere

  • @wj11jam78
    @wj11jam78 Рік тому

    0 bit samus

  • @hicknopunk
    @hicknopunk 2 роки тому +14

    Heck yeah! Thanks so much for more 6502 stuff. 😍

    • @NesHacker
      @NesHacker  2 роки тому +3

      For sure, it’s pretty fun to make :)

  • @RealSpectrum
    @RealSpectrum 2 роки тому +10

    I hope you keep going with these videos, I feel like I've learned more on your channel than I did in 3 and 1/2 years of computer science classes in academic settings

    • @NesHacker
      @NesHacker  2 роки тому +7

      Wow, that’s a huge complement, thank you very much. I am really glad you find the videos so useful, and I hope my future ones help just the same :)

  • @ChrisBouchard
    @ChrisBouchard 2 роки тому +7

    Very nice video! I'll just mention for anyone reading: I'd usually use the term "bitmask" to refer to what this video calls an "isolation mask", and "bit string" or "bit field" for what the video calls a "bit mask". It probably depends what programming communities we learned in.

    • @matthollandsf
      @matthollandsf Рік тому

      I'm glad someone already pointed this out. I don't think calling the bit field a mask makes much sense. After all it's the thing that gets masked, not the thing doing the masking. The bits in a bit field are also sometimes called flags.

  • @rubywpn
    @rubywpn 2 роки тому +4

    To flip a bit, just XOR it with a 1.

  • @bcchithrck
    @bcchithrck 2 роки тому +6

    been waiting for this. thanks for the lesson and high quality vid!

  • @marcelomarcon323
    @marcelomarcon323 2 роки тому +2

    Its a great series of videos, please explain what is each register for, and how it can be used, PC, AX,BX etc...

  • @pizletwizzler198
    @pizletwizzler198 2 роки тому +4

    ...if item-collection is completely linear (working lowest to highest bit) then this might work, _maybe_ :
    LDA $40 ; (EG: #00001111)
    SEC ;sets carry-flag to "1" (EG: #(1)00001111)
    ROL A ;Rolls Acc. left and puts carry into 0-bit pos. (EG: #0001111(1)
    STA $40 ;Store "item-enabled" Acc.byte in mem
    🤔... maybe?

    • @kirby0louise
      @kirby0louise 2 роки тому +5

      You should be using EOR with the desired bitmask to easily and quickly flip any given bit. Exclusive-OR with 1 will always flip whatever the source bit is, just like a NOT gate/operation
      However I think you've got some good ideas, because while simply flipping the bits was the question here, in a real game the algorithm would be more complex and looking at the big picture is important too. For example, you might define your level data in such a way that -
      $00 - ground object
      $01 - powerup 1
      $02 - powerup 2
      $03 - powerup 3
      $04 - powerup 4
      $05 - powerup 5
      $06 - powerup 6
      $07 - powerup 7
      $08 - powerup 8
      Then when say your "collected powerup" routine runs, it would take the object ID as an input, then we can reuse some of your above code -
      LDA $#00 ;init bitmask
      LDX $41 ;or whatever address you passed the collected powerup's object ID in, used as loop counter
      setProperPowerUpBitmask:
      SEC ;sets carry-flag to "1" (EG: #(1)00001111)
      ROL A ;Rolls Acc. left and puts carry into 0-bit pos. (EG: #%0001111(1)
      DEX ;Decrement loop counter
      CMX #$00 ;Loop counter check
      BNE setProperPowerUpBitmask; Loop if counter still greater than 0
      STA $42 ;Save completed bitmask to temp RAM
      LDA $40 ;Get powerup state
      EOR $42 ;Flip the correct bit for the collected powerup
      (P.S. - There might be a more efficient way to write this code and/or I may have made an off-by-one error, the point here was to demonstrate that your original code may still be viable in more complex/real world uses)

    • @NesHacker
      @NesHacker  2 роки тому +2

      Haha, I look away for a few days and come back to find this great response from @KirbyOfLouise, looks solid to me :)

    • @pizletwizzler198
      @pizletwizzler198 2 роки тому +1

      @@kirby0louise Yeah, I realize he wanted us to regurgitate those bitwise operations, but I was attempting to _think outside of the box_ . Not much value for that around here apparently. 😒

    • @kirby0louise
      @kirby0louise 2 роки тому +1

      @@pizletwizzler198 Nah I think you had a good idea. Again, thinking about the big picture is also important. In a full game example like I suggested your carry flag + rotate loop could save valuable CPU cycles and/or ROM space vs 8 different compares and branches

    • @sirgouki6207
      @sirgouki6207 2 роки тому

      @@pizletwizzler198 Its a good idea but its kind of flawed - ROL is ROtate Left (not roll). It may seem small, but it's important not to use words like this or you might confuse what ROL does with another operation. Also, Calling ROL roll makes ROR (ROtate Right) look unrelated.
      As for the flaw in the logic itself, ROL or ROR will only do what you want it to do if the bit you want to swap is perfectly in line (as in your example, you turn bit 3 off but bits 0-2 stay on) with other like bits. But, if you have something like 0x10101010, however, using ROL will toggle ALL the bits to their opposite state. This is something you need to take into account when designing code: "Does this do only what I intend for it to do, and if not, what else does it affect? Will it work the way I intend in ALL cases?". Nothing wrong with thinking outside the box, just reserve that for when you fully understand the implications of what you're coding.
      Also no reason to respond like "Not much value for that around here apparently. 😒". Programming is often about thinking outside the box, but its better to reserve that for when you have more experience so you don't cause an error you couldn't have known was going to happen. Reacting the way you did makes it seem like you think you're better than you are.

  • @tillow9511
    @tillow9511 2 роки тому +1

    Can this be used to change npc dialogue when you progress further in an rpg and get more key items? Or is that too much?

  • @LukeAvedon
    @LukeAvedon 2 роки тому +2

    Very nice! I forgot all about Bitmask.

    • @NesHacker
      @NesHacker  2 роки тому +4

      It’s one of those things you barely use in high level programming that turns out to be absolutely critical for low level, old school, and embedded stuff xD

  • @meinelaterne5598
    @meinelaterne5598 2 роки тому +2

    Hey Ryan! Great videos so far, but i feel like there is no need for background music. It's really distracting..

    • @NesHacker
      @NesHacker  2 роки тому

      Yeah it was mostly an experiment, I’m gonna make the next one without it.

  • @thachoice
    @thachoice Рік тому

    After watching your assembly crash course I wondering if you could make a new 8-bit console that allowed more memory by using the Hitachi HD64180 or the Motorola 68008 instead of the 6502. These would keep the 8-bit constraits while allowing for much more memory which could yeild some interesting results in games. 🤷‍♂

  • @sociologie4507
    @sociologie4507 2 роки тому +1

    great explanation of bitwise logic.

  • @twobob
    @twobob Рік тому +1

    Wait, what. there are videos about mystical code... give me strennnnn...

    • @NesHacker
      @NesHacker  Рік тому +1

      Iknorite

    • @twobob
      @twobob Рік тому

      150kg industrial spingle "just spins up" and destroys n million dollars worth of stock and nailguns the entire room to look like the exorcist set while everyone's on break. ME: Yeah, must have been like a mystical thing? I watched this one video... @@NesHacker

    • @twobob
      @twobob Рік тому

      altho if we are discussing industrial shielded equipment just mysteriously doing "something" there is always the ridiculously slim chance of a super dense particle flipping a channel inside a PLC. Such collisions have a none zero chance but it so close to zero for all (hmm most) practical applications it doesnt matter anyway. Unless you program Exorcist nailgun robots at which point... you better be planning for failure regardless. okay, hopefully, somewhat genuine point, somewhat ironically made. But that is an almost "Mystical" interaction WITH code, I guess, even if the code itself was not. @@NesHacker

  • @Borhamus
    @Borhamus Місяць тому

    que maestro! genio total!

  • @JarppaGuru
    @JarppaGuru 7 місяців тому

    13:53 you just show how

  • @amaiorano
    @amaiorano Рік тому

    Fantastic video, and series as a whole! My one nit: what you call a bit mask is really a bit field. The mask is what's used to set or read the bits of a bit field. Anyway, thank you for making these!

  • @savageredbeard
    @savageredbeard 2 роки тому

    This is like doing a logic table. OMG, I have PTSD now.

  • @MakhnoDripGod
    @MakhnoDripGod Рік тому

    could you make a series on 65c816 ASM? I've read a lot of tutorials but I fail to understand anything past a simple custom block in super mario world lol, I don't really understand how a lot of these things actually do things in the game, like with custom bosses, how does adding and subtracting numbers actually do anything in terms of like displaying a sprite on the screen, because for super mario world those ram addresses are mapped out, but with custom bosses with custom moves, etc I don't understand how they do anything, do they rewrite what those ram addresses do?

    • @NesHacker
      @NesHacker  Рік тому +1

      Yeah maybe, I’m definitely gonna branch into other systems at some point and SNES programming is high up on my priority list!

    • @MakhnoDripGod
      @MakhnoDripGod Рік тому

      that would be sick! all the tutorials I can find for it online and very hard for me to comprehend and actually apply anything with, so a video would definitely be helpful, and could help many people who wanna learn SNES ASM especially in the Super Mario World community@@NesHacker