Samuel
Samuel

Reputation: 5

How do I know if something in a MIPS memory address is signed or unsigned data?

I am having a hard time wrapping my head around how memory works with MIPS. I realize that there are both signed and unsigned instructions in MIPS such as add and addu but how do I know if what I am loading from or storing to memory is signed or unsigned bits? Additionally, why is the lw instruction signed but the instructions for loading a half word or loading a byte only come in unsigned ie lhu and lbu?

Any clarification would be greatly appreciated!

Upvotes: 0

Views: 2277

Answers (2)

Alexey Frunze
Alexey Frunze

Reputation: 62106

Memory is just that, memory, a collection of bytes or bits. By itself it does not carry any meta data, layout or type information.

If you want it to contain this information, you store it there and then retrieve and interpret it. However, in many cases this isn't done as it's an overkill. Would you like to deal with something like XML or JSON just to read or write a few bytes or words? You probably wouldn't.

And so if you don't, it's your responsibility to treat memory locations appropriately for your program. If you want to use a memory cell for a 16-bit signed type, you keep that in mind and use the appropriate for that type load/divide/shift/etc instructions.

Assembly language, for the most part, unlike the vast majority of other programming languages, does not provide any means for declaring typed variables and automatically selecting instructions to work with them based on their type. This is intentional so as to keep the assemblers small, simple and fast and closely reflect the hardware, CPU architecture. CPUs know nearly nothing about types and aren't capable of making any sense of the software they are running so long as it doesn't cause the CPUs to reset or shut down (perhaps, due to overheating). E.g. an arbitrary value can be loaded into a general purpose register, even a floating-point constant and it may be something very well intended, despite there being special floating-point registers in the CPU seemingly better suited for such types/values.

Sign extending or zero extending a byte or a half word with those lb/lbu/lh/lhu instructions makes sense only when there's space for extending. MIPS general purpose registers are 32-bit. lw loads 32 bits and there's nowhere to extend, so, there's no lwu... until you get a 64-bit MIPS CPU with 64-bit registers, where unsurprisingly you do have lwu (loads 32-bit with zero extension; lw does sign extension) and ld (loads 64-bit). But there's no ldu. Do you know why? :)

Upvotes: 4

a3f
a3f

Reputation: 8657

how do I know if what I am loading from or storing to memory is signed or unsigned bits?

You have no type system when you program in MIPS assembly. So you are responsible to keep track of whether an operation ought to be signed or unsigned. In the case of add/addu and sub/subu, the only difference is that the signed version cause an exception on overflow.

instructions for loading a half word or loading a byte only come in unsigned ie lhu and lbu

There are signed versions of those, lh and lb.

why is the lw instruction signed

If you load a halfword with lh/lhu into a register, you do so to the lower half of the register. This leaves 16 bit at the top of register that need to be filled with some value. When using the unsigned version, zero-extension is performed, which is fine as zeros to the "left" don't affect the value. When using signed loads, sign-extension is performed instead.

With lw there are no upper bits, that need to be filled. So, only one version is required.

Upvotes: 5

Related Questions