Live data from Hacker News

Compiling to Assembly from Scratch

keleshev.com

21–30 of 51 posts

Re: Compiling to Assembly from Scratch

#21
post #15
post #12

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 witho…

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.

You should definitely look at: https://github.com/akkartik/mu/blob/main/subx.md

Re: Compiling to Assembly from Scratch

#22
post #21
post #15

Earlier quoted context omitted.

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.

You should definitely look at: https://github.com/akkartik/mu/blob/main/subx.md

Also, running machine code directly can be done like this:

https://github.com/eterps/loader/blob/master/syscall.nim

Re: Compiling to Assembly from Scratch

#23
post #15
post #12

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 witho…

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.

I’ve told this before, but it’s so amazing I like to give Tim credit. I worked on Visual Basic at Microsoft with Tim Paterson, who also created the operating system that became MS-DOS. He worked on code generation and debugged by looking at the opcodes in a hex dump. Assembly was too slow for him him.

Re: Compiling to Assembly from Scratch

#24
post #22
post #21

Earlier quoted context omitted.

You should definitely look at: https://github.com/akkartik/mu/blob/main/subx.md

Also, running machine code directly can be done like this: https://github.com/eterps/loader/blob/master/syscall.nim

This is cool! This is how I used to do also by embedding hand-written machine code within a BASIC program and calling it to run natively.

Re: Compiling to Assembly from Scratch

#25
post #21
post #15

Earlier quoted context omitted.

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.

You should definitely look at: https://github.com/akkartik/mu/blob/main/subx.md

SubX seems like an assembly language itself following a subset of x86 32-bit instructions. Would looking into this help me understand how to translate from assembly to machine code manually? Thanks.

Re: Compiling to Assembly from Scratch

#26
post #15

Earlier quoted context omitted.

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.

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 format starts at "2.2 Base Instruction Formats". The actual opcodes you can find in chapter 9,"RV32/64G Instruction Set Listings". But be aware that some assembler instructions (called e.g. pseudo instructions) generate more than o…

Awesome! Thanks!

Re: Compiling to Assembly from Scratch

#27
post #15
post #12

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 witho…

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.

Here's an example for x86: https://www.hanshq.net/ones-and-zeros.html

Re: Compiling to Assembly from Scratch

#28
post #15
post #12

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 witho…

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.

"ARM Architecture reference manual": https://documentation-service.arm.com/static/5f8daeb7f86e165... (assuming that link works)

Start at section A5 describing the encoding. The instruction set is very much designed for clean decode, so instructions are grouped by bit pattern; every instruction in the manual has its bit pattern described.

Very much a "but why?" situation, since translation from assembly to machine code is so easily automatable and doing it by hand adds so little value.

Re: Compiling to Assembly from Scratch

#29
Was slightly assuming from the title that this would be something like "you have brought up a new machine with no tooling whatsoever; how do you bootstrap up to an assembler and your first real language?" such as the PDP-11 front panel toggles. But this version is probably more useful.

Re: Compiling to Assembly from Scratch

#30
post #9

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)

Yes, capitalisation of scratch was incredibly misleading in this case. It's easily checkable but slightly annoying. Hard to avoid though!
Post reply on HN