I have to code some sort of compiler and it works quite good, but I have a problem with the semantic of an "is-greater-than" expression.
As example I want to return the smallest argument:
func mymin(x y)
cond
x>y then return y; end;
end;
return x;
end;
Conditional-Statements should return -1 if they are true (otherwise 0). Therefore I copy 0 into the destination register (here it's %rax). If the left operand is greater I copy -1 into %rax (with the conditional move). The comparison between -1 and the result of the condition (cmp $-1, %rax
) ensures a jump if it's true (-1 - -1 = 0).
But it's just the opposite. so
mymin(2,3) = 3
mymin(1,0) = 1
// ...
What's wrong?!
.text
.globl mymin
.type mymin, @function
mymin:
movq %rsi, %rax
# calculate greater one between %rdi and %rsi to %rax (reg & reg)
movq $0, %rax
movq $-1, %r10
cmpq %rdi, %rsi
cmovgq %r10, %rax
cmp $-1, %rax
jz cond_1
jmp cond_2
cond_1:
movq %rsi, %rax
ret
jmp cond_2
cond_2:
movq %rdi, %rax
ret