Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. Analyze the x86-64 assembly given below. ... Q3: subq $16, %rsp movb $0, (%rsp) # The repeated movb instructions are initializing movb $8, 1(%rsp)

3. Analyze the x86-64 assembly given below. ... Q3: subq $16, %rsp movb $0, (%rsp) # The repeated movb instructions are initializing movb $8, 1(%rsp) # an array, relative to %rsp. %rsp points to the movb $4, 2(%rsp) # top of the stack,which grows downward toward movb $12, 3(%rsp) #lower addresses.%rsp stores the address of the movb $2, 4(%rsp) # first array element (lowest address), while movb $10, 5(%rsp) #(%rsp + 15) stores the address of the last movb $6, 6(%rsp) #element (highest address). movb $14, 7(%rsp) movb $1, 8(%rsp) movb $9, 9(%rsp) movb $5, 10(%rsp) movb $13, 11(%rsp) movb $3, 12(%rsp) movb $11, 13(%rsp) movb $7, 14(%rsp) movb $15, 15(%rsp) #Parameter 2 is stored in %rsi. # testq is like cmpq, but uses a bitwise and testq %rsi, %rsi # operation instead of a subtraction. je .L1 movq %rdi, %rax # Parameter 1 is stored in %rdi (an array). addq %rdi, %rsi .L3: movzbl (%rax), %edx # Get a (zero extended) byte from the parameter. movq %rdx, %rcx andl $15, %ecx # The next instruction indexes the local array. movzbl (%rsp,%rcx), %r8d sall $4, %r8d # The instructions (sal, shr) are shift shrb $4, %dl # instructions, like << and >> in C code. movq %rdx, %rcx # There are signed and logical (unsigned) shifts: andl $15, %ecx # sar, sal are "arithmetic" or signedshifts. movl %r8d, %edx #shr, shl are logical shifts. orb (%rsp,%rcx), %dl movb %dl, (%rax) # %dl is the bottom 8 bits of %edx or %rdx. addq $1, %rax cmpq %rsi, %rax jne .L3 .L1: addq $16, %rsp ret ...

Note: this function has been optimized and manipulates the stack directly through %rsp.

a) [20 points] Assume the function is named Q3. Suppose that any parameters are named P1, P2, etc., in the order the parameters would be listed in the C code. Suppose the local variables are called L1, L2, etc., in the order they occur in the assembly code.

Write C code for the function that could have yielded the x86-64 assembly code given above, in the provided .c file. Your final answer must not include any goto statements! Be advised: you may derive a perfectly acceptable answer and find that when you compile it you get slightly different assembly code. Minor differences should be expected, so don't tweak your C code trying to replicate the assembly code exactly.

#include #include

// // // // void Q3(uint8_t * P1, size_t P2) { // Reverse engineered code here. }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago