0

Here is some of my Code:

    la     $t2, $LC1
    la      $t9,($t2)
    addiu   $t3,$t2,%lo($LC1)
    lw      $t8,4($t3)
    addiu   $t3,$t2,%lo($LC1)
    lw      $t7,8($t3)
    addiu   $t3,$t2,%lo($LC1)
    lw      $t6,12($t3)
    addiu   $t3,$t2,%lo($LC1)
    lw      $t5,16($t3)
    addiu   $t3,$t2,%lo($LC1)
    lw      $t4,20($t3)
    addiu   $t3,$t2,%lo($LC1)

Here are the errors:

Error in /Applications/mips7.asm line 101 column 9: "addiu": Too many or incorrectly formatted operands. Expected: addiu $t1,$t2,-100
Error in /Applications/mips7.asm line 103 column 9: "addiu": Too many or incorrectly formatted operands. Expected: addiu $t1,$t2,-100
Error in /Applications/mips7.asm line 105 column 9: "addiu": Too many or incorrectly formatted operands. Expected: addiu $t1,$t2,-100
Error in /Applications/mips7.asm line 107 column 9: "addiu": Too many or incorrectly formatted operands. Expected: addiu $t1,$t2,-100
Error in /Applications/mips7.asm line 109 column 9: "addiu": Too many or incorrectly formatted operands. Expected: addiu $t1,$t2,-100
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • See also: https://stackoverflow.com/questions/4175450/is-there-a-way-to-use-gcc-to-convert-c-to-mips/63386888#63386888 – Erik Eidt Apr 10 '22 at 18:44

1 Answers1

1

MIPS compilers, assemblers and linkers use multiple instruction and %hi with lui and %lo with addiu.

MARS does not support the %hi or %lo address manipulation functions — instead it offers a pseudo instruction la that loads the whole 32-bit address of the label into the register.  (This instruction expands into two machine code instructions that involve lui and another like ori.)

So, collapse the two instructions using %hi & %lo into one la for MARS, and then use that address to accomplish the intended effect: load an address, or load or store to global data label.

If the intended effect is to load or store from a global/static data label, MARS also supports lw $t0, label, and sw $t0, label pseudo instruction forms.

Generally speaking, MARS pseudo instructions hide underlying machine code details, including hiding opportunities for manual optimization when one of the lui instructions could have been eliminated or shared.  If we do global_var++ for example, that should require only one lui, one lw and one sw but in MARS lw $, label followed by sw $, label will involve two lui instructions.


The following doesn't make sense for MARS:

la     $t2, $LC1
addiu   $t3,$t2,%lo($LC1)

Because the la has already produced the full 32-bit address for $LC1, there's no point in adding an offset %lo($LC1) to that pointer.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • Ok so, that means I do not have to use the addiu function at all, because the la function for the full 32 bit address accounts for it already? – Apple DOnut Apr 10 '22 at 18:58
  • And also each time there is an addiu function with the %lo($LC1)or %hi($LC1) what should I put instead?? Here is an example within the code I already provided: lw $t8,4($t3) addiu $t3,$t2,%lo($LC1) – Apple DOnut Apr 10 '22 at 19:02
  • The idea is to understand that `%hi` and `%lo` are used together to form a 32-bit address that is used to either: make the address into a register, load a value from data label, or store a value to data label. Then to do the equivalent to one of those in MARS, which is an `la` to make the address into a register, or, something like `lw` or `sw` to a label. – Erik Eidt Apr 10 '22 at 20:18