Mail Archives: djgpp/1997/09/09/03:33:45
Shawn Hargreaves (Shawn AT talula DOT demon DOT co DOT uk) wrote:
: It's really not too complicated once you get used to it. It works out
: the value of disp+base+index*scale, where disp is a constant known at
: assembley time, base and index are two registers, and scale is one of
: the constants 1, 2, 4, or 8 (any of these may be left out altogether).
Thank you _very much_ for such an informative and patient explanation,
on which I've done a screen capture with ProComm Plus and will study at
leisure.
: You can also use any of these addressing modes with the LEA (load
: effective address) instruction, to store the calculated address in a
: register. This can be a really efficient way of working out some
: complicated things, for example the assignment:
:
: %ecx = 1 + %eax + %ebx*2;
:
: can be implemented in a single clock cycle with the line:
:
: leal 1(%eax, %ebx, 2), %ecx
Ah, this explains Brennan's trick for multiplying _quickly_ by n+1, where
n is the scale factor, e.g.
--------- Warning: Beginner's code, absolutely no warranties -------
#include <stdio.h>
int main(void)
{
int n1 = 20, n2 = 0;
__asm__ __volatile__ ( "leal (%1,%1,4), %0"
: "=g" (n2) : "0" (n1) );
printf("\n\nNow n1 = %d, and n2 = %d, or %d times n1.\n\n", n1, n2, n2/n1);
return(0);
}
--------- End of code; debuggers and delinters warmly invited -------
:
: Hope that helps...
:
Immensely, and now I actually have an idea of how the quick multiplication
trick actually works.
Most appreciatively indeed,
Margo Schulter
mschulter AT value DOT net
(To reply, please remove the extra . in my default address)
- Raw text -