« BackCompiling to Assembly from Scratchkeleshev.comSubmitted by AlexeyBrin 17 hours ago
  • hirvi74 2 hours ago

    God, the title reminds me of when I when I took an x86 assembly class in college about a decade ago. Only 6 of the dumbest souls in the CS program dared to take the class the semester. The professor for the class was an ex-NASA computer engineer. Our test used to be writing assembly by hand. We were graded for accuracy too. I swear, at that point in time, I could convert between Hex, Dec, Oct, and Binary almost without thinking. I made a D in class with 40+ hours a week of studying. However, I learned more in that one class than my entire degree.

    Anyway, thank you, OP, for sharing this. I have been looking into picking up ARM as way to crawl out of burnout from my career. It's been a years since I even touched x86 with any seriousness. I will add this book to my list resources.

    • alok-g an hour ago

      Ever tried writing machine code (not assembly) by hand? I used to do that a few decades back for an 8-bit microprocessor. I am still looking for good resources on how to do that for a modern processor.

      • ReleaseCandidat 3 minutes ago

        You look up the opcodes for each assembler instructions in the ISA specifications, like https://www2.eecs.berkeley.edu/Pubs/TechRpts/2016/EECS-2016-.... Information about the Machine Code starts at "2.2 Base Instruction Formats". The actual opcodes you can find in chapter 19,"RV32/64G Instruction Set Listings". But be aware that some assembler instructions (called e.g. pseudo instructions) generate more than one.

        The linked document is an old one, current ones are at https://riscv.org/technical/specifications/

    • ggorlen 5 hours ago

      As is often the case, the title is unfortunately overloaded. I initially read this as writing code in the Scratch programming language[1] that compiles to assembly.

      [1]: https://en.wikipedia.org/wiki/Scratch_(programming_language)

      • userbinator 2 hours ago

        Given that we've seen things like C compilers written in Bash[1] and TLS libraries in Visual Basic[2], I think it's only a matter of time before someone with the right skills and motivation actually does it (or writes a compiler for $language in Scratch).

        [1] https://github.com/otakuto/bashcc

        [2] https://news.ycombinator.com/item?id=35882985

        • __MatrixMan__ 2 hours ago

          Me also. The actual thing is probably pretty cool, but now that my hopes were up I'm finding it insufficiently whimsical.

          • MiguelX413 4 hours ago

            I also misunderstood it in that manner.

          • pjmlp 9 hours ago

            I got the ebook when it came out, and is relatively nice as ramp up into the world of compiler development.

            • stonethrowaway 8 hours ago

              Speaking of embedded systems, I have an old board, an offshoot of Zilog Z80 called Rabbit. I think recently Dave from EEVBlog took apart one of his ancient projects and I was floored to see a Rabbit. Talk about a left hook. Assuming this is Hacker News, I suspect someone probably knows what I’m talking about. The language used (called “Dynamic C”) has some unconventional additions, a kind of coroutine mechanism in addition to chaining functions to be called one after another in groups. It’s mostly C otherwise, so I suspect some macro shennanigans with interrupt vector priority for managing this coroutine stuff.

              Anyhow, so I’ve got a bunch of .bin files around for it, no C source code, just straight assembly output ready to be flashed. And the text and data segments are often interwoven, each fetch being resolved to data or instruction in real time by the rabbit processor. So I’ve been thinking of sitting down, going through the assembly/processor manual for the board and just writing a board simulator hoping to get it back to source code by blackbox reversing in a sense. I’d have to rummage through JEDEC for the standard used by the EEPROM to figure out what pins it’s using there and the edge triggering sequences. Once I can execute it and see what registers and GPIOs are written when, I can probably figure out the original code path. Not sure if anyone has tips or guides or suggestions here.

              • Something1234 8 hours ago

                Any chance a decompiler like ghidra might get you part ways there?

                • stonethrowaway 7 hours ago

                  The closest I came across was a Z80 simulator, I forget the name of it. But it allowed you to step through command by command giving you ability to query the processor state in a terminal.

                  So I don’t know if it would be easier trying to find this, and updating it to support rabbit, vs trying to wedge something into ghidra which itself is an undertaking of a behemoth platform. As far as I know ghidra does not have Z80 support.

                  • Jtsummers 7 hours ago

                    Ghidra has Z80 support (based on the installation on my laptop), but I've never used it.

                    • stonethrowaway 6 hours ago

                      Ended up answering my own question. The Z88-DK[0] seems to be what I found. It supports most Rabbit processors, so I ran one of binaries through it and it spat out assembly my way which seems to make sense. Will have to run it through the simulator and see if I can get it to act like a microcontroller with setting voltage levels on particular pins.

                      [0] https://github.com/z88dk/z88dk

                      • westurner 7 hours ago

                        An Emu86.assembler.virtual_machine.Z8Machine (like MIPSMachine, RISCVMachine, and IntelMachine) could be stepped through in a notebook: https://news.ycombinator.com/item?id=41576922