News for hackers. Stuff that matters.
Re:A gentle intro to assembly with Rust by Koshkin on Monday August 03, 2020 @04:12PM (#24042772)
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.
Re:A gentle intro to assembly with Rust by butterthebuddha on Monday August 03, 2020 @08:15PM (#24045080)
TAOCP?
Re:A gentle intro to assembly with Rust by saagarjha on Monday August 03, 2020 @06:25PM (#24044190)
> 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…

Re:A gentle intro to assembly with Rust by sunfish on Monday August 03, 2020 @06:32PM (#24044253)
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.
Re:A gentle intro to assembly with Rust by saagarjha on Monday August 03, 2020 @06:37PM (#24044310)
Shouldn't the C runtime do that before calling main?
Re:A gentle intro to assembly with Rust by sunfish on Monday August 03, 2020 @07:02PM (#24044508)
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.
Re:A gentle intro to assembly with Rust by SilasX on Monday August 03, 2020 @08:46PM (#24045288)
Any reason not to just do some other “increment stack pointer” option?
Re:A gentle intro to assembly with Rust by glandium on Monday August 03, 2020 @09:26PM (#24045545)
The instruction is smaller. Push rax is encoded into 1 bytes, while an increment stack pointer would be 4.
Re:A gentle intro to assembly with Rust by amelius on Tuesday August 04, 2020 @04:11AM (#24047774)
But there is a missing feature: how do you generate assembly at runtime?
Re:A gentle intro to assembly with Rust by ATsch on Tuesday August 04, 2020 @05:07AM (#24048100)
You need to use an external code generator library like llvm or cranelift, just like in any other language.
Re:A gentle intro to assembly with Rust by db48x on Tuesday August 04, 2020 @05:23AM (#24048196)
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.
Re:A gentle intro to assembly with Rust by ngcc_hk on Tuesday August 04, 2020 @04:46AM (#24047957)
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.

Re:A gentle intro to assembly with Rust by vaxman on Tuesday August 04, 2020 @09:37AM (#24049786)
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.
Re:A gentle intro to assembly with Rust by im3w1l on Monday August 03, 2020 @10:34PM (#24045946)
Why setb-test-jne? Couldn't you just do jc?
Re:A gentle intro to assembly with Rust by monocasa on Monday August 03, 2020 @11:46PM (#24046349)
Yeah, it's just being built without optimizations in that case.
Re:A gentle intro to assembly with Rust by tus88 on Monday August 03, 2020 @06:00PM (#24043975)
I didn't know Rust was an assembler.