Xref: utzoo comp.sys.ibm.pc:23157 comp.sys.intel:664 Path: utzoo!attcan!uunet!lll-winken!ames!amdahl!drivax!alexande From: alexande@drivax.UUCP (Mark Alexander) Newsgroups: comp.sys.ibm.pc,comp.sys.intel Subject: Re: correct code for pointer subtraction Summary: look at the actual assembly output Message-ID: <4121@drivax.UUCP> Date: 12 Jan 89 18:07:17 GMT References: <597@mks.UUCP> <3845@pt.cs.cmu.edu> <18123@santra.UUCP> <142@bms-at.UUCP> <6604@killer.DALLAS.TX.US> Reply-To: alexande@drivax.UUCP (Mark Alexander) Organization: Bob-ist Temple of Monterey Lines: 20 Keywords: When all else fails, look at the actual code generated by your compiler to see where the problem lies. The problem people are having with Turbo C and MS C doing incorrect pointer subtraction is probably due to the compiler generating a SIGNED shift (SAR) instead of an UNSIGNED shift (SHR) when subtracting INT pointers. Datalight C 3.14 has this problem, as shown below. With DLC, the code generated to do the pointer subtraction look like this: mov BX,offset DGROUP:U0EA60h ; &a[30000] mov SI,offset DGROUP:U0 ; &a[0] sub BX,SI ; &a[30000] - &a[0] sar BX,1 ; convert bytes to words That last 'sar' instruction should really be a 'shr'. I'm sure Turbo C and MS C have a similar problem. -- Mark Alexander (UUCP: amdahl!drivax!alexande) "Bob-ism: the Faith that changes to meet YOUR needs." --Bob (as heard on PHC)