1) Consider the following source code, where R, S, and T are constants declared with #define: long A[R][S][T];
long store_ele(long i, long j, long k, long *to)
*to = A[i][j][k];
return sizeof(A);
Copyright By PowCoder代写 加微信 powcoder
In compiling this program, gcc generates the following assembly code:
long store_ele(long i, long j, long k, long *to)
i in %rdi, j in %rsi, k in %rdx, to in %rcx 1 store_ele:
2 leaq (%rsi,%rsi,2), %rax
3 leaq (%rsi,%rax,4), %rax
4 movq %rdi, %rsi
5 salq $6, %rsi
6 addq %rsi, %rdi
7 addq %rax, %rdi
8 addq %rdi, %rdx
9 movq A(,%rdx,8), %rax
10 movq %rax, %(rcx)
11 movl $3640, %eax
%rax = j+2j=3j
%rax = j+4(3j)=13j %rsi=i
%rsi = i*2^6=64i
%rdi = i+64i = 65i %rdi=65i+13j %rdx=k+(65i+13j) %rax = A[8(k+65i+13j)] %(rcx) = A[i][j][k] R*S*T*8=3640
A. Which register is used to pass each one of the arguments? It is in the header of the assembly code.
B. Use your reverse engineering skills to determine the values of R, S, and T based on this assembly code. Write your answer here:
This is what each line will execute: 2 – %rax = 3j
3 – %rax = j + 4*3j = 13j
4 – %rsi = i
5 – %rsi = 64i 6 – %rdi = 65i
7 – %rdi = 65i +13j
8 – %rdx = k + 65i +13j
9 – %rdx = A[8(65i + 13j + k)] 10 – %rax = R*S*T = 3640
From the above statements, we know that T=13. We also know that S*T=65, therefore S=5. Finally, we know that R*S*T*8=3640, therefore R=7
Page 2 of 4
2) Consider the following assembly code:
%rax = 0 %rdx = 0
if (%rax < %rdi) continue (%rax - %rdi) ZF = 0, SF = 1 if (%rax > %rdi) go_to end(.L4)
%rdx=%rdx+2
%rax=%rax+1
“StudyGuide.c”
my_proc, @function
$0, %eax $0, %edx
%edi, %eax .L4
$2, %edx $1, %eax .L2
.file .text .globl .type
my_proc: .LFB23:
cmpl jge addl addl jmp
for (i=0; i <
movl %edx, %eax %rax = %rdx ret
.size my_proc, .-my_proc
.string "The sum is %d.\n\n" .text
.globl main
.type main, @function
main: .LFB24:
subq $8, %rsp
movl $5, %edi
call my_proc
movl %eax, %edx
leaq .LC0(%rip), %rsi movl $1, %edi
movl $0, %eax
call movl $0, %eax
addq $8, %rsp .cfi_def_cfa_offset 8
.size main, .-main
.ident "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0" .section
.section .note.gnu.property,"a"
go_to .L2 parameter; i++) {val = val + 2;} return val;
Page 3 of 4
.long 1f-0f .long 4f-1f .long 5
.string "GNU"
.long 0xc0000002
.long 3f-2f 2:
.long 0x3 3:
.align 8 4:
Write the code for the procedure my_proc.
int ret = 0;
for (int i=0; i < times; i++) {
ret += 2; }
return ret;
The return of my_proc (%rdx).
What is printed by function printf?
Page 4 of 4
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com