# # 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, # 1era convocatòria, any 2003. # Homepage : http://eia.udg.es/etc/ # # Module : Exercici 5. Transposta d'una matriu. # # File : transposta.s # Date : 11/06/2003 - 30/06/2003 # # Compiler : Spim 6.3 # Libraries : - # # Notes : - Càlcul de la transposta d'una Matriu de dimensions N x M. # # ---------------------------------------------------------------------------- # # 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 d'N Files M=2 # per M Columnes S=24 # S = N * M * 4 ( L'Spim 6.3 no precompila # expressions p.e. N * M * 4 ) # Un 'float' ocupa 4 bytes * N Files * M Columnes Matriu: .space S # Reservar espai per la matriu origen Resultat: .space S # Reservar espai per la matriu resultant .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) li $t1, M # $t1 = M (total de columnes) li $t2, 0 # $t2 = i <- 0 for ( i = 0; Fori: # For exterior ( i ) slt $t5, $t2, $t0 # $t5 <- i < N ; i < N; beq $t5, $0, ExitFori # Si $t5 == 0 -> Acabar les iteracions de i li $t3, 0 # $t3 = j <- 0 for ( j = 0; Forj: # For interior ( j ) slt $t5, $t3, $t1 # $t5 <- j < M ; j < M; beq $t5, $0, ExitForj # Si $t5 == 0 -> Acabar les iteracions de j # Copiar el valor Mij a Rji # L'origen és a i * M + j # $t2 * $t1 + $t3 mul $t5, $t2, $t1 # $t5 <- $t2 * $t1 (i * M) add $t5, $t5, $t3 # $t5 <- $t5 + $t3 (i * M + j) sll $t5, $t5, 2 # $t5 <- $t5 * 4 (i * M + j) * 4 (floats) lw $t4, Matriu($t5) # $t4 <- Matriu[ ( i * M + j ) * 4 ] # El destí es a j * N + i # $t3 * $t0 + $t2 mul $t5, $t3, $t0 # $t5 <- $t3 * $t0 (j * N) add $t5, $t5, $t2 # $t5 <- $t5 + $t2 (j * N + i) sll $t5, $t5, 2 # $t5 <- $t5 * 4 (j * N + i) * 4 (floats) sw $t4, Resultat($t5) # Resultat[ ( j * N + i ) * 4 ] <- $t4 addi $t3, $t3, 1 # $t3 <- $t3 + 1 ; j++ ) j Forj ExitForj: addi $t2, $t2, 1 # $t2 <- $t2 + 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