I need help with implementing the following two functions in assembly language,
ID: 3859570 • Letter: I
Question
I need help with implementing the following two functions in assembly language,
/***********************************************************************************************
PART 4: Vector Operation
The vectorOperations function gets as parameters 4 arrays of the same length and
the number of elements in those arrays.
For each pair of elements in first_array and second_array, it calls performOperation
and stores the returned result in the same index of output_array.
output_array[index] = performOperation(operation_array[index], first_array[index], second_array[index]).
You also need to implement the body of performOperation function according to the provided
C code.
************************************************************************************************/
__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
asm{
performOperation(int, int, int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
mov DWORD PTR [rbp-12], edx
cmp DWORD PTR [rbp-4], 0
jne .L2
mov eax, DWORD PTR [rbp-8]
sub eax, DWORD PTR [rbp-12]
jmp .L3
.L2:
cmp DWORD PTR [rbp-4], 1
jne .L4
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-12]
add eax, edx
jmp .L3
.L4:
cmp DWORD PTR [rbp-4], 2
jne .L5
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-12]
jge .L6
mov eax, DWORD PTR [rbp-8]
jmp .L3
.L6:
mov eax, DWORD PTR [rbp-12]
jmp .L3
.L5:
cmp DWORD PTR [rbp-4], 3
jne .L8
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-12]
jle .L9
mov eax, DWORD PTR [rbp-8]
jmp .L3
.L9:
mov eax, DWORD PTR [rbp-12]
jmp .L3
.L8:
mov eax, DWORD PTR [rbp-8]
.L3:
pop rbp
ret
}
__asm{
vectorOperations(int, int*, int*, int*, int*):
push rbp
mov rbp, rsp
push rbx
sub rsp, 56
mov DWORD PTR [rbp-28], edi
mov QWORD PTR [rbp-40], rsi
mov QWORD PTR [rbp-48], rdx
mov QWORD PTR [rbp-56], rcx
mov QWORD PTR [rbp-64], r8
mov DWORD PTR [rbp-12], 0
.L13:
mov eax, DWORD PTR [rbp-12]
cmp eax, DWORD PTR [rbp-28]
jge .L14
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-64]
lea rbx, [rdx+rax]
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-48]
add rax, rdx
mov edx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-12]
cdqe
lea rcx, [0+rax*4]
mov rax, QWORD PTR [rbp-40]
add rax, rcx
mov ecx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-12]
cdqe
lea rsi, [0+rax*4]
mov rax, QWORD PTR [rbp-56]
add rax, rsi
mov eax, DWORD PTR [rax]
mov esi, ecx
mov edi, eax
call performOperation(int, int, int)
mov DWORD PTR [rbx], eax
add DWORD PTR [rbp-12], 1
jmp .L13
.L14:
nop
add rsp, 56
pop rbx
pop rbp
ret
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.