10.07.2015 Views

Instruction Sets

Instruction Sets

Instruction Sets

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

70 CHAPTER 2 <strong>Instruction</strong> <strong>Sets</strong>Example 2.3 shows how to implement an if statement.Example 2.3Implementing an if statement in ARMWe will use the following if statement as an example:if (a < b) {x = 5;y = c + d;}else x = c – d;The implementation uses two blocks of code, one for the true case and another for the falsecase. A branch may either fall through to the true case or branch to the false case:; compute and test the conditionADR r4,a ; get address for aLDR r0,[r4] ; get value of aADR r4,b ; get address for bLDR r1,[r4] ; get value of bCMP r0, r1 ; compare a < bBGE fblock ; if a >= b, take branch; the true block followsMOV r0,#5 ; generate value for xADR r4,x ; get address for xSTR r0,[r4] ; store value of xADR r4,c ; get address for cLDR r0,[r4] ; get value of cADR r4,d ; get address for dLDR r1,[r4] ; get value of dADD r0,r0,r1 ; compute c + dADR r4,y ; get address for ySTR r0,[r4] ; store value of yB after ; branch around the false block; the false block followsfblock ADR r4,c ; get address for cLDR r0,[r4] ; get value of cADR r4,d ; get address for dLDR r1,[r4] ; get value of dSUB r0,r0,r1 ; compute c – dADR r4,x ; get address for xSTR r0,[r4] ; store value of xafter ... ; code after the if statement

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!