In: Computer Science
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;
}
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