Making choice in a computer program
 

Assembler program uses 2 instructions to make decisions:

  1. The cmp (= compare) instruction:

    • compares (= subtracts) 2 operands                 and

    • updates the N, Z, V, C flags according to result of the comparison

     

  2. The conditional branch instructions (plural !):

    • uses the settings of the N, Z, V, C flags to decide if the branch should be "taken"   or   "not taken".

Note:   "taking a branch" means "jump"

The cmp (compare) assembler instruction

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


cmp rN, rM // Compare the values in rN and rM Computes the difference rN − rM and sets the status flags (N,Z,V,C) according to the outcome of the subtraction
 

Notes:

  1. The result of x − y can tell you which value is larger or smaller !

  2. The cmp instruction does not update the any registers (only updates the flags)

The cmp (compare) assembler instruction - Example 1

Given: int x.
How to compare     x with 0:

   .text

   // Compare x with 0



 
  


 

   ....

   .data
x: .skip 4
   

The cmp (compare) assembler instruction - Example 1

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
   

The cmp (compare) assembler instruction - Example 1

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
   

The cmp (compare) assembler instruction - Example 2

Given: short x and byte y
How to compare     x with y:

   .text
   // Compare x with y


  
  
 

  

  
 

  
  

   .data
x: .skip 2
y: .skip 1  

The cmp (compare) assembler instruction - Example 2

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  

The cmp (compare) assembler instruction - Example 2

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  

The cmp (compare) assembler instruction - Example 2

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  

The conditional branch assembler instructions (plural !)
 

  • There are 16 different conditional branch instructions in the ARM assembler language

  • 6 (six) of the conditional branch instructions are used to compare signed integer values (i.e.: 2s complement codes)

  • We will only use:

    1. beq label   (branch to label when equal)
    2. bne label   (branch to label when not equal)
    3. blt label   (branch to label when less than)
    4. ble label   (branch to label when less than or equal)
    5. bgt label   (branch to label when greater than)
    6. bge label   (branch to label when greater than or equal)      

All conditional branch instruction have the same syntax
I will use   blt   to explain the conditional branch instructions

The meaning of the blt instruction
 

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 program to show the effect of the blt 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
   

Be careful when you use cmp rN, rM

Make sure you use the correct ordering of the registers when you use cmp rN, rM

  • This sequence of assembler code:

        cmp  r0, r1              
        blt  there
      

    will branch to the label there if r0 < r1

  • This sequence of assembler code:

        cmp  r1, r0              
        blt  there
      

    will branch to the label there if r1 < r0 (or: r0 > r1)