Print sum of vectors MIPS - vector

I am trying to do exercise of MIPS, i have to sum to vector into another vector and then print the sum, I know for sure something is wrong with the sum, because the output is 0 0 0 0 0. Can someone explain me where i am wrong? Be patient please :) Thanks
.data
str: .asciiz " "
v1 : .word 3, 10, 5, 22, 13
v2 : .word 7, 1, 9, 16, 8
.text
la $a1, v1
la $a2, v2
addi $sp, $sp, -20
sw $s1, 0($sp)
add $t0, $0, $0 # i = 0
somma: slti $t1, $t0, 5 #if i < 5 continua else esci
beq $t1, $0, esci
sll $t2, $t0, 2 # i = i * 4
add $t3, $a1, $t2 # $t2 = v1 + i*4
add $t4, $a2, $t2 # $t3 = v2 + i*4
lw $t3, 0($t3) #$t3 = v1[i]
lw $t4, 0($t4) #$t4 = v2[i]
add $s1, $t3, $t4 # v3 = v1 + v2
addi $t0, $t0, 1
j somma
esci:
add $t0, $0, $0 # i = 0
stampa: slti $t1, $t0, 5 # se i<5 continua else fine
beq $t1, $0, fine
lw $a0, 0($sp)
li $v0, 1
syscall
li $v0, 4
la $a0, str
addi $t0, $t0, 1
addi $sp, $sp, 4
syscall
j stampa
fine: li $v0, 10
syscall

If your intent is to use some stack space for a temporary vector holding the sum:
addiu $sp, $sp, -20
allocates the stack space. The allocated stack space is addressable from $sp+0 through $sp+16. (Storing $s1 there is pointless, so just remove that.)
After the vector element addition into $t1, store the element sum result into the temporary vector in the stack, using indexing like you do with the source vectors:
addu $t5, $sp, $t2 # compute address of temp vector element to store
sw $t1, 0($t5)

Related

Octave Gauss Jordan

I have been trying to do Gauss jordan method in matrices in octave, and I have found diferent ways to call the function, but none works. How can I do that?
A=[1 -2 -1; -1 1 1; 1 1 5]
B=[1; 4; -3]
#format rat
#Transposta = transpose(A) #Transposta
#Inversa = inv(A) #Inversa
#Adjunta = det(A)*inv(A) #Adjunta se A tiver inversa
determinante = det(A) #Determinante.
Resultado = Gaussian(A)
Use the rref function...
A = [1 -2 -1; -1 1 1; 1 1 5]
B = [1; 4; -3]
C = [A, B]
[a, b] = rref(C)
Result
a = 1.0000 0 0 -7.1667
0 1.0000 0 -5.0000
0 0 1.0000 1.8333
More Info: https://docs.octave.org/v7.3.0/Basic-Matrix-Functions.html

got 3 arguments but expected 2 lui a1, %hi(.LJTI0_0)

