Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

_asm{ //find the smallest value in arrayA in assembler } COMP375 Assembler Progr

ID: 3787039 • Letter: #

Question

_asm{ //find the smallest value in arrayA in assembler } COMP375 Assembler Programming Assignment 2 l. Write a program to find the smallest value in an array of 8 integers. #include kio stream> using namespace std; int main (int argn char argv int smallest i, arrayA [8] cout Enter 8 numbers endl; for (i 0; i 8 i++) cin array A. [i]; asm Find the smallest value in arrayA in assembler cout "The smallest value is smallest endl. return 0; 2. write a program that will calculate the log base 2 ofan integer using the Intel BSR instruction. T

Explanation / Answer

#include <iostream>
using namespace std;

int main(int argn, char *argv[]) {
   int smallest, i, arrayA[8];
   cout << "Enter 8 numbers" << endl;
   for(i=0; i<8; i++) {
       cin >> arrayA[i];
   }
   _asm {
               push rbp
               mov rbp, rsp
               mov QWORD PTR [rbp-24], rdi
               mov rax, QWORD PTR [rbp-24]
               mov eax, DWORD PTR [rax]
               mov DWORD PTR [rbp-4], eax
               mov DWORD PTR [rbp-8], 1
               jmp .L2
       .L4:
               mov eax, DWORD PTR [rbp-8]
               cdqe
               lea rdx, [0+rax*4]
               mov rax, QWORD PTR [rbp-24]
               add rax, rdx
               mov eax, DWORD PTR [rax]
               cmp eax, DWORD PTR [rbp-4]
               jge .L3
               mov eax, DWORD PTR [rbp-8]
               cdqe
               lea rdx, [0+rax*4]
               mov rax, QWORD PTR [rbp-24]
               add rax, rdx
               mov eax, DWORD PTR [rax]
               mov DWORD PTR [rbp-4], eax
       .L3:
               add DWORD PTR [rbp-8], 1
       .L2:
               cmp DWORD PTR [rbp-8], 7
               jle .L4
               pop rbp
               ret
   }
   cout <<"The smallest value is " << smallest << endl;
   return 0;
}