imulq and unsigned long long overflow detection in C and asm
<p>As somebody new to assembly, I use gcc for reverse engineering. But now I ran in a somehow funny problem: I try to multiply two 64bit-integer for x86-64. The C - code looks as follows:</p>

<pre><code>unsigned long long 
val(unsigned long long a, unsigned long long b){
    return a*b;
}
</code></pre>

<p>and compiled with gcc:</p>

<pre><code>val:
    movq    %rdi, %rax
    imulq   %rsi, %rax
    ret
</code></pre>

<p>It might be counterintuitive to use signed multiplication for unsigned integers, but it works for C. </p>

<p>However, I would like to check the multiplication for overflows. Now, the overflow flag is set if the result is greater than <code>2^63-1</code> (I guess because it is signed multiplication after all). But for unsigned 64bit this would be still OK as long as the result is not greater than <code>2^64-1</code>.</p>

<p>What is the right way to do the multiplication (in assembly) in this case?</p>