|
In this webpage, I will show you what will happen when you use an incorrect load/store instruction (and quiz you if you can understand the cause of the error)
|
Syntax and meaning of the basic form of the load register instruction is:
Syntax Meaning of the instruction ---------------- -------------------------------------------------- ldr rN, [rM] Load 4 bytes from memory at the address given in rM into the (destination) register rN The 4 bytes are stored in the register as follows: |
Let's see how well you can apply your understanding...
|
In the following program, I transfer the value stored at the memory location x: into the registers using 3 different load instruction:
main:
// short x contains 254 = 0000000011111110
movw r0, #:lower16:x // Moves the address of memory
movt r0, #:upper16:x // variable x into register r0
// Move short x into r1 using ldrsh (correct !)
ldrsh r1,[r0] // r1 = 254, correct
// Move short x into r2 using ldrsb (wrong value !)
ldrsb r2,[r0] // r2 = -1, WRONG !!
// Can you explain why r2 contains -1 ?
// Move short x into r3 using ldr (wrong value !)
ldr r3,[r0] // r2 = 65790 = 216 + 254, WRONG !!
// Can you explain why r2 contains 216 + 254 ?
/* --------------------------------------------------
Begin of the permanent program variables
-------------------------------------------------- */
.data
x: .2byte 254 // short typed variable
y: .2byte 1 // short typed variable
.end
|
The ldrsh instruction will transfer the correct value (254) into the register r1
But::
|
Can you explain why using what you know about these instructions (see above) ???
How to run the program:
|
|
|
Syntax and meaning of the
basic form of
the store register instruction
Let's see
how well you can
apply your understanding...
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:
|
In the main( ) method, I will update the memory variable at the memory location x: with the value -2 (stored in the register r1) using 3 different store instructions:
main:
movw r0, #:lower16:x // Moves the address of memory
movt r0, #:upper16:x // variable x into register r0
mov r1, #-2
// Move -2 into short var x (correct !)
strh r1,[r0] // x = -2, correct
// Change strh to strb and you will get x = 254 !!
// Can you explain why x contains 254 ?
// Change strh to str and you will update y = -1 !!
// Can you explain why y gets updated to -1 ?
/* --------------------------------------------------
Begin of the permanent program variables
-------------------------------------------------- */
.data
x: .2byte 4 // a short typed variable
y: .2byte 7 // a short typed variable
.end
|
You need to edit the program and recompile and run again to see the following results:
|
Can you explain why using what you know about these store instructions (see above) ???
How to run the program:
|
|
|