0

I looked in the instruction reference for cmova, and it says that it moves if the carry flag and the zero flag are both zero, but I don't understand what does it mean "above" in this context.

We were given an example to find a minimum in a linked list:

int t1 = 0;
Node *t2 = head;
if(t2)
    t1 = t2->data;
while (t2) {
    if(t1 > t2->data) {
        t1 = t2->data;
    }
    t2 = t2->next;
}

where Node was:

typedef struct _Node {
    int data;
    int id;
    struct _Node *next;
} Node;

And the asm code was:

.global _start

.section .data
head: .quad A
A:  .int 4  # int data
    .int 1  # int id
    .quad B # Node *next
B:  .int 6
    .int 2
    .quad C
C:  .int -5
    .int 3
    .quad D
D:  .int 1
    .int 4
    .quad 0

.section .text
_start:
    movq head(%rip), %rdi
    movl $0, %eax
    testq %rdi, %rdi
    je end
    movl (%rdi), %eax
.L3:
    movl (%rdi), %ecx
    cmpl %ecx, %eax
    cmovg %ecx, %eax
    movq 8(%rdi), %rdi
    testq %rdi, %rdi
    jne .L3
end:

This followed by the question: say we change cmovg %ecx, %eax to cmova %ecx, %eax , what would change? I ran the code, and this time it returned 1 instead of -5, so I understood it that the comparison would be unsigned, but I am not sure whether I got it right.

I would appreciate it if someone could clarify me what does it mean to "move if above/below."

Zipzap
  • 37
  • 8
  • 1
    From the Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2 (2A, 2B, 2C & 2D): Instruction Set Reference, A-Z: *The condition for each CMOVcc mnemonic is given in the description column of the above table. The terms “less” and “greater” are used for comparisons of signed integers and the terms “above” and “below” are used for unsigned integers.* – EOF Apr 30 '21 at 10:55
  • 1
    The same FLAG conditions have the same meaning for `ja` / `seta` as for `cmova`: *unsigned* above. – Peter Cordes Apr 30 '21 at 11:19
  • 1
    Small negative numbers, treated as unsigned, are near the very top of the value-range, closest to `0xFFFF...FFFF`. Is that what you're asking? I could look for a duplicate for that, too. – Peter Cordes Apr 30 '21 at 11:31

0 Answers0