Live data from Hacker News

Compiling to Assembly from Scratch

keleshev.com

31–40 of 51 posts

Re: Compiling to Assembly from Scratch

#31
post #28
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.

"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 cod…

>> but why?

Agreed. More of a curiosity for me from learning and research purposes.

Re: Compiling to Assembly from Scratch

#32
post #24
post #22

Earlier quoted context omitted.

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.

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

Me too; that's also the reason why I wanted that possibility back.

Re: Compiling to Assembly from Scratch

#33
post #20

I really liked how short and concise these chapters are. What took me months of effort has been condensed to these few chapters and It's well worth the read. Though Why use 32bit instead of 64, why add so much friction for a first time learner.

ARM32 is much simpler to explain compared to ARM64. Registers are so much simpler, conditional execution is orthogonal, three-operand form is consistent. ARM64 made all the right practical choices, but has doubled the complexity. Still much better than x86 with its 10x complexity.

ARM32 is a gem, in my opinion. A truly simple instruction set, and easy to get your hands on with Raspberry Pi or emulation.

Re: Compiling to Assembly from Scratch

#34
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 is the nicest instruction encoding you can get actual hardware for (not thumb or aarch64). risc-v is pretty okay at the assembly level but the instruction encoding is almost deliberately sadistic. amd64 isn't too terrible but not nearly as nice as arm

older official arm documentation is a lot better than recent, which is very poor quality (though still pretty reliable.) oldnewthing and azeria-labs have good tutorials, though she got some of the condition flags wrong

Re: Compiling to Assembly from Scratch

#35
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…

this could be easy or hard depending on what you had to write in assembly on the test. like it wouldn't be that hard to write a subroutine to add up an array of integers or something

    addem:   xor eax, eax
        loop:
             test ecx, ecx
             jnz ok
             ret
        ok:  add eax, [ebx + ecx * 4]
             dec ecx
             jmp loop
(i haven't tested this, it'd be hilarious if i got it wrong)

Re: Compiling to Assembly from Scratch

#36
post #35
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…

this could be easy or hard depending on what you had to write in assembly on the test. like it wouldn't be that hard to write a subroutine to add up an array of integers or something addem: xor eax, eax loop: test ecx, ecx jnz ok ret ok: add eax, [ebx + ecx * 4] dec ecx jmp loop (i haven't tested this, it'd be hilarious if i got it wrong)

Assuming ebx is the pointer to the array and ecx is the length, doesn't this sum the slots from 1 to ecx (incusive) instead of 0 to ecx-1 (inclusive)?

Re: Compiling to Assembly from Scratch

#37
post #35

Earlier quoted context omitted.

this could be easy or hard depending on what you had to write in assembly on the test. like it wouldn't be that hard to write a subroutine to add up an array of integers or something addem: xor eax, eax loop: test ecx, ecx jnz ok ret ok: add eax, [ebx + ecx * 4] dec ecx jmp loop (i haven't tested this, it'd be hilarious if i got it wrong)

Assuming ebx is the pointer to the array and ecx is the length, doesn't this sum the slots from 1 to ecx (incusive) instead of 0 to ecx-1 (inclusive)?

hahaha, yes! i guess it wasn't as trivial as i thought. that's what i get for trying to be clever — guess i wouldn't have done that well on that exam ;)

Re: Compiling to Assembly from Scratch

#38
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)

Even thought the S in Scratch is uppered, I still read it correctly.

Re: Compiling to Assembly from Scratch

#39
post #25
post #21

Earlier quoted context omitted.

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.

SubX is a weird thing (I built it) that is somewhere between machine code and Assembly language. You have to type in the opcodes directly, which people typically associate with machine code. But it smooths some aspects of programming in machine code. You'll get nice errors if you accidentally write invalid machine code, it won't just go off and run data as code or something like that.

I'd be happy to support you if you choose to try it out! Ask as many questions as you like.

Even if you choose not to, you might like the cheatsheet in the repo (from https://net.cs.uni-bonn.de/fileadmin/user_upload/plohmann/x8...)

Re: Compiling to Assembly from Scratch

#40
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.

Oh man you gave me flashbacks. The TRS-80 Model II's OS, TRSDOS-II, had a built-in debugger that was little more than a monitor. You could step through instructions, examine and write to memory, set breakpoints to absolute memory locations, and that was it. I remember hand-assembling tiny Z80 programs in that thing and jumping into them, just to test my understanding of how machine code programs worked and how the computer executed them, and being super thrilled when I could get an A to appear somewhere on the screen or something.

The machine had a much more complete assembly language programming toolkit which I also used to write more sophisticated programs, employing this debugger to examine them. But I felt like I'd "cracked the code" of the computer when I plugged hex numbers into RAM and then ran them straight from there.

Most CPU ISA documentation should give you the opcodes that correspond to instruction mnemonics. You may have to plug in your own operands (registers, etc.) into bit fields in the instruction encoding. If you're serious about hand-assembling to begin with this should be no problem.

Post reply on HN