CHAPTER 3 - 2-Memory Access Instructions-Lecturer
CHAPTER 3 - 2-Memory Access Instructions-Lecturer
2: ARM
ASSEMBLY LANGUAGE
PROGRAMMING
BENM 2123: MICROPROCESSOR
TECHNOLOGY
Data Register
Data
Register
Data
Data
Memory Memory
Memory to memory data processing is not allowed because it can slow
down the memory access. Therefore, all data operation needs to be
done in register before transfer it back to memory.
Loads and Stores – The instructions
• The ARM has three sets of instructions which interact with main
memory. These are:
• Single register data transfer (LDR/STR)
• Block data transfer (LDM/STM) - ignore
• Single Data Swap (SWP) - ignore
Base register
A register which contains a pointer to memory
Example: [r0]
• specifies the memory address pointed to by the value in r0
• This is called register indirect addressing
Data Transfer: Memory to Register (LDR)
Load Instruction Syntax:
Data flow
LDR r1, [r0] ; Load r1 with contents of memory location pointed to by
contents of r0.
where
1) operation name (LDR)
2) register that will receive value (r1)
3) register containing pointer to memory/base register [r0]
0x00000200 0x00000200
0x12345678
r1 0x12345678
r0
Base register 0x00000204 The value contained in memory
location 0x00000200 is loaded in r1
0x00000208
0x0000020C
LDR r1, [r0]
Loads
LDR
Example:
Consider the instruction
LDR r9, [r0] ; load a word from the memory address pointed by r0 into r9
Assuming the address in register r0 is 0x6000, before and after the instruction is
executed, the data appear as follows:
Before load
r0 0×00006000 Address
0×6000 0×EE
r9 0×12345678
0×6001 0×FF
After load 0×6002 0×80
r0 0×00006000 0×6003 0×A7
r9 0×A780FFEE
Loads (cont.)
LDRB
Example:
Consider the instruction
LDRB r9, [r0] ; load a byte into r9
Assuming the address in register r0 is 0x6000, before and after the instruction is
executed, the data appear as follows:
Before load
r0 0×00006000 Address Memory
0×6000 0×EE
r9 0×12345678
0×6001 0×FF
After load 0×6002 0×80
r0 0×00006000 0×6003 0×A7
r9 0×000000EE
Assume little endian. If big 8 bits of memory copied to bottom of register; upper 24
endian, r9 = 0xEE000000 bits of register zeroed
Loads (cont.)
LDRH
Example:
Consider the instruction
LDRH r9, [r0] ; load a halfword into r9
Before load
r0 0×00006000 Address Memory
0×6000 0×EE
r9 0×12345678
0×6001 0×FF
After load 0×6002 0×80
r0 0×00006000 0×6003 0×A7
r9 0×0000FFEE
Loads (cont.)
LDRSB
Example:
Consider the instruction
LDRSB r9, [r0] ; load signed byte into r9
Assuming the address in register r0 is 0x6000, before and after the instruction is
executed, the data appear as follows:
Before load
r0 0×00006000 Address Memory
0×6000 0×EE
r9 0×12345678
0×6001 0×8C
After load 0×6002 0×80
r0 0×00006000 0×6003 0×A7
Example:
Consider the instruction
LDRSH r9, [r0] ; load signed half into r9
Assuming the address in register r0 is 0x6000, before and after the instruction is
executed, the data appear as follows:
Before load
r0 0×00006000 Address Memory
0×6000 0×EE
r9 0×12345678
0×6001 0×8C
After load 0×6002 0×80
r0 0×00006000 0×6003 0×A7
r9 0×FFFF8CEE
Data Transfer: Register to Memory (STR)
Store Instruction Syntax:
Data flow
STR r0,[r1] ; Store contents of r0 into the location pointed by r1
where
1) operation name (STR)
2) register that will transfer value (r0)
3) register containing pointer to memory/base register [r1]
1
3
Base register
0x5
r1 0x00000200 0x00000200
0x00000204
0x00000208
2 0x0000020C
r0 contains value 0x5 Write the value 0x5
contained in r0 to
r0 0x5 memory location
pointed to by r1
Stores
STR
Example:
Consider the instruction
• STR r9, [r0] ; Store word from r9 to location pointed to by contents of r0
Assuming the address in register r0 is 0x6000, before and after the instruction is
executed, the data appear as follows:
Example:
Consider the instruction
• STRB r9, [r0] ; Store byte from r9 to location pointed to by
contents of r0
Example:
Consider the instruction
• STRH r9, [r0] ; Store halfword from r9 to location pointed to by
contents of r0
Assuming the address in register r0 is 0x6000, before and after the instruction is
executed, the data appear as follows:
• type
• B unsigned byte, zero extend on load
• SB signed byte, sign extend (LDR only)
• H unsigned halfword, zero extend on load
• SH signed halfword, sign extend (LDR only)
• (omit) word
Source: http://infocenter.arm.com/
PC-relative LDR label must be within a
certain range
Source: http://infocenter.arm.com/
PC-relative LDR examples
• LDR R0, LookUpTable
• Load R0 with a word from the data memory location LookUpTable
• label
• Location of stored variable of which its address is to be loaded into
Rd
• Label must be within -4095 and +4095 of the current program
counter value
• In other words, ADR makes Rd as a pointer to the constant
label
Load an address into register (cont.)
Application of ADR
ADR example
ADR R0, lit ; returns the address of lit
LDR R2, [R0], #4 ; load 0x12345678 into R2
; increment R0 by 4
; to point to next value
LDR R3, [R0] ; load 0x88888888 into R3