Question

In: Computer Science

Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...

Change the following C++ functions to Assembly x86:

int problem1_ ( )
{
int numberArray [3] = {1, -2, 15 };
int result = 0, index = 0;
int numElements = 3;
while (index < numElements)
{
if ( index >= 1 && numberArray[index] > 3 )
{
result += numberArray[index] ;
}
else
{
result -= 3;
}
index++;
}
return result;
}

int problem2_ ( )
{
int a, modulo
int answer = 0, b = 3;
for ( a = 5; a > 0; a-- )
{
if ( ( a % 4 == 0 ) || ( a % 4 == 3) )
{
answer += b ;
}
else
{
answer *= 2;
}
}
return answer;
}

Solutions

Expert Solution

Solution:-

When it comes to code conversion c++ to assembly it is very complicated and time consuming.

problem1_():

        push    rbp

        mov     rbp, rsp

        mov     DWORD PTR [rbp-24], 1

        mov     DWORD PTR [rbp-20], -2

        mov     DWORD PTR [rbp-16], 15

        mov     DWORD PTR [rbp-4], 0

        mov     DWORD PTR [rbp-8], 0

        mov     DWORD PTR [rbp-12], 3

.L5:

        mov     eax, DWORD PTR [rbp-8]

        cmp     eax, DWORD PTR [rbp-12]

        jge     .L2

        cmp     DWORD PTR [rbp-8], 0

        jle     .L3

        mov     eax, DWORD PTR [rbp-8]

        cdqe

        mov     eax, DWORD PTR [rbp-24+rax*4]

        cmp     eax, 3

        jle     .L3

        mov     eax, DWORD PTR [rbp-8]

        cdqe

        mov     eax, DWORD PTR [rbp-24+rax*4]

        add     DWORD PTR [rbp-4], eax

        jmp     .L4

.L3:

        sub     DWORD PTR [rbp-4], 3

.L4:

        add     DWORD PTR [rbp-8], 1

        jmp     .L5

.L2:

        mov     eax, DWORD PTR [rbp-4]

        pop     rbp

        ret

problem2_():

        push    rbp

        mov     rbp, rsp

        mov     DWORD PTR [rbp-8], 0

        mov     DWORD PTR [rbp-12], 3

        mov     DWORD PTR [rbp-4], 5

.L12:

        cmp     DWORD PTR [rbp-4], 0

        jle     .L8

        mov     eax, DWORD PTR [rbp-4]

        and     eax, 3

        test    eax, eax

        je      .L9

        mov     eax, DWORD PTR [rbp-4]

        cdq

        shr     edx, 30

        add     eax, edx

        and     eax, 3

        sub     eax, edx

        cmp     eax, 3

        jne     .L10

.L9:

        mov     eax, DWORD PTR [rbp-12]

        add     DWORD PTR [rbp-8], eax

        jmp     .L11

.L10:

        sal     DWORD PTR [rbp-8]

.L11:

        sub     DWORD PTR [rbp-4], 1

        jmp     .L12

.L8:

        mov     eax, DWORD PTR [rbp-8]

        pop     rbp

        ret

Note:- One simple approach that you can use is to write down the c++ code and in the cmd you can call the following command, this command will convert the cpp code to assembly code.

gcc -S programName.cpp


Related Solutions

Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value)...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value) If the value passed in is even, return 1 If the value passed in is odd, return 0 Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to their initial states prior to exiting Allow an end user to test this function in the following manner: Prompt...
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
Convert the following C++ statements to an ARM assembly language program: const int size = 10;...
Convert the following C++ statements to an ARM assembly language program: const int size = 10; int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4}; int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25}; for i = 0; i < size; i++) if (x([ i ] > y[ i ]) z[ i ] = 0 else z[ i ] = 1;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT