flat assembler
Message board for the users of flat assembler.
Index
> Main > the register dance |
Author |
|
sts-q 26 Sep 2021, 04:47
Even after years an unsolved open question!
I like programming in assembler. But there are two things i haven' t found a good solution for: * when push to stack * ( nested) if-then-else constructions What i do is: have low-level, mid-level and higher-level functions: low-level uses: a b c d mid-level uses: si di k v ( source index, destination index, key, value ) higher-level uses: tos sos bp ... and RAM i do a lot of register renaming: k equ r8 v equ r8 ... I think a better solution would be to do more paper-and-pencil-programming, that is first under stand what i would like to do, than hack it into the computer. https://codeberg.org/sts-q/minal/src/branch/master/fasm/declarations.fasm |
|||
26 Sep 2021, 04:47 |
|
bitRAKE 26 Sep 2021, 05:57
It's like an ebb-and-flow when I am trying to optimize register usage. Top->down and then bottom->up -- repeat. External APIs are like a boundary condition - it's very strict and much of the state afterward is undefined, and so we can work backward from the API as well as starts over when it is complete.
I too like to use register renaming as well as memory renaming with VIRTUAL blocks. Macros can be scaled (i.e. called by other macros) if all the operands are constant or parametric. These type of macros are quite universal, whereas using registers or memory directly in a macro restricts it's utility. Another pattern is to code for both positive and negative logic as a type of control flow optimization. Sometimes the code works out better with the polarity reversed. It's possible to encapsulate this within a macro as well (contrived example): Code: macro PARITY? true,false,regmem*,bits assert 0 < bits & bits < 9 test regmem,(1 shl bits) - 1 match ,true jpo false else match ,false jpe true else err end match end macro macro ODD? even*,regmem* PARITY even,<>,regmem,1 end macro macro EVEN? odd*,regmem* PARITY <>,odd,regmem,1 end macro |
|||
26 Sep 2021, 05:57 |
|
sylware 26 Sep 2021, 12:03
@sts-q This is roughly what I wrote down in the code, but for each significant code paths (usually following the call/jump graph). I did use the "define" instruction for tracking though, like "define node_p r12".
@bitRAKE yep, multi-pass seems kind of mandatory, and "external" calls are "expensive" if many registers are to be "saved". What I try to do, is to have a "first pass" kind of already trying to roughly optimize register usage, that with this "fill them all" policy. I noticed too that I tend to avoid to use "call" and then go on the stack to save the RIP, I am more and more using a "link" register with jumps. And I follow the paranoid "nops align everything" I saw in the HeavyThing assembly project. |
|||
26 Sep 2021, 12:03 |
|
revolution 26 Sep 2021, 12:22
The "nops to align" can be beneficial, and it can be harmful also.
Be aware of your cache wastage when you start inserting nops. Some code segments might see an advantage, others might see a disadvantage. A lot of it is CPU dependent. On some CPUs it makes no difference, on others it can have an impact, either negative and/or positive and/or opposite from other CPUs. IME putting nops everywhere is a bad strategy overall. I found it just made code larger and more cluttered, with any "benefits" so tiny to be unmeasurable. But I guess on some critical code there might be a use case that can see something useful. |
|||
26 Sep 2021, 12:22 |
|
sylware 26 Sep 2021, 20:51
@revolution the thing with the nops everything: I got my hands on AMD and Intel optimizing manuals, the "16bytes paragraph" seems really critical for both microarchitectures.
I did set up some RDTSC related macros, just in case. I may fool around to torture the alignment of my code to see if it has an obvious impact (on 2 CPUs, zen2 and oooold intel). To be fair, since this code is memory bound, I am not supposed to see anything significant though. Back to how to track register usage: I did post already something about such of an software assistant. Now, it starts to look like it would be overkill since writing down the register usage at pertinent points in the code did feel like a better compromise. |
|||
26 Sep 2021, 20:51 |
|
sylware 25 Oct 2021, 20:58
I have a pseudo mechanic way (namely it varies a lot based on the context) to use the general registers for variables: first the callee-saved regs, with rbp last (for rsp saving upon C ABI external calls), then the argument passing registers in reverse order. The basic scratch general registers would be rax, rcx and r11.
|
|||
25 Oct 2021, 20:58 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.