程序代写代做代考 C assembly Recitation 11: Assembly and Loops Date: (11-12-2020 Thursday)

Recitation 11: Assembly and Loops Date: (11-12-2020 Thursday)
The following x86-64 “cheat sheet” from Brown may come in handy if you do not have the textbook with you to lookup individual instructions and registers.
If you are having trouble getting started, this video may give you some hints in how to read assembly and reverse engineer the C code from which it came from: Assembly to C Example by Chris Gregg
Try to complete at least two questions in the class. If you are not able to complete in the class you will have to consider it as a part of homework. Submit answers for all the three questions on NYU classes by Wednesday 17 Nov. Start the assignment well in advance. No extensions for the submission will be given.
Problem 1
Consider the following assembly code: # long loop(long x, int n)
# xin%rdi,nin%esi
loop:
movl %esi, %ecx movl $1, %edx movl $0, %eax jmp .L2
.L3:
movq %rdi, %r8 andq %rdx, %r8 orq %r8, %rax salq %cl, %rdx
.L2:
testq %rdx, %rdx
jne .L3 rep; ret
The preceding code was generated by compiling C code that had the following overall form:

long loop(long x, long n) {
long result = ____________; long mask;
for (mask = _____________; mask ____________; mask = ____________) {
result |= ____________; }
return result; }
Your task is to fill in the missing parts of the C code to get a program equivalent to the generated assembly code. Create a file called problem1.c with your complete version of the above loop function.
You should be able to compile your code to assembly and determine if the results are equivalent to the assembly instructions above.

Problem 2
Consider the following assembly instructions: Main:
.L3:
.L2:
leal
testl
jg .L3 movl popq
ret
pushq movl jmp .L2
%rbx
$100, %ebx
(%rbx,%rbx), %esi $.LC0, %edi
$0, %eax printf
$10, %ebx
%ebx, %ebx
$0, %eax %rbx
movl movl call subl
Note, $.LCO is an address of the string constant defined as .LC0:
.string “%d\n”
It is the string with the format specifier that is passed to printf as its first argument.
Figure out the missing parts of the program in C:
int main () {
int x = _______________________; while ( _______________________ ) {
printf(“%d\n”, ____________ );

x = _______________________; }
}
Create a file called problem2.c with your answer.

Problem 3
Here are the assembly instructions for the functions that is missing in the C code below. Figure out the implementation of the function. The name of the functions has been XXXX-ed out on purpose
XXXXXXXX: jmp .L2
.L3: .L2:
.L5:
ret
.L6:
ret
subl
cmpl
jg .L3 jl .L5 cmpl
je .L6 rep ret
%esi, %edi %esi, %edi
%esi, %edi
%edi, %eax $0, %eax
movl
movl
The C program is as follows:
#include #include
int XXXXXXXX ( int val, int div ) { ???
}
int main () {
printf ( “%d\n”, XXXXXXXX(17,3) ); printf ( “%d\n”, XXXXXXXX(21,7) );

}
Create a file called problem3.c with your answer.