there are 3 questions in file that I attached.
3/20/2020 Lctr 18 for CEC 320 p. 1
4.1.4 The if-then-else construct
Note: due to the existence of the compound OR expression, the conditional execution
approach (Asm Prog 2 above) is not so advantageous. Hence, we will only use the
conditional branch approach.
The if-then-else construct with compound OR expression
3/20/2020 Lctr 18 for CEC 320 p. 2
4.2 The for loop to master in both C and assembly
Note:
1. The condition_expression is tested before the block of the intended code is executed,
which means that the block of code may not have a chance to run. We need to
update the loop-counting variable in each pass after executing the block of code.
2. We prefer to use Approach 2 due to its clean structure. Yet, Approach 1 is slightly
more efficient.
3. Approach 1 is similar to the do-while loop we will see soon.
3/20/2020 Lctr 18 for CEC 320 p. 3
4.3 The while loop to master in both C and assembly
Note: the condition_expression for the while loop is also tested before executing the
block of intended code, in the same way as the for loop. We also need to update the
loop-counting variable in each pass after executing the block of the intended code.
We can see from Tasks 5 and 7 of 170_conditional_execution that the implementation of
for loop and a while loop can be exactly the same in assembly.
3/20/2020 Lctr 18 for CEC 320 p. 4
4.4 The do-while loop to master in both C and assembly
Note: the condition_expression is tested after executing the block of intended code, which
means that the block of code will be executed at least once.
The do-while loop
The while loop
Note that the assembly code for the do-while loop is slightly more compact/efficient than that
of the while loop.
3/20/2020 Lctr 18 for CEC 320 p. 5
4.5 The continue and break statements to master in C and to know in assembly
The continue and break statements affect the flow of a loop in different ways:
• Continue = jump to the end of the loop and continue the next pass of the loop
• Break = break (jump out of) the loop
3/20/2020 Lctr 18 for CEC 320 p. 6
3/20/2020 Lctr 18 for CEC 320 p. 7
4.6 The switch statement to master in C
The switch statement can be seen as a special case of the if-then-else statement, with
the following caution:
The ARM Cortex-M processors use a special Branch Table to handle the addresses of the case blocks.
See the textbook for details. We just need to be informed of the BranchTable approach; no need to
master them at this stage of learning. As will be seen in a later chapter, we can always call C code,
such as the switch statement, in the assembly code.
3/20/2020 Lctr 18 for CEC 320 p. 8
4.7 The IT (If-Then) instruction to know in assembly
The IT instruction can handle a block of code of “then” and “else”. The number of
instructions in the block is at most 4 using the following syntax:
Notes:
* While the IT construct is important for the GNU assembly, it is not as important for the ARM
assembly—the latter can add this construct automatically.
* The IT version of the instructions may be more compact than the non-IT version. See the example
of ADD in line 196 and ADDEQ in line 202 above.
1. (30 points total, 5 points each) We have learned in the class (see lecture 18) that using the barrel shifter, we can implement
multiplication/division of a number for some special cases with addition, subtraction, and reversed subtraction. Write the
assembly code to perform the following calculations using this approach assuming signed integers A and B are saved in r0 and
r1, respectively.
a. A=16*B
b. A=17*B
c. A=15*B
d. A=B/16
e. A=17*B/16
f. A=15*B/16
2. (70 points) We have learned the following in the class:
1. LDR with immediate offset:
2. LDR with pre-indexed offset:
3. LDR with post-indexed offset:
4. LDR with register offset:
LDR Rt, [Rn, #offset]
LDR Rt, [Rn, #offset]!
LDR Rt, [Rn], #offset
LDR Rt, [Rn, Rm, shift]
Now, we practice these instructions here with the given program snippets below: Part A: C code:
// Functions defined in a .s file
extern void task1(uint8_t *pIpt, uint32_t *pOup);
extern void task2(uint8_t *pIpt, uint32_t *pOup);
extern void task3(uint8_t *pIpt, uint32_t *pOup);
extern void task4(uint8_t *pIpt, uint32_t *pOup, int i);
// Global variable
uint8_t ipt[16]; // Input data
uint32_t opt[4]; // Output data
int main(void) {
for (int i = 0; i < 16; i++) {
ipt[i] = i<<4;
}
// Task 1.
task1(ipt, opt);
printf(“Out1 = 0x%X, Out2 = 0x%X\n”, opt[0], opt[1]);
// Task 2.
task2(ipt, opt);
printf(“Out1 = 0x%X, Out2 = 0x%X\n”, opt[0], opt[1]);
// Task 3.
task3(ipt, opt);
printf(“Out1 = 0x%X, Out2 = 0x%X\n”, opt[0], opt[1]);
// Task 4.
task4(ipt, opt, 1);
printf(“Out1 = 0x%X, Out2 = 0x%X\n”, opt[0], opt[1]);
while (1);
}
Part B: asm code
EXPORT task1
EXPORT task2
EXPORT task3
EXPORT task4
ALIGN ; Align the data in the boundary of 4 bytes.
task1 PROC
LDR r2, [r0, #4]
STR r2, [r1, #0]
LDR r2, [r0, #0xC]
STR r2, [r1, #4]
BX lr
ENDP
task2 PROC
LDR r2, [r0, #4]!
STR r2, [r1, #0]
LDR r2, [r0, #4]!
STR r2, [r1, #4]
BX lr
ENDP
task3 PROC
LDR r2, [r0], #4
STR r2, [r1, #0]
LDR r2, [r0], #4
STR r2, [r1, #4]
BX lr
ENDP
task4 PROC
LDR r3, [r0, r2, LSL #2]
STR r3, [r1, #0]
ADD r2, #1
LDR r3, [r0, r2, LSL #2]
STR r3, [r1, #4]
ADD r2, #1
BX lr
ENDP
Answer the following questions (without running the program first):
a. (10 points) What will be the print out after Task 1?
b. (15 points) What will be the print out after Task 2 and what will be the values in r0 and r1 in hexadecimal after running
Task 2 assuming their values are 0x20001010 and 0x20001040, respectively when the function is called?
c. (15 points) What will be the print out after Task 3 and what will be the values in r0 and r1 in hexadecimal after
running Task 3 assuming their values are 0x20001010 and 0x20001040, respectively when the function is called?
d. (15 points) What will be the print out after Task 4 and what will be the values in r2 and r3 in hexadecimal after running
Task 4?
e. (15 points) What will be the corresponding C code for each task?
3. (20 points) We have learned in the class that for signed numbers, we have the following mapping between the
relationship testing and condition flags (page 5 in lecture notes for lctr160) :
Determine the mapping between the relationship testing and condition flags for unsigned numbers.