‹ BackHN Continuity

Thread

Show HN: I wrote a custom assembler for CHIP-8 in C++

27 points · 4 comments · tack1234

Loading the complete thread in the background. This saved snapshot is available now. Refresh

  1. rfgplk · · focus · HN ↗
    Nice job!
    1. tack1234 · · focus · HN ↗
      Thanks!!
  2. Stevenrojas888 · · focus · HN ↗
    Awesome milestone! As someone diving into C and C++ right now, what was the steepest part of the learning curve for you when structuring the assembler compared to writing the emulator in C?
    1. tack1234 · · focus · HN ↗
      Thanks! Well at first I did not have much structure, I just went full procedural and did what I needed to do at each step. It was an ugly, long chain of loops and switches. Then I read up about assemblers a little bit, figured out what should go into the lexing/parsing/emitting stages and split it into classes.

      The steepest part.. probably trying to do things efficiently, using string views instead of strings where relevant, references and also ranges + iterators in C++. The from_char function and how it reports errors (e.g. when the iterator it returns is the end of the provided range, there were no errors) etc.

  3. Retr0id · · focus · HN ↗
    Nice, chip8 is a lot of fun. I looked at your emulator implementation too, and it looks like you have an OOB access bug in the &quot;I&quot; register (it can be incremented beyond the RAM array bounds). This is a very common chip8 emulator bug, which can (in some cases) be exploited to allow a malicious ROM to escape the emulator. I wrote about this here: <a href="https:&#x2F;&#x2F;www.da.vidbuchanan.co.uk&#x2F;blog&#x2F;bggp3.html" rel="nofollow">https:&#x2F;&#x2F;www.da.vidbuchanan.co.uk&#x2F;blog&#x2F;bggp3.html
    1. tack1234 · · focus · HN ↗
      Thanks for pointing that out, I think it was somewhere on my TODO list which miraculously got lost. I&#x27;ll have a look at it!
  4. adityamishra241 · · focus · HN ↗
    Really cool project. How much harder did the assembler feel compared to writing the emulator?
    1. tack1234 · · focus · HN ↗
      Thank you!

      From the conceptual standpoint, it felt way clearer to me from the start. I knew I &quot;just&quot; had to parse some input text and figure out the instructions into which it should be turned into. The actual lexing and parsing of the source code text character by character lead me to way more errors and debugging sessions than I expected, but that&#x27;s probably just me being a bit rusty.

      So I would say the emulator was harder to grasp at first, as I had no prior lower level experience, had no idea what a register or a stack pointer or a program counter is, etc.

  5. Loren_SL · · focus · HN ↗

    [dead]

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.