QuestionQuestion

Q1
Convert this C++ program (and accompanying function) into x86 assembly language.
Make sure to use the proper "Chapter 8" style parameter passing and local variables.

#include <iostream>

using namespace std;

void RecursiveFunction(int x)
{
if (x > 0)
{
if ((x & 1) == 0)
{
RecursiveFunction(--x);
cout << x << " ";
}
else
{
cout << x << " ";
RecursiveFunction((x >> 1) - 1);
}
}
}

int main()
{
RecursiveFunction(109);
cout << endl;
system("PAUSE");
return 0;
}

Q2
One hundred students are assigned lockers 1 through 100
The student assigned to locker number 1 opens all 100 lockers
The student assigned to locker number 2 then closes all lockers whose numbers are multiples of 2
The student assigned to locker number 3 toggles the status of all lockers whose numbers are multiples of 3
Locker number 3, which is open gets closed,
Locker number 6, which is closed, gets opened
And so on for as many lockers need to be toggled

The student assigned to locker number 4 toggles the status of all lockers whose numbers are multiples of 4
And so on and on for all 100 students and all 100 lockers

Which lockers will be left open after everyone completes their turns?
Convert this C++ program into x86 assembly language and find out!

#include <iostream>
using namespace std;

// In this case we will be using a global array
// This is not generally the case for any other variables
// 101 element array, all elements initialized to 0
// NOTE: For simplicity, element 0 is unused
int Lockers[101]{ 0 };

// Boolean NOT operation just like the exam question
int NotOperation(int x)
{
return (x == 0) ? 1 : 0;
}

// Student x toggles all lockers { x, 2x, 3x, 4x, ... }
// up to and including locker 100
void StudentTogglingLockers(int x)
{
for (int i = x; i <= 100; i += x)
{
Lockers[i] = NotOperation(Lockers[i]);
}
}

// 100 students walk through and toggle lockers
void ToggleLockers()
{
for (int i = 1; i <= 100; ++i)
{
StudentTogglingLockers(i);
}
}

void PrintOpenLockers()
{
for (int i = 1; i <= 100; ++i)
{
if (Lockers[i] == 1)
{
cout << i;
cout << ' ';
}
}
cout << endl;
}

int main()
{
ToggleLockers();
PrintOpenLockers();
system("PAUSE");
}

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

ASM x86 Solution

For this implementation the recursivity must be first understood and then implemented in assembly language.

The Recursive Function’s parameter is unnamed in the C code. It was named “Function_parameter” in the assembly code and its value can be modified in the Data Segment (“.data”). After each modification, recompiling is needed and new results will be produced (this is how the program can be tested).

1. Main procedure

In the main procedure, we start by clearing registers EAX and EBX. We use Based-indexed addressing with no scale factor to access the Function_parameter from the memory and copy it in EBX (“MOV EBX, [Function_parameter + EAX]” ).

When the Recursive Function is called, first the function’s parameter is pushed on the Stack, then the returning address is pushed on the Stack. The Stack looks like this:


The ASM implementation follows the C implementation.

I set a memory variable called Students, if somebody wants to modify it (currently it is 100, but can for instance be 150 – but less than 256).

In the main code, all the main registers (EAX, EBX, ECX and EDX) are loaded with 0 and then the two functions ToggleLockers and PrintOpenLockers are called.

The Function TL starts by storing the iterator i (i.e. the Student) in register EBX. Then EBX is pushed on the Stack, because it is the argument of the function STL (StudentTogglingLockers). The function STL is called by the statement CALL STL....

By purchasing this solution you'll be able to access the following files:
Solution.zip, Solution1.docx and Solution2.docx.

$40.50
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Assembly Language Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats