Thread View: alt.msdos.programmer
3 messages
3 total messages
Started by Walter Bright
Wed, 30 Sep 1992 18:00
Programming challenge
Author: Walter Bright
Date: Wed, 30 Sep 1992 18:00
Date: Wed, 30 Sep 1992 18:00
4 lines
473 bytes
473 bytes
I have heard that there is a "well-known trick" for evaluating (a > b) ? a : b in assembler language without using any jump instructions. I haven't been able to discover what that trick is. Does anyone know?
Re: Programming challenge
Author: Jerzy Tarasiuk
Date: Mon, 05 Oct 1992 18:14
Date: Mon, 05 Oct 1992 18:14
13 lines
1137 bytes
1137 bytes
>>>>> On 30 Sep 92 18:00:46 GMT, bright@nazgul.UUCP (Walter Bright) said: Walter> I have heard that there is a "well-known trick" for evaluating Walter> (a > b) ? a : b Walter> in assembler language without using any jump instructions. I haven't Walter> been able to discover what that trick is. Does anyone know? Probably something like: mov r1,a / mov r2,b / sub r2,r1 / cmc sbb r3,r3 / and r3,r2 / add r3,r1 / mov result,r3 The SUB sets CF when a>b, CMC complements the CF, after the SBB if a>b r3 is 0 (and then AND gives 0, ADD gives r1=a), otherwise r3 is 0xFFFF (and then AND gives b-a, ADD gives b). This all for unsigned arithmetic only... Jerzy Tarasiuk
Re: Programming challenge
Author: Jerzy Tarasiuk
Date: Tue, 06 Oct 1992 14:08
Date: Tue, 06 Oct 1992 14:08
24 lines
2168 bytes
2168 bytes
>>>>> On Mon, 5 Oct 1992 18:14:10 GMT, jt@fuw.edu.pl (Jerzy Tarasiuk) said: >>>>> On 30 Sep 92 18:00:46 GMT, bright@nazgul.UUCP (Walter Bright) said: Walter> I have heard that there is a "well-known trick" for evaluating Walter> (a > b) ? a : b Walter> in assembler language without using any jump instructions. I haven't Walter> been able to discover what that trick is. Does anyone know? Jerzy> Probably something like: mov r1,a / mov r2,b / sub r2,r1 / cmc Jerzy> sbb r3,r3 / and r3,r2 / add r3,r1 / mov result,r3 Jerzy> The SUB sets CF when a>b, CMC complements the CF, after the SBB Jerzy> if a>b r3 is 0 (and then AND gives 0, ADD gives r1=a), otherwise Jerzy> r3 is 0xFFFF (and then AND gives b-a, ADD gives b). Jerzy> This all for unsigned arithmetic only... Jerzy Tarasiuk I thinked what to do if SIGNED arithmetic compare is to be used and I found few solutions of the problem: 1. make compare then push flags, pop them to register (cannot use LAHF because need overflow flag which is in bit 11 of flag register) and try extract XOR(bit11,bit7) to put it in CF and further as for unsigned... very complicated; 2. XOR both values with 8000h and SUB will set CF as we need; 3. sign extend values to 32-bit (using CWD instruction), and make 32-bit subtraction to get value in range FFFF0001h..0000FFFFh, use high order 16 bits for the AND. I don't know what is more efficient: 2 or 3, would need code it and see. Remember to move the 8000h to register instead putting it in code 3 times in method 2, use XCHG instead MOV in method 3. Jerzy
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads