forked from daltonbr/MatrixAssembly
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainATT.c
More file actions
146 lines (122 loc) · 3.99 KB
/
mainATT.c
File metadata and controls
146 lines (122 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
https://github.com/daltonbr/MatrixAssembly
Authors:
Bruno Vedovetto @bleandro
Dalton Lima @daltonbr
Lucas Pinheiro @lucaspin
Wesley Otto @ottozinho
Description:
Consider three matrices of integers A, B, C and L x L size, defined in C
language (these matrices could be randomly generated). Implement three
equivalent functions (all functions must run in C language application):
The matrices must be static instantiates in C, at compiling-time.
These matrix must be ramdonly POPULATED, not randonly sized. In relation to L,
its good that the size could vary in fixed intervals (5x5, 10x10, 50x50,
100x100, etc).
Group 1: (a * 2b) - returning the greater value in the main diagonal.
* This version uses Inline function using AT&T Assembly
$ as --32 -o greaterInDiagonalATT.o greaterInDiagonal.s
$ gcc -m32 -o mainATT.out mainATT.c greaterInDiagonalATT.o
*/
#include <stdio.h>
#include <stdlib.h> //malloc
#include <time.h> // srand
#define L 3
void printMatrix(int matrix[L][L]);
void printDiagonal(int matrix[L][L]);
void randomPopulateMatrix(int matrix[L][L]);
int greaterValueInDiagonal(int matrix[L][L]);
void scalarTimesMatrix (int _scalar, int matrix[L][L], int outputMatrix[L][L]);
void matrixTimesMatrix (int firstMatrix[L][L], int secondMatrix[L][L], int outputMatrix[L][L]);
int main(void) {
int a[L][L];
int b[L][L];
int outputMatrix[L][L];
int bTimes2[L][L];
int i, j, r;
srand(time(NULL));
printf("--== Matrix Operations in Assembly AT&T! ==--\n");
printf("* All matrices bellow has a fixed [%dx%d] size\n",L, L);
printf("* They are randomly populate with integers between 0 and 99\n");
printf("* We calculate (a*2b)\n");
printf("* We return the greater element from the main diagonal\n");
printf("\nPrinting 'a' matrix\n");
randomPopulateMatrix(a);
printMatrix(a);
int greaterAssembly = 0;
extern int greaterInDiagonal_s (int, int *);
greaterAssembly = greaterInDiagonal_s(L, *a);
printf("[AT&T]Greater Element in 'a' matrix's diagonal: %d\n", greaterAssembly);
randomPopulateMatrix(b);
printf("\nPrinting 'b' matrix: \n");
printMatrix(b);
/* (2*b) */
extern int scalarTimesMatrix_s (int, int, int *, int *);
scalarTimesMatrix_s(L, 2, *b, *bTimes2);
printf("\nPrinting 2 * 'b' matrix: \n");
printMatrix(bTimes2);
/*
// [WIP] matrixTimesMatrix_s
extern int matrixTimesMatrix_s (int, int *, int *, int *);
int test;
test = matrixTimesMatrix_s(L, *a, *bTimes2, *outputMatrix);
printf("\n(a * 2b) = %d\n", test);
printMatrix(outputMatrix);
*/
}
void printMatrix(int matrix[L][L]) {
int i, j, tempElement;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
tempElement = matrix[i][j];
printf(" [%d%d]: (%d) |", i, j, tempElement);
}
printf("\n");
}
}
void printDiagonal(int matrix[L][L]) {
int i, tempElement;
for (i = 0; i < L; i++) {
tempElement = matrix[i][i];
printf(" [%d%d]: (%d) |", i, i, tempElement);
}
}
void randomPopulateMatrix(int matrix[L][L]) {
int i, j, tempElement;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
matrix[i][j] = rand() % 100; //returns a pseudo-random integer between 0 and 99
}
}
}
int greaterValueInDiagonal(int matrix[L][L]) {
int i = 0, greaterElement;
greaterElement = matrix[i][i];
for (i = 1; i < L; i++) {
if (greaterElement < matrix[i][i]) {
greaterElement = matrix[i][i];
}
}
return greaterElement;
}
/* void or return an outputMatrix */
void scalarTimesMatrix (int _scalar, int matrix[L][L], int outputMatrix[L][L]) {
int i, j;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
outputMatrix[i][j] = _scalar * matrix[i][j];
}
}
}
void matrixTimesMatrix (int firstMatrix[L][L], int secondMatrix[L][L], int outputMatrix[L][L]) {
int i, j, k, accumulator;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
accumulator = 0;
for (k = 0; k < L ; k++) {
accumulator += firstMatrix[i][k] * secondMatrix[k][j];
}
outputMatrix[i][j] = accumulator;
}
}
}