PI CSE30 Lecture 4 Part2
PI CSE30 Lecture 4 Part2
Instructions
Diba Mirza
Dept. of Computer Science and Engineering
University of California, San Diego
Assembly Operands: Memory
v Memory: Think of as single one-dimensional array where each cell
v Stores a byte size value
v Is referred to by a 32 bit address e.g. value at 0x4000 is 0x0a
v Specify
a register which contains the
memory address
v In case of the load instruction (LDR) this is the memory
address of the data that we want to retrieve from memory
v In case of the store instruction (STR), this is the memory
address where we want to write the value which is
currently in a register
v Example: [r0]
v specifiesthe memory address pointed to by the
value in r0
Data Transfer: Memory to Register
v Load Instruction Syntax:
1 2, [3]
v where
1) operation name
2) register that will receive value
3) register containing pointer to memory
v ARM Instruction Name:
v LDR(meaning Load Register, so 32 bits or one
word are loaded at a time)
Data Transfer: Memory to Register
v LDR r2,[r1]
This instruction will take the address in r1, and then load a 4
byte value from the memory pointed to by it into register r2
v Note: r1 is called the base register
Memory
r1 r2
0x200 0x200 0xaa 0xddccbbaa
0x201 0xbb
Base Register Destination Register
0x202 0xcc
0x203 0xdd for LDR
Data Transfer: Register to Memory
v STR r2,[r1]
This instruction will take the address in r1, and then store a 4
byte value from the register r2 to the memory pointed to by r1.
v Note: r1 is called the base register
Memory
r1 r2
0x200 0x200 0xaa 0xddccbbaa
0x201 0xbb
Base Register Source Register
0x202 0xcc
0x203 0xdd for STR
Base Displacement Addressing Mode
v To
specify a memory address to copy from,
specify two things:
v A register which contains a pointer to memory
v A numerical offset (in bytes)
A. 0x200
STR r2,[r1, #-4]! B. 0x1fc
C. 0x196
D. None of the above
Memory
r1 r2
0x200 0x20_ 0xaa 0xddccbbaa
0x20_ 0xbb
Base Register Destination Register
0x20_ 0xcc
0x20_ 0xdd for LDR
Base Displacement Addressing Mode
1. Post-indexed addressing:Base register is updated after load/
store
LDR/STR <dest_reg>[<base_reg>] ,offset
Examples:
LDR/STR r1 [r2], #4; offset: immediate 4
;Load/Store to/from memory address in r2, update r2=r2+4
LDR/STR r1 [r2], r3; offset: value in register r3
;Load/Store to/from memory address in r2, update r2=r2+r3
LDR r1 [r2] r3, LSL #3; offset: register value left shifted
;Load/Store to/from memory address in r2, update r2=r2+r3*23
Post-indexed Addressing Mode
Memory
* Example: STR r0, [r1], #12
r1 Offset r0
Updated Source
Base 0x20c 12 0x20c 0x5 Register
Register for STR
0x200 0x5
r1
Original
Base 0x200
Register
* If r2 contains 3, auto-increment base register to 0x20c by multiplying
this by 4:
• STR r0, [r1], r2, LSL #2