What\'s the conversion from C++ to assembly code x86 int findMinIndex (int integ
ID: 3676741 • Letter: W
Question
What's the conversion from C++ to assembly code x86
int findMinIndex (int integer_array[], int i, int j)
{
// C code to be converted to x86 assembly
/*
int iMin = i;
// test against elements after i and before j to find the smallest
for ( i ; i < j; i++) {
// if this element is less, then it is the new minimum
if (integer_array[i] < integer_array[iMin]) {
// found new minimum; remember its index
iMin = i;
}
}
return iMin;
*/
void selectionSort (int integer_array[], int array_size)
{
// C code to be converted to x86 assembly
/*
int j;
int iMin;
int temp;
// advance the position through the entire array //
// (could do j < n-1 because single element is also min element) //
for (j = 0; j < array_size-1; j++) {
// find the index of min element in the unsorted a[j .. n-1] //
iMin = findMinIndex (integer_array, j, array_size);
if(iMin != j) { // swap values
temp = integer_array[iMin];
integer_array[iMin] = integer_array [j];
integer_array[j] = temp;
}
}
*/
Explanation / Answer
Assembly Code:
Find_Min_Index(int*, int, int):
//to push the value
push rbp
// move the value
mov rbp, rsp
//move the value
mov QWORD PTR [rbp-24], rdi
//move the value
mov DWORD PTR [rbp-28], esi
//move the value
mov DWORD PTR [rbp-32], edx
//move the value
mov eax, DWORD PTR [rbp-28]
//move the value
mov DWORD PTR [rbp-4], eax
.LBL1:
//lable 1 loop
mov eax, DWORD PTR [rbp-28]
//compare the values
cmp eax, DWORD PTR [rbp-32]
//jump if greater than or equal to
jge .LBL3
//move the value
mov eax, DWORD PTR [rbp-28]
cdqe
lea rdx, [0+rax*4]
//move the value
mov rax, QWORD PTR [rbp-24]
add rax, rdx
//move the value
mov edx, DWORD PTR [rax]
//move the value
mov eax, DWORD PTR [rbp-4]
cdqe
lea rcx, [0+rax*4]
//move the value
mov rax, QWORD PTR [rbp-24]
add rax, rcx
//move the value
mov eax, DWORD PTR [rax]
cmp edx, eax
jge .LBL2
mov eax, DWORD PTR [rbp-28]
//move the value
mov DWORD PTR [rbp-4], eax
.LBL2:
add DWORD PTR [rbp-28], 1
jmp .LBL1
.LBL3:
nop
pop rbp
ret
selection_Sort(int*, int):
//to push the value
push rbp
//initialise
mov rbp, rsp
sub rsp, 32
//move the value
mov QWORD PTR [rbp-24], rdi
//move the value
mov DWORD PTR [rbp-28], esi
//move the value
mov DWORD PTR [rbp-4], 0
.LBL4:
mov eax, DWORD PTR [rbp-28]
sub eax, 1
cmp DWORD PTR [rbp-4], eax
jge .LBL6
//move the value
mov edx, DWORD PTR [rbp-28]
//move the value
mov ecx, DWORD PTR [rbp-4]
//move the value
mov rax, QWORD PTR [rbp-24]
//move the value
mov esi, ecx
//move the value
mov rdi, rax
call findMinIndex(int*, int, int)
//move the value
mov DWORD PTR [rbp-8], eax
//move the value
mov eax, DWORD PTR [rbp-8]
//to compare the values
cmp eax, DWORD PTR [rbp-4]
je .LBL5
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
//move the value
mov rax, QWORD PTR [rbp-24]
//perform addition
add rax, rdx
mov eax, DWORD PTR [rax]
//move the value
mov DWORD PTR [rbp-12], eax
//move the value
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
//move the value
mov rax, QWORD PTR [rbp-24]
add rax, rdx
//move the value
mov edx, DWORD PTR [rbp-8]
movsx rdx, edx
lea rcx, [0+rdx*4]
//move the value
mov rdx, QWORD PTR [rbp-24]
add rdx, rcx
//move the value
mov eax, DWORD PTR [rax]
//move the value
mov DWORD PTR [rdx], eax
//move the value
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
//move the value
mov rax, QWORD PTR [rbp-24]
add rdx, rax
//move the value
mov eax, DWORD PTR [rbp-12]
//move the value
mov DWORD PTR [rdx], eax
.LBL5:
add DWORD PTR [rbp-4], 1
jmp .LBL4
.LBL6:
nop
//leave the process and return the values
leave
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.