Any reasonably size computer program will have more than 10 variables.
Clearly: you cannot store all the variables used in your program in the registers
But you need to store the values in a register when you need to use that value in a computation.
Rule of thumb:
|
Value byte (8 bits 2s compl code) 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 |
The reverse conversion (from int representation to byte representation) is simply:
|
Value short (16 bits 2s compl code) 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 |
The reverse conversion (from int representation to short representation) is simply:
|
|
Notice that moving (= copying) instruction only need to know how many bytes it needs to move (= copy) to perform correctly.
So you can use the same move instruction on data types that contains the same number of bytes
|
If the Store Instruction tranfers multiple bytes of data, the data in the register will be stored in consecutive memory locations
The Store Register instruction also has (too) many different formats, and we will only learn 3 formats to save time for other more important material.
The ARM processor has 3 different load register instructions:
str (store register (full)): moves 4 bytes from a register to memory (in consecutive locations) strh (store register half): moves 2 bytes from a register to memory (in consecutive locations) strb (store register byte): moves 1 byte from a register to memory |
I will now present the most basic format of the store register instruction that will be used (discussed soon) to move values in a register to simple variables in the memory.
Syntax and meaning of the
basic form of
the store register instruction
Example:
(Remember that register
r0 contains
a binary number, what I mean is:
the binary number in
register r0 represents
the decimal value 4000)
The instruction
str r1,[r0]
will:
In picture:
So the str instruction basically
does the reverse operation
of the ldr instruction.
Syntax Meaning of the instruction
---------------- --------------------------------------------------
str rN, [rM] Store 4 bytes from register rN to memory (consecutively) at
the address given in register rM
The 4 bytes are taken from the register as follows:
strh rN, [rM] Store 2 bytes from register rN to memory (consecutively) at
the address given in register rM
The 2 bytes are taken from the register as follows:
strb rN, [rM] Store 1 byte from register rN to memory (consecutively) at
the address given in register rM
The byte is taken from the register as follows:
|
1. Move the address of the memory variable into a (free) register rX 2. Then use: str srcreg, [rX] for an int typed memory variable strh srcreg, [rX] for a short typed memory variable strb srcreg, [rX] for a byte typed memory variable |
.data i: .4byte 444444 // int typed variable s: .2byte 2222 // short typed variable b: .byte 111 // byte typed variable |
Suppose register r1 contains 10101010 10110111 11001100 11011101 (= Hexadecimal AABBCCDD):
We will use the value in register r1 to update the variables i, s and b
The following instructions will move (copy) 4 bytes in register r1 to the int typed (4 bytes) variable i:
// Move i into r1 movw r0, #:lower16:i // Moves the address of memory movt r0, #:upper16:i // variable i into register r0 str r1,[r0] // Move int value from reg r1 to var |
After the execution, the memory variable i will contain 10101010 10110111 11001100 11011101 (= Hexadecimal AABBCCDD)
The following instructions will move (copy) the right-most 2 bytes in register r1 to the short typed (2 bytes) variable s:
// Move s into r1 movw r0, #:lower16:s // Moves the address of memory movt r0, #:upper16:s // variable s into register r0 strh r1,[r0] // Move short value from reg r1 to var |
After the execution, the memory variable s will contain 11001100 11011101 (= Hexadecimal CCDD)
The following instructions will move (copy) the right-most byte in register r1 to the byte typed (1 byte) variable b:
// Move b into r2 movw r0, #:lower16:b // Moves the address of memory movt r0, #:upper16:b // variable b into register r0 strb r1,[r0] // Move byte value from reg r1 to var |
After the execution, the memory variable b will contain 11011101 (= Hexadecimal DD)
How to run the program:
|