Total number of instructions executed in MIPS code? - mips

In this MIPS program
When this code fragment is run, how many instructions will be executed in total?
i1: ori $t0, $0, 1000
i2: ori $t1, $0, 2000
i3: addi $t2, $t0, 100
i4: lw $t3, 0($t1)
i5: lw $t4, 0($t0)
i6: add $t3, $t3, $t4
i7: sra $t3, $t3, 1
i8: sw $t3, 0($t0)
i9: sw $t3, 0($t1)
i10: addi $t0, $t0, 4
i11: addi $t1, $t1, 4
i12: slt $t3, $t0, $t2
i13: bne $0, $t3, i4

On every cycle, it adds 4 to $t0, which is initialized to 1000. At the end of the cycle, it checks if $t0 is greater than $t2, which is initialized to 1100. If it is, then the cycle breaks. So, it's the original 13 instructions, plus 10 for every cycle until it breaks. And it cycles an additional 25 times. And I may have added wrong, but if not, it's 263 instructions.

Related

What is the MIPS equivalent on these pseudoinstructions?

rol $t7, $t6, $t8
This is like:
sll $t0, $t3, 8 # get rid of bits 31-23
srl $t1, $t3, 24 #move 31-32 to 7-0
or $t7, $t0, $t1 # do an or statement
right?
What about this one:
ld $t2, 0($t8)

rol pseudo-instruction MIPS code

The instructions is:
rol $t0, $t1, n
rolv $t0, $t1, $t2
n: 1bit- 31bit
Is this correct translation of the above instructions?
srl $t1, $s1, 1
sll $t2, $s1, 31
or $s2, $t1, $t2 #combine_words
For rol $t0, $t1, n I would use $at as the scratch register.
Suppose you want to rotate 4 binary digits to the left:
srl $at, $t1, 28 # 32-4 = 28
sll $t0, $t1, 4
or $t0, $t0, $at
For rolv $t0, $t1, $t2 I would emit ($t2 holds the number of bits to rotate):
sllv $t0, $t1, $t2
subu $at, $zero, $t2
addiu $at, $at, 32 # $at = 32-$t2
srlv $at, $t1, $at
or $t0, $t0, $at

Run time Exception in MIPS Assembly code

Recently, I am learning MIPS. Professor give us an example of his code, asking us to find out the value. My problem is that after I type in his code, the run time exception comes out at " lw $t1, 0($s0) ".
line 8: Runtime exception at 0x0040001c: address out of range 0x00000000
Can someone helps me?
addi $t7, $zero,0x10010000
lw $t0, 20($t7)
srl $t0, $t0, 16
andi $t0, $t0, 0x00000FF
andi $s0, $t7, 16
lw $t1, 0($s0)
andi $t1, $t1, 0x0000FF00
srl $t1, $t1, 8
addi $s0, $s0, -8
lw $t2, 0($s0)
sll $t2, $t2, 24
srl $t2, $t2, 24
addi $s0, $zero, 1
sll $s0, $s0, 4
lw $t3, 0($t7)
srlv $t3, $t3, $s0
andi $t3, $t3, 255
addi $s0, $zero, 1
sll $s0, $s0, 2
add $s0, $s0, $t7
lw $t4, 0($s0)
andi $t4, $t4, 0x00FF0000
srl $t4, $t4, 16

MIPS 32 always stores 10 mul and li in $t

This is part of my code, and I don't know why, $t1 always ends up with 10, when it should be 16. At this point, the data is the following:
$t5 = 4
$t3 = 1
$t2 = 0
and $t1 is 0
(and $t0 is an address)
This is the part of the code:
mul $t1, $t3 , $t5
add $t1, $t1, $t2
mul $t1, $t1, 4
**From here, $t1 should be 16, but it always turns out 10 even if I do li $t1, 16****
add $t1, $t1, $t0
lw $t6, ($t1)
I'm using MIPS 32 with QTSpim
If you set registers->Decimal, the result will be 16. I guess, going from the information you give.

Mips incrementing a count within an Array

I have this code that goes through an array of 5000 ASCII values of characters and then counts the number of each character and stores it into an array in alphabetical order.
.text
la $s7,results
array1 is the array of 5000 ASCII characters
la $s6,array1
main:
addi $s0,$zero, 5000
addi $s3, $zero,96
addi $s4, $zero,65
loop:lw $s2,0($s6)
addi,$t1,$zero,0
addi $s6,$s6,4
addi, $s5,$zero, 0
addi $s0,$s0, -1
bge $s2, $s3, convert
The substaction of of $s2, - $s4($s4 is # )is to get the positon of where the count needs to be stored
ex F - # = 6
sub $s5, $s2, $s4
I was wondering if this was right for it getting to the correct position and to increment the count?
addi $t1, $t1, 1
loop2: addi $s7, $s7, 4
lw $t0,($s7)
add $t0, $t0, $t1
sw $t0,($s7)
addi, $s5,$s5, -1
beq $s5, $zero, loop2
bne $s0,$zero, loop
This converts the character to an upper case ASCII value.
convert: subi $s2,$s2, 32
addi $s5, $s5, 0
sub $s5, $s2, $s4
j loop2
.data
results: .space 128 # Words to be used to store results