1
votes

The Background

I am a student just beginning to learn MIPS for one of my courses, and my professor is not allowing the usage of pseudo-instructions such as Load Address (la) in our code. I am wondering what an example of the correct usage of standard instructions would look like to store the address of a declared variable into a register for use later in the code.

My Solution

I have currently been attempting to use this code, though I am getting a syntax error in the lui instruction.

main:
.data
    Array:
    .space 80             #Declares that Array will hold 20 integers
.text
    lui  $s0, Array       #loads most significant bits into $s0
    ori  $s0, $s0, Array  #loads least significant bits into $s0

My Question

From what I understand, this should result in the address of Array being placed into $s0. However, as that does not seem to be the case, I'm wondering if anyone would be able to help me out on what I should be doing here.

1
In the "real world" you would indeed use la. If you are not allowed to use it but you know the address where your array will be stored, then you can use those two instructions from your question with the correct values instead of using a label.gusbro
@gusbro How would I go about finding the address where my array will be stored? I'm very new to this so please excuse any stupid questions.Taylor Bigham
Usually you can set the location where the .data will be stored by telling the assembler where you want it to be put. For example .data 0x3000gusbro
@gusbro Could you recommend any resources that I could use to get a better understanding of this process from a beginner's level? I greatly appreciate your help.Taylor Bigham
I don't have any resources at hand. Any book on MIPS programming (or even any book describing MIPS architecture) should explain how to form an address using two instructions. Note that it really does not make much sense to hardcore the address manually... you should always use la with a label name and let the assembler do the math for you...gusbro

1 Answers

2
votes

You need to refer to a label in the data section in the lui and ori instructions. The following works for gnu assembler (as):

    .data
    Array:
    .space 80             #Declares that Array will hold 20 integers
...
.text
    lui $s0, %hi(Array)
    ori $s0, %lo(Array)
    lw  $s1, 0($s0)       # load 1st word of Array
...

The %hi and %lo directives tell the linker what is going on, so that it can put the address of the label "Array" in the machine code. (NOTE: this likely doesn't work for SPIM or MARS.)

See this question.

See MIPS Run is the canonical book on MIPS CPUs. This book explains the MIPS instruction set, CPU architecture and how they relate to MIPS Linux.