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

GIVEN C++ CODE

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;
}

Converted x86 Assembly code as follows:

Code                                                                        Comment

problem1_:                                                                     //problem 1 function is started

push rbp                                                                      //pushing base pointer to stack

mov rbp, rsp                                                           //moving stack pointer content to base pointer

mov DWORD PTR [rbp-24], 1                                 //assigning values to array as 1

mov DWORD PTR [rbp-20], -2                              //assigning values to array as -2

mov DWORD PTR [rbp-16], 15                             //assigning values to array

mov DWORD PTR [rbp-4], 0                               //Declaring variable result as 0

mov DWORD PTR [rbp-8], 0                               //Declaring variable index as 0

mov DWORD PTR [rbp-12], 3                             //Declaring variable numElements as 3

jmp .L2                                                            //Jump to block L2

.L5:                                                                     //L5 block started

cmp DWORD PTR [rbp-8], 0                               //compare if index==0

jle .L3                                                                 //if less than equal then goto block L3

mov eax, DWORD PTR [rbp-8]                        //else move value of index to eax register

cdqe

mov eax, DWORD PTR [rbp-24+rax*4]         //move numArray[index] value to eax register

cmp eax, 3                                                  //compare if eax is equal to 3

jle .L3                                                          //if less than equal then goto block L3

mov eax, DWORD PTR [rbp-8]                //else move index value to eax register

cdqe

mov eax, DWORD PTR [rbp-24+rax*4]      //move value of numArray[index] to register eax

add DWORD PTR [rbp-4], eax                //Add value of eax to result variable

jmp .L4                                                   //Jump to block L4

.L3:                                                            //L3 Block Started

sub DWORD PTR [rbp-4], 3                        //Subtract 3 from result as result=result-3

.L4:                                                      //L4 block started

add DWORD PTR [rbp-8], 1                      //increment index by 1

.L2:                                                          //L2 block started

mov eax, DWORD PTR [rbp-8]                       //move content of index to eax register

cmp eax, DWORD PTR [rbp-12]                    //compare eax with numElements

jl .L5                                                             //if less than goto block L5

mov eax, DWORD PTR [rbp-4]                     //move result variable to eax register

pop rbp                                                       //pop base pointer from stack

ret                                                              //return the function

problem2_:                                          //problem2_() function started

push rbp                                              //push base pointer to stack

mov rbp, rsp                                  //move stack pointer to base pointer

mov DWORD PTR [rbp-8], 0               //declare answer as 0

mov DWORD PTR [rbp-12], 3        //declare variable b as 3

mov DWORD PTR [rbp-4], 5         //declare variable a as 5

jmp .L8                                                 //jump to block L8

.L12:                                                  //L12 block started

mov eax, DWORD PTR [rbp-4]                 //move variable a to eax register

and eax, 3                                                //Perform And operation between eax and value 3

test eax, eax                                            //test if value of eax is equal to eax

je .L9                                                        //if equal jump to L9 block

mov eax, DWORD PTR [rbp-4]                  //move variable a to eax register

cdq

shr edx, 30                                                //right shift

add eax, edx                                             //add register eax and edx

and eax, 3                                                //perform and operation between register eax and 3

sub eax, edx                                            //subtract edx from eax

cmp eax, 3                                            //compare eax value to 3

jne .L10                                                //if not equal jump to L10

.L9:                                                          //Block L9 started

mov eax, DWORD PTR [rbp-12]                    //move variable b to register eax

add DWORD PTR [rbp-8], eax                      //Add answer to register eax

jmp .L11                                                    //jump to block L11

.L10:                                                     //Block L10 started

sal DWORD PTR [rbp-8]                              //multiply answer by 2 by performing left shift

.L11:                                                       //L11 block started

sub DWORD PTR [rbp-4], 1           //Decrement a by 1

.L8:                                                          //L8 block started

cmp DWORD PTR [rbp-4], 0                          //Compare if a is equal to 0

jg .L12                                                       //if greater goto L12

mov eax, DWORD PTR [rbp-8]                    //else move answer to register eax

pop rbp                                                      //pop base pointer from stack

ret                                                            //return to function call


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...
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
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 the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT