Topic: The Intel x86 ISA, x86 Assembly Programming, Conditionals (if-then-else), and Loops (while, for, and do-while)
Related Reading: Chapter 3 and class notes
Practice problems from the textbook (answers are at the end of the chapter):
You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not accept these emails; i.e. the instructor may not receive your email.)
Given the following C code:
if (a - 3 > b) c = a + 7; else if (b > (c & 7)) c = b >> 3; else c = -1;
Given the equivalent Intel x86 assembly code.
Assume that: variables a, b, and c are in registers %eax, %ebx, and %ecx, respectively.
Given the following assembly program, hw4_prob2.s, determine the equivalent C code corresponding to the procedure hw4_prob2(), and fill in the blanks below:
Note: These blanks may include assignment statements and/or other conditional (if-then-else) statements. Also, you may not need to use all the blanks (in the conditional's true and false code bodies).
int hw4_prob2 (int x, int y) { int result = ________________________________________; if (( x ___________________________) || ( ___________________________)) { _________________________________________________; // one or more of these lines will start with: result = _________________________________________________; _________________________________________________; _________________________________________________; _________________________________________________; } else { _________________________________________________; // one or more of these lines will start with: result = _________________________________________________; _________________________________________________; _________________________________________________; _________________________________________________; } return result; }
Consider the following assembly code: Note: x at %ebp+8, n at %ebp+12 1 movl 8(%ebp), %esi 2 movl 12(%ebp), %ebx 3 movl $-1, %edi 4 movl $1, %edx 5 .L2: 6 movl %edx, %eax 7 andl %esi, %eax 8 xorl %eax, %edi 9 movl %ebx, %ecx 10 sall %cl, %edx 11 testl %edx, %edx 12 jne .L2 13 movl %edi, %eax The preceding code was generated by compiling C code that had the following overall form: 1 int loop(int x, int n) 2 { 3 int result = __________; 4 int mask; 5 for (mask = __________; mask __________; mask = __________) { 6 result ^= __________; 7 } 8 return result; 9 } Your task is to fill in the missing parts of the C code to get a program equivalent to the generated assembly code. Recall that the result of the function is returned in register %eax. You will find it helpful to examine the assembly code before, during, and after the loop to form a consistent mapping between the registers and the program variables. A. Which registers hold program values x, n, result, and mask? B. What are the initial values of result and mask? C. What is the test condition for mask? D. How does mask get updated? E. How does result get updated? F. Fill in all the missing parts of the C code.
Given the following x86 assembly code that sums the numbers from 1 to N:
movl $0, %eax # %eax: sum = 0 movl $1, %ecx # %ecx: i = 1 LOOP: addl %ecx, %eax addl $1, %ecx cmpl %edi, %ecx jle LOOP LOOP_EXIT: ...
Note: The variables of i, N, and sum are stored in registers %ecx, %edi, and %eax, respectively.
What one line would you need to add and/or change in order to sum the following number sequence?
1 4 7 10 13 16 19 22 25 28 ...
What two lines (of the original code) would you need to add and/or change in order to compute the factorial of N?
What lines (of the original code) would you need to add and/or change in order to compute the sum of squares from 1 to N?
i.e. 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + ...
= 1 + 4 + 9 + 16 + 25 + ...
Add a conditional to the loop that checks if the number is even or odd, and negates the value if it's odd. In essence, this will generate the sum: -1 + 2 - 3 + 4 - 5 + 6 - 7 + 8 - 9 + ...
Hint: What one bit in a binary integer representation indicates whether the number is even or odd? And what bit-wise Boolean operation can you use to isolate that number?
Using the following C program for the Fibonacci sequence:
a = 0; // F(0) b = 1; // F(1) for (i = 2; i < n; i++) { c = b + a; // F(i) a = b; b = c; }
Give the equivalent sequence of assembly instructions for computing the Nth value of the Fibonacci sequence