Hey in this C++ code there is ssort function written I want that function in NASM assembly language. Just want to run th

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Hey in this C++ code there is ssort function written I want that function in NASM assembly language. Just want to run th

Post by answerhappygod »

Hey in this C++ code there is ssort function written I want that
function in NASM assembly language. Just want to run that ssort
function through assembly rest in C++.
#include <iostream>
#include <cstdlib>

using namespace std;
int* alloc_fill_mem(int size)
{
int* ar = new int[size];
for (int i=0; i < size; i++) {
ar = rand() % 10;
}
return ar;
}

void print_arr(int* ar, int size) {

for (int i=0; i < size; i++) {
printf(" %d ", ar);
}

}


void swap(int* p, int *r) {
int t = *p;
*p = *r;
*r = t;
}


int* find_smallest(int* ar, int size){
int *p = ar;
for (int i=1;i<size; i++) {
if (*p > ar) {
p = &ar;
}
}
return p;
}

void ssort(int* ar,int size) {
for (int i = 0; i< size; i++) {
int* p = find_smallest(&ar, size - i);
swap(p, &ar);
}
}

bool binary_search(int *ar, int size, int data)
{
int *s = ar;
int *e = ar + size;
int d = size/2;
int *m = ar + d;

while (s <= e) {
if (data == *m) return true;
if (data > *m) // reject left side of the array
{
s = m;
d = d/2;
m = s+d;
}
if (data < *m) // reject right side of the array
{
e = m;
d = d/2;
m = s+d;
}
}
return false;
}

int main()
{
int size = 10;
int* ar = alloc_fill_mem(size);
//print_arr(ar, size);
ssort(ar, size) ;
print_arr(ar, size);
bool g = binary_search(ar, size, 10);
if (g == true) printf("\nFound!!!\n");
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply