Riccardo
Riccardo

Reputation: 299

MIPS - How to store the first 256 numbers in memory

I have to write a program in assembly language that is to store in memory the first 256 integers from 0 to 255.

Here is the code I wrote (I am a beginner with the assembly language):

.data

memory: .space 256


.text

       li $t0, 0
       la $s0, memory
       sb $s0, 0($t0)

loop: 
       add $t0, $t0, 1
       sb $s0, 0($t0)
       j loop

I tried to run the program with SPIM but does not work: SPIM says there is an error to instruction sb $s0, 0($t0).

Anyone can help me with this?

Upvotes: 0

Views: 919

Answers (1)

Michael
Michael

Reputation: 58427

You don't have any code to break out of your loop, so you'll just keep on writing bytes forever, which eventually will result in a Bad Address exception.

You need to add some instructions in your loop that check if you have written 256 bytes, and if so doesn't jump back to loop:.

Another problem is that you've written the operands for sb in the wrong order. You want to store $t0 at $s0, so it should be sb $t0,0($s0). And you also need to increment $s0 each time you increment $t0.

(Also, you should end your program by invoking syscall 10).

Upvotes: 1

Related Questions