flat assembler
Message board for the users of flat assembler.
Index
> Main > x86 convert flags from unsigned cmp to flags from signed cmp |
Author |
|
revolution 13 Jun 2023, 02:13
LAHF & SAHF.
Use a 256 byte table: Code: lahf movzx eax,ah mov ah,[eax+conversion_table] sahf conversion_table: db ... |
|||
13 Jun 2023, 02:13 |
|
bitRAKE 13 Jun 2023, 13:54
The only problem I see with the LAHF/SAHF table solution is that overflow flag is undefined, but it could be fixed with SAR AL, 1.
I haven't tested these, but I think this solves the setup for the signed branch (it's a little tricky): Code: setnc ah setnz al ror ah, 1 shl ax, 1 Code: lahf seto al ; there are smaller solutions ror al, 1 ; putting overflow flag in bit 7 xor al, ah sahf bt ax, 7 ; CF = OF xor CF _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
13 Jun 2023, 13:54 |
|
Tomasz Grysztar 13 Jun 2023, 14:40
Interesting exercise. My attempt:
Code: sets al rcr al,2 rol al,1 And the other direction seems trivial: Code: setl al ; LSB(AL) <- SF xor OF rcr al,1 ; CF <- LSB(AL), ZF unaffected |
|||
13 Jun 2023, 14:40 |
|
bitRAKE 13 Jun 2023, 15:48
Tomasz Grysztar wrote: My attempt: My solution is incorrect as well. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
13 Jun 2023, 15:48 |
|
Tomasz Grysztar 13 Jun 2023, 16:18
bitRAKE wrote: ... seems to fail with CMP $7F, $FF flags, SF=OF=1. Your conversion produces SF<>OF. After your CMP there is CF = 1, SF = 1, conversion sets up OF = 0, so (SF xor OF) = (1 xor 0) = 1, which is correct because this was the value of CF. |
|||
13 Jun 2023, 16:18 |
|
bitRAKE 13 Jun 2023, 16:41
Ah, I got my test cases mixed up. It works now, and I can fix my solution to:
Code: setc ah setnz al ror ah, 1 shl ax, 1 This is what I use to verify (I've named them quite verbosely, now.): Code: macro GATHER_STATES_SIGNED ; need to preserve S<>O, Z pushfq sets al seto ah cmp al, ah setnz ah popfq setz al shl ah,1 or al,ah end macro macro GATHER_STATES_UNSIGNED ; need to preserve C, Z setz al setc ah shl ah,1 or al,ah end macro mov ecx, 0x10000 @@: cmp cl, ch GATHER_STATES_UNSIGNED ; need to preserve C, Z mov dl, al cmp cl, ch SOLUTION GATHER_STATES_SIGNED ; need to preserve S=O, Z cmp dl, al loopz @B |
|||
13 Jun 2023, 16:41 |
|
tthsqe 14 Jun 2023, 01:01
Wow, so apparently there is a lahf and a sahf instruction, and the rotate instructions leave both ZF and SF unchanged. I would shorten revolution's LUT to 4 bytes
Code: setz cl adc cl,cl shl cl,3 mov eax, the right 4 bytes ror eax,cl sahf But, yes, there is the problem with OF, so what is the point of sahf if OF is undefined (or is it just unaffected?)? Thomasz' solution seems to take the cake. By the way, I also realized that only 7 of the 16 possible combinations of ZF,CF,SF,OF are possible after a cmp instruction, though I don't think any of the solutions assume, for example, that ZF=CF=1 is impossible. However, bitRAKE's test harness won't generate this input flag combination(?). |
|||
14 Jun 2023, 01:01 |
|
bitRAKE 14 Jun 2023, 01:22
tthsqe wrote: though I don't think any of the solutions assume, for example, that ZF=CF=1 is impossible. However, bitRAKE's test harness won't generate this input flag combination(?). (One more abstraction layer and I should be able to make a completely general test.) _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
14 Jun 2023, 01:22 |
|
bitRAKE 14 Jun 2023, 03:51
Code: ; RSI : expectations ; EDX : source flag mask ; EBX : destination flag mask popcnt eax, edx xor ecx, ecx bts ecx, eax .checks: ; generally update masked flags based on present iteration pdep eax, ecx, edx pushfq or dword [rsp], edx xor dword [rsp], edx or dword [rsp], eax popfq SOLUTION pushfq pop rax pext eax, eax, ebx cmp [rsi + (rcx-1)*4], eax loopz .checks Note: the CMP at the end is not sufficient as we really want to match equations of result bits. |
|||
14 Jun 2023, 03:51 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.