college/ws2010/gdi3/p3/matrixmultiplikation.asm
2011-10-18 10:28:57 +02:00

218 lines
5.4 KiB
NASM

.data
# Input Data
matsize: .long 3
matA: .long 1, 2, 3, 4, 5, 6, 7, 8, 9
matB: .long -1, 9, -2, 3, -11, 5, 7, 4, -2
matResult: .long 0, 0, 0, 0, 0, 0, 0, 0, 0 # Initialisiere Datenfeld
#should be 26, -1, 2, 53, 5, 5, 80, 11, 8 -> Test okay!
# 10 x 10
#matsize: .long 10
#matA: .long 5, 4, 8, 10, 20, -5, -11, -14, 9, 100, 3, 8, 57, -1, -2, 41, 11, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, -14, -15, -16, -17, -18, -19, -20, 1, 2, 5, 8, 9, -7, -9, 1, -7, -4, 5, 4, 8, 10, 20, -5, -11, -14, 9, 100, 3, 8, 57, -1, -2, 41, 11, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, -14, -15, -16, -17, -18, -19, -20, 1, 2, 5, 8, 9, -7, -9, 1, -7, -4
#matB: .long 5, 4, 8, 10, 20, -5, -11, -14, 9, 100, 3, 8, 57, -1, -2, 41, 11, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, -14, -15, -16, -17, -18, -19, -20, 1, 2, 5, 8, 9, -7, -9, 1, -7, -4, 5, 4, 8, 10, 20, -5, -11, -14, 9, 100, 3, 8, 57, -1, -2, 41, 11, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, -14, -15, -16, -17, -18, -19, -20, 1, 2, 5, 8, 9, -7, -9, 1, -7, -4
#matResult: .long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
intout: .string "Ergebnismatrix: \n"
out_newline: .string "\n"
out_int: .string "%d\t"
.text
.globl main
main:
movl $0, %edi # %edi Index von matResult
movl $1, %eax # %eax = i = 1
movl $1, %ebx # %ebx = j = 1
movl matsize, %edx # %edx = matsize
incl %edx # %edx = matsize + 1
##########################################################################################
.loop_line: # loop über i
cmp %edx, %eax # i = matsize + 1 ?
je .loop_line_end
#########################################################################
.loop_column: # loop über j
movl matsize, %edx # %edx = matsize
incl %edx # %edx = matsize + 1
cmp %edx, %ebx # j = matsize + 1 ? -> zeile fertig
je .loop_column_end
pushl %eax # Sichere %eax, da get_cij es ändert
pushl %ebx # Sichere %eax, da get_cij es ändert
call .get_cij # gibt wert c(ij) in %eax zurück
movl %eax, matResult(,%edi,4) # Speichere Wert im Datenfeld
popl %ebx # Stelle ursprüngliche Werte (j, i) wieder her
popl %eax
incl %edi # %edi += 1
incl %ebx # j++
jmp .loop_column
.loop_column_end:
#########################################################################
incl %eax # i++
movl $1, %ebx # j = 1
jmp .loop_line
.loop_line_end:
##########################################################################################
jmp .end
####################################################################################################
/*
get_cij konsumiert Indizes i und j und gibt Summe von k=1 bis m=matsize von (a(ik) * b(kj)) zurück
Eingabe: %eax = i
%ebx = j
Ausgabe: %eax = Ergebnis der Summation
*/
.get_cij:
pushl %eax # i liegt bei 12(%ebp)
pushl %ebx # j liegt bei 8(%ebp)
pushl $0 # Zwischenergebnis liegt bei 4(%ebp) -> initialisiert mit 0
pushl %ebp
movl %esp, %ebp
movl $1, %ecx # %ecx = k
.loop_sum:
movl matsize, %eax
incl %eax # matsize + 1
cmp %eax, %ecx # matsize + 1 = k
je .end_loop_sum
# Hole Wert von Matrix A
movl matsize, %ebx # %ebx = matsize
movl 12(%ebp), %eax # i in %eax
mull %ebx # i * matsize (Adressberechnung)
subl %ebx, %eax # %eax = %eax - matsize
addl %ecx, %eax # %eax = %eax + k
decl %eax # %eax = %eax - 1, denn Index von Array beginnt bei 0
movl matA(,%eax,4), %eax # passender Wert der Matrix A in %eax
pushl %eax # Sichere Wert von Matrix A
#Hole Wert von Matrix B
movl matsize, %eax # %eax = matsize
mull %ecx # k * matsize (Adressberechnung)
movl %eax, %ebx # %eax nach %ebx verschieben
movl matsize, %eax # matsize wiederherstellen
subl %eax, %ebx # %ebx = %ebx - matsize
movl 8(%ebp), %eax # j in %eax
addl %eax, %ebx # %ebx = %ebx + j
decl %ebx # %eax = %eax - 1, denn Index von Array beginnt bei 0
movl matB(,%ebx,4), %ebx # passender Wert der Matrix B in %ebx
popl %eax # Wert der Matrix A wiederherstellen
#Führe nun eigentliche Berechnung durch
imull %ebx, %eax # %eax = a(ij) * b(kj) !!Vorzeichenbehaftet!!
addl %eax, 4(%ebp) # Zwischenergebnis + aktuelle Multiplikation
incl %ecx # k += 1
jmp .loop_sum
.end_loop_sum:
movl 4(%ebp), %eax # Ergebnis der Summe in %eax ausgeben
leave
addl $12, %esp # Stack abbauen
ret
####################################################################################################
## Ausgabe:
.end:
#Stack
pushl $0 //loop a counter
pushl $0 //loop b-counter
# print "Ergebnismatrix"
pushl $intout
call printf
#print Datafield
#Loop Rows
.end_loop_row:
movl 4(%esp), %eax
cmp matsize, %eax
je .end_loop_row_end
movl $0, (%esp)
#Loop Columns
.end_loop_column:
movl (%esp), %eax
cmp matsize, %eax
je .end_loop_column_end
movl matsize, %edi
imull 4(%esp), %edi
addl (%esp), %edi
pushl matResult(,%edi,4)
pushl $out_int
call printf
addl $8, %esp
incl (%esp)
jmp .end_loop_column
.end_loop_column_end:
pushl $out_newline
call printf
addl $4, %esp
incl 4(%esp)
jmp .end_loop_row
.end_loop_row_end:
# Exit
movl $1, %eax
int $0x80