flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > How many FLAGS can you juggle at once?

Author
Thread Post new topic Reply to topic
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Given flags set by cmp al, 1, which values of al can we distinguish with ordinary jccs? Let's see…
Code:
   al     al-1  OSZAPC
   00      FF   010111
   01      00   001010
 02..7F  01..7E 000..0
   80      7F   100100
 81..FF  80..FE 010..0    
So we can use something like
Code:
        cmp     al, 1
        jc      al_00
        jz      al_01
        jo      al_80
        js      al_81_FF
al_02_7F:    
to identify three values and two ranges. But, there is more! Let alone AF (no cc to check it and aaa/aas/daa/das are invalid in 64-bit mode), FLAGS contains not-so-often-used PF. Now we try
Code:
       cmp     al, 2

   al     al-2  OSZAPC
   00      FE   010101
   01      FF   010111
   02      00   001010
 03..7F  01..7D 000..0
   80      7E   100110
   81      7F   100100
 82..FF  80..FD 010..0    

Five values and two ranges with one cmp and several jccs? It's good.

Could this be improved? Your suggestions are welcome.

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 27 Nov 2008, 18:44
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3050
Location: vpcmipstrm
bitRAKE
Very nice.

If we look at it from the perspective of X = ( (code + data) bytes / branch targets) then as branches -> inf, X -> 1. What is the smallest number of branches where X < 2?

(Using same initial assumption of variable in register. Also targets cannot be sequential or direct mapping because then we could just use JMP EAX, or silly stuff like NEG EAX/JMP EAX.)

For amd64, 17 branches minimum:
Code:
mov al,[tab+rax]
lea rax,[base+rax*8]
jmp rax    
...16 branches if all targets in low memory 2^32.
Post 27 Nov 2008, 20:54
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitRAKE,

Is your tab 4 GiB in size? Wink xlat for sure. Wink I'm talking about understanding of [RE]?FLAGS.

I have an essay about flags, around 10…15 lines, explaining them a bit. Would it be useful to post it?
Post 27 Nov 2008, 21:46
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3050
Location: vpcmipstrm
bitRAKE
Something like:
Code:
sahf ; why cmp?
ja  _..0..0
.
.
.
(many more branches...)

_..0..0:
jge _OS0..0 ; =
jno _010..0
jnp _100.00 ; X
jmp _100.10 ; X

_OS0..0:
jno _000..0
jnp _110.00 ; X
jmp _110.10 ; X

_000..0:
jnp _000.00 ; X
jmp _000.10 ; X

_010..0:
jnp _010.00 ; X
jmp _010.10 ; X    
...is not a solution?
(bytes greater than two per branch target)

No tab is only 15/16 bytes. XLAT is good, but requires (E/R)BX which is additional byte to load register. What to limit input values, huh?


Last edited by bitRAKE on 27 Nov 2008, 22:11; edited 1 time in total
Post 27 Nov 2008, 22:01
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitRAKE,

With lahf you'll lose 3 bits of ah, definitely. Test for AF will be complicated too. Wink Not to mention #UD in 64-bit mode occasionally…


Last edited by baldr on 27 Nov 2008, 22:24; edited 2 times in total
Post 27 Nov 2008, 22:10
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3050
Location: vpcmipstrm
bitRAKE
bah, SAHF -- not LAHF.
Post 27 Nov 2008, 22:13
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitRAKE,

Same mistake. Looked up sahf and wrote lahf
Post 27 Nov 2008, 22:16
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3050
Location: vpcmipstrm
bitRAKE
baldr wrote:
I have an essay about flags, around 10…15 lines, explaining them a bit. Would it be useful to post it?
Personally, I think this is the number one reason people use MASM like syntax - they don't know when to use what branches. Remembering what instructions effect which flags and why OF<>SF? A whole chapter on x86 program flow control would be a great asset to the community, imho. Could cover general purpose, FPU, and SIMD patterns.

_________________
¯\(°_o)/¯ unlicense.org
Post 28 Nov 2008, 02:06
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.