flat assembler
Message board for the users of flat assembler.

Index > Main > fine grained register stack saving

Author
Thread Post new topic Reply to topic
sylware



Joined: 23 Oct 2020
Posts: 619
Location: Marseille/France
sylware 16 Jul 2026, 19:33
For an ABI function, call-preserved registers are usually saved on the stack at function prolog and restored at function epilog. Same for register spilling, but at some code "blocks" level.


Based on the control flow of a function, it is possible to dodge some save-restore operations, but a function can be long and hairy.


Do you have some tricks in your source code to help your future self at tracking any find-grained stack registers save-restore operations? Or do you have some insights on that matter?


(I am currently not doing it in a fine-grained fashion but rather in some coarse-grained one, because I consider this like an "optimization" which will happen only when the code is stable in time, and it can be VERY aggressive)
Post 16 Jul 2026, 19:33
View user's profile Send private message Reply with quote
Fastestcodes



Joined: 13 Jun 2022
Posts: 76
Fastestcodes 25 Jul 2026, 19:05
Save registers to memory.
mrax dq 0
start:
mov qword[mrax],rax
do something
mov rax, qword[mrax]
Post 25 Jul 2026, 19:05
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1817
Location: Toronto, Canada
AsmGuru62 25 Jul 2026, 22:09
So, if I understand this, we talking about preserving registers in a code flow:
Code:
; --- I am using "C" just for better look:

if (...)
{
    ; using only rbx, rsi
    
    push rbx rsi
    
    ; --- some code flow is here using the regs...
    
    pop  rsi rbx
}
else
{
    ; using only rdi, rsi, rbx, r12, r14
    
    push rdi rsi rbx r12 r14
    
    ; --- some code flow is here using the regs...
    
    pop  r14 r12 rbx rsi rdi
}
    

It is unclear if you can achieve a significant optimization here.
Only if your code flows are very small in size.
If code flows are big, then a couple of additional PUSH/POP opcodes will not do much good.
In my opinion, it looks like premature optimization (an enemy of a coder).
Post 25 Jul 2026, 22:09
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.