Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How can I multiply and divide an array by a scalar, Assembly-C++?

This is a post edit: Brilliant classmate got procedure 4 and 5. Now just one left to do. Thanks The following code is working good(except Number 6). Thanks everyone for your assistance.

enter code here

  .386
    .MODEL FLAT, C
    .STACK 4096

    ;ExitProcess PROTO, dwExitCode:DWord

    .data
    .code

    CheckForGoodMenuInput PROC
    push ebp
    mov ebp,esp
    mov edi,[ebp+8]
    cmp edi, 1
    je Good
    cmp edi, 2
    je Good
    cmp edi, 3
    je Good
    cmp edi, 4
    je Good
    cmp edi, 5
    je Good
    mov eax,1
    pop ebp
    ret
    Good :
    mov eax,0
    pop ebp
    ret
    CheckForGoodMenuInput ENDP


    CheckForGoodRowInput PROC
    push ebp
    mov ebp, esp
    mov edi,[ebp+8]
    cmp edi, 1
    je Good
    cmp edi, 2
    je Good
    cmp edi, 3
    je Good
    mov eax,1
    pop ebp
    ret
    Good :
    mov eax,0
    pop ebp
    ret
    CheckForGoodRowInput ENDP


    SwitchTwoRows PROC
    push ebp
    mov ebp, esp
    mov edi, [ebp+12]
    mov esi, [ebp+8]
    mov ecx, 4
L1:
    mov eax, [edi]
    mov edx, [esi]
    mov [edi], edx
    mov [esi], eax
    add esi, 4
    add edi, 4
    loop L1

    pop ebp
    ret

SwitchTwoRows ENDP


MultiplyRowByScalar PROC
    push ebp
    mov ebp,esp
    mov esi, [ebp+8]
    mov ecx, 4
    mov edi,eax
L1:
    mov ebx, [esi]
    imul ebx, edi
    mov [esi], ebx
    add esi, 4
    loop L1
    pop ebp
    ret


MultiplyRowByScalar ENDP

DivideRowByScalar PROC
COMMENT @
@

    push ebp
    mov ebp,esp
    mov esi, [ebp+8]
    mov ecx, 4
    mov ebx, eax
L1:
    mov eax, [esi]
    cdq
    idiv ebx
    mov [esi], eax
    add esi, 4
    loop L1
    pop ebp
    ret

DivideRowByScalar ENDP

AddMultipleOfOneToAnother PROC
    xor eax, eax
    ret
AddMultipleOfOneToAnother ENDP

END

Hello I need some help with my final project in Assembler. I don't expect it to be solved for me but maybe some hints on what to use for the last three Assembly codes that are called by the C++ program. Thanks Here's the C++ program and what I have so far for the Assembly code. Please help, Thanks

// This is the final project.  It is the cpp file that will call your 
assembler procedures.
// I do not want this file to be changed at all.

#include "stdafx.h"
#include <iostream>

using namespace std;

void Print_Matrix(int[], int[], int[]);

//This procedure checks the input to see if the user selected a number 
between
//1 and 5.  It returns 0 if good and 1 if bad.
extern "C" int CheckForGoodMenuInput(int);

//This procedure checks the input to see if the user selected a valid row
//(1, 2, or 3).  It returns 0 if good and 1 if bad.
extern "C" int CheckForGoodRowInput(int);

//This procedure takes two arrays (of size 4) and switches all of their 
values.
extern "C" void SwitchTwoRows(int[], int[]);

//This procedure multiplies each value of an array (of size 4), by a scalar 
(integer).
extern "C" void MultiplyRowByScalar(int[], int);

//This procedure divides each value of an array (of size 4), by a scalar 
(integer).
//If there is a remainder at any point, it returns 1, if not, it returns 0.
extern "C" int DivideRowByScalar(int[], int);

