E
E
Elizabeth2020-05-20 15:46:24
C++ / C#
Elizabeth, 2020-05-20 15:46:24

How to swap the first row and column of a matrix in assembler?

Hello! The problem is this: Given a task, you need to write a program in which the main module is in C C++, and the additional module is in Assembly language. The program must necessarily consist of 2 separate files, i.e. You cannot embed the assembler code in the C ++ code. From the main module I pass the number of matrix elements, and a pointer to the first matrix element.

In the assembler, you need to: swap the elements of the first column and the first row in the matrix, if the sum of the first column = the sum of the first row.

Code written in C++:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <conio.h>

//extern "C" void asm_proc(int **a, int, int); // описание прототипа ASM-подпрограммы
using namespace std;

void Input(int** a, int n);
void Output(int** a, int n);
void Delete(int** a, int n);
void Obm (int** a, int n);
int main()
{
  setlocale(LC_ALL, "russian");
  srand(time(0)); // генерация случайных чисел
  int** a = new int* [3]; // 3 строки в массиве
  for (int count = 0; count < 3; count++)
    a[count]= new int[3]; // и 3 столбцов
  cout << "Задание: Если сумма элементов первой строки = сумме элементов первого столбца, " << endl << "обмнять элементы первой строки с элементами первого столбца " << endl<<endl;
  cout << "Введите элементы массива: " << endl;
  Input(a, 3);
  //asm_proc(a, 3);
  Obm(a, 3);
  cout << "Массив после оброботки: " << endl;
  Output(a,3);
  Delete(a, 3);
  getchar(); getchar();
  return 0;
}
void Obm(int** a, int n)
{
  int str = 0, stb = 0, d, i= 0, j =0;
va: 
    str += a[0][i];
    stb += a[i][0];
    i++;
    if (i != n)
      goto va;

  if (str == stb)
  {
vb:		
      d = a[0][j];
      a[0][j] = a[j][0];
      a[j][0] = d;
      j++;
      if (j != n)
        goto vb;
  }
}
void Input(int **a, int n)//функция заполнения массива с входными параметрами: массив и ограничение
{
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
      cin >> a[i][j] ; // заполняем случайными числами
  }
}
void Output(int** a, int n)//функция вывода массива в консоль
{
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
      cout << a[i][j] << "\t";//потоковый вывод
    cout << endl;//переход на следующую строку
  }
}
void Delete(int** a, int n)
{
  for (int i = 0; i < n; i++)
    delete[]a[i];
}


Help, please write the code in assembler. I started writing something, but I don’t know how it should look like and got confused:
.586
.model flat, stdcall
option   casemap  :  none    
 
.data
 
A   dd  1,2,3,11,12,13,21,22,23
len_str  dd 0       
n_str   dd 3        
n_colum   dd 3      
 
.code
start:
 
mov eax,n_colum
shl eax,2
mov len_str,eax     
 
Xor esi,esi
Mov ecx,n_str           
    m2: Xor edi,edi
        Push ecx
        Mov ecx,   n_colum         
    m1: Mov eax,   A[esi][edi*4]    
        Inc edi             
        Loop  m1            
        add  esi,len_str            
                 
        Pop ecx         
        Dec ecx
        Jnz m2              
    end start


Or at least write how it should look like passing an array from C ++ to assembler and returning back the edited one. I will write the edit myself

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-05-20
@freeExec

Instead of assembler, write C code first. Then you get rid of the loops and rewrite them to goto. And now transfer it to asm.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question