home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 2,908 of 4,675   
   Terje Mathisen to Steve   
   Re: Converting some way to clever PL/I c   
   04 Aug 17 14:54:49   
   
   From: terje.mathisen@nospicedham.tmsw.no   
      
   Hi Steve!   
      
   You are of course correct that I had a typo in one of the versions,   
   thanks for spotting that!   
      
   Re the idea to split the loop into first finding a valid input and then   
   handling the rest, that is of course the fastest solution as long as the   
   body of the loop isn't too complicated, i.e. like in the current case.   
      
   The only real problem is that you have to look out for the additional   
   fencepost issue, i.e. what happens if the first valid input is the last   
   entry?   
      
   You should probably do a JMP Label4 after finding that first entry, and   
   since it is perfectly possible for a bunch of additions to wrap around   
   and end up with -1 as the sum, I would also keep a separate valid flag:   
      
      sum = 0;   
      valid = 0;   
      int i = 0;   
      while (i < len && arr[i] < 0) { i++; }   
      if (i < len) {   
        valid = 1;   
        sum = arr[i++];   
        while (i < len) {   
          a = arr[i++];   
          if (a > 0) sum += a; // Don't need to add any zeroes!   
        }   
      }   
      
   Terje   
      
   Steve wrote:   
   > Hi,   
   >   
   >  Robert Prins  writes:   
   >> I've recently come across some really clever/very nasty PL/I code, that   
   would,   
   >> theoretically, save CPU by eliminating a conditional jump. It relies on   
   >> initializing a BCD-encoded ***integer*** variable ("sum") with -0.1, which   
   >> results, on IBM mainframes, the last nibble of the BCD encoded value to   
   contain   
   >> 0xD (rather than the normal 0xC). The author uses this to avoid a costly   
   >> (Phuleeze, pass me a bucket!) test, so rather than coding:   
   >   
   >    If I understand Terje's answer, he might have a typo   
   >   
   >    add edx,eax   
   >   
   > should be   
   >   
   >    add edx, edi   
   >   
   > Or maybe I missed something.   
   >   
   >    Anyway, why not something like:   
   >         MOV     [Sum],-1        ; Set sum to illegal value.   
   >         MOV     ECX,[WhatEver]  ; Your loop count.   
   >         MOV     ESI, OFFSET a   ; Your data array.   
   >   
   > Label1:   
   >         LODSD                   ; EAX is an array value.   
   >         TEST    EAX,EAX   
   >         JL      Label2          ; Skip unwanted values.   
   >   
   >         MOV     [Sum],EAX       ; Sum initial value   
   >         JMP     Label3          ; Go process the rest   
   >   
   > Label2:   
   >         LOOP    Label1   
   >         JMP     Label5          ; No valid data, Sum still -1.   
   >   
   > Label3:   
   >         LODSD                   ; EAX is an array value.   
   >         TEST    EAX,EAX   
   >         JL      Label4          ; Skip unwanted values.   
   >   
   >         ADD     [Sum],EAX       ; Update Sum.   
   >   
   > Label4:   
   >         LOOP    Label3   
   >   
   > Label5:          ; No valid data, Sum still -1.   
   >                  ; Valid data, Sum not -1.   
   >   
   > Regards,   
   >   
   > Steve N.   
   >   
      
      
   --   
   -    
   "almost all programming can be viewed as an exercise in caching"   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca