1

I'm very new to MIPS and I have a homework problem here.

I'm asked this: Before starting your loop, you should initialise $s0 to hold the base address of the array, and $s1 to hold the index of the array. The index should be zero-based, meaning the tenth element would have a zero-based array index of 9.

I honestly have no idea what any of this means. I think I've initialised the base address but I don't know what the index of the array is at all. Here is the code I have been given/have worked on:

.data  #by default, the "data segment" starts at address 0x10010000
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0

.text #instructions start below

lui $s0,0x1001

I have added the bottom line, the rest was given to me. Any help would be appreciated, thanks.

2
  • First index is 0. The current element is always $s0 + $s1 * 4 Commented Feb 19, 2014 at 3:26
  • 1
    "I honestly have no idea what any of this means." Then you should consult your course material and/or teacher. Proceeding without even understanding the assignment is pointless. Commented Feb 19, 2014 at 11:21

1 Answer 1

1

Ok, if "I honestly have no idea what any of this means" is an accurate statement, then let's go one "this" at a time.

I'm very new to MIPS and I have a homework problem here.

This means, you're considering relying on Stack Overflow to do your homework. Maybe it will, maybe it won't, but it won't be there for the exam. :)

Before starting your loop, you should initialise $s0 to hold the base address of the array

Do you know what "initialize a variable" means? If not, you need to go back to "intro to programming". Assuming that's not the problem, we want to define "set the value in a register" and "address of the array".

Your instruction does set all 32 bits of the register, and you seem to have gathered that it clears the lower 16 bits, giving you 0x10010000:

lui $s0,0x1001

But that's a really bad idea. You don't want your code to be so locked in to a specific memory location. A different assembler might put you in a different segment of memory, or (more likely) you might want to move that block of memory (which we're calling an "array", even though that's meaningless) to a different place. Instead, you should let the assembler do some work for you, by using labels and pseudo-ops.

.data # I don't know or care where the data segment starts
myArray:
.word 0x00000001 # aka 0x01 or just 1, but this shows what's really happening
.word 0x00000002
# etc etc
.text # Instructions do indeed start below
la $s0 myArray # magic pseudo-op

The assembler will turn the la (Load Address) pseudo-op into a lui with the upper half of the address for the upper bits, and then ori that with the lower half of the address for the lower bits. And that's code that will still work when you do this:

.data
somethingElse:
.word 0xDEADBEEF # I want anyone decompiling my code to see "DEAD BEEF" at the top of the dump
myArray:
.word 0x00000001
.word 0x00000002
# etc etc
.text # Instructions do indeed start below
la $s0 myArray # I still have a pointer to the first word in myArray!

Ok, now let's go to the next thing you have no idea about:

$s1 to hold the index of the array. The index should be zero-based, meaning the tenth element would have a zero-based array index of 9... I don't know what the index of the array is at all.

Again, if you don't know what A[0] through A[9] would mean in a high-level language, ask your advisor how you got into this class without the proper prerequisites. I'm going to assume you have that part down... because the first thing you have to do is forget it entirely.

MIPS doesn't have arrays. It just has memory locations that can be whatever you decide they are. If I send the address myArray to a function expecting a string of ASCII characters, it will read the memory locations as though they contained ASCII characters. There Is No Type (only Zuul). What you have to do is "pointer arithmetic".

As @Konrad pointed out, the first word (four bytes you intend to treat as an integer) in your "array" (a bunch of memory locations you intend to treat as a sequence of words) is at memory address myArray (possibly but not definitively located at 0x10010000). So if you wanted to get the value stored in your first "array element", you would do something like this:

lw $t0, 0($s0) # load word into $t0, from address pointed to by $s0, offset by 0

And then, you could get the next word like this, which will grab the data in memory address $s0+4. (Why +4? Because each word is 4 bytes long.)

lw $t0, 4($s0) # load word into $t0, from address pointed to by $s0, offset by 4

Well, that's not very helpful for accessing a random array element. In MIPS, you can't just replace 4 with your array index $s1. You'll have to do the addition yourself. But not like this!

add $t1, $s0, $s1 # Base address + oops!

If $s1 = 1, that will give you $s0+1, which isn't what you want. You need $s0+(4*$s1). In the spirit of not doing all your homework for you, I'm going to throw an opcode you may not know at you, and force you to consult the spirit of Google to find out why this works.

sll $t2, $s1, 2 # Look it up!
add $t1, $s0, $s1 # address of "array element"
lw $t0, 0($t1) # value of "array element"
Sign up to request clarification or add additional context in comments.

2 Comments

Hey. Thanks a lot for your comment. I do understand all of the basics I was just panicking when I set the question. Sorry :) This is what my code looks like now lui $s0,0x1001 addi $s1,$zero,1 loop: add $t1,$s0,$t2 lw $t0,0($t1) beq $t0,$zero,end addi $t0,$t0,2 sw $t0,0($t1) addi $s1,$s1,1 sll $t2,$s1,2 j loop It just keeps looping forever and I don't know what to do to fix it. Thanks a lot for your answer though! It really helped.
That must not be all your code - I assume you have an end label defined somewhere. The other thing I see missing is that $t2 isn't initialized. There might be more - that's just what jumps out. Have you single-stepped through the code (you can do that in MARS)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.