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

Can you explain the following c code and how it matches up with the assembly cod

ID: 3890765 • Letter: C

Question

Can you explain the following c code and how it matches up with the assembly code on the left? I want to be able to understand the assembly code on the left.

mysteryFunction push %rbp mov %rsp , %rbp mov %edi ,-0x14 (Zrbp) mov %esi , %eax mov %al ,-0x18 (%rbp) mov! $0x0 ,-0x4 (%rbp) movw $0x1,-0x6(/rbp) [jmp L5 C Code int mysteryFunction (int x,char c) signed int result = 0: unsigned short int ; char bl5] for (; i= 0){ resul-resul i else result = result 2 movsbl -0x18(%rbp) ,%eax [sub] $0x61,/ea:x test %eax , %eax [js] L3 movzw1-0x6 (%rbp), %eax movzw1-0x6 (%rbp), %eax [inul] %eax , %eax add %eax ,-0x4 (%rbp) jmp L4 bi--re sult a'; return result 3 movzw1-0x6 (%rbp), %eax add %eax,-0x4([Zrbp]) jmp L4 4 movzw1-0x6 (%rbp), %eax [lea] -ox 1 (%rax) ,%ecx mov add $0x61 , [%eax] mov %eax , %edx movs1q %ecx.xrax mov %dl ,-0x10( [Zrbp] ,Arax , 1) movzw1-0x6 (%rbp), %eax [add] $0x1,%eax mov %ax,-0x6 (%rbp) jmp L5 -0x4 (%rbp) ,%eax .L5 movzwi cmp [jle] mov pop retq -0x6 (%rbp) ,%eax -Ox 14 (%rbp) ,%eax L2 -0x4 (Zrbp) ,%eax rbp

Explanation / Answer

mysteryfunction:
push    %rbp                       # push value to stack
mov    %rsp, %rbp
mov       %edi, -0x14(%rbp)
mov    %esi, %eax
mov       %al, -0x18(%rbp)
mov1   $0x0, -0x4(%rbp)
movw   $0x1, -0x6(%rbp)
[jmp]   L5                           #jump to loop 5

.L2
movsb1   -0x18(%rbp), %eax           # Move eax value. eg: eax = c
[sub]   $0x61, %eax                   # subtract with $0x61. Like c-'a'
test   %eax, %eax                   # TEST is used to check binary AND of the operands, It will check the equality of the values. eg: c-'a' >= 0 (already done
                                   greater than condition as per loop 5)
[js]   L3                                          # jump to loop3 L3. If previous conditions failed.
movzw1   -0x6(%rbp), %edx           # move value to edx
movzw1    -0x6(%rbp), %eax           # move value to eax
[imul]   %edx, %eax                       # mulitiply edx and eax. like i*i. and save the result to edx
add       %edx, -0x4(%rbp)              # add edx value. eg: result = result + i*i;
jmp       L4                                    # jump to loop4 L4

.L3
movzw1    -0x6(%rbp), %eax           # Move value to eax
add    %eax, -0x4([%rbp])                # add eax value to result. eg: result = result + i;
jmp       L4                                  # Jump loop4 L4.

.L4
movzw1   -0x6(%rbp), %eax           # Move value to eax
[lea]   -0x1(%rax), %ecx
mov       -0x4(%rbp), %eax              # Move value to eax
add       $0x61, [%eax]                   # Add eax to values, like result+'a';
mov    %eax, %edx                        # Move eax value to edx
movslq   %ecx, %rax                  
mov       %dl, -0x10([%rbp], %rax, 1)   # mov values.Eg: i-1
movzw1   -0x6(%rbp), %eax                # move this value to eax
[add]   $0x1, %eax                             # add eax to value
mov    %ax, -0x6(%rbp)                      # Move the result. eg: b[i-1] = result + 'a'
jmp       L5                                        # Jump loop5 L5. again check the condition and process the result. eg: if(c-'a' >= 0)

.L5
movzw1   -0x6(%rbp), %eax
cmp       -0x14(%rbp), %eax           # CMP is used to check the compare the 2 values. Like greater than or smaller than operands. eg: i <= x
[jle]   L2                              # If the previous comparision of greater than condition is true then jumb iteself to L2 loop. like (c > 0)
mov    -0x4(%rbp), %eax          # Move the result
pop       %rbp                       # restore the stack
retq

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