flat assembler
Message board for the users of flat assembler.

Index > Main > Stack Realignment "Techniques"

Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
Author
Thread Post new topic Reply to topic
sinsi



Joined: 10 Aug 2007
Posts: 789
Location: Adelaide
sinsi 03 Sep 2017, 09:56
You seem to have it backwards...

You can use whatever registers you wish in your code (unless it's a callback function).
If an API call uses a NV register, it needs to save and restore it.

MessageBoxA and printf couldn't give a flying fuck what you have in R15 but if they use it they will save it for you.

Code:
;Code that will work properly
    mov rbx,5
@@: call AWinAPI    ;RBX saved as per the ABI
    dec rbx
    jnz @b

;Code that will probably not work properly
    mov rcx,5
@@: call AWinAPI    ;RCX possibly used and not saved as per the ABI
    dec rcx
    jnz @b
    
Post 03 Sep 2017, 09:56
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 10:19
That's due to your poor understanding of "requirement" vs "recommendation". It is not my fault. Btw, you can also save something in the memory. You don't have to use any non-volatiles register. See, that's the difference between "recommendation" vs "requirement".

Requirement:
First integer argument: RCX. Violate this, and your face will be BOOMED.

Recommendation:
rbx, rsi, rdi,.... blah blah blah. But you can always use a memory for saving data. Not necessarily using non-volatiles registers. No boom.

hehehehe xD
Post 03 Sep 2017, 10:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 10:19
system error wrote:
Scratching R15 will not create any boom if you're not interested in using or preserving it.
If a C function calls your code then you cannot guarantee that failing to preserve R15 will have no effect. If an API callback function calls your code then you cannot guarantee that failing to preserve R15 will have no effect.

But the opposite of that is this: If you call a C function it makes no difference what you put in R15, it will ignore it, and it will preserve it. If you call an API function it makes no difference what you put in R15, it will ignore it, and it will preserve it.

And an alternative to that is this: If your internal code follows random calling conventions, then when calling another function it might or might not care about R15 and/or might or might not preserve R15. But that is your own lookout to make sure everything works as expected. Like I mentioned above I also call this mode "chaos". It creates some wonderful bug hunting problems, unless you are really really careful about absolutely everything. And making small changes can become a nightmare.
Post 03 Sep 2017, 10:19
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 10:26
revolution wrote:
system error wrote:
Scratching R15 will not create any boom if you're not interested in using or preserving it.
If a C function calls your code then you cannot guarantee that failing to preserve R15 will have no effect. If an API callback function calls your code then you cannot guarantee that failing to preserve R15 will have no effect.

But the opposite of that is this: If you call a C function it makes no difference what you put in R15, it will ignore it, and it will preserve it. If you call an API function it makes no difference what you put in R15, it will ignore it, and it will preserve it.

And an alternative to that is this: If your internal code follows random calling conventions, then when calling another function it might or might not care about R15 and/or might or might not preserve R15. But that is your own lookout to make sure everything works as expected. Like I mentioned above I also call this mode "chaos". It creates some wonderful bug hunting problems, unless you are really really careful about absolutely everything. And making small changes can become a nightmare.


C functions uses no R15. If I were to create a callable function from C, I'll stick to the RCX, RDX, R8, R9..... and the XMMs. That's the ABI 64 CONVENTION. How difficult is this to be fully understood? xD
Post 03 Sep 2017, 10:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 10:33
system error wrote:
C functions uses no R15.
What? I'm pretty sure that isn't correct. I'm not particularly familiar with C compilers but if they don't make use of all the available registers then the compilers are really really crappy. Crappier than I ever imagined.
system error wrote:
If I were to create a callable function from C, I'll stick to the RCX, RDX, R8, R9..... and the XMMs. That's the ABI 64 CONVENTION. How difficult is this to be fully understood? xD
That is fine. It is your choice of course. By doing that you are in fact following the convention requirements by preserving the state of R15. By not touching R15 you implicitly preserve it. We all already do that within our code. For a register we don't touch we don't bother to save/restore it, that would be inefficient.
Post 03 Sep 2017, 10:33
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 789
Location: Adelaide
sinsi 03 Sep 2017, 11:04
Quote:
That's due to your poor understanding of "requirement" vs "recommendation".

Genuine question: is English your first language?

sinsi wrote:

MSDN wrote:
The registers RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15 are considered nonvolatile and must be saved and restored by a function that uses them.

Must=Required
May=Recommended
Post 03 Sep 2017, 11:04
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:08
Preserving R15 or any other non-volatiles doesn't involve any specific ABI requirement. In assemblers book for beginners, it is called "CHAPTER 2: PRESERVING ANY REGISTERS". It's just a general technique commonly employed everywhere from 8-bit processors to 64-bit processors of the x86 family.

push rbx
call aWinApi
pop rbx

This is just a recommendation if you wished to preserve RBX. You are not FORCED to save it if you don't want it to. This is why it serves as "recommendation" rather than "requirement". It is up to the users!

This techniques is EXACTLY THE SAME in dealing with 32-bit APIs. It's called register preservation. Not a specific 64-bit ABI requirement.
Post 03 Sep 2017, 11:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 11:16
system error wrote:
push rbx
call aWinApi
pop rbx
Your don't need to save and restore RBX when calling an API because the API already guarantees that RBX will be preserved, as per the ABI requirements and guarantees.

BTW: No one has proposed the example you show above except you, so you have in fact created a strawman argument which you then argue against. So it appears that we are all in violent agreement. Yay. End of thread?
Post 03 Sep 2017, 11:16
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:18
sinsi wrote:
You seem to have it backwards...

You can use whatever registers you wish in your code (unless it's a callback function).
If an API call uses a NV register, it needs to save and restore it.

MessageBoxA and printf couldn't give a flying fuck what you have in R15 but if they use it they will save it for you.

Code:
;Code that will work properly
    mov rbx,5
@@: call AWinAPI    ;RBX saved as per the ABI
    dec rbx
    jnz @b

;Code that will probably not work properly
    mov rcx,5
@@: call AWinAPI    ;RCX possibly used and not saved as per the ABI
    dec rcx
    jnz @b
    


No smarty pant, just use this simple register-preservation technique, IF, you want to preserve it.

Code:
    mov rcx,5
     push rcx
@@: call AWinAPI    ;RCX possibly used and not saved as per the ABI
    pop rcx
    dec rcx
    jnz @b    


This technique is also availlable in 32-bit APIs. It's common sense - you want to use it later, just preserve it. You don't create a special stack frame inside it for ALL non-volatiles or else your code will be EXTREMELY BLOATED. Probably more bloated than your brain. hahahaha xD

THEN AGAIN, since it is a recommendation and not a 64-bit requirement, you can also do this;

Code:
    mov rcx,5
    mov [_rcx],rcx
@@: call AWinAPI    ;RCX possibly used and not saved as per the ABI
    mov rcx,[_rcx]
    dec rcx
    jnz @b    


See smarty pant. That's why we call it recommendation. It's up to you if you want to save it, how you want to it and where you want to preserve it. xD

I say your English is not good enough to make you FULLY understand a simple technical documentation. You need something else... like BANGING YOUR HEAD to the wall. hahahaha xD
Post 03 Sep 2017, 11:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 11:22
system error: RCX is defined in the ABI as volatile. Once again this is your strawman argument. You setup a situation where RCX (a volatile) requires saving and then state that it requires saving (umm well yeah of course). But no one else said that RCX didn't need saving, only you. And once again we are in violent agreement. Yay. Now end of thread?


Last edited by revolution on 03 Sep 2017, 11:23; edited 1 time in total
Post 03 Sep 2017, 11:22
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:22
revolution wrote:
system error wrote:
push rbx
call aWinApi
pop rbx
Your don't need to save and restore RBX when calling an API because the API already guarantees that RBX will be preserved, as per the ABI requirements and guarantees.

BTW: No one has proposed the example you show above except you, so you have in fact created a strawman argument which you then argue against. So it appears that we are all in violent agreement. Yay. End of thread?


You probably have no idea that users also create a ABI-compliant API codes. Now let me see how you would implement all the non-volatile savings in your stack frame. Then you'll understand what I meant by "recommendation" vs "requirements". Well, you asked for it xD
Post 03 Sep 2017, 11:22
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:24
revolution wrote:
system error: RCX is defined in the ABI as volatile. Once again this is your strawman argument. You setup a situation where RCX (a volatile) requires saving and then state that it requires saving. But no one else said that, only you. And once again we are in violent agreement. Yay. Now end of thread?


Not my code. It's Sinsi's code. I am just demonstrating where he missed the point of register preservations. Now you're starting to sound like an idiot too. Are you really that MAD? hahahaha xD
Post 03 Sep 2017, 11:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 11:24
system error wrote:
You probably have no idea that users also create a ABI-compliant API codes. Now let me see how you would implement all the non-volatile savings in your stack frame. Then you'll understand what I meant by "recommendation" vs "requirements". Well, you asked for it xD
I really have no idea what you are saying now.
Post 03 Sep 2017, 11:24
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:25
revolution wrote:
system error wrote:
You probably have no idea that users also create a ABI-compliant API codes. Now let me see how you would implement all the non-volatile savings in your stack frame. Then you'll understand what I meant by "recommendation" vs "requirements". Well, you asked for it xD
I really have no idea what you are saying now.


Of course you don't. You are clueless right from the beginning anyway. HAHAHA xD
Post 03 Sep 2017, 11:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 11:26
system error wrote:
Not my code. It's Sinsi's code. I am just demonstrating where he missed the point of register preservations. Now you're starting to sound like an idiot too. Are you really that MAD? hahahaha xD
There was nothing wrong with sinsi's code. sinsi correctly demonstrated that RCX was not preserved (as per the ABI) and that it might have problems, and that RBX was preserved (also as per the ABI) and it would work okay.
Post 03 Sep 2017, 11:26
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:33
revolution wrote:
system error wrote:
Not my code. It's Sinsi's code. I am just demonstrating where he missed the point of register preservations. Now you're starting to sound like an idiot too. Are you really that MAD? hahahaha xD
There was nothing wrong with sinsi's code. sinsi correctly demonstrated that RCX was not preserved (as per the ABI) and that it might have problems, and that RBX was preserved (also as per the ABI) and it would work okay.


So I preserved it for him the usual "preserving the register" way. What's your problem?
Are you extremely mad at me? xD
Post 03 Sep 2017, 11:33
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20303
Location: In your JS exploiting you and your system
revolution 03 Sep 2017, 11:40
system error wrote:
So I preserved it for him the usual "preserving the register" way. What's your problem?
Yes, you did. So why call sinsi a "smarty pant"? It was just a demonstration of a potential problem. There was no need for name calling.
system error wrote:
Are you extremely mad at me? xD
I am confused by you. You appear to misunderstand a lot of what is being discussed. I hope it is not deliberate!
Post 03 Sep 2017, 11:40
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 789
Location: Adelaide
sinsi 03 Sep 2017, 11:43
Stack frame, stack frame, stack frame.
What?
Quote:
BANGING YOUR HEAD to the wall
Yep, just about.

Image
Bye bye
Post 03 Sep 2017, 11:43
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:44
revolution wrote:
system error wrote:
So I preserved it for him the usual "preserving the register" way. What's your problem?
Yes, you did. So why call sinsi a "smarty pant"? It was just a demonstration of a potential problem. There was no need for name calling.
system error wrote:
Are you extremely mad at me? xD
I am confused by you. You appear to misunderstand a lot of what is being discussed. I hope it is not deliberate!


I will continue the discussion once you have calmed your tits down. In the mean time, try to figure out how "saving ALL the non-volatiles" in your ABI-compliant functions would look like. Cheers xD
Post 03 Sep 2017, 11:44
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 03 Sep 2017, 11:46
sinsi wrote:
Stack frame, stack frame, stack frame.
What?
Quote:
BANGING YOUR HEAD to the wall
Yep, just about.

Image
Bye bye


hahaha. You are one funny guy. That's more like it. Why so serious? xD
Post 03 Sep 2017, 11:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.