Is it possible to square the number stored in the register (for example, eax), without any multiplication (using shifts, etc.)? I will be hosting a 16-bit number in a 32-bit assembly, so overflow should not be a problem. I am using the NASM x86 build to create a program. Thanks in advance for your help.
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 will leave NASM to you.
Shift and Add are always a good starting point for doing multiplications on computers without involving multiplication instructions.
- , .
A bit late. Here is the logic: - Square N can be obtained by adding the first N odd numbers.
In C,
int sqr(int num){ int j=1; int sum=0; while(num>0){ sum += j; j += 2; num--; } return sum; }
but applicable only for integers.
Source: https://habr.com/ru/post/1740613/More articles:Onpaint events (invalid) change the order of execution after a period of normal operation (runtime) - c #Makefile - How to save .o one directory up? - makefileHow to search from a list with keywords without a prefix - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1740611/rails-how-can-i-combine-multiple-model-attributes-to-create-a-unique-permalink-using-permalinkfu&usg=ALkJrhgU4cg2OiIu90YKfq0ie9iV7k1oKgHow can I put an image in a row using NSString on an iPhone? - iphoneHow to create a runtime protocol in Objective-C? - objective-c$ .each and confusion animations - jqueryEasy loading of flash objects - javascriptMinGW error "undefined reference to 'typeof' '" - cDistributed message distribution - architectureAll Articles