Review of the assembler instructions

  • Previously, we have learned:

         mov  rN, #smallConstant  // Store a small constant 
                                  // ([-100,100]) in register rN 
      
         movw rN, #:lower16:x     // Store the address of the
         movt rN, #:upper16:x     // variable x in register rN
      
         ldrsb rN, [rM]           // Load rN with byte  at mem[rM]
         ldrsh rN, [rM]           // Load rN with short at mem[rM]
         ldr   rN, [rM]           // Load rN with int   at mem[rM]
      
         strb rN, [rM]            // Store byte  from rN in mem[rM]
         strh rN, [rM]	    // Store short from rN in mem[rM]
         str  rN, [rM]	    // Store int   from rN in mem[rM] 
      

  • We will now combine the different instructions to perform the assignment statements b = 4; and b = a;

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

How does a compiler translates b = 4; to ARM instructions:

  int b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4:

What assembler instruction can store a value into memory ?

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

How does a compiler translates b = 4; to ARM instructions:

  int b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4: str r1, [r0] // Store value in reg r1 into mem at r0

How must you initialize r0 and r1 ?

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

How does a compiler translates b = 4; to ARM instructions:

  int b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4: // Preparation: // r1 = 4 // r0 = address of variable b str r1, [r0] // Store value in reg r1 into mem at r0

What instruction will store the value 4 into register r1 ?

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

How does a compiler translates b = 4; to ARM instructions:

  int b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4: mov r1, #4 // Preparation: // r0 = address of variable b str r1, [r0] // Store value in reg r1 into mem at r0

What instructions will store the address of variable b into register r0 ?

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

How does a compiler translates b = 4; to ARM instructions:

  int b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4: mov r1, #4 movw r0, #:lower16:b movt r0, #:upper16:b str r1, [r0] // Store value in reg r1 into mem at r0

This is the translation of the statement b = 4; into ARM assembler code.

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

Question: what will change if variable b was a short typed variable:

  short b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4: mov r1, #4 movw r0, #:lower16:b movt r0, #:upper16:b str r1, [r0] // Store value in reg r1 into mem at r0

In answering this question, you will know why you must recompile your program!!

Translating variable = smallConstant (e.g.: b = 4;) into assembler code
 

Answer: we must use strh to store the value into a short typed variable

  short b;

  b = 4;  // Store the value 4 into the variable b in memory


Assembler instructions that perform b = 4: mov r1, #4 movw r0, #:lower16:b movt r0, #:upper16:b strh r1, [r0] // Store value in reg r1 into mem at r0

The store instruction must match the target (memory) data type exactly !

Translating var2 = var1 (e.g.: b = a;) into assembler code

How does a compiler translates b = a; to ARM instructions:

  byte a;
  int b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a:

What assembler instruction can store a value into memory ?

Translating var2 = var1 (e.g.: b = a;) into assembler code

How does a compiler translates b = a; to ARM instructions:

  byte a;
  int b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: str r1, [r0] // Store value in reg r1 into mem at r0

How must you initialize r0 and r1 ?

Translating var2 = var1 (e.g.: b = a;) into assembler code

How does a compiler translates b = a; to ARM instructions:

  byte a;
  int b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: // Preparation: // r1 = value in var a // r0 = address of variable b str r1, [r0] // Store value in reg r1 into mem at r0

How do you fetch the value in variable a into register r1 ?

Translating var2 = var1 (e.g.: b = a;) into assembler code

How does a compiler translates b = a; to ARM instructions:

  byte a;
  int b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: movw r0, #:lower16:a movt r0, #:upper16:a ldrsb r1, [r0] // Preparation: // r0 = address of variable b str r1, [r0] // Store value in reg r1 into mem at r0

What instructions will store the address of variable b into register r0 ?

Translating var2 = var1 (e.g.: b = a;) into assembler code

How does a compiler translates b = a; to ARM instructions:

  byte a;
  int b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: movw r0, #:lower16:a movt r0, #:upper16:a ldrsb r1, [r0] movw r0, #:lower16:b movt r0, #:upper16:b str r1, [r0] // Store value in reg r1 into mem at r0

Translating var2 = var1 (e.g.: b = a;) into assembler code

Question: what will change if variable b was a short typed variable:

  byte  a;
  short b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: movw r0, #:lower16:a movt r0, #:upper16:a ldrsb r1, [r0] movw r0, #:lower16:b movt r0, #:upper16:b str r1, [r0] // Store value in reg r1 into mem at r0

In answering this question, you will know why you must recompile your program!!

Translating var2 = var1 (e.g.: b = a;) into assembler code

Answer: we must use strh to store the value into a short typed variable

  byte  a;
  short b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: movw r0, #:lower16:a movt r0, #:upper16:a ldrsb r1, [r0] movw r0, #:lower16:b movt r0, #:upper16:b strh r1, [r0] // Store value in reg r1 into mem at r0

The store instruction must match the target (memory) data type exactly !

Translating var2 = var1 (e.g.: b = a;) into assembler code

Question: what will change if variable a was a short typed variable:

  short a;
  int   b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: movw r0, #:lower16:a movt r0, #:upper16:a ldrsb r1, [r0] movw r0, #:lower16:b movt r0, #:upper16:b str r1, [r0] // Store value in reg r1 into mem at r0

In answering this question, you will know why you must recompile your program!!

Translating var2 = var1 (e.g.: b = a;) into assembler code

Answer: we must use ldrsh to load the value into a short typed variable

  short a;
  int   b;

  b = a;  // Store val in var a into the var b 


Assembler instructions that perform b = a: movw r0, #:lower16:a movt r0, #:upper16:a ldrsh r1, [r0] movw r0, #:lower16:b movt r0, #:upper16:b str r1, [r0] // Store value in reg r1 into mem at r0

The load instruction must match the target (memory) data type exactly !

Apply what we have learned so far

I will show you how to translate these simple Java statements into ARM assembler:

   Data types:

        byte  b;
        short s;
	int   i;

   Translate these statements into ARM assembler:

      b = 4;  // Use the correct str instruction
      s = 4;  // for the variable on the RHS 
      i = 4;

      b = i;  // I won't do all of them...
      s = i;  // because it will be very boring...
      i = b;  // Principle:
      i = s;  //  Use the correct ldr instruction for var on LHS 
      s = b;  //  Use the correct str instruction for var on RHS 
      b = s; 
 

DEMO: use /home/cs255001/demo/asm/2-mov/ldr+str.s