I am trying to translate this for-loop from C to assembly using AT&T/GAS syntax:
for(int j = i; i*j < N; j++) {
A[i*j] = 0;
}
I have i stored in %eax and j stored in %ebx. The problem I am having, is to actually multiply i and j, as the instruction imul "reg32", "reg32" stores the result in the second register, which I obviously don't want. What I do want, is the ability to store the result in another register, say %ecx for example, and then use this to access the value in the array at index i*j.
When I look up the usage for the instruction imul, there seems to be no way to actually multiply two registers and have the result stored in a third register. Off course, I could make a loop and do some addition and so on, but that seems ineffective and not the way to do go about this. Note that I am completely new to assembly (only used it for a couple of days) as we are just starting out with learning the basics at my CS course.
TL;DR
What is the best way to multiply the values stored in two registers like this: %eax * %ebx = %ecx ?
imul %eax, %ebx
then, if you need the result in%ecx
, you could domov %eax, %ecx
. If you need to preserve the value in%eax
you can save it (e.g., on the stack). When I look up the usage for the instruction imul, there seems to be no way to actually multiply two registers and have the result stored in a third register. This is true. But it's not cumbersome to work around. – lurker