title: Writing a function in 64 bit assembly
body: <p>I am trying to write a function (max) in 64 bit assembly and I don't know what I am doing wrong, maybe some of you guys can determine what I am doing wrong :/</p>
  
  <p>Here's the function:</p>
  
  <pre><code>int max(int a, int b) {
  /* Return the larger of the two integers `a' and `b'. */
  if (a &gt; b)
  return a;
  else
  return b;
  }
  </code></pre>
  
  <p>And here is my assembly code (with comments):</p>
  
  <pre><code>    push %rbp
      mov %rsp, %rbp
      mov %rdi, %rax
      mov %rsi, %rcx
      test %rax, %rax                // Checking if first parameter is signed
      js .signedRAX
      test %rcx, %rcx                // Checking if second parameter is signed
      js .signedRCX
      jmp .compare                   // If either one signed then jump to .compare
    .signedRAX:
      test %rcx, %rcx                // Checking if both are signed
      js .signedRAXandRCX
      mov %rcx, %rax                 // If not then return the positive number
      jmp .end                       // finish the function
    .signedRCX:
      jmp .end                       // If only the second parameter is signed then jump  
    .signedRAXandRCX:                // straight to end of function and return %rax
      cmp %rax, %rcx                 // If both are signed compare which one is the max
      jl .end 
      mov %rcx, %rax
      jmp .end
    .compare:
      cmp %rax, %rcx                 // If both are positive then compare which one is 
      jg .end                        // the max
      mov %rcx, %rax
    .end:
      mov %rbp, %rsp
      pop %rbp
      ret
  </code></pre>
  
  <p>I am getting the wrong output when comparing two parameters that are both signed and then both positive.</p>