flat assembler
Message board for the users of flat assembler.

flat assembler > Main > Flags Reference/General Assembly Hacks

Author
Thread Post new topic Reply to topic
moveax41h



Joined: 18 Feb 2018
Posts: 47
Hello,

I remember a while ago, I had found either a written work or video which essentially explained a lot of tips and tricks that the flags register and flags-based mnemonics could be used for. Things like, for example:

"If after performing an add instruction the CF is set and the OF is not set, then this means X"

Or "You can use ADC rather than ADD here"

That kind of stuff. I still remember what the flags mean in and of themselves, but I am looking for some guide which provides interpretation, tips, tricks, and hints as to how to effectively use the flags. Also, any other general x86 "hacks" would be appreciated.

I have the book Hacker's Delight, and I also found this page which is pretty cool.

Thank you.

_________________
-moveax41h
Post 20 Jul 2018, 21:57
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 453
Location: Belarus
Kind of related stuff: https://board.flatassembler.net/topic.php?t=20547
And there also used to be a topic where a lot of discussion was related to replacing DIV with MUL.
Post 20 Jul 2018, 22:42
View user's profile Send private message Visit poster's website Reply with quote
moveax41h



Joined: 18 Feb 2018
Posts: 47
DimonSoft,

Thanks, that is cool stuff!!!

What do you recommend for figuring out the "higher level" view of what some code is doing... This is C syntax but ofc this could easily be in assembly:
Code:
static av_always_inline av_const uint32_t bswap_32(uint32_t x)
 {
     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
     x= (x>>16) | (x<<16);
     return x;
 }
    


There are probably some folks here who can glance at that and tell me exactly what it is doing and why, however, just by looking, I cannot yet tell and I would have to give it some input and try to make sense of it. Any tips on figuring out this kind of stuff?
Post 21 Jul 2018, 03:50
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 453
Location: Belarus
moveax41h wrote:
What do you recommend for figuring out the "higher level" view of what some code is doing... This is C syntax but ofc this could easily be in assembly:
Code:
static av_always_inline av_const uint32_t bswap_32(uint32_t x)
 {
     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
     x= (x>>16) | (x<<16);
     return x;
 }
    

I guess, in such cases you either have enough experience to know such a trick and recognize it in spite of small implementation differences or you sit down and carefully step through the operations performing the equivalent of abstract interpretation in your head. Well, in this case you can just take a look at the function identifier Smile

How I see this code. Let x is some hex value consisting of 4 bytes, say AABBCCDD (I use these here not as exact values but as some means to mark values of bytes).

First line:
(x << 8​) is BBCCDD00
(x << 8​) & 0xFF00FF00 is BB00DD00
(x >> 8​) is 00AABBCC
(x >> 8​) & 0x00FF00FF is 00AA00CC
First line results in x = BBAADDCC
Second line:
(x >> 16) is 0000BBAA
(x << 16) is DDCC0000
Second line results in x = DDCCBBAA

So, basically it is some kind of cool bit/byte shaking that does what is required. I personally don’t like such tricks since (1) they are tied to fixed operand size in most cases (although can be extrapolated for any size), (2) they may often be implemented more efficiently in ASM than in HLLs and (3) learning them doesn’t pay back ’cause in most cases your task is slightly different but renders the whole trick useless.
Post 21 Jul 2018, 08:42
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16133
Location: Hyperborea
moveax41h wrote:
DimonSoft,

Thanks, that is cool stuff!!!

What do you recommend for figuring out the "higher level" view of what some code is doing... This is C syntax but ofc this could easily be in assembly:
Code:
static av_always_inline av_const uint32_t bswap_32(uint32_t x)
 {
     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
     x= (x>>16) | (x<<16);
     return x;
 }
    


There are probably some folks here who can glance at that and tell me exactly what it is doing and why, however, just by looking, I cannot yet tell and I would have to give it some input and try to make sense of it. Any tips on figuring out this kind of stuff?
CPUs for the last 25+ years have the bwsap instruction. So you can replace all of that with a single instruction.
Post 21 Jul 2018, 10:10
View user's profile Send private message Visit poster's website 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-2018, Tomasz Grysztar.

Powered by rwasa.