Bitwise Logic - 6502 Assembly Crash Course 04

  Рет қаралды 20,381

NesHacker

NesHacker

Күн бұрын

In this part of the 6502 Crash Course I introduce you to bitwise logic operations, explain how they work, and show some practical ways to use them in your own games.
Support the channel on Patreon: / neshacker
Chapters:
0:00 Introduction
1:31 Perspectives on Data
3:22 Logic Operations
4:39 Bitwise AND
6:55 Bitwise OR & Exclusive OR
8:30 Example: Testing Bits
11:23 Example: Flipping Bits
13:24 Coding Challenge
14:00 Conclusion

Пікірлер: 78
@NesHacker
@NesHacker 2 жыл бұрын
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 жыл бұрын
Amazing video as always
@JoshCano
@JoshCano Жыл бұрын
Thank you so much for these videos! I really enjoyed them!
@steveHoweisno1
@steveHoweisno1 Жыл бұрын
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 10 ай бұрын
Yesssss ....
@BillAnt
@BillAnt 8 ай бұрын
​@@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.
@kirby0louise
@kirby0louise 2 жыл бұрын
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 жыл бұрын
Indeed, I think I’ve only every seen it as XOR elsewhere.
@kirby0louise
@kirby0louise 2 жыл бұрын
@@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 Жыл бұрын
6 months late lol. Anyway, I managed to came across the solution before jumpint into the comments!
@jwalshmorrissey
@jwalshmorrissey 2 жыл бұрын
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 Жыл бұрын
Yeah, his way of teaching is very clear & clean. Going into didactics myself (hopefully), I like his style and progression.
@mrmadmaxalot
@mrmadmaxalot Жыл бұрын
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.
@RealSpectrum
@RealSpectrum 2 жыл бұрын
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 жыл бұрын
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 :)
@SoulcatcherLucario
@SoulcatcherLucario Жыл бұрын
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.
@hicknopunk
@hicknopunk 2 жыл бұрын
Heck yeah! Thanks so much for more 6502 stuff. 😍
@NesHacker
@NesHacker 2 жыл бұрын
For sure, it’s pretty fun to make :)
@AT-zr9tv
@AT-zr9tv 2 жыл бұрын
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.
@ChrisBouchard
@ChrisBouchard Жыл бұрын
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.
@haydenfrobenius9818
@haydenfrobenius9818 Жыл бұрын
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 Жыл бұрын
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.
@haydenfrobenius9818
@haydenfrobenius9818 Жыл бұрын
@@kythrathesuntamer9715 yeah, every other tutorial is really low quality and stops at the first episode.
@kamara4700
@kamara4700 2 жыл бұрын
I've really enjoyed the videos and learned a lot. Please keep going and I'm sure your channel will grow in no time
@Favourites669
@Favourites669 2 жыл бұрын
Excellent stuff man. The videos keep getting better, more interesting and even easier to follow!
@NesHacker
@NesHacker 2 жыл бұрын
Thanks :)
@Wineman3383
@Wineman3383 Жыл бұрын
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.
@bcchithrck
@bcchithrck 2 жыл бұрын
been waiting for this. thanks for the lesson and high quality vid!
@NesHacker
@NesHacker 2 жыл бұрын
Happy to oblige :)
@mysteryemblem8806
@mysteryemblem8806 2 жыл бұрын
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 KZbin, I jsut wish you would keep going. I
@lean.drocalil
@lean.drocalil 2 жыл бұрын
Excellent content, as usual!! 👏👏
@marcelomarcon323
@marcelomarcon323 2 жыл бұрын
Its a great series of videos, please explain what is each register for, and how it can be used, PC, AX,BX etc...
@sociologie4507
@sociologie4507 Жыл бұрын
great explanation of bitwise logic.
@ebanavorio
@ebanavorio Жыл бұрын
XOR 1 This series is just magnificent
@Momuzzz
@Momuzzz 2 ай бұрын
Excellent video!
@amaiorano
@amaiorano 11 ай бұрын
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!
@rubywpn
@rubywpn 2 жыл бұрын
To flip a bit, just XOR it with a 1.
@AsciiKing
@AsciiKing 2 жыл бұрын
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'.
@Mireneye
@Mireneye Жыл бұрын
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 10 ай бұрын
This might help me too getting started, thanks!
@LukeAvedon
@LukeAvedon 2 жыл бұрын
Very nice! I forgot all about Bitmask.
@NesHacker
@NesHacker 2 жыл бұрын
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
@mzxrules
@mzxrules 2 ай бұрын
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
@tillow9511
@tillow9511 2 жыл бұрын
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?
@DaNewMusicChannel
@DaNewMusicChannel 2 жыл бұрын
Please do a video about how sound chip works!
@NesHacker
@NesHacker 2 жыл бұрын
Planning on it, but its gonna be a tough one xD
@savageredbeard
@savageredbeard 2 жыл бұрын
This is like doing a logic table. OMG, I have PTSD now.
@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. 🤷‍♂
@msmalik681
@msmalik681 11 ай бұрын
You would need a manual to keep track of all the bits. I would like to make a game only using bitwise operations.
@pizletwizzler198
@pizletwizzler198 2 жыл бұрын
...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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
@@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.
@HamsterImWinterschlaf
@HamsterImWinterschlaf 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 жыл бұрын
I agree, it always seems kinda amazing to me what folks did with limited tooling back in the day :)
@spacecomplex7180
@spacecomplex7180 2 жыл бұрын
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 жыл бұрын
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.
@meinelaterne5598
@meinelaterne5598 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.
@BGDMusic
@BGDMusic 10 ай бұрын
and i thought i knew 6502 pretty well...
@twobob
@twobob 8 ай бұрын
Wait, what. there are videos about mystical code... give me strennnnn...
@NesHacker
@NesHacker 8 ай бұрын
Iknorite
@twobob
@twobob 8 ай бұрын
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 8 ай бұрын
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
@JarppaGuru
@JarppaGuru 3 ай бұрын
13:53 you just show how
@wj11jam78
@wj11jam78 Жыл бұрын
0 bit samus
@MakhnoDripGod
@MakhnoDripGod 8 ай бұрын
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 8 ай бұрын
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 8 ай бұрын
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
Coding NES Loops
17:10
NesHacker
Рет қаралды 26 М.
Why You Should Learn To Program The Hard Way
15:42
Theodore Bendixson
Рет қаралды 241 М.
原来小女孩在求救#海贼王  #路飞
00:32
路飞与唐舞桐
Рет қаралды 22 МЛН
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Рет қаралды 61 МЛН
Surprise Gifts #couplegoals
00:21
Jay & Sharon
Рет қаралды 26 МЛН
The World's Fastest Cleaners
00:35
MrBeast
Рет қаралды 110 МЛН
The ARM chip race is getting wild… Apple M4 unveiled
4:07
Fireship
Рет қаралды 46 М.
Basics - 6502 Assembly Crash Course 01
12:49
NesHacker
Рет қаралды 105 М.
Comparing C to machine language
10:02
Ben Eater
Рет қаралды 4,9 МЛН
The 6502 Rotate Right Myth
9:16
TubeTimeUS
Рет қаралды 47 М.
Where GREP Came From - Computerphile
10:07
Computerphile
Рет қаралды 929 М.
I Made a 32-bit Computer Inside Terraria
15:26
From Scratch
Рет қаралды 3,1 МЛН
computers suck at division (a painful discovery)
5:09
Low Level Learning
Рет қаралды 1,5 МЛН
MMC2 Magic - How Punch-Out's Graphics Work
14:57
Displaced Gamers
Рет қаралды 50 М.
6502 Assembly Language: Getting Started
45:43
TokyoEdtech
Рет қаралды 29 М.
🤏 САМЫЙ ТОНКИЙ гаджет #Apple! 🍏
0:29
Яблочный Маньяк
Рет қаралды 501 М.
Распаковал Xiaomi SU7
0:59
Wylsacom
Рет қаралды 407 М.
🤯Самая КРУТАЯ Функция #shorts
0:58
YOLODROID
Рет қаралды 2,6 МЛН
Компьютерная мышь за 50 рублей
0:28
dizzi
Рет қаралды 2,1 МЛН