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):
When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).
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 1: 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).
Note 2: This file can be downloaded along with a Makefile in hw4_code.zip.
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; }
Problem 3.56 at the end of Chapter 3 (p. 296).
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 one line 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 same C program for the Fibonacci sequence as on Exam #1:
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