sw, sh, and sb Instructions


The store instruction copies data from a register to memory and the register is not changed when its data is copied to memory. The following gives three store instructions.
sw rd, mem         # store word
Stores the contents of a word from the source register in the memory location specified by the effective address. The effective address must be divisible by four, otherwise the machine signals an address error exception.

sh rd, mem         # store halfword
Stores the two least-significant bytes of the source register in the halfword that is at the memory location specified by the effective address. The effective address must be divisible by two, otherwise the machine signals an address error exception.

sb rd, mem         # store byte
Stores the contents of the source register’s least-significant byte in the byte specified by the effective address.
sw sh sb
     .data
 X:  .word  1
     .byte  2
 Y:  .word  3

     .text 
     li  $t0, 1
     sw  $t0, X+4
     lw  $a0, Y
     li  $v0, 1
     syscall
     .data
 X:  .byte  1
 Y:  .half  2

     .text 
     li  $t0, 1
     sh  $t0, X+2
     lh  $a0, Y
     li  $v0, 1
     syscall
     .data
 s:  .asciiz  "abc"

     .text 
     li  $t0, 'd'
 # 'd': character d
     sb  $t0, s+2
     la  $a0, s
     li  $v0, 4
     syscall
Output Output Output













      Johnny collected lots of money from trick or treating and    
      he went to the candy store to buy some chocolate.    
      “You should give that money to charity”, said the shopkeeper.    
      “No, I’ll buy the chocolate. YOU give the money to charity!”