If-Then-Else Statements in a Loop
6-87
Optimizing Assembly Code via Linear Assembly
6.8.2
Translating C Code to Linear Assembly
Example 6–48 shows the linear assembly instructions needed to execute in-
ner loop of the C code in Example 6–47.
Example 6–48. Linear Assembly for If-Then-Else Inner Loop
AND
codeword,mask,cond
; cond = codeword & mask
[cond]MVK
1,cond
; !(!(cond))
CMPEQ
theta,cond,if
; (theta == !(!(cond)))
LDH
*aptr++,ai
; a[i]
[if]
ADD
sum,ai,sum
; sum += a[i]
[!if] SUB
sum,ai,sum
; sum –= a[i]
SHL
mask,1,mask
; mask = mask << 1;
[cntr]ADD
–1,cntr,cntr
; decrement counter
[cntr]B
LOOP
; for LOOP
CMPEQ is used to create IF. The ADD is conditional when IF is nonzero (corre-
sponds to then); the SUB is conditional when IF is 0 (corresponds to else).
A conditional MVK performs the !(!(cond)) C statement. If the result of the
bitwise AND is nonzero, a 1 is written into cond; if the result of the AND is 0,
cond remains at 0.
Optimizing Assembly Code via Linear Assembly