SPARC Assembly/Memory Instructions

Instruction Format edit

This is going to be a quick primer for how to write these memory instructions.

Load Instruction Format edit

Memory instructions load a value from memory, or store a value into memory. The target address is computed as the addition of a base value with an optional offset value.

For example:

ld [%r4 + 10], %r5

loads the register %r5 with the value in memory located at [%r4 + 10]. If %r4 contains the number 25, then the memory location read would be [25 + 10] = 35. The op2 parameter, like the arithmetic instructions can be a register or an immediate. This means that the following are both valid:

ld [%r4 + %r3], %r5
ld [%r4 + 100], %r5

The offset value is the number of bytes offset, not the number of machine words or halfwords. This means that to load consecutive words on a 32 bit machine, the offset needs to be incremented by 4:

ld [%r4 + 4], %r5
ld [%r4 + 8], %r6
...

Also, the offset value is optional. If the offset is omitted, the assembler will assume a value of 0. For example:

ld [%r4], %r5

will read from location %r4, with no offset.

Store Instruction Format edit

Store instructions are similar to load instructions, except that the operands are reversed. This means that we have the following instruction format:

st rd, [rs1 + op2]

All the rest of the above material applies.

Load Instructions edit

Instruction:
ldub

Loads an unsigned byte from memory into rd.

Instruction:
ldsb

loads a signed byte from memory into rd. sign-extends result.

Instruction:
lduh

loads an unsigned halfword from memory into rd.

Instruction:
ldsh

loads a signed halfword from memory into rd. Sign extends result.

Instruction:
ld

loads a machine word into rd.

Instruction:
ldd

Loads a double word into 2 consecutive registers.

Store Instructions edit

Instruction:
stb

Stores a byte from register rd into memory.

Instruction:
sth

stores a halfword from register rd into memory.

Instruction:
st

stores a machine word from register rd into memory.

Instruction:
std

stores a double value from two consecutive registers into memory.

Swap Instruction edit

Instruction:
swap

performs a simultaneous load and store operation, which effectively swaps the value in memory with the value in rd. Is formatted like a load instruction, for example:

swap [%r3 + 4], %r5