Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I convert this to C code? Thank you! Analyze the x86-64 assembly given b

ID: 3824390 • Letter: H

Question

How can I convert this to C code? Thank you!

Analyze the x86-64 assembly given below. ... Q1 pushq %rbp movq %rsp, %rbp subq $40, %rsp movq %rdi, -40(%rbp) cmpq $1, -40(%rbp) ja .L2 movl $1, %eax jmp .L3 .L2: movq $1, -8(%rbp) movq $1, -16(%rbp) movq $2, -32(%rbp) jmp .L4 .L5: movq -8(%rbp), %rax movq -16(%rbp), %rdx addq %rdx, %rax movq %rax, -24(%rbp), %rax movq -16(% rbp), %rax, movq %rax, -8 (%rbp) movq -24(%rbp), %rax movq %rax, -16(%rbp) addq $1, -32(%rbp) .L4: movq -32(%rbp), %rax cmpq -40(%rbp), %rax jbe .L5 movq -24(%rbp), %rax .L3: leave ret ... Assume the function is named Q1. 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 stack frame for the function, from high addresses to low addresses.

Explanation / Answer

For that, you have to known about the working of this code. How this language will work and how in the output language, we can write the code.

Now first try to understand the complete process of this written code in assembly language.

1. Such as Movq and subq, these are the commands used for subtraction and initialize the variables.

So you can easily convert it into according to the by applying direct operators in C such as '=' and '-'.

2. If some label is given like L2, L3. This is basically executed after making a Jump instructions. So this we can resolve it by if else and for or while loop as per the requirement of the program. If there is simple condition jump so you can use if else. if there are continuous jump till the condition will not satisfied then we can use while or for loop.

So it's simple, first understand the assembly code and then convert it into C code by applying loops and operators to perform the operations.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote