# # Autor(s) : Jordi Ferrer Plana # e-mail : jferrerp@eia.udg.es # Branch : Estructura i Tecnologia de Computadors (ETIS/ETIG) # # Working Group : Departament d'Electrònica, Informàtica i Automàtica # Project : Exemples # # Homepage : http://eia.udg.es/etc/ # # Module : Mitja dels elements d'un vector en assemblador de # MIPS (SPIM). # # File : mitja.s # Date : 16/05/2003 - 16/05/2003 # Encoding : ISO-8859-1 (Latin-1) # # Compiler : SPIM >= ver. 6.5 # Libraries : - # # Notes : - Realitza la mitja d'un vector d'enters (32 bits). # # ---------------------------------------------------------------------------- # # 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. # # ---------------------------------------------------------------------------- # .text main: add $t1, $0, $0 # $t1 <- 0 m = 0 lw $t0, n($0) # $t0 = n add $t2, $0, $0 # $t2 = i <- 0 for ( i = 0; Loop: slt $t5, $t2, $t0 # $t5 <- i < n ;i < n; beq $t5, $0, Exit # No: Sortir sll $t4, $t2, 2 # $t4 <- i * 4 lw $t6, X($t4) # $t6 <- X[i] add $t1, $t1, $t6 # $t7 <- m + X[i] m = m + X[i] addi $t2, $t2, 1 # $t2 <- $t2 + 1 ;i++ ) j Loop # Propera iteració Exit: # Fora de for div $t1, $t0 # Lo <- m div n mflo $t1 # $t1 <- Lo sw $t1, m($0) # Guardar el resultat .end .data # Les dades n: .word 4 # Número d'elements X: .word 6, 3, 2, 5 # Vector m: .word 0 # Resultat de la mitja (entera) .end