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: 3890783 • Letter: C

Question

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

myFunction (char a, int x) %ebp signed int b171: unsigned short i; signed int y = 15; push mov sub mov mov movl mov mov %ESP-, %ebp $0x34 , %esp 0x8(%ebp) ,%eax %al,-0x34 (%ebp) $Oxf ,-0x20 (%ebp) 0xc (-%EBP--),%eax %ax ,-0x22 (%ebp) JMP L1 for (i = x, i > 0 ; i--) 4 $0x0,-0x20 (kebp) jns L2 movzw1-0x22(%ebp), %eax sub$0x1,%eax movsbl -0x34 (%ebp),--%ECX-- movzwl -0x22(ebp),Ledx -ADD mov %eax ,-0x1c(Xebp,--%EAX--,4) jmp L3 if (y

Explanation / Answer

push    %ebp                                     #push value to stack
mov    __%ESP__, %ebp                    # Move value to ebp
sub       $0x34, $esp                           # subtract values
mov    0x8(%ebp), %eax                   # Move value to register
mov    %a1, -0x34(%ebp)
movl   $0xf, -0x20(%ebp)
mov       0xc(__%EBP__), %eax
mov    %ax, -0x22(%ebp)
__JMP__   L1                                        #Jump loop to L1, so it goes to .L1 loop

.L4
__CMPL__$0x0, -0x20(%ebp)               # Compare the contents of register
jns       L2                                           # Jumb no sign to loop L2
movzwl   -0x22(%ebp),%eax               # Move value to register eax
sub       $0x1, %eax                            # Sub value with eax eg:i-1
movsbl   -0x34(%ebp), __%ECX__        # move sub value to register, eg: b[i-1]
movzwl   -0x22(%ebp), %edx               # move value to register
__ADD__   %ecx, %edx                      # add value of ecx and edx and assign this to edx. eg: a+i
mov       %edx, -0x1c(%ebp, __%EAX__, 4)   # Move value to register eg:b[i-1] = a+i
jmp    L3                                                       # Jump loop to L3.

.L2
__MOVZWL__ -0x22(%ebp), %eax           # move value to register eax
movsbl   -0x34(%ebp), %edx                    # movsbl - sign extends a single byte and it will be copied to double word destination. eg: b[i] = a;
mov    __%EDX__, -0x1c(%ebp, %eax, 4)   # Move value to register


.L3
movzwl    -0x22(%ebp), %eax               # movzwl will copy only the bits in %ebp to %eax with high 16 bits. So it always end up wiht positive number.
mov       -0x1c(%ebp, $eax, 4), %eax    # Move value to eax
add       %eax, -0x20(%ebp)               # Add 2 content of registers, eg: y+b[i];
movzwl   -0x22(%ebp), %eax               # Move those values to eax. eg: y = y+b[i];
__SUB__   $0x1, %eax                       # sub i value by 1 and copy it to eax. Eg: i--
mov       %ax, -0x22(%ebp)                  # Move value to register


.L1
cmpw   $0x0, -0x22(%ebp)               # Compare 2 operands, Eg: i>0,
jne       .L4                                       # If the value is not equal then jumb to L4.
mov       -0x20(%ebp), %eax           # Move value to register
leave
ret

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