Review of knowledge for understanding the ldr and str instructions

(1) Program variables are used to store data (= information) in a computer program:

The program variables are stored in RAM (= memory)

Review of knowledge for understanding the ldr and str instructions

(2) Only the to ALU inside the CPU can perform computations:

The ALU gets its inputs from the registers inside the CPU

Review of knowledge for understanding the ldr and str instructions

(3) How do the CPU execute: z = x + y

(3A) fetch the value of the variable x into a register (e.g.: R0)

Review of knowledge for understanding the ldr and str instructions

(3) How do the CPU execute: z = x + y

(3B) fetch the value of the variable y into another register (e.g.: R1)

Review of knowledge for understanding the ldr and str instructions

(3) How do the CPU execute: z = x + y

(3C) Add the copies of the x and y and save the result in a register (e.g.: R2)

Review of knowledge for understanding the ldr and str instructions

(3) How do the CPU execute: z = x + y

(3D) Store the sum the the variable x in memory

Fetching data from memory: The load register instructions in ARM

The load register instructions ( plural !) are used to fetch data from memory into a register:

We will learn 3 (three) load data instructions of ARM

Data types used in CS255
 

We will use the following data types in assembler programming in CS255:

  • 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 will learn to access (= read and write) these 3 integer (= whole number) typed variables

 

Furthermore:

  • We usually want to perform computations on values in the memory variable...

Background info: Arithmetic operations in ARM
 

  • Important fact about arithmetic operations in ARM:

    • Arithmetic operations (+, and ×) in ARM only operate on 32 bits (binary) numbers

  • In other words:

    • ARM cannot perform +, and × operations on byte or short values

  • How to perform arithmetic operations on byte or short values in ARM:

      1. Convert the (8/16 bits) representation to the int 32 bits representation

      2. Perform the arithmetic operation

      3. Convert the result back to the byte/short representation

The load instructions of ARM
 

  • The load instruction of ARM has many syntax formats !!

    (I will need at least 3 lectures to cover all the different formats...)

  • We will limit to the following 3 formats of the load instruction:

         ldr rn, [rm]       // The basic load instruction   
         ldr rn, [rm, #n]   // Load with constant offset
         ldr rn, [rm, rk]   // Load with variable offset
      

We will now learn to use the basic load instruction

We will learn to use the other (more advanced) load instruction formats later when we access array variables

The ldrsb instruction

Syntax and meaning of the ldrsb instruction:

   ldrsb rN, [rM]       

      Load 1 byte from memory at the address given
      in rM into the (destination) register rN 
      AND convert it into int (32 bits) representation

      The byte is stored at the right rN
      and then it is sign extended to 32 bits:


          
   

Note:   initialize rM = address of a byte variable before using the ldrsb instruction

The ldrsb instruction - explained graphically

ldrsb r1, [r0]:   (1) sends out r0 on the address bus to fetch the byte at that address into R1

The ldrsb instruction - explained graphically

ldrsb r1, [r0]:   (2) converts the 8 bits 2s compl code into 32 bits 2s compl code

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            

  • To move (= put) the memory address b (it's a binary number !) into the register r0, use:

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

You can then use the ldrsb instruction to retrieve the byte value at the address in r0 !!!

Example: using the ldrsb instruction to fetch a byte typed variable into a register

    .text
main:
    // Move variable b into register r1
    movw    r0, #:lower16:b  // Moves the address of memory
    movt    r0, #:upper16:b  // variable b into register r0

    ldrsb   r1,[r0]          // Move byte value from var into r1

    .data
b:  .byte  -2       // variable b (byte b = -2 or  11111110)  

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

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

The ldrsh instruction

Syntax and meaning of the ldrsb instruction:

   ldrsh rN, [rM]       

      Load 2 bytes from memory at the address given
      in rM into the (destination) register rN 
      AND convert it into int (32 bits) representation

      The byte is stored at the right rN
      and then it is sign extended to 32 bits:


          
   

Note:   initialize rM = address of a short variable before using the ldrsh instruction

The ldrsh instruction - explained graphically

ldrsh r1, [r0]:   (1) sends out r0 on the address bus to fetch the byte at that address into R1

The ldrsh instruction - explained graphically

ldrsh r1, [r0]:   (2) converts the 16 bits 2s compl code into 32 bits 2s compl code

Example: using the ldrsh instruction to fetch a short typed variable into a register

    .text
main:
    // Move short typed variable s into register r2
    movw    r0, #:lower16:s  // Moves the address of memory
    movt    r0, #:upper16:s  // variable s into register r0

    ldrsh   r2,[r0]          // Move short value from var into r2

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

Result of the execution of ldrsh r2,[r0]:

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

The ldr instruction

Syntax and meaning of the ldr instruction:

   ldr rN, [rM]       

      Load 4 bytes from memory at the address given
      in rM into the (destination) register rN 

      (No sign-extension necessary because rN has 32 bits)

          
   

Note: initialize rM = address of a short variable before using the ldr instruction

The ldr instruction - explained graphically

ldr r1, [r0]:   sends out r0 on the address bus to fetch the int at that address into R1

Example: using the ldr instruction to fetch a int typed variable

    .text
main:
    // Move variable i into register r3
    movw    r0, #:lower16:i    // Moves the address of memory
    movt    r0, #:upper16:i    // variable i into register r0

    ldr     r3,[r0]            // Move int value from var into r3

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

Result of the execution of ldr r3,[r0]:

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