home bbs files messages ]

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

   comp.lang.pascal.borland      Borland Pascal was actually pretty neat      2,978 messages   

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

   Message 2,093 of 2,978   
   Femme Verbeek to Jim Leonard   
   Re: Multi-dimensional array access via i   
   20 Mar 06 13:26:45   
   
   From: fv@nospam.tcenl.com   
      
   Jim Leonard schreef:   
   > I'm trying to precalculate a 2-dimensional lookup table of screen   
   > coordinates and look them up via in-line asm, and I'm doing something   
   > wrong.  I can get single-dimension stuff to work:   
   >   
   > var   
   >   precalc:array[0..maxy-1] of word;   
   >   
   > (...fill the array...)   
   >   
   > asm {lookup value for index "y" and stick in ax}   
   >   mov si,y   
   >   shl si,1 {array is words, not bytes}   
   >   mov ax,[offset precalc+si]   
   > end;   
   >   
   > ...this works fine.  But 2-dimensional, it doesn't:   
   >   
   > var   
   >   precalc:array[0..maxx-1,0..maxy-1] of word;   
   >   
   > (...fill the array...)   
   >   
   > asm {lookup values for index "x,y" and stick in ax}   
   >   mov bx,x   
   >   shl bx,1   
   >   mov si,y   
   >   shl si,1   
   >   mov ax,[offset precalc+bx+si]   
   > end;   
   >   
   > I'm missing something obvious; can anyone help?   
   >   
   As Marco points out the address calculation is probably incorrect.   
   Further it is not clear how you want to store your rows and cols.   
      
   Take a look at this example   
      
   const maxx=3;   
          maxy=4;   
   type tarr=array[0..(maxx*maxy)-1]of integer;   
   var  parr:^tarr;   
         xyarr:array[0..maxx-1,0..maxy-1] of integer;   
   var  x,y,i:integer;   
   begin   
      parr:=@xyarr;   
      for y:=0 to maxy-1 do   
      begin   
        for x:=0 to maxx-1 do   
        begin   
          xyarr[x,y]:=y*10+x;   
          write(parr^[y+maxY*x]:2,' ');   
        end;   
        writeln;   
      end;   
      for i:= 0 to maxx*maxy-1 do write(parr^[i],' ');   
      readln;   
   end.   
      
   output:   
     0  1  2   
   10 11 12   
   20 21 22   
   30 31 32   
   0 10 20 30 1 11 21 31 2 12 22 32   
      
   Note that by the choise of adressing [X,Y] there is an inconsistency.   
   Normally X denotes the horizontal direction or the cols and Y the   
   vertical or the rows. So xyarr[2,3] is rownr y=3 colnr x=2 value 32. So   
   X is least significant in the table, but most significant in the address.   
      
   -- Femme   
      
   --- 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