Important warning:   always use the correct load instruction
 

Warning:

  • When you load data from a byte typed variable into a register:

    • you must use the ldrsb instruction

  • When you load data from a short typed variable into a register:

    • you must use the ldrsh instruction

  • When you load data from a int typed variable into a register:

    • you must use the ldr instruction

Important warning:   always use the correct store instruction
 

Warning:

  • When you store data from a register into a byte typed variable:

    • you must use the strsb instruction

  • When you store data from a register into a short typed variable:

    • you must use the strsh instruction

  • When you store data from a register into a int typed variable:

    • you must use the str instruction

The next examples will show you what can go wrong if you don't...

What can go wrong if you use an incorrect load instruction ?

Consider a program with 2 short typed variables:

      .data
  x:  .2byte     254   // short typed variable
  y:  .2byte     1     // short typed variable  

How these 2 short variables are stored in memory:

   movw  r0, #:lower16:x   // Moves the address of memory
   movt  r0, #:upper16:x   // variable x into register r0

   ldrsb r1,[r0]  ??
   ldr   r1,[r0]  ??    

Use: tmp/orig.s DEMO: /home/cs255001/demo/asm/2-mov/wrong-ldr.s

The correct result when fetching x into register R1

Effect when you use the correct ldrsh r1, [r0] instruction to fetch short x:

The value in short x = 254
The ldrsh instruction loads 254 into the register r1

What can go wrong if you use an incorrect load instruction ?

Effect when you use the incorrect ldrsb r1, [r0] instruction to fetch short x:

The value in short x = 254
The ldrsb instruction loads -2 (error !) into the register r1 (because it did not fetch the first byte)!

What can go wrong if you use an incorrect load instruction ?

Effect when you use the incorrect ldr r1, [r0] instruction to fetch short x:

The value in short x = 254
The ldr instruction loads 65790(error!) into the register r1 (because it fetched some extraneous data)!

What can go wrong if you use an incorrect store instruction ?

Consider a program with 2 short typed variables:

      .data
  x:  .2byte     4   // short typed variable
  y:  .2byte     7   // short typed variable  

How these 2 short variables are stored in memory:

   movw  r0, #:lower16:x   // Moves the address of memory
   movt  r0, #:upper16:x   // variable x into register r0

   mov   r1, #-2
   strb  r1,[r0]  ??
   str   r1,[r0]  ??  (Watch y !!)  

Use: tmp/orig.s DEMO: /home/cs255001/demo/asm/2-mov/wrong-str.s

The correct result when storing register R1 into the variable x

Effect when you use the correct strh r1, [r0] instruction to write the short x variable:

The value in register R1 = -2
The strh instruction changes the value in x to -2

What can go wrong if you use an incorrect store instruction ?

Effect when you use the incorrect strb r1, [r0] instruction to write the short x variable:

The value in register R1 = -2
The strb instruction changes the value in x to 254 (because it did not update 2nd byte!)

What can go wrong if you use an incorrect store instruction ?

Effect when you use the incorrect str r1, [r0] instruction:

The str instruction will write 4 bytes to memory !!
The variable y is also updated !!