Arrays arrays in c are little more than a shorthand

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: will ignore it in this general discussion.) The 'if and 'else' sequences may be a few instructions long with the same condition on each instruction (provided none of the conditionally executed instructions changes the condition codes), but beyond two or three instructions it is generally better to fall back on the more conventional solution: CMP r0, rl ; if (a>b)... BLE MOV ELSE r2, r0 ; skip clause if false ; ..c=a.. ELSE ENDIF B MOV ENDIF r2, rl ; skip else clause ; ...else c=b Here the 'if and 'else' sequences may be any length and may use the condition codes freely (including, for example, for nested if statements) since they are not required beyond the branch immediately following the compare instruction. Conditional statements 171 Note, however, that whichever branch is taken, this second code sequence will take approximately twice as long to execute as the first for this simple example. Branches are expensive on the ARM, so the absence of them from the first sequence makes it very efficient. switches A switch, or case, statement extends the two-way decision of an if...else statement to many ways. The standard C form of a switch statement is: switch (expression) { case constant-expression]: statements; case constant-expressio^: statementS2 case constant-expression^: statements^ default: statements^ } Normally each group of statements ends with a 'break' (or a 'return') to cause the switch statement to terminate, otherwise the C semantics cause execution to fall through to the next group of statements. A switch statement with 'breaks' can always be translated into an equivalent series of if..else statements: temp = expression; if (temp==constant-expressionj) {statements]} else ... else if (temp==constant-expressionN) {statements^} else {statementsj)} However this can result in slow code if the switch statement has many cases. An alternative is to use a jump table. In its simplest form a jump table contains a target address for each possible value of the switch expression: Clearly it is not possible for the jump table to contain an address for every possible value of a 32-bit integer, and equally clearly it is vital th...
View Full Document

This document was uploaded on 10/30/2011 for the course CSE 378 380 at SUNY Buffalo.

Ask a homework question - tutors are online