
A gentle intro to assembly with Rust - lfn3
https://lfn3.net/2020/08/03/a-gentle-intro-to-assembly-with-rust/
======
Koshkin
Looking at the output of a compiler is a great way to quickly learn the basics
of assembly coding for a particular platform, but learning the principles of
programming in assembler and machine architecture is an entirely different
story. Knuth's 1st volume remains the best in that regard.

~~~
butterthebuddha
TAOCP?

------
saagarjha
> And indeed, inside main we can see push rax - saving the value in the
> register rax to the stack, then a call to our add function, then we pop rax
> off the stack. The push call pop sequence is to preserve whatever values are
> in the registers used in add.

There's really no need to save rax here; I know that optimizations are off but
I'm still curious why rustc thinks this is necessary…

~~~
sunfish
The compiler is using push and pop here just for their side effect of
adjusting the stack pointer, to keep the stack pointer 16-byte aligned.

~~~
saagarjha
Shouldn't the C runtime do that before calling main?

~~~
sunfish
It does; the stack pointer starts out aligned, but then the function does a
call, and the call instruction adjusts the stack pointer by 8 bytes to push
the return address, which would cause it to be misaligned. The push pushes an
extra 8 bytes so that the stack pointer is aligned in the callee.

------
amelius
But there is a missing feature: how do you generate assembly at runtime?

~~~
ATsch
You need to use an external code generator library like llvm or cranelift,
just like in any other language.

~~~
db48x
Not quite. Some languages bundle their compiler and code generated as part of
the language runtime. Common Lisp is the one that comes to mind first.

------
ngcc_hk
Assemble learnt via high level language would encounter a lot of issues as the
language provide a lot of feature.

Stacks, stack frames .bss, heap etc. And variable scoping and where doesn’t it
store hence.

And interesting issues like interrupt, memory mapped io (volitile ) etc.

Plus many low end features like CAN ...

Only do c and assembly (ignore 370) and the mapping is not easy.

~~~
vaxman
You are correct, Sir, but application-level people are happy to write a few
lines of funny looking mnemonics to optimize their boring sort functions and
then brag on LinkedIn. Also, if you are still in "_hk", please stop wasting
time and get your friends and family out.

------
im3w1l
Why setb-test-jne? Couldn't you just do jc?

~~~
monocasa
Yeah, it's just being built without optimizations in that case.

------
tus88
I didn't know Rust was an assembler.

