Assembly Language for Beginners

(nextflipdebug2) #1

1.15 switch()/case/default


Listing 1.146: Check for less than (unsigned)

SLTU REG1, REG2, REG3
BEQ REG1, label


Branchless


If the body of a condition statement is very short, the conditional move instruction can be used:MOVccin
ARM (in ARM mode),CSELin ARM64,CMOVccin x86.


ARM


It’s possible to use conditional suffixes in ARM mode for some instructions:


Listing 1.147: ARM (ARM mode)

CMP register, register/value
instr1_cc ; some instruction will be executed if condition code is true
instr2_cc ; some other instruction will be executed if other condition code is true
... etc...


Of course, there is no limit for the number of instructions with conditional code suffixes, as long as the
CPU flags are not modified by any of them.


ThumbmodehastheITinstruction, allowingtoaddconditionalsuffixestothenextfourinstructions. Read
more about it:1.19.7 on page 263.


Listing 1.148: ARM (Thumb mode)

CMP register, register/value
ITEEE EQ ; set these suffixes: if-then-else-else-else
instr1 ; instruction will be executed if condition is true
instr2 ; instruction will be executed if condition is false
instr3 ; instruction will be executed if condition is false
instr4 ; instruction will be executed if condition is false


1.14.6 Exercise


(ARM64) Try rewriting the code in listing.1.128by removing all conditional jump instructions and using the
CSELinstruction.


1.15 switch()/case/default


1.15.1 Small number of cases.


#include <stdio.h>


void f (int a)
{
switch (a)
{
case 0: printf ("zero\n"); break;
case 1: printf ("one\n"); break;
case 2: printf ("two\n"); break;
default: printf ("something unknown\n"); break;
};
};


int main()
{

Free download pdf