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

Convert the following from C++ to assembly code x86 __declspec(naked) int perfor

ID: 3676750 • Letter: C

Question

Convert the following from C++ to assembly code x86

__declspec(naked)
int performOperation (int operation, int first, int second)
{

// C code to be converted to x86 assembly
/*
   if (operation==0)
       return first + second;
   else if (operation==1)
       return first - second;
   else if (operation==2)
       return (first > second)? first : second;
   else if (operation==3)
       return (first < second)? first : second;
   else
       return first;
*/

   __asm {
           // BEGIN YOUR CODE HERE

           // END YOUR CODE HERE
           ret
   }
  
}

void vectorOperations (int number_of_elements, int *first_array, int *second_array,
                   int *operation_array, int *output_array)
{
// C code to be converted to x86 assembly
/*
   int i;
   for (i=0; i<number_of_elements; i++)
   {
       output_array[i] = performOperation(operation_array[i], first_array[i], second_array[i]);
   }
*/

   __asm {
           // BEGIN YOUR CODE HERE

           // END YOUR CODE HERE
   }
}

Explanation / Answer

Convert the following from C++ to assembly code x86

__declspec(naked) :


int performOperation (int operation, int first, int second)
{

// C code to be converted to x86 assembly
/*
   if (operation==0)
       return first + second;
   else if (operation==1)
       return first - second;
   else if (operation==2)
       return (first > second)? first : second;
   else if (operation==3)
       return (first < second)? first : second;
   else
       return first;
*/

   __asm {
           // BEGIN YOUR CODE HERE

                 .Ltext0: ;int performOperation (int operation, int first, int second){

                          .globl   _Z16performOperationiii

                 _Z16performOperationiii:

                 .LFB0:

                          .cfi_startproc

0000 55                   pushq    %rbp

                          .cfi_def_cfa_offset 16

                          .cfi_offset 6, -16

0001 4889E5               movq     %rsp, %rbp

                          .cfi_def_cfa_register 6

0004 897DFC               movl     %edi, -4(%rbp)

0007 8975F8               movl     %esi, -8(%rbp)

000a 8955F4               movl     %edx, -12(%rbp)

000d 837DFC00             cmpl     $0, -4(%rbp) ;if (operation==0)

0011 750A                 jne      .L2

0013 8B45F4               movl     -12(%rbp), %eax ;return first + second;

0016 8B55F8               movl     -8(%rbp), %edx

0019 01D0                 addl     %edx, %eax

001b EB45                 jmp      .L3

                 .L2:

001d 837DFC01             cmpl     $1, -4(%rbp) ;else if (operation==1)

0021 750C                 jne      .L4

0023 8B45F4               movl     -12(%rbp), %eax ;return first - second;

0026 8B55F8               movl     -8(%rbp), %edx

0029 29C2                 subl     %eax, %edx

002b 89D0                 movl     %edx, %eax

002d EB33                 jmp      .L3

                 .L4:

002f 837DFC02             cmpl     $2, -4(%rbp) ;else if (operation==2)

0033 7512                 jne      .L5

0035 8B45F8               movl     -8(%rbp), %eax ;return (first > second)? first : second;

0038 3B45F4               cmpl     -12(%rbp), %eax

003b 7E05                 jle      .L6

003d 8B45F8               movl     -8(%rbp), %eax

0040 EB03                 jmp      .L7

                 .L6:

0042 8B45F4               movl     -12(%rbp), %eax

                 .L7:

0045 EB1B                 jmp      .L3

                 .L5:

0047 837DFC03             cmpl     $3, -4(%rbp) ;else if (operation==3)

004b 7512                 jne      .L8

004d 8B45F8               movl     -8(%rbp), %eax ;return (first < second)? first : second;

0050 3B45F4               cmpl     -12(%rbp), %eax

0053 7D05                 jge      .L9

0055 8B45F8               movl     -8(%rbp), %eax

0058 EB03                 jmp      .L10

                 .L9:

005a 8B45F4               movl     -12(%rbp), %eax

                 .L10:

005d EB03                 jmp      .L3

                 .L8:

005f 8B45F8               movl     -8(%rbp), %eax ; return first;

                 .L3:

0062 5D                   popq     %rbp ;end function

                          .cfi_def_cfa 7, 8

0063 C3                   ret

                          .cfi_endproc

           // END YOUR CODE HERE
           ret
   }
  
}

void vectorOperations (int number_of_elements, int *first_array, int *second_array,
                   int *operation_array, int *output_array)
{
// C code to be converted to x86 assembly
/*
   int i;
   for (i=0; i<number_of_elements; i++)
   {
       output_array[i] = performOperation(operation_array[i], first_array[i], second_array[i]);
   }
*/

   __asm {
           // BEGIN YOUR CODE HERE

   }
}