Swap two array elements in MIPS -
i have swap 2 array elements. i've done this, in lw $t2, 0($t0) #$t2 = array [i] starts crashing , don't know how fix it.
here code:
.data vector: .word 10, 20, 3, 20, 20, 50, 560, 100, 1005, 10 j: .word 0 i: .word 0 cad: .asciiz "introduce el indice del primer elemento:\n" cad2: .asciiz "introduce el indice del segundo elemento:\n" .text main: la $a1, vector #cargo en a1 la dirección base del vector la $a2, #cargo en a2 el índice la $a3, j #cargo en a3 el índice j move $a1, $zero #inicializo cero move $a2, $zero #inicializo j cero li $v0,4 #imprimo un string la $a0, cad syscall li $v0, 5 #leo el primer índice syscall move $a1, $v0 #muevo el entero de v0 a1 li $v0,4 #imprimo un string la $a0, cad2 syscall li $v0,5 #leo el segundo índice syscall move $a2, $v0 #muevo el entero de v0 a2 sll $t0, $a2,2 # $t0 = i*4 add $t0, $t0, $a1 # $t0 contiene la dirección del vector[i] sll $t1, $a3, 2 # $t1 = j*4 add $t1, $t1, $a1 # $t1 contiene la dirección del vector[j] lw $t2, 0($t0) #$t2 = array [i] lw $t3, 0($t1) #$t3 = array[j] sw $t2, 0($t1) #array[j]=array[i] sw $t3, 0($t0) #array[i]=previous values of array[j] syscall li $v0,1 la $a0, vector syscall li $v0, 10 syscall thanks help, , sorry if i've said wrong, see i'm spain.
looking @ code see 2 issues:
you using $a1 store base address of vector, $a1 used argument syscalls. in code , see overwriting $a1 syscall. suggest use other register store base address of vector.
you overwriting $a1 when want initialize index variables (i , j), according code should $a2 , $a3.
Comments
Post a Comment