//This procedure multiplies each value of one array (of size 4) by a scalar
//and adds the results to another array (of size 4).
extern "C" void AddMultipleOfOneToAnother(int[], int[], int);
int main()
{
int first_equation[4];
int second_equation[4];
int third_equation[4];
int menu_choice = 0;
int good_choice = 1;
int good_choice_two = 1;
int row_one = 0;
int row_two = 0;
int scalar;
int possible_error = 0;
cout << "Enter 'a', 'b', 'c' and 'd' for the first equation." << endl;
cout << "ax + by + cz = d" << endl;
for (int i = 0; i < 4; i++)
    cin >> first_equation[i];
cout << "Enter 'a', 'b', 'c' and 'd' for the second equation." << endl;
cout << "ax + by + cz = d" << endl;
for (int i = 0; i < 4; i++)
    cin >> second_equation[i];
cout << "Enter 'a', 'b', 'c' and 'd' for the third equation." << endl;
cout << "ax + by + cz = d" << endl;
for (int i = 0; i < 4; i++)
    cin >> third_equation[i];
Print_Matrix(first_equation, second_equation, third_equation);
do
{
    while (good_choice == 1)
    {
        menu_choice = 0;
        good_choice_two = 1;
        cout << "Would you like to perform an elementary row operation on 
 your matrix?" << endl;
        cout << "1. Switch two rows." << endl;
        cout << "2. Multiply a row by a number (integer)." << endl;
        cout << "3. Divide a row by a number (integer)." << endl;
        cout << "4. Add a multiple of one row to another row." << endl;
        cout << "5. Quit." << endl;
        cin >> menu_choice;
        good_choice = CheckForGoodMenuInput(menu_choice);
    }
    switch (menu_choice)
    {
    case(1):
        while (good_choice_two == 1)
        {
            cout << "Which rows would you like to switch?" << endl;
            cin >> row_one;
            cin >> row_two;
            good_choice_two = CheckForGoodRowInput(row_one);
            if (good_choice_two == 0)
                good_choice_two = CheckForGoodRowInput(row_two);
            if (row_one == row_two)
                good_choice_two = 1;
        }
        if (row_one == 1 || row_two == 1)
        {
            if (row_one == 2 || row_two == 2)
                SwitchTwoRows(first_equation, second_equation);
            else
                SwitchTwoRows(first_equation, third_equation);
        }
        else SwitchTwoRows(second_equation, third_equation);
        Print_Matrix(first_equation, second_equation, third_equation);
        good_choice = 1;
        break;
    case(2):
        while (good_choice_two == 1)
        {
            cout << "Which row would you like to change?" << endl;
            cin >> row_one;
            cout << "What would you like to multiply it by?" << endl;
            cin >> scalar;
            good_choice_two = CheckForGoodRowInput(row_one);
            if (scalar == 0)
                good_choice_two = 1;
        }
        if (row_one == 1)
            MultiplyRowByScalar(first_equation, scalar);
        else if (row_one == 2)
            MultiplyRowByScalar(second_equation, scalar);
        else MultiplyRowByScalar(third_equation, scalar);
        Print_Matrix(first_equation, second_equation, third_equation);
        good_choice = 1;
        break;
    case(3):
        while (good_choice_two == 1) {
            cout << "Which row would you like to change?" << endl;
            cin >> row_one;
            cout << "What would you like to divide it by?" << endl;
            cin >> scalar;
            good_choice_two = CheckForGoodRowInput(row_one);
            if (scalar == 0)
                good_choice_two = 1;
        }
        if (row_one == 1)
            possible_error = DivideRowByScalar(first_equation, scalar);
        else if (row_one == 2)
            possible_error = DivideRowByScalar(second_equation, scalar);
        else possible_error = DivideRowByScalar(third_equation, scalar);
        Print_Matrix(first_equation, second_equation, third_equation);
        if (possible_error == 1)
            cout << "There were remainder(s) with the division, so your 
 numbers are not exact." << endl;
        good_choice = 1;
        break;
    case(4):
        while (good_choice_two == 1) {
            cout << "Which row will by multiplied?" << endl;
            cin >> row_one;
            cout << "What will it by multiplied by?" << endl;
            cin >> scalar;
            cout << "Which row will it be added to?" << endl;
            cin >> row_two;
            good_choice_two = CheckForGoodRowInput(row_one);
            if (good_choice_two == 0)
                good_choice_two = CheckForGoodRowInput(row_two);
            if (row_one == row_two)
                good_choice_two = 1;
            if (scalar == 0)
                good_choice_two = 1;
        }
        if (row_one == 1)
            if (row_two == 2)
                AddMultipleOfOneToAnother(first_equation, second_equation, 
scalar);
            else AddMultipleOfOneToAnother(first_equation, third_equation, 
scalar);
        else if (row_one == 2)
            if (row_two == 1)
                AddMultipleOfOneToAnother(second_equation, first_equation, 
scalar);
            else AddMultipleOfOneToAnother(second_equation, third_equation, 
scalar);
        else
            if (row_two == 1)
                AddMultipleOfOneToAnother(third_equation, first_equation, 
scalar);
            else AddMultipleOfOneToAnother(third_equation, second_equation, 
scalar);
            Print_Matrix(first_equation, second_equation, third_equation);
            good_choice = 1;
            break;
    }
} while (menu_choice != 5);
system("pause");

return 0;
}

