Introduction: result of computation stored in registers

Registers are used to store result of computations:

Note:   you must save (= store) the result in its variable in memory to complete the assignment statement !

The store register instructions

The "store register" instructions are used to store a value in a register into memory:

Note:   there are many syntax forms of the "store register" instruction

Storing integer data type values in a register to memory
 

We will learn to store a value in a register into these 3 integer (whole number) typed variables

  • byte type (stored in 1 byte of memory)

  • short type (stored in 2 bytes of memory)

  • int type (stored in 4 bytes of memory)

 

We start with the byte typed variable

 

Note:   The value in a register is a 32 bits 2s complement representation
            We will store this 32 bits representation into a byte (= 8 bits) variable
            Therefore:   overflow is possible !!!

The strb instruction

Syntax and meaning of the strb instruction:

   strb rN, [rM]       // store register byte

      Store 1 byte from register rN to memory
      at the address given by the value in rM 

      The byte stored at the right of register rN:
 
          
   

Note:   initialize rM = address of a short variable before you use the strb instruction  

Note:   the strb instruction will convert the 32 bits int value in register rN into a byte

The strb instruction - explained graphically

strb r1, [r0]:   (1) sends out r0 on the address bus and the byte in r1 on the data bus

The CPU will issue a write command on the control bus

The strb instruction - explained graphically

strb r1, [r0]:   (2) the memory will store the data in the designated memory location:

The old data will be overwritten

The strb instruction will perform a   intbyte conversion

Recall that for value in rage [−128, 127], the int byte conversion will produce correct results:

   Value    byte      int (32 bits 2s compl code)
  -------  -------  ----------------------------------     
   127     01111111  00000000000000000000000001111111
   ...
     3     00000011  00000000000000000000000000000011
     2     00000010  00000000000000000000000000000010
     1     00000001  00000000000000000000000000000001
     0	   00000000  00000000000000000000000000000000
    -1	   11111111  11111111111111111111111111111111
    -2	   11111110  11111111111111111111111111111110
    -3	   11111101  11111111111111111111111111111101
   ...
  -128     10000000  11111111111111111111111110000000
   

If the register contains a values outside the range [−128, 127], then strb will result in overflow

Reminder:   How to move memory addresses into a register in ARM

Reminder:

  • Suppose the label b is the symbolic name for the memory address of the (any type of) variable a:

         b:  .byte -2            

  • If you want to move (= put) the memory address a (it's a binary number !) into the register r0, you can use:

         movw  r0, #:lower16:b 
         movt  r0, #:upper16:b   
      

We will also use the address in the register in the store operation !!!

Example: using the strb instruction to store a register into byte typed variable

    // r1 = 00000000 00000001 00000001 00000111
    // Store byte in register r1 into (memory) variable b 
    movw    r0, #:lower16:b    // Moves the address of memory
    movt    r0, #:upper16:b    // variable b into register r0

    strb    r1,[r0]            // Store byte value from r1 
                               // into memory location r0
    .data
b:  .byte  -2       // variable b (byte b = -2 or  11111110) 

Result of the execution of strb r1,[r0]:

DEMO: /home/cs255001/demo/asm/2-mov/str-v2.s
Use tmp/ldr+str.s

The strh instruction

Syntax and meaning of the strh instruction:

   strh rN, [rM]       // store register half

      Store 2 bytes from register rN to memory
      at the address given by the value in rM 

      The 2 bytes stored at the right of register rN:
 
          
   

Note:   initialize rM = address of a short variable before you use the strb instruction  

Note:   the strb instruction will convert the 32 bits int value in register rN into a short

The strh instruction - explained graphically

strh r1, [r0]:   (1) sends out r0 on the address bus and the short in r1 on the data bus

The CPU will issue a write command on the control bus

The strh instruction - explained graphically

strh r1, [r0]:   (2) the memory will store the data in the designated memory location:

The old data will be overwritten

The strh instruction will perform a   intshort conversion

Recall that for value in rage [−32768, 32767], the int short conversion will produce correct results:

  Value   short            int (32 bits 2s compl code)
  -----  ----------------  --------------------------------  
  32767  0111111111111111  00000000000000000111111111111111
   ...
     3   0000000000000011  00000000000000000000000000000011
     2   0000000000000010  00000000000000000000000000000010
     1   0000000000000001  00000000000000000000000000000001
     0	 0000000000000000  00000000000000000000000000000000
    -1	 1111111111111111  11111111111111111111111111111111
    -2	 1111111111111110  11111111111111111111111111111110
    -3	 1111111111111101  11111111111111111111111111111101
   ...
  -32768 1000000000000000  11111111111111111000000000000000
   

If the register contains a values outside the range [−32768, 32767], then strh will result in overflow

Example: using the strh instruction to store a register into short typed variable

    // r1 = 00000000 00000001 00000001 00000111
    // Store short in register r1 into (memory) variable s 

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

    strh    r1,[r0]            // Store short value from r1 
                               // into memory location r0

    .data
s:  .2bytes  -3  // variable s (short = -3 or  1111111111111101)
   

Result of the execution of strh r1,[r0]:

DEMO: /home/cs255001/demo/asm/2-mov/str-v2.s

The str instruction

Syntax and meaning of the str instruction:

   str  rN, [rM]       

      Store 4 bytes from register rN to memory
      at the address given by the value in rM 

      The 4 bytes comprise of the register rN:
 
          
   

Note:   initialize rM = address of a short variable before you use the str instruction  

The str instruction - explained graphically

str r1, [r0]:   (1) sends out r0 on the address bus and the all 32 bits in r1 on the data bus

The CPU will issue a write command on the control bus

The str instruction - explained graphically

str r1, [r0]:   (2) the memory will store the data in the designated memory location:

The old data will be overwritten

Example: using the str instruction to store a register into int typed variable

    // r1 = 00000000 00000001 00000001 00000111
    // Store int in register r1 into (memory) variable i 

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

    str     r1,[r0]            // Store int value from r1 
                               // into memory location r0

    .data
i:  .4byte  -4   // variable i (int i = -4)  

Result of the execution of str r1,[r0]:

DEMO: /home/cs255001/demo/asm/2-mov/str-v2.s