Review of knowledge for understanding the data movement instructions

Review 1: Instructions are stored in RAM (memory)

 

Review of knowledge for understanding the data movement instructions

Review 2: The CPU fetches instructions into its Instruction Register first:

The fetched instruction is then executed (by steps 2,3,4 of the instruction execution cycle)

The data "movement" instructions in ARM

Important fact about assembler programming:

  • all computations begin by moving values into registers and
  • end with storing the result into memory

We will study the data transfer instructions:

  1. The mov (move) instruction:

      • to "move" (= copy) a small (binary) number (< 100) into a register and
      • to "move" (= copy) the value (= binary number) from one register into another register

  2. The movw and movt instructions to "move" (= copy) large numbers into a register

  3. The ldr (load) instructions to fetch a (binary) number in memory into a register

  4. The str (store) instructions to store a (binary) number in a register into RAM (memory)

Assigning a small constant to a register using the mov instruction
 

The first way to use the mov instruction:

  • The mov instruction can be used to assign a small (integer) constant to a register

  • The syntax of this mov instruction is:

           mov rN, #constant  // rN = any ARM register name  
      
       Examples:
      
           mov  r0,  #0   // Assign the value  0 to register r0
           mov  r2,  #-1  // Assign the value -1 to register r2   
           mov  pc,  #4   // Assign the value  4 to the Program Counter

    The constant will be stored as a 32 bit 2's complement number in the register rN

Moving a small (constant) number into a register - example
 

Examples:

  mov  r0, #15
  // Stores  00000000000000000000000000001111 in r0

  mov  r1, #-15
  // Stores  11111111111111111111111111110001 in r1   

Effect of the mov instructions:

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

Also try:   mov r0, #123456    (1e240(16) = 12345610)

How the computer executes the mov r0,#15 instruction
 

Suppose the next instruction executed is   mov r0, #15:

The mov r0, #15 instruction is first fetched into the Instruction Register (IR) by the Instruction Fetch step

How the computer executes the mov r0,#15 instruction
 

Then the mov r0, #15 will be executed by steps 2,3,4 of the Instruction Execution Cylce:

The mov r0, #15 will store the (binary) number 15 (= 00..1111) into register R0

Why can the mov instruction only use a small constant ?
 

The mov instruction is encoded as follows:

  mov  rN, #x    is encoded in ARM as:

    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | | | | |0|0|1|1|1|0|1|S|x|x|x|x|   N   |            value (x)          |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
     <----->     ^ <----->   <----->                 <--------------------->
     1 1 1 0     |   "mov"   not used                       12 bits
 unconditional   |
           immediate bit (= use x as a number)

 

Notice: there are only 12 bits available in the instruction code to encode the value x !!!

Therefore:

  • the mov instruction can only use binary numbers that can be represented by at most 12 bits

The range of values that can be used in the mov rN, #x
 

Simplified rule for using mov rN, #x:

  • mov rN, #x can correctly use values in the range x ∈ [-100, 100]

 

Comment:

  • the exactly "range" of values is very complicated

  • Also, you do not to this knowledge to learn assembler programming

For the curious: you can read the online lecture note for the explanation: click here )

What if I need to use a large number into a register ?

  • Important fact:

    • The mov rN, #x instruction cannot use large values

      Example: this mov instruction will cause a assembler/compile error:

         mov  r0,  #1234567   // Compile error
      

  • How to move a large integer value(= 32 bits) into a register in ARM assembler:

    1. Divide the 32 bits number into two (2) 16 bits numbers

    2. Use the movw instruction to ove the lower half into the register

    3. Use the movt instruction to ove the upper half into the register

    We will learn how to do this soon