There are four ways to access data in assembly language:
- Register mode addressing
- Immediate Data (#)
- Register indirect addressing (@)
- Absolute address (&)
- Indexed Mode X(Rn)
- Using labels
Register mode addressing
It is the simplest mode: we can use the registers name as operands directly:
mov.w PC, R4 ; copy PC to R4. R4 now contains the
; address of the next instruction
mov.w R4, R5 ; copy the R4 content to R5
mov.b PC, R4 ; copy the lower bits of PC to R7
Immediate Data (#)
We can use constant by prefixing it by #:
- decimal number #1234 : Decimal 1234
- hexadecimal number #1234h : hexadecimal value Ox1234. If the number starts with a letter, we must start with a 0 (#0BEEFh)
- binary number #1011b : 11 in binary
mov.w #0BEEFh, R4 ; copy the hex value BEEF in R4
mov.b #19h, R4 ; put the value 19h in R4
mov.w #1234, R4 ; put the dec 1234 value in R4
mov.b #10110b, R4 ; put the 11 dec value in R4
mov.b #'G', R9 ; put he G's ASCII code in R4
Register indirect addressing (@)
; to check!
; mov instruction with Indirect Register Mode (@Rn) Address
mov.w #2400h, R4 ; We store the address of Const1 in R4
mov.w @R4, R5 ; We copy the content stored at the address 2400h in R5
; indirect autoincrement
mov.w #Block1, R4 ; Copy the address of List1 in R4
mov.w @R4+, R5 ; Copy the content stored at the address List1 and increment R4
; R5 = CAFEh
mov.w @R4+, R6 ; R6 = 1234h
mov.w @R4+, R7 ; R7 = DEADh
mov.w @R4+, R8 ; R8 = BEEFh
.data ; go to data memory (2400h)
.retain ; keep this section even if not used
Const1: .short 0BEEFh ; init first word to BEEFh address &2400h
List1: .short 0CAFEh, 01234h, 0DEADh, 0BEEFh
Absolute address (&)
; to check !
; memory allocation
; mov instruction with absolute mode (&ADDR) address
; we use the .data section to instate a variable.
; in the MSP430F5229, the fist variable is stored from address &2400h
mov.w &2400h, R4 ; copy the content from address 2400h into R4
mov.w R4, &2402h ; copy the content from R4 into address 2402h (Var1)
mov.w &2402h, &2408h; copy BEEFh from Var1 to Var2
.data ; go to data memory (2400h)
.retain ; keep this section even if not used
Const1: .short 0BEEFh ; init first word to BEEFh address &2400h
Var1: .space 2 ; reserve a space for a word (2 bytes), address &2402h
Const2: .short 4567Eh ; initialize a second variable Const2 containing 4567, address &2404h
Var2: .space 2
Indexed Mode X(Rn)
; mov instruction with Indexed Mode (X(Rn)) addressing
mov.w #Block1, R4 ; Copy the address of List1 in R4
mov.w 0(R4), 8(R4) ; copy the first word of List1 to the first word of List2
mov.w 2(R4), 10(R4) ; and so on...
mov.w 4(R4), 12(R4) ; and on
mov.w 6(R4), 14(R4) ; and on...
; memory allocation
.data ; go to data memory (2400h)
.retain ; keep this section even if not used
List1: .short 0CAFEh, 01234h, 0DEADh, 0BEEFh
List2: .space 8 ; space for 4 words (4 * 2nspaces)
using labels
; mov instruction with symbolic mode (ADDR) Address
mov.w Cow, R4
mov.w R4, Food ; Food contains "BEEFh"
; memory allocation
.data ; go to data memory (2400h)
.retain ; keep this section even if not used
Food: .space 2
Cow: .short 0BEEFh
Comments !