Is it possible to square a number stored in a register (say eax) without doing any multiplication (by using shifts, etc)? I will be squaring a 16-bit number in 32-bit assembly so overflow shouldn't be an issue. I am using NASM x86 assembly to create the program. Thanks in advance for your help.
+3
A:
Shift and Add is always a good starting point for doing multiplications on computers without involving multiplication instructions.
Precomputing a table is another option that could be suitable for this problem.
Laserallan
2010-04-11 01:31:24
+4
A:
In C:
int square(int n) {
int i, r = 0;
for (i = n; i; i >>= 1, n <<= 1)
if (i & 1)
r += n;
return r;
}
I'll leave the NASM to you.
Marcelo Cantos
2010-04-11 01:36:04