🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.msdos.programmer
Article #696

Re: Programming challenge

#696
From: Jerzy Tarasiuk
Date: Tue, 06 Oct 1992 14:08
24 lines
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
Technical Details
Message-ID: <JT.92Oct6150816@fizyk1.fuw.edu.pl>
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!news.utdallas.edu!wupost!uwm.edu!psuvax1!rutgers!jvnc.net!gmd.de!Germany.EU.net!mcsun!sunic!fuw.edu.pl!cocos!jt
References: <939@nazgul.UUCP> <JT.92Oct5191410@fizyk1.fuw.edu.pl>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!news.utdallas.edu!wupost!uwm.edu!psuvax1!rutgers!jvnc.net!gmd.de!Germany.EU.net!mcsun!sunic!fuw.edu.pl!cocos!jt
From: jt@fuw.edu.pl (Jerzy Tarasiuk)
Newsgroups: alt.msdos.programmer
Subject: Re: Programming challenge
Message-ID: <JT.92Oct6150816@fizyk1.fuw.edu.pl>
Date: 6 Oct 92 14:08:16 GMT
References: <939@nazgul.UUCP> <JT.92Oct5191410@fizyk1.fuw.edu.pl>
Sender: news@fuw.edu.pl
Distribution: alt
Organization: Warsaw University Physics Dept.
Lines: 24
In-Reply-To: jt@fuw.edu.pl's message of Mon, 5 Oct 1992 18:14:10 GMT
Nntp-Posting-Host: fizyk1