Learning2Code
Learning2Code

Reputation: 191

Mips programming jump problems

Supposed to count number of R, I, and J instructions in the code starting from the first line in main. I have updated the code to all suggestions but results do not seem correct still. :(

EDIT: added those syntax changes, the variables seem to be working better, but the loop doesn't seem to be properly running through itself at all. Any noticeable problems anyone sees?

EDIT2: How do i change the loop to make $t1 rotate through 1 instruction set at a time? THAT is my key problem right now other than the silly syntax from a new mips user.

EDIT3: I have updated the code to all suggestions but results do not seem correct still. :(

.text    
Main:    
la $t1, 0x400000    
li $t5, 0    
li $t3, 2    
li $t4, 3    
li $t2, 0    
la $s0, 0x400000 

loop:    
lw $t1, 0($s0)    
addi $s0, 4    
addi $t5, 1     
beq $t5, 20 exit    
srl $s4, $t1, 26    
beq $s4, $t2 R    
beq $s4, $t3 J    
beq $s4, $t4 J    
addi $s3, 1    
j loop    

R:
addi $s1, 1    
j loop      

J:    
addi $s2, 1    
j loop     

exit:    
sw $s1, RType    
sw $s2, JType    
sw $s3, IType    
jr $ra

Upvotes: 0

Views: 455

Answers (2)

Michael
Michael

Reputation: 58507

In addition to Miguel's points I'd like to add that you're using the instruction set in a strange manner. Perhaps your assembler is capable of figuring out what you meant, but it's still makes the code confusing to read.

lw $t3, 2
lw $t4, 3

These should be li. The lw instruction is used for loading words from memory.


la $t0, 4($t1)

This should be lw, and unless you only want to check the instructions starting from address 0x400004 you should drop the offset.

I also can't seem to find anywhere where you increment $t1, so you'll be using the same address for every iteration of the loop.

Upvotes: 0

Miguel Prz
Miguel Prz

Reputation: 13792

I see a set of problems:

1) Initialize the registers (don't assume register initial value is zero). For example, addi $t5, 1 requieres a previous add $t5, $zero, $zero (or li to the value you want).

2) the beq jump instruction requires two registers. For example, you have to change beq $s4, 3 J by:

li $t6, 3
beq $s4, $t6, J

Upvotes: 1

Related Questions