user7549908
user7549908

Reputation:

Can't understand small part of a MIPS assembly instruction

This program prints the ASCII characters from 0 to Z. The output is

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ

The question is how to change the program so it prints every third ASCII character. So that the output must look like this

0369<?BEHKNQTWZ]

When I change the constant in addi $s0,$s0,1 to addi $s0,$s0,3 the output is a lot of ASCII characters and it's like an infinite loop.

.text


main:
          li    $s0,0x30


loop:
         move   $a0,$s0     

         li $v0,11      
         syscall            

         addi   $s0,$s0,1       # what happens if the constant is changed?

          li    $t0,0x5b
         bne    $s0,$t0,loop
         nop


stop:    j       stop       
         nop    

I don't understand the reason behind why the program goes crazy when I change that constant.

I wrote my own code as shown below which works fine and do the job but I want to understand the code above because it's an assignment.

 .data



 .text

        main:
              li $s0,0x30



       for: 
             addi $a0,$s0,0
             li $v0,11
             syscall
             li $t0,0x5a
             bgt $s0,$t0, done
             addi $s0,$s0,3
             j for
       done:            

Upvotes: 0

Views: 329

Answers (1)

Christian
Christian

Reputation: 79

The number of characters this prints (43) is not divisible by 3, so by adding 3 each time, your loop goes past its exit condition (s0 == t0). Try changing the bne to blt.

Your own code does exactly the same, except that it jumps out of the loop when it goes past the end point, rather than back to the top unless it has.

Upvotes: 1

Related Questions