# # Autor(s) : Jordi Ferrer Plana # e-mail : jferrerp@eia.udg.es # Branch : - # # Working Group : Departament d'Electrōnica, Informātica i Automātica # Project : Examen d'ETIS/ETIG d'Estructura i Tecnologia de Computadors, # 2era convocatōria, any 2003. # Homepage : http://eia.udg.es/etc/ # # Module : Problema 5. Producte d'un vector columna per un vector fila. # # File : producte.s # Date : 30/06/2003 - 01/07/2003 # # Compiler : Spim 6.3 # Libraries : - # # Notes : - Cālcul del producte d'un vector columna (n,1) per un vector # fila (1,n). El resultat és una matriu quadrada de (n,n). # # M(n,n) = x(n,1) * y(1,n) # # - El cālcul de cada un dels a elements de la mātriu és # # aij = xi * yj # # - La matriu resultant estarā contigua per files a memōria. # # ---------------------------------------------------------------------------- # # Copyright (C) 2002-2003, Jordi Ferrer Plana # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # See the GNU General Public License (http://www.gnu.org/copyleft/) # for more details. # # ---------------------------------------------------------------------------- # .data # Segment de dades N=3 # Matriu resultant d'N files per N columnes Sv=12 # S = N * 4 ( L'Spim 6.3 no precompila Sm=36 # S = N * N * 4 expressions p.e. N * 4 ) x: .space Sv # Reservar espai pel vector columna x y: .space Sv # Reservar espai pel vector fila y M: .space Sm # Reservar espai per la matriu M = x * y .end .text # Segment de text (codi) main: # Funciķ principal 'main' subu $sp, $sp, 4 # Reservar espai pels registres a salvar sw $ra, 0($sp) # Salvar l'adreįa de retorn li $t0, N # $t0 = N (total de files o columnes) li $t1, 0 # $t1 = i <- 0 for ( i = 0; Fori: # For exterior ( i ) slt $t5, $t1, $t0 # $t5 <- i < N ; i < N; beq $t5, $0, ExitFori # Si $t5 == 0 -> Acabar les iteracions de i li $t2, 0 # $t2 = j <- 0 for ( j = 0; Forj: # For interior ( j ) slt $t5, $t2, $t0 # $t5 <- j < N ; j < N; beq $t5, $0, ExitForj # Si $t5 == 0 -> Acabar les iteracions de j # Calcular l'adreįa origen de x sll $t5, $t1, 2 # $t5 <- i * 4 lw $t4, x($t5) # $t4 <- x(i) # Calcular l'adreįa origen de y sll $t5, $t2, 2 # $t5 <- j * 4 lw $t6, y($t5) # $t6 <- y(j) # Realitzar el cālcul de xi * yj mul $t4, $t4, $t6 # $t4 <- x(i) * y(j) # Calcular l'offset destí de M, # que és a i * N + j # $t1 * $t0 + $t2 mul $t5, $t1, $t0 # $t5 <- $t1 * $t0 (i * N) add $t5, $t5, $t2 # $t5 <- $t5 + $t2 (i * N + j) sll $t5, $t5, 2 # $t5 <- $t5 * 4 (i * N + j) * 4 (word) sw $t4, M($t5) # M[ ( i * N + j ) * 4 ] <- $t4 addi $t2, $t2, 1 # $t2 <- $t2 + 1 ; j++ ) j Forj ExitForj: addi $t1, $t1, 1 # $t1 <- $t1 + 1 ; i++ ) j Fori ExitFori: or $v0, $0, $0 # Retornar un 0 al Sistema Operatiu (Spim) lw $ra, 0($sp) # Restaurar l'adreįa de retorn salvada addu $sp, $sp, 4 # Eliminar espai de pila reservat j $ra # Retorn al Sistema Operatiu (Spim) .end