Control Structures CS2253, Owen Kaser
Control Structures ● Implementing familiar HLL control structures: – if-then – if-then-else – while – do..while ● Omit: switch ● See textbook Chapter 8
Basic Mechanism ● Essentially, to disrupt the flow of control you need to set PC (alias R15) to a new value. ● The b command does this ● But so does any other allowable instruction that writes to R15! ● Consider this instruction: add R15, R15, R3 shl 2 Number of instructions skipped ahead depends on R3.
Nesting ● A typical HLL program has nested control structures: if inside of an if , inside of while ... ● We'll look at how to replace a HLL control structure (that might have another control structure within it) by corresponding assembly language. ● The inner control structure can be replaced similarly. ● In the following templates, the first use of newlabel1 … newlabel9 means to generate and use a label that was not already in use. Any subsequent occurrence of, say, newlabel1 means to use that same label.
If Without Else ● Replace if (<condition>) { <body> } by code to test the condition (often using CMP) b<opposite of condition> newlabel1 code for body newlabel1
Example ● a1 is in R1, a2 is in R2 ● Translate if (a1 >= a2) { a1++;} cmp R1, R2 blt xyz0001 ; lt is opposite to >= add R1, R1, #1 ; translation of a1++ xyz0001 ; my new label
ARM Optimization ● If the body doesn't have nested control statements or other statements that set the flags, can have the following code to test the condition code for the body, with every instruction conditional. Eg cmp R1, R2 addge R1, R1, #1 ; add made conditional on >=
If With Else Replace if (<condition>) { <body1>} else {<body2>} with code to test condition b<opposite of condition> newlabel1 code for body1 b newlabel2 newlabel1 code for body2 newlabel2
Example ● if (a1 >= a2) a1++; else a2++; ● Following the template: cmp R1, R2 ; a1 >= a2 ?? blt xyz001 add R1, R1, #1 b xyz002 ; don't fall into else code xyz001 add R2, R2, #1 ; the else's body xyz002
ARM Optimization ● Since the bodies are simple, can use predicated [i.e., conditional] instructions: cmp R1, R2 addge R1, R1, #1 ; the “then” body addlt R2, R2, #1 ; the “else” body ● Look Ma, no labels and no branching. No “branch penalty”.
While Statement ● Recall that a while statement checks the condition before every iteration, including the first. ● while (<cond>) {<body>} can turn into b newlabel1 newlabel2 code for <body> newlabel1 code for <cond> b<the condition> newlabel2 Other translations are possible, but this is the book's
Example ● for (i=0; i<j; i+=2) ++k; ← for is just while disguised. mov R1, #0 ; say R1 stores I b xyz001 xyz002 add R3, R3, #1 ; body: say R3 has k add R1, R1, #2 ; code for i+=2 xyz001 cmp R1, R2 ; say R2 has j blt xyz002
Counting Down To Zero ● If you can arrange for your for loops to count down from N to zero AND if it is guaranteed to do at least one iteration, better to use code like mov R1, #N ; counting down with R1 newlabel1 code for the body of the loop subs R1, R1, #1 ; set the flags bne newlabel1
Do...While Statement Translate do { <body> } while (<cond>); as newlabel1 code for <body> code to check condition b<cond> newlabel1 ● Slightly simpler than the while loop
Nesting ● Let's do Euclid's algo together: while (a != b) if (a>b) a=a-b; else b=b-a;
Conditional Execution ● Using conditional execution, we can reduce Euclid's code to GCD CMP R0, R1 SUBGT R0, R0, R1 SUBLT R1, R1, R0 BNE GCD ● Book also shows how to use conditional execution to handle something like if (x==1 || x==5) ++x
Recommend
More recommend