The key to perform comparison is the subtract instruction (the version that update the flags, i.e. subcc
).
You can specify %r0
as destination operand since the result of such subtraction is not needed (taking advantage of the ARC three operands form).
Suppose you do this
subcc %r1, %r2, %r0
The following are true
- The result is discarded as it is written in
r0
- If the
C
flag is set then r2 > r1
- If the
C
flag is not set then r2 <= r1
- If the
Z
flag is set then r2 == r1
- If the
Z
flag is not set then r2 !=r1
I have never written ARC assembly code. Here is an attempt to translate the given code, I cannot debug or even assemble this code. So consider it as a concise way to explain the concept above.
! This program executes an If/else statement
.begin
.org 2048
prog1: ld [X], %r1 ! this loads memory X into register 1
ld [Y], %r2 ! this loads memory y into register 2
ld [Z], %r3 ! this loads memory z into register 3
subcc %r2, %r1, %r0 !y ? x
bcs .else_if ;x > y
add %r2, 1, %r2 !y = y + 1
ba .end
.else_if:
subcc %r1, %r3, $r0 ;x ? z
beq .else ;x == z
sub %r2, 1, %r2
ba .end
.else:
add %r3, 1, %r3 !z = z + 1
ba .end
X: 0
Y: 0
Z: 0
.end
Indeed I hope this is not a working version since it will at least force you into some thinking-learning process in the attempt to correct it :)