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...
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...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT