Load/Store Word Left/Right 

LWL, LWR, SWL, SWR

These instructions are used together to read (LWL, LWR) or write (SWL, SWR) an entire 32-bit word to memory, but starting at an address that is not word aligned.

Recall, lw and sw instructions will read/write a word to memory, but the address must be word aligned (address mod 4 = 0).  When a 32-bit word crosses a word boundary the LWL and LWR instructions can be used to read the value.  Similarly, SWR and SWL can be used together to write a single 32-bit register value to memory locations that cross a word boundary.  Note, that since we are generating the binary file on a SUN, your simulator must assume a big-endian byte-order for memory.  This is different than if things were generated on an Intel machine.

LWR: 

This instruction reads the right portion of a register (low-order bytes). Conceptually, LWR starts at the specified byte in memory and loads that byte into the low-order (right most) byte of the destination register.  Then, it proceeds to the high-order byte of the word in memory (since SPARC is big-endian, this is toward lower memory addresses) and the high-order byte of the register, loading bytes from memory into the register until it reaches the high-order byte of the memory word.  So, start at some specified address, and start copying bytes from memory and decrement the memory byte address until you reach the next lowest memory address that is word aligned, including the byte at this last address. The most significant (left-most) byte(s) of the register will not be changed.  Consider the following instruction, memory contents for two words, and register 4's values before and after executing a LWR instruction.

lwr $4, 2($0)    # this is a dummy instruction that starts a byte 2 wants to read the 32-bit word starting at that location.

			Memory				Register 4
            byte 0, byte 1, byte 2, byte 3	byte 0, byte 1, byte 2, byte 3		
address 4:    4       5       6       7 	  A       B       C       D	before 
address 0:    0       1       2       3		  A       0       1	  2    	after

Notice, that we are starting at address 0+2, that's byte 2 of word at memory address 0.  The byte at that memory location (value 2) is placed in the right-most (least significant) byte of the destination register.  Bytes are copied toward the left until the word aligned address is reached (byte 0 of word at memory address 0, which has a value of zero in this example).

LWL: 

This instruction reads the left portion of a register (high-order bytes). Conceptually, LWL starts at the specified byte in memory and loads that byte into the high-order (left most) byte of the destination register.  Then, it proceeds to the low-order byte of the word in memory (since SPARC is big-endian, this is toward higher memory addresses) and the low-order byte of the register, loading bytes from memory into the register until it reaches the low-order byte of the memory word.  So, start at some specified address, and start copying bytes from memory and increment the memory byte address until you reach the next highest memory address that is word aligned, not including the byte at this last word aligned address (that would be the high-order byte of the next word in memory). The least significant (right-most) byte(s) of the register will not be changed.  Consider the following instruction, memory contents for two words, and register 4's values before and after executing a LWL instruction.

lwl $4, 2($0)    # this is a dummy instruction that starts a byte 2 wants to read the 32-bit word starting at that location.

			Memory				Register 4
            byte 0, byte 1, byte 2, byte 3	byte 0, byte 1, byte 2, byte 3
address 4:    4       5       6       7 	  A       B       C       D	before 
address 0:    0       1       2       3		  2       3       C	  D    	after

Notice, that we are starting at address 0+2, that's byte 2 of word at memory address 0.  The byte at that memory location (value 2) is placed in the left-most (most significant) byte of the destination register.  Bytes are copied toward the right until the last byte of this memory word is reached (byte 3 of word at memory address 0, which has a value of 3 in this example).

 

SWR: 

This instruction stores the right portion of a register (low-order bytes). Conceptually, SWR starts at the specified byte in memory and stores the low-order (right most) byte of the source register to that memory location.  Then, it proceeds to the high-order byte of the word in memory (since SPARC is big-endian, this is toward lower memory addresses) and the high-order byte of the register, storing bytes from the register into memory until it reaches the high-order byte of the memory word.  So, start at some specified address, and start copying bytes from the register to memory and decrement the memory byte address until you reach the next lowest memory address that is word aligned, including the byte at this last address. The least significant (right-most) byte(s) of the memory location will not be changed.  Consider the following instruction, memory contents for two words, and register 4's values before and after executing a SWR instruction.

swr $4, 5($0)    # starting address is byte 5 (byte 1 of word at memory address 4)

			Memory				Register 4
            byte 0, byte 1, byte 2, byte 3	byte 0, byte 1, byte 2, byte 3		
address 4:    4       5       6       7 	  A       B       C       D	before 
address 0:    0       1       2       3		  A       B       C	  D 	before 

address 4:    C       D       6       7 	  A       B       C       D	after
address 0:    0       1       2       3		  A       B       C	  D    	after

Notice, that we are starting at address 0+5, that's byte 1 of word at memory address 4.  The byte at that memory location (value 5 ) is replaced by the right-most (least significant) byte of the source register.  Bytes are copied toward the left until the word aligned address is reached (byte 0 of word at memory address 4, which has the initial value of 4 in this example).

SWL: 

This instruction stores the left portion of a register (high-order bytes). Conceptually, SWL starts at the specified byte in memory and stores the high-order (left most) byte of the source register to that memory location.  Then, it proceeds to the low-order byte of the word in memory (since SPARC is big-endian, this is toward higher memory addresses) and the low-order byte of the register, storing bytes from the register into memory until it reaches the low-order byte of the memory word.  So, start at some specified address, and start copying bytes from the register to memory and increment the memory byte address until you reach the next highest memory address that is word aligned, not including the byte at this last address. The most significant (left-most) byte(s) of the memory location will not be changed.  Consider the following instruction, memory contents for two words, and register 4's values before and after executing a SWL instruction.

swr $4, 5($0)    # starting address is byte 5 (byte 1 of word at memory address 4)

			Memory				Register 4
            byte 0, byte 1, byte 2, byte 3	byte 0, byte 1, byte 2, byte 3		
address 4:    4       5       6       7 	  A       B       C       D	before 
address 0:    0       1       2       3		  A       B       C	  D 	before 

address 4:    4       A       B       C 	  A       B       C       D	after
address 0:    0       1       2       3		  A       B       C	  D    	after

Notice, that we are starting at address 0+5, that's byte 1 of word at memory address 4.  The byte at that memory location (value 5 ) is replaced by the left-most (most significant) byte of the source register.  Bytes are copied toward the right until the the last byte of this memory word is reached (byte 3 of word at memory address 4, which has the initial value of  7 in this example).