Page 1 of 1

MIPS Assembly, loading value from address

Posted: Sun Apr 18, 2010 11:03 am
by Passworder
I want to load a byte by using the 'lb' instruction. In this case I want to use lb to load from the memory mapped address 0x180003fb. The address 0x180003fb is mapped to a serial device used to read/print to a console. However, for some reason I cannot communicate with this device.

Here's the instruction in question.
lb t1, 0x180003fb

That doesn't however load the value located at the address 0x180003fb, it simply stores the hexadecimal value into the register. in this case 0x18000000 (LSD byte). How can I load a value from an address? I've been searching around for about 13 hours now and read trough two MIPS assembly guides รก 200 pages each, just saying in case someone thinks it's obvious and I should Google it : /

Thankful for help.

Re: MIPS Assembly, loading value from address

Posted: Sun Apr 18, 2010 2:33 pm
by Gigasoft
Addresses are always signed 16-bit displacements relative to a register. You probably don't want to sign extend the byte read from the device, so you'd write:

Code: Select all

lui t1, 0x1800
lbu t1, 0x3fb(t1)
Similarly, to store t1 into address 0x8009f000, you'd write:

Code: Select all

lui at, 0x800a
sw t1, 0xf000(at)
Notice that in this case, the upper part of the address has to be incremented by one, since the lower part is negative.

If you just want to load or store into an address between 0xffff8000 and 0x7fff, you can skip the lui and use the register which various assemblers call zr, zero or 0 as the base address.

As you may already know, to load an arbitrary 32-bit value into a register, you use lui and addiu. Let's say you want to load 0xfedcba98 into v0. This becomes:

Code: Select all

lui v0, 0xfedd
addiu v0, v0, 0xba98
Although addiu stands for "add immediate unsigned", the operand is nevertheless sign extended.