## Comparison Instructions

R-FormatI-Formatcodedescription
signedunsignedsignedunsigned
cmpeq
cmpeqi
a == ba equals b
cmpne
cmpnei
a != ba not equal to b
cmpltcmpltucmplticmpltiua < ba less than b
cmpgtcmpgtucmpgticmpgtiua > ba greater than b
cmplecmpleucmpleicmpleiua <= ba not greater than b
cmpgecmpgeucmpgeicmpgeiua >= ba not less than b

 pseudo-instruction

 cmplt rC, rA, rB if (signed) rA < (signed) rB then rC = 1 else rC = 0 cmplti rB, rA, IMM16 if (signed) rA < (signed) se(IMM16) then rB = 1 else rB = 0 cmpltu rC, rA, rB if (unsigned) rA < (unsigned) rB then rC = 1 else rC = 0 cmpltui rB, rA, IMM16 if (unsigned) rA < (unsigned) se(IMM16) then rB = 1 else rB = 0

## C Source

```01: #include "sys/alt_stdio.h"
02:
03: void compare(int a, int b);
04:
05: int main()
06: {
07:         compare(5,3);
08:         compare(-2,7);
09:         compare(3,3);
10:         compare(0,0);
11:
12:
13:         return 0;
14: }
15:
16: void compare(int a, int b)
17: {
18:         int is_gt, is_lt, is_ge, is_le;
19:         int is_eq, is_ne, is_and, is_or;
20:         int a_gt_0, a_eq_0, is_a, is_not_a;
21:
22:         is_gt = a>b;
23:         is_lt = a<b;
24:         is_ge = a>=b;
25:         is_le = a<=b;
26:
27:         is_and = a&&b;
28:         is_or = a||b;
29:
30:         is_eq = a==b;
31:         is_ne = a!=b;
32:
33:         a_gt_0 = a>0;
34:         a_eq_0 = a==0;
35:         is_a = !!a;
36:         is_not_a = !a;
37:
38: #if defined(DBUG)
39:         alt_printf("\na = %d compared to b = %d\n",a,b);
40:         alt_printf("is_gt %d is_lt %d is_ge %d is_le %d\n",is_gt,is_lt,is_ge,is_le);
41:         alt_printf("is_eq %d is_ne %d is_and %d is_or %d\n",is_eq,is_ne,is_and,is_or);
42:         alt_printf("a_gt_0 %d a_eq_0 %d is_a %d is_not_a %d\n",a_gt_0,a_eq_0,is_a,is_not_a);
43: #endif
44:
45: }
```

## Dissaembly

```0000806c <main>:

void compare(int a, int b);

int main()
{
8070:    dfc00115     stw    ra,4(sp)
8074:    df000015     stw    fp,0(sp)
8078:    d839883a     mov    fp,sp
compare(5,3);
807c:    01000144     movi    r4,5
8080:    014000c4     movi    r5,3
8084:    00080c00     call    80c0 <compare>
compare(-2,7);
8088:    013fff84     movi    r4,-2
808c:    014001c4     movi    r5,7
8090:    00080c00     call    80c0 <compare>
compare(3,3);
8094:    010000c4     movi    r4,3
8098:    014000c4     movi    r5,3
809c:    00080c00     call    80c0 <compare>
compare(0,0);
80a0:    0009883a     mov    r4,zero
80a4:    000b883a     mov    r5,zero
80a8:    00080c00     call    80c0 <compare>

return 0;
80ac:    0005883a     mov    r2,zero
}
80b0:    dfc00117     ldw    ra,4(sp)
80b4:    df000017     ldw    fp,0(sp)
80bc:    f800283a     ret

000080c0 <compare>:

void compare(int a, int b)
{
80c4:    df000e15     stw    fp,56(sp)
80c8:    d839883a     mov    fp,sp
80cc:    e1000015     stw    r4,0(fp)
80d0:    e1400115     stw    r5,4(fp)
int is_gt, is_lt, is_ge, is_le;
int is_eq, is_ne, is_and, is_or;
int a_gt_0, a_eq_0, is_a, is_not_a;

is_gt = a>b;
80d4:    e0c00017     ldw    r3,0(fp)
80d8:    e0800117     ldw    r2,4(fp)
80dc:    10c4803a     cmplt    r2,r2,r3
80e0:    e0800215     stw    r2,8(fp)
is_lt = a<b;
80e4:    e0c00017     ldw    r3,0(fp)
80e8:    e0800117     ldw    r2,4(fp)
80ec:    1884803a     cmplt    r2,r3,r2
80f0:    e0800315     stw    r2,12(fp)
is_ge = a>=b;
80f4:    e0c00017     ldw    r3,0(fp)
80f8:    e0800117     ldw    r2,4(fp)
80fc:    1884403a     cmpge    r2,r3,r2
8100:    e0800415     stw    r2,16(fp)
is_le = a<=b;
8104:    e0c00017     ldw    r3,0(fp)
8108:    e0800117     ldw    r2,4(fp)
810c:    10c4403a     cmpge    r2,r2,r3
8110:    e0800515     stw    r2,20(fp)

is_and = a&&b;
8114:    e0000815     stw    zero,32(fp)
8118:    e0800017     ldw    r2,0(fp)
811c:    1005003a     cmpeq    r2,r2,zero
8120:    1000051e     bne    r2,zero,8138 <compare+0x78>
8124:    e0800117     ldw    r2,4(fp)
8128:    1005003a     cmpeq    r2,r2,zero
812c:    1000021e     bne    r2,zero,8138 <compare+0x78>
8130:    00800044     movi    r2,1
8134:    e0800815     stw    r2,32(fp)
is_or = a||b;
8138:    e0000915     stw    zero,36(fp)
813c:    e0800017     ldw    r2,0(fp)
8140:    1004c03a     cmpne    r2,r2,zero
8144:    1000041e     bne    r2,zero,8158 <compare+0x98>
8148:    e0800117     ldw    r2,4(fp)
814c:    1004c03a     cmpne    r2,r2,zero
8150:    1000011e     bne    r2,zero,8158 <compare+0x98>
8154:    00000206     br    8160 <compare+0xa0>
8158:    00800044     movi    r2,1
815c:    e0800915     stw    r2,36(fp)

is_eq = a==b;
8160:    e0c00017     ldw    r3,0(fp)
8164:    e0800117     ldw    r2,4(fp)
8168:    1885003a     cmpeq    r2,r3,r2
816c:    e0800615     stw    r2,24(fp)
is_ne = a!=b;
8170:    e0c00017     ldw    r3,0(fp)
8174:    e0800117     ldw    r2,4(fp)
8178:    1884c03a     cmpne    r2,r3,r2
817c:    e0800715     stw    r2,28(fp)

a_gt_0 = a>0;
8180:    e0800017     ldw    r2,0(fp)
8184:    10800048     cmpgei    r2,r2,1
8188:    e0800a15     stw    r2,40(fp)
a_eq_0 = a==0;
818c:    e0800017     ldw    r2,0(fp)
8190:    1005003a     cmpeq    r2,r2,zero
8194:    e0800b15     stw    r2,44(fp)
is_a = !!a;
8198:    e0800017     ldw    r2,0(fp)
819c:    1004c03a     cmpne    r2,r2,zero
81a0:    e0800c15     stw    r2,48(fp)
is_not_a = !a;
81a4:    e0800017     ldw    r2,0(fp)
81a8:    1005003a     cmpeq    r2,r2,zero
81ac:    e0800d15     stw    r2,52(fp)

#if defined(DBUG)
alt_printf("\na = %d compared to b = %d\n",a,b);
alt_printf("is_gt %d is_lt %d is_ge %d is_le %d\n",is_gt,is_lt,is_ge,is_le);
alt_printf("is_eq %d is_ne %d is_and %d is_or %d\n",is_eq,is_ne,is_and,is_or);
alt_printf("a_gt_0 %d a_eq_0 %d is_a %d is_not_a %d\n",a_gt_0,a_eq_0,is_a,is_not_a);
#endif

}
81b0:    df000e17     ldw    fp,56(sp)