void Print_Matrix(int first_equation[], int second_equation[], int 
third_equation[])
{
cout << "[" << endl;
for (int j = 0; j < 3; j++)
{
    switch (j)
    {
    case(0):
        for (int i = 0; i < 4; i++) {
            if (first_equation[i] >= 0)
                cout << " ";
            if (first_equation[i]<10 && first_equation[i]>-10)
                cout << " ";
            cout << first_equation[i] << "  ";
            if (i == 2)
                cout << "|  ";
        }
        cout << endl;
        break;
    case(1):
        for (int i = 0; i < 4; i++) {
            if (second_equation[i] >= 0)
                cout << " ";
            if (second_equation[i]<10 && second_equation[i]>-10)
                cout << " ";
            cout << second_equation[i] << "  ";
            if (i == 2)
                cout << "|  ";
        }
        cout << endl;
        break;
    case(2):
        for (int i = 0; i < 4; i++) {
            if (third_equation[i] >= 0)
                cout << " ";
            if (third_equation[i]<10 && third_equation[i]>-10)
                cout << " ";
            cout << third_equation[i] << "  ";
            if (i == 2)
                cout << "|  ";
        }
        cout << endl;
        break;

    }
}
cout << "]" << endl;


}

;The following is Assembly file called by cpp file

.386
.MODEL FLAT, C
.STACK 4096

;ExitProcess PROTO, dwExitCode:DWord

.data
.code

CheckForGoodMenuInput PROC
push ebp
mov ebp,esp
mov edi,[ebp+8]
cmp edi, 1
je Good
cmp edi, 2
je Good
cmp edi, 3
je Good
cmp edi, 4
je Good
cmp edi, 5
je Good
mov eax,1
pop ebp
ret
Good :
mov eax,0
pop ebp
ret
CheckForGoodMenuInput ENDP


CheckForGoodRowInput PROC
push ebp
mov ebp, esp
mov edi,[ebp+8]
cmp edi, 1
je Good
cmp edi, 2
je Good
cmp edi, 3
je Good
mov eax,1
pop ebp
ret
Good :
mov eax,0
pop ebp
ret
CheckForGoodRowInput ENDP


SwitchTwoRows PROC
COMMENT @
@
push ebp
mov ebp, esp
mov edi, [ebp+12]
mov esi, [ebp+8]
mov ecx, 4
L1:
mov eax, [edi]
mov edx, [esi]
mov [edi], edx
mov [esi], eax
add esi, 4
add edi, 4
loop L1

pop ebp
ret

SwitchTwoRows ENDP


MultiplyRowByScalar PROC,
ptrArray: ptr DWORD, scalar: DWORD
mov ecx, 4
L1:
push ebp
mov ebp, esp
mov edx, [ebp+8]
mov edi, [ebp+12]
mov esi, [ebp+20]
mov ebx, scalar
mov eax, [ebp]
imul ebx
sub ecx, 1
cmp ecx, 0
je DONE
loop L1
DONE:
pop ebp
ret


MultiplyRowByScalar ENDP

DivideRowByScalar PROC
xor eax, eax
ret
DivideRowByScalar ENDP

AddMultipleOfOneToAnother PROC
xor eax, eax
ret
AddMultipleOfOneToAnother ENDP

END

Hi, here we have gotten the row to multiply the first time through but when I choose to multiply again(without restarting) the numbers don't come out right. Here's a screenshot and the code for this procedure. Thanks for your help! Screenshot of numbers showing wrong values

MultiplyRowByScalar PROC push ebp mov ebp,esp mov esi, [ebp+8] mov ecx, 4 L1: mov ebx, [esi] imul ebx mov [esi], eax add esi, 4 loop L1 pop ebp ret

MultiplyRowByScalar ENDP

Hi it looks like it's putting by exponent instead of multiplying. I can't see why. Any idea?

MultiplyRowByScalar PROC
    push ebp
    mov ebp,esp
    mov esi, [ebp+8]
    mov ecx, 4
L1:
    mov ebx, [esi]
    imul ebx
    mov [esi], eax
    add esi, 4
    loop L1
    pop ebp
    ret

MultiplyRowByScalar ENDPShowing by exponent 3

Comments