(1) Program variables are used to store data (= information) in a computer program:
The program variables are stored in RAM (= memory)
(2) Only the to ALU inside the CPU can perform computations:
The ALU gets its inputs from the registers inside the CPU
(3) How do the CPU execute: z = x + y
(3A) fetch the value of the variable x into a register (e.g.: R0)
(3) How do the CPU execute: z = x + y
(3B) fetch the value of the variable y into another register (e.g.: R1)
(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)
(3) How do the CPU execute: z = x + y
(3D) Store the sum the the variable x in memory
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
We will use the following data types in assembler programming in CS255:
|
We will learn to access (= read and write) these 3 integer (= whole number) typed variables
Furthermore:
|
|
|
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
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
ldrsb r1, [r0]: (1) sends out r0 on the address bus to fetch the byte at that address into R1
ldrsb r1, [r0]: (2) converts the 8 bits 2s compl code into 32 bits 2s compl code
Reminder:
|
You can then use the ldrsb instruction to retrieve the byte value at the address in r0 !!!
.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
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
ldrsh r1, [r0]: (1) sends out r0 on the address bus to fetch the byte at that address into R1
ldrsh r1, [r0]: (2) converts the 16 bits 2s compl code into 32 bits 2s compl code
.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
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
ldr r1, [r0]: sends out r0 on the address bus to fetch the int at that address into R1
.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