Assembler program uses 2 instructions to make decisions:
|
Note: "taking a branch" means "jump"
Syntax and meaning of the cmp instruction:
cmp rN, #n // Compare the value in rN with a constant Computes the difference rN − n and sets the status flags (N,Z,V,C) according to the outcome of the subtraction |
Notes:
|
Given:
int x.
How to
compare
x with 0:
.text // Compare x with 0 .... .data x: .skip 4 |
Given:
int x.
How to
compare
x with 0:
.text // Compare x with 0 // Preparation: // r0 = x // Compare the value x (in r0) with the value 0 cmp r0, #0 // Compare x with 0 .... .data x: .skip 4 |
Given:
int x.
How to
compare
x with 0:
.text // Compare x with 0 // Get the value x into register R0 movw r0, #:lower16:x movt r0, #:upper16:x ldr r0, [r0] // r0 = x // Compare the value x (in r0) with the value 0 cmp r0, #0 // Compare x with 0 .... .data x: .skip 4 |
Given:
short x and
byte y
How to
compare
x with y:
.text // Compare x with y .data x: .skip 2 y: .skip 1 |
Given:
short x and
byte y
How to
compare
x with y:
.text // Compare x with y // Preparations: // r0 = x // r1 = y // Compare the value x (in r0) against the value y (in r1) cmp r0, r1 // Compare x with y .data x: .skip 2 y: .skip 1 |
Given:
short x and
byte y
How to
compare
x with y:
.text // Compare x with y // Get the value x into register R0 movw r0, #:lower16:x movt r0, #:upper16:x ldrsh r0, [r0] // r0 = x // Preparation: // r1 = y // Compare the value x (in r0) against the value y (in r1) cmp r0, r1 // Compare x with y .data x: .skip 2 y: .skip 1 |
Given:
short x and
byte y
How to
compare
x with y:
.text // Compare x with y // Get the value x into register R0 movw r0, #:lower16:x movt r0, #:upper16:x ldrsh r0, [r0] // r0 = x // Get the value y into register R1 movw r1, #:lower16:y movt r1, #:upper16:y ldrsb r1, [r1] // r1 = y // Compare the value x (in r0) against the value y (in r1) cmp r0, r1 // Compare x with y .data x: .skip 2 y: .skip 1 |
|
All conditional branch
instruction have the
same syntax
I will use
blt to
explain
the
conditional branch
instructions
The conditional branch instruction always follows a cmp instruction:
cmp rN, x // x = a number #n or a register rM blt label next instruction |
The effect of this construct is:
if ( rN < x ) // x = a number #n or a register rM
{
b label
}
next instruction
|
Note:
if the
condition is
not satisfied,
the
blt label instruction
will
do nothing !!
(I.e.: the program will
continue with
the
next instruction
that follows the
blt label instruction)
DEMO: /home/cs255001/demo/asm/6-if/blt.s
mov r0, #5 // r0 = 5 - try changing this to 15 // Compare r0 against 10 and branch when less than cmp r0, #10 blt there // Branch to label "there" if r0 < 10 mov r1, #4444 mov r2, #4444 mov r3, #4444 there: mov r4, #4444 mov r5, #4444 mov r6, #4444 |
Make sure you use the correct ordering of the registers when you use cmp rN, rM
|