# # 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 : Suma de dos vectors en assemblador de MIPS (SPIM). # # File : suma.s # Date : 05/05/2003 - 05/05/2003 # Encoding : ISO-8859-1 (Latin-1) # # Compiler : SPIM >= ver. 6.5 # Libraries : - # # Notes : - Realitza la suma de dos vector d'enters (32 bits) i # deixa el resultat en un tercer vector # # ---------------------------------------------------------------------------- # # Copyright (C) 2004-2005, 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 = 5 X: .word 10, 20, 30, 5, 3 # Vector X Y: .word 5, 11, 18, 2, 1 # Vector Y Z: .word 0, 0, 0, 0, 0 # Resultat. Una altra declaració pot ser: # Z: .space 20 .end .text # Segment de programa main: addi $t1, $0, N # $t1 = N add $t0, $0, $0 # $t0 = i for ( i = 0; for: slt $t2, $t0, $t1 # $t2 <- i < N i < N; beq $t2, $0, exit # No: Sortir sll $t3, $t0, 2 # Obtenir offset: $t3 <- i * 4 lw $t4, X($t3) # $t4 <- X[i] lw $t5, Y($t3) # $t5 <- Y[i] add $t6, $t4, $t5 # $t6 <- X[i] + Y[i] sw $t6, Z($t3) # Guardar $t6 Z[i] <- X[i] + Y[i] addi $t0, $t0, 1 # $t0 <- $t0 + 1 i++ ) j for # Propera iteració exit: .end