You know the following assembly code snippet is from a recursive function in C. You
also know that the stack contents at a particular point in time when we are in the
recursive function are shown on the next page. Answer the following questions: a) how
many Foo stack frames are on the stack? b) what is the return address back to the
function that called Foo for the first time? c) what is the return address back into the
function Foo? d) how many bytes is in one instance of the Foo stack frame? (be sure to
exaplain your answer). Recall the jg instruction jumps if the result is greater than 0. (20
points, 5 points each for a-d)
Foo:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
cmpl $1, -4(%rbp)
jg .L2
movl $10, %eax
jmp .L3
.L2:
movq -16(%rbp), %rax
movq %rax, %rdx
sarq $2, %rdx
movl -4(%rbp), %eax
subl $2, %eax
movq %rdx, %rsi
movl %eax, %edi
call Foo
movq -16(%rbp), %rdx
sarq $2, %rdx
addq %rdx, %rax
addq %rax, -16(%rbp)
movq -16(%rbp), %rax
.L3:
leave
ret
(gdb) x/30x $rsp
0x7fffffffe780: 0x00007fffffffe848 0x0000000000000400
0x7fffffffe790: 0x00007fffffffe7b0 0x00000000004005d3
0x7fffffffe7a0: 0x0000000000000000 0x00000002f7de8c95
0x7fffffffe7b0: 0x00007fffffffe7d0 0x00000000004005d3
0x7fffffffe7c0: 0x0000000000000000 0x00000004f7a17190
0x7fffffffe7d0: 0x00007fffffffe7f0 0x00000000004005d3
0x7fffffffe7e0: 0x0000000000000000 0x00000006f7def725
0x7fffffffe7f0: 0x00007fffffffe810 0x00000000004005d3
0x7fffffffe800: 0x0000000000000002 0x00000008ffffe844
0x7fffffffe810: 0x00007fffffffe830 0x00000000004005d3
0x7fffffffe820: 0x000000000000000a 0x0000000a00000000
0x7fffffffe830: 0x00007fffffffe850 0x000000000040062a
0x7fffffffe840: 0x0000000affffe930 0x000000000000000a
0x7fffffffe850: 0x0000000000000000 0x00007ffff7a32ea5
0x7fffffffe860: 0x0000000000000000 0x00007fffffffe938
You know the following assembly code snippet is from a recursive function in C. You also...
Write the C Code for the Intel assembley below. Note: Don't copy and paste anything from github because it won't be the right answer. .file "mystery.c" .comm num,1600,32 .text .globl add .type add, @function add: .LFB2: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movq %rdi, -8(%rbp) movq %rsi, -16(%rbp) movq -16(%rbp), %rax movq -8(%rbp), %rdx addq %rdx, %rax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE2: .size add, .-add .globl dothething .type dothething, @function...
Consider the following source code, where b, c, and d are constants declared with #define. You will need to determine the values for b, c, and d. struct lnode { char *str; struct lnode *next; } struct lnode A[b][c][d]; int store_ele(int h, int i, int j, struct lnode dest) { A[h][i][j] = dest; return sizeof(A);} On compiling this program (with -O2), GCC generates the following assembly code for the store_ele function: store_ele: movslq %edi, %rdi movslq %esi, %rsi movslq %edx,...
May you explain how you got the answer The following code sequence occurs near the beginning of the assembly code generated by gcc for a c procedure: 9. 1, subq 2" movq 3" movq 4" movq 5. movq 6. movq 7. movq 8. movq 9. movq 10. movq $24,%rsp %rbx, (%rsp) %r12, 8(%rsp) %r13, 16(%rsp) 16(%rbp), %rbx 24(%rbp), %r13 (%rbx), %r12 (%r13), %rax 16(%rbp ) ,%rdx (%rdx), %rcx we see that just three registers (%rbx, %r12 and %r13) are saved...
Problem 2 Consider the following source code, where R, S, and T are constants declared with #define. long int AIRISJIT int store_ele(int h, int i, int j, long int 'dest) Alh][i]01-.dest; return sizeof(A) In compiling this program, GCC generates the following assembly code (with-02) store_ ele: movslq %esi, %rsi movslq %edi, %rdi movq (%rcx), %rax leaq (%rdi,%rdi,4), %rdi leaq (%rsi,%rsi,4), %rcx movslq %edx, %rdx addq %rcx, %rdx movq %rax, A( %rdx,8) movl $1120, %eax ret Create a text file to...
And also when recursive(5). Consider the following funtion int recursive(int n) f The assembly code equivalent of the above function is: recursive push %ebp mov %esp,%ebp push %ebx sub $0x14,%esp cmpl $0x1,0x8(%ebp) je L1 cmpl $0x2,0x8(%ebp) jne L2 L1 mov 0x8 (%ebp),%eax jmp L3 L2 mov 0x8 (%ebp),%eax sub $0x1,%eax mov %eax, (%esp call recursive mov %eax,%ebx mov ox8(%ebp),%eax sub $0x2,%eax mov %eax, (%esp call recursive imul %ebx,%eax L3 add $0x14,%esp pop %ebx pop %ebp ret
The following problem concerns the following, low-quality code: void foo(int x) { int a[3]; char buf[1]; a[1] = x; a[2] = 0xA0B1C2D3; gets(buf); printf("a[0] = 0x%x, a[2] = 0x%x, buf = %s\n", a[0], a[2], buf); } In a program containing this code, procedure foo has the following disassembled form on an x86/64 machine: 000000000040057d <foo>: 40057d: push %rbp 40057e: mov %rsp,%rbp 400581: sub $0x30,%rsp 400585: mov %edi,-0x24(%rbp) 400588: mov -0x24(%rbp),%eax 40058b: mov %eax,-0xc(%rbp) 40058e: movl $0xa0b1c2d3,-0x8(%rbp) 400595: lea -0x11(%rbp),%rax 400599:...
Binary Bomb phase 4 Dump of assembler code for function phase_4: > 0x0000000000400fe7 <+0>: sub $0x18,%rsp 0x0000000000400feb <+4>: lea 0x8(%rsp),%rcx 0x0000000000400ff0 <+9>: lea 0xc(%rsp),%rdx 0x0000000000400ff5 <+14>: mov $0x40290d,%esi 0x0000000000400ffa <+19>: mov $0x0,%eax 0x0000000000400fff <+24>: callq 0x400c00 <__isoc99_sscanf@plt> 0x0000000000401004 <+29>: cmp $0x2,%eax 0x0000000000401007 <+32>: jne 0x401010 <phase_4+41> 0x0000000000401009 <+34>: cmpl $0xe,0xc(%rsp) 0x000000000040100e <+39>: jbe 0x401015 <phase_4+46> 0x0000000000401010 <+41>: callq 0x401662 <explode_bomb> 0x0000000000401015 <+46>: mov $0xe,%edx 0x000000000040101a <+51>: mov $0x0,%esi...
a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. Note: You cannot change the algorithm in any way so your assembly function must still be recursive. (20 points) long Catalan(long n) { long sum = 0; if (n == 0) return 1; for (int i = 0; i < n; i++) { sum += Catalan(i) * Catalan(n - i - 1); } return sum; } b) The...
a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. long fibonacci (long n) { if (n == 0) return 0; else if (n == 1) return 1; else return (fibonacci (n - 1) + fibonacci (n - 2)); } b) The Windows x86-64 calling convention passes function parameters in the registers RCX, RDX, R8 and R9 and returns values on register RAX. Caller saved registers are: RAX,...