I am converting the dhrystone benchmark to test its performance on a riscv compiled version. I have generated the assembly code and now intend to convert it into a hexcode format using the venus-simulator.
But when i run the code on the simulator i get multiple warnings regarding the same few lines. it seems that i am passing extra arguments in the 'lui', 'addi' and a few other instructions. I cant seem to make any sense of it.
Your help is greatly appreciated.
Here is the code
Proc_6: # #Proc_6
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
sw a1, -20(s0)
lw a0, -16(s0)
lw a1, -20(s0)
sw a0, 0(a1)
lw a0, -16(s0)
call Func_3
li a1, 0
bne a0, a1, .LBB0_2
j .LBB0_1
.LBB0_1:
lw a1, -20(s0)
li a0, 3
sw a0, 0(a1)
j .LBB0_2
.LBB0_2:
lw a1, -16(s0)
sw a1, -24(s0) # 4-byte Folded Spill
li a0, 4
bltu a0, a1, .LBB0_12
lw a0, -24(s0) # 4-byte Folded Reload
slli a0, a0, 2
lui a1, %hi(.LJTI0_0)
addi a1, a1, %lo(.LJTI0_0)
add a0, a0, a1
lw a0, 0(a0)
jr a0
.LBB0_4:
lw a1, -20(s0)
li a0, 0
sw a0, 0(a1)
j .LBB0_12
.LBB0_5:
lui a0, %hi(Int_Glob)
lw a0, %lo(Int_Glob)(a0)
li a1, 101
blt a0, a1, .LBB0_7
j .LBB0_6
.LBB0_6:
lw a1, -20(s0)
li a0, 0
sw a0, 0(a1)
j .LBB0_8
.LBB0_7:
lw a1, -20(s0)
li a0, 3
sw a0, 0(a1)
j .LBB0_8
.LBB0_8:
j .LBB0_12
.LBB0_9:
lw a1, -20(s0)
li a0, 1
sw a0, 0(a1)
j .LBB0_12
.LBB0_10:
j .LBB0_12
.LBB0_11:
lw a1, -20(s0)
li a0, 2
sw a0, 0(a1)
j .LBB0_12
.LBB0_12:
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
.LJTI0_0:
.word .LBB0_4
.word .LBB0_5
.word .LBB0_9
.word .LBB0_10
.word .LBB0_11
Proc_7: # #Proc_7
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
sw a1, -20(s0)
sw a2, -24(s0)
lw a0, -16(s0)
addi a0, a0, 2
sw a0, -28(s0)
lw a0, -20(s0)
lw a1, -28(s0)
add a0, a0, a1
lw a1, -24(s0)
sw a0, 0(a1)
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
Proc_8: # #Proc_8
addi sp, sp, -48
sw ra, 44(sp) # 4-byte Folded Spill
sw s0, 40(sp) # 4-byte Folded Spill
addi s0, sp, 48
sw a0, -16(s0)
sw a1, -20(s0)
sw a2, -24(s0)
sw a3, -28(s0)
lw a0, -24(s0)
addi a0, a0, 5
sw a0, -36(s0)
lw a0, -28(s0)
lw a1, -16(s0)
lw a2, -36(s0)
slli a2, a2, 2
add a1, a1, a2
sw a0, 0(a1)
lw a0, -16(s0)
lw a1, -36(s0)
slli a1, a1, 2
add a1, a1, a0
lw a0, 0(a1)
sw a0, 4(a1)
lw a0, -36(s0)
lw a2, -16(s0)
slli a1, a0, 2
add a1, a1, a2
sw a0, 120(a1)
lw a0, -36(s0)
sw a0, -32(s0)
j .LBB2_1
.LBB2_1: # =>This Inner Loop Header: Depth=1
lw a1, -32(s0)
lw a0, -36(s0)
addi a0, a0, 1
blt a0, a1, .LBB2_4
j .LBB2_2
.LBB2_2: # in Loop: Header=BB2_1 Depth=1
lw a0, -36(s0)
lw a1, -20(s0)
li a2, 200
mul a2, a0, a2
add a1, a1, a2
lw a2, -32(s0)
slli a2, a2, 2
add a1, a1, a2
sw a0, 0(a1)
j .LBB2_3
.LBB2_3: # in Loop: Header=BB2_1 Depth=1
lw a0, -32(s0)
addi a0, a0, 1
sw a0, -32(s0)
j .LBB2_1
.LBB2_4:
lw a1, -20(s0)
lw a0, -36(s0)
li a4, 200
mul a2, a0, a4
add a1, a1, a2
slli a0, a0, 2
add a1, a1, a0
lw a0, -4(a1)
addi a0, a0, 1
sw a0, -4(a1)
lw a0, -16(s0)
lw a3, -36(s0)
slli a2, a3, 2
add a0, a0, a2
lw a0, 0(a0)
lw a1, -20(s0)
addi a3, a3, 20
mul a3, a3, a4
add a1, a1, a3
add a1, a1, a2
sw a0, 0(a1)
lui a1, %hi(Int_Glob)
li a0, 5
sw a0, %lo(Int_Glob)(a1)
lw a0, -12(s0)
lw ra, 44(sp) # 4-byte Folded Reload
lw s0, 40(sp) # 4-byte Folded Reload
addi sp, sp, 48
ret
Func_1: # #Func_1
addi sp, sp, -16
sw ra, 12(sp) # 4-byte Folded Spill
sw s0, 8(sp) # 4-byte Folded Spill
addi s0, sp, 16
sb a0, -13(s0)
sb a1, -14(s0)
lb a0, -13(s0)
sb a0, -15(s0)
lb a0, -15(s0)
sb a0, -16(s0)
lbu a0, -16(s0)
lbu a1, -14(s0)
beq a0, a1, .LBB3_2
j .LBB3_1
.LBB3_1:
li a0, 0
sw a0, -12(s0)
j .LBB3_3
.LBB3_2:
lb a0, -15(s0)
lui a1, %hi(Ch_1_Glob)
sb a0, %lo(Ch_1_Glob)(a1)
li a0, 1
sw a0, -12(s0)
j .LBB3_3
.LBB3_3:
lw a0, -12(s0)
lw ra, 12(sp) # 4-byte Folded Reload
lw s0, 8(sp) # 4-byte Folded Reload
addi sp, sp, 16
ret
Func_2: # #Func_2
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
sw a1, -20(s0)
li a0, 2
sw a0, -24(s0)
j .LBB4_1
.LBB4_1: # =>This Inner Loop Header: Depth=1
lw a1, -24(s0)
li a0, 2
blt a0, a1, .LBB4_5
j .LBB4_2
.LBB4_2: # in Loop: Header=BB4_1 Depth=1
lw a0, -16(s0)
lw a1, -24(s0)
add a0, a0, a1
lbu a0, 0(a0)
lw a2, -20(s0)
add a1, a1, a2
lbu a1, 1(a1)
call Func_1
li a1, 0
bne a0, a1, .LBB4_4
j .LBB4_3
.LBB4_3: # in Loop: Header=BB4_1 Depth=1
li a0, 65
sb a0, -25(s0)
lw a0, -24(s0)
addi a0, a0, 1
sw a0, -24(s0)
j .LBB4_4
.LBB4_4: # in Loop: Header=BB4_1 Depth=1
j .LBB4_1
.LBB4_5:
lbu a0, -25(s0)
li a1, 87
blt a0, a1, .LBB4_8
j .LBB4_6
.LBB4_6:
lbu a1, -25(s0)
li a0, 89
blt a0, a1, .LBB4_8
j .LBB4_7
.LBB4_7:
li a0, 7
sw a0, -24(s0)
j .LBB4_8
.LBB4_8:
lbu a0, -25(s0)
li a1, 82
bne a0, a1, .LBB4_10
j .LBB4_9
.LBB4_9:
li a0, 1
sw a0, -12(s0)
j .LBB4_13
.LBB4_10:
lw a0, -16(s0)
lw a1, -20(s0)
call strcmp
mv a1, a0
li a0, 0
bge a0, a1, .LBB4_12
j .LBB4_11
.LBB4_11:
lw a0, -24(s0)
addi a0, a0, 7
sw a0, -24(s0)
lw a0, -24(s0)
lui a1, %hi(Int_Glob)
sw a0, %lo(Int_Glob)(a1)
li a0, 1
sw a0, -12(s0)
j .LBB4_13
.LBB4_12:
li a0, 0
sw a0, -12(s0)
j .LBB4_13
.LBB4_13:
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
Func_3: # #Func_3
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
lw a0, -16(s0)
sw a0, -20(s0)
lw a0, -20(s0)
li a1, 2
bne a0, a1, .LBB5_2
j .LBB5_1
.LBB5_1:
li a0, 1
sw a0, -12(s0)
j .LBB5_3
.LBB5_2:
li a0, 0
sw a0, -12(s0)
j .LBB5_3
.LBB5_3:
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
debug_printf: # #debug_printf
addi sp, sp, -48
sw ra, 12(sp) # 4-byte Folded Spill
sw s0, 8(sp) # 4-byte Folded Spill
addi s0, sp, 16
sw a7, 28(s0)
sw a6, 24(s0)
sw a5, 20(s0)
sw a4, 16(s0)
sw a3, 12(s0)
sw a2, 8(s0)
sw a1, 4(s0)
sw a0, -12(s0)
lw ra, 12(sp) # 4-byte Folded Reload
lw s0, 8(sp) # 4-byte Folded Reload
addi sp, sp, 48
ret

How to efficiently extract a row or column from a "dist" distance matrix

I'm working with a 39000+ data points and I'm computing the distance between a point and every single other one of them, resulting in a (39000+)^2 matrix that consumes 11GB (and I can't allocate this in the memory).
Great thing we have the dist function that allows me to reduce this to a little bit less than 6GB. But now, I need to calculate the inverse distances powered by 2 and then regularize every row so that they sum up to 1. This is necessary because I will later multiply every row of the matrix by a vector and store this result. So, the big matrix is actually a temporary thing.
My question is, how can I extract rows of this dist matrix?
A sample "dist" matrix obtained with dist(cbind(runif(5),runif(5))
1 2 3 4
2 0.47
3 0.63 0.72
4 0.79 0.62 0.37
5 0.53 0.15 0.62 0.48
What I'm looking for is to extract the entire first line, for instance:
0 0.47 0.63 0.79 0.53
Resort to function f in my old answer here.
f <- function (i, j, dist_obj) {
if (!inherits(dist_obj, "dist")) stop("please provide a 'dist' object")
n <- attr(dist_obj, "Size")
valid <- (i >= 1) & (j >= 1) & (i > j) & (i <= n) & (j <= n)
k <- (2 * n - j) * (j - 1) / 2 + (i - j)
k[!valid] <- NA_real_
k
}
A helper function to extract a single row / column (a slice).
SliceExtract_dist <- function (dist_obj, k) {
if (length(k) > 1) stop("The function is not 'vectorized'!")
n <- attr(dist_obj, "Size")
if (k < 1 || k > n) stop("k out of bound!")
##
i <- 1:(k - 1)
j <- rep.int(k, k - 1)
v1 <- dist_obj[f(j, i, dist_obj)]
##
i <- (k + 1):n
j <- rep.int(k, n - k)
v2 <- dist_obj[f(i, j, dist_obj)]
##
c(v1, 0, v2)
}
Example
set.seed(0)
( d <- dist(cbind(runif(5),runif(5))) )
# 1 2 3 4
#2 0.9401067
#3 0.9095143 0.1162289
#4 0.5618382 0.3884722 0.3476762
#5 0.4275871 0.6968296 0.6220650 0.3368478
SliceExtract_dist(d, 1)
#[1] 0.0000000 0.9401067 0.9095143 0.5618382 0.4275871
SliceExtract_dist(d, 2)
#[1] 0.9401067 0.0000000 0.1162289 0.3884722 0.6968296
SliceExtract_dist(d, 3)
#[1] 0.9095143 0.1162289 0.0000000 0.3476762 0.6220650
SliceExtract_dist(d, 4)
#[1] 0.5618382 0.3884722 0.3476762 0.0000000 0.3368478
SliceExtract_dist(d, 5)
#[1] 0.4275871 0.6968296 0.6220650 0.3368478 0.0000000
Sanity check
as.matrix(d)
# 1 2 3 4 5
#1 0.0000000 0.9401067 0.9095143 0.5618382 0.4275871
#2 0.9401067 0.0000000 0.1162289 0.3884722 0.6968296
#3 0.9095143 0.1162289 0.0000000 0.3476762 0.6220650
#4 0.5618382 0.3884722 0.3476762 0.0000000 0.3368478
#5 0.4275871 0.6968296 0.6220650 0.3368478 0.0000000
Note: Function to extract diagonals readily exists.

How to remove items in a vector from another vector

I have two vectors:
a = c(1,1,2,2,3,3,4,4)
b = c(1)
I want to remove the first match of b from a. Thus, here only the first 1 is removed from a:
c = c(1,2,2,3,3,4,4)
The order of items in a is not important.
I tried this code:
a[a != b]
a[! a %in% b]
Both results are:
[1] 2 2 3 3 4 4.
All numbers of 1 are removed. However, I only want to remove the specific item in b from a.
If b = c(1, 1, 2), then I wish the result
[1] 2 3 3 4 4
a[-(1:3)]
The above code could lead to the result of [1] 2 3 3 4 4. However, I wish it could be more flexible. For example when the order of items are unknown or random:
a = c(3,4,3,1,2,2,1,4)
How can I do it using R?
vecsets package can perform standard set operations, while retaining duplicates:
vecsets::vsetdiff( c(1,1,2,2,3,3,4,4), c(1) )
## [1] 1 2 2 3 3 4 4
vecsets::vsetdiff( c(1,1,2,2,3,3,4,4), c(1,1,2) )
## [1] 2 3 3 4 4
Note that it will preserve the order of the first argument. Using your last example:
vecsets::vsetdiff( c(3,4,3,1,2,2,1,4), c(1,1,2) )
## [1] 3 4 3 2 4
Taking inspiration from this answer to one of the questions I linked in comment, you can use fsetdiff from the package data.table.
It takes all as argument, which avoids having only the unique values returned, as happens with setdiff:
library(data.table)
# with your first example (b = c(1)):
unlist(fsetdiff(data.table(v1=a), data.table(v1=b), all = TRUE))
# v11 v12 v13 v14 v15 v16 v17
# 1 2 2 3 3 4 4
# with second example (b = c(1, 1, 2)):
unlist(fsetdiff(data.table(v1=a), data.table(v1=b), all = TRUE))
# v11 v12 v13 v14 v15
# 2 3 3 4 4
You can use which()
a = c(3, 4, 3, 1, 2, 2, 1, 4)
a
## [1] 3 4 3 1 2 2 1 4
b = 1
a[- which(a %in% b)[1]]
## [1] 3 4 3 2 2 1 4
Case b has two elements:
b2 = c(1, 2)
sapply(seq_along(b1), function(x) a <<- a[- which(a == x)[1]])[[2]]
## [1] 3 4 3 2 1 4
Or three...
b3 <- c(1, 2, 3)
sapply(seq_along(b1), function(x) a <<- a[- which(a == x)[1]])[[3]]
# [1] 4 3 2 1 4
I don't think that the following is the best solution (the vecsets approach strikes me as the best), but #Aaron's comment about possibly using Rcpp struck me as interesting. This is the first time I used that package. If nothing else, the fact that I was able to get working code in less than 20 minutes underscores his point that Rcpp makes it relatively easy:
library(Rcpp)
cppFunction('
NumericVector difference(NumericVector xs, NumericVector ys){
int m = xs.size();
int n = ys.size();
float flag = 1 + abs(max(xs)) + abs(max(ys)); //occurs in neither xs nor ys
NumericVector zs = clone(xs);
for(int i = 0; i < n; i++){
double y = ys[i];
int j = 0;
while(j < m && zs[j]!= y) j++;
if(j < m) zs[j] = flag;
}
int count = 0;
for(int k = 0; k < m; k++){
if(zs[k] < flag) count++;
}
NumericVector ws(count);
int k = 0;
for(int j = 0; j < m; j++){
if(zs[j] < flag){
ws[k] = zs[j];
k++;
}
}
return ws;
}
')
After you source this:
> a = c(1,1,2,2,3,3,4,4)
> b = c(1,2,1)
> difference(a,b)
[1] 2 3 3 4 4
Since this was my first attempt at such code, I'm sure that it could be improved in multiple ways.
A little frustrating, the syntactical order, but Reduce and which do it with just Base R.
Reduce(b, a) a[-which(a==b)[1]], a, b)

Sort Rows in R by certain characteristic

I've read a few questions that have been answered and are similar, but for the life of me, I can't figure out how to apply it to my question. I have a data frame like this (some entries are blank, the number of columns are determined by the pronunciation guide of a word)
5 SUPERNATURALISM S UW2 P ER0 N AE1 CH ER0 AH0 L IH2 Z AH0 M
6 ESTABLISH IH0 S T AE1 B L IH0 SH
7 FABRIC F AE1 B R IH0 K
1504 AMTRAK AE1 M T R AE0 K
I'd basically like to be able to sort the table according to the number of syllables, and that's easily visible by the number of times you see a number (0,1 or 2) in each row (the 0,1,or 2 indicates the stress placed on the vowels. The consonants like S, P, and so on, don't come attached with a number)
For example, Row 7 and 1504 both have 2 syllables because there are 2 numbers in each row (although in different columns), so I'd like them to be together.
In the end, I'd just like to sort it so out of the 4 rows I showed, the order should be 7,1504,6,5
Does anyone know the code I should write?
You can extract digit from each line and order them according to the number of digits(length of the list)
library(stringr)
## read lines
ll <- readLines(textConnection('5 SUPERNATURALISM S UW2 P ER0 N AE1 CH ER0 AH0 L IH2 Z AH0 M
6 ESTABLISH IH0 S T AE1 B L IH0 SH
7 FABRIC F AE1 B R IH0 K
1504 AMTRAK AE1 M T R AE0 K '))
## extract digit , remove the first one and order
ord <- order(sapply(str_extract_all(ll,'\\d+'),function(x)length(x[-1])))
## get the result
ll[ord]
[1] "7 FABRIC F AE1 B R IH0 K "
[2] "1504 AMTRAK AE1 M T R AE0 K "
[3] "6 ESTABLISH IH0 S T AE1 B L IH0 SH "
[4] "5 SUPERNATURALISM S UW2 P ER0 N AE1 CH ER0 AH0 L IH2 Z AH0 M"
Here's a solution using base and positive look-behind regex (using #agstudy's data):
len = unlist(lapply(ll, function(x)
length(gregexpr("(?<=[A-Za-z])[0-2]", x, perl=TRUE)[[1L]])))
ll[order(len)]
The regex (?<=[A-Za-z])[0-2] basically means if there's a number between 0-2 and it's previous character is any of the alphabets, then there's a match. perl=TRUE is required for matching with look-ahead and look-behind regexps as it requires PCRE.
We loop through ll using this regex. gregexpr returns a list with it's first index returning the position of matches. So, we extract it using length(.) and then use order to rearrange ll.
HTH
You would have to process the data first, probably regex is the way to go. Here is a solution (I will use the package stringr):
## your data
df <- read.table(text=
"5 SUPERNATURALISM S UW2 P ER0 N AE1 CH ER0 AH0 L IH2 Z AH0 M
6 ESTABLISH IH0 S T AE1 B L IH0 SH
7 FABRIC F AE1 B R IH0 K
1504 AMTRAK AE1 M T R AE0 K ",
fill=TRUE,
stringsAsFactors=FALSE)
Manipulating to extract numbers and count:
library(stringr)
text <- apply(df[,3:16], 1,paste, collapse="")
numbers <- str_extract_all(text, "\\d+")
df$syllables <- sapply(numbers, length)
Order:
df <- df[order(df$syllables),]
df
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 syllables
3 7 FABRIC F AE1 B R IH0 K 2
4 1504 AMTRAK AE1 M T R AE0 K 2
2 6 ESTABLISH IH0 S T AE1 B L IH0 SH 3
1 5 SUPERNATURALISM S UW2 P ER0 N AE1 CH ER0 AH0 L IH2 Z AH0 M 7

Resources