Module 1 and 2 Maiin
Module 1 and 2 Maiin
Chapter 1
The microprocessors do not contain RAM, ROM, or I/O peripherals. As a result, they
must be connected externally to RAM, ROM and I/O through buses
ARM Assembly Language Programming & Architecture by Mazidi, et al.
Simplified View of the Internal Parts of Microcontrollers (SOC)
In microcontroller, CPU, RAM, ROM, and I/Os, are put together on a single IC chip
and it is called microcontroller. SOC (System on Chip) and MCU (Micro Controller
Unit) are other names used to refer to microcontrollers.
ARM Assembly Language Programming & Architecture by Mazidi, et al.
Embedded System
• A system in which hardware and software application are embedded
together and are designed to do a specific task.
• Example: Kindle, washing machine
ARM supports byte, half word (16 bit) and word (32 bit) data types.
ARM Registers
The ARM CPU uses 32-bit addresses which gives us a maximum of 4 GB (gigabytes)
of memory space. This 4GB of memory space has addresses 0x00000000 to
0xFFFFFFFF, meaning each byte is assigned a unique address (ARM is a byte-
addressable CPU).
PPB – Private
Peripheral Bus
(c) With address space of 0000 to 7FFFF, we have 7FFFF – 0 = 7FFFF bytes. Converting
7FFFF to decimal, we get 524,287 + 1, which is equal to 512K bytes.
Every instruction of ARM is fixed at 32-bit. The fixed size instruction is one of the most
important characteristics of RISC architecture.
The first column of each line is always considered as label. Thus, be careful to
press a Tab at the beginning of each line that does not have label; otherwise, your
instruction is considered as a label and an error message will appear when
compiling. ARM Assembly Language Programming & Architecture by Mazidi, et al.
Pseudo Instructions:
LDR Pseudo Instruction
LDR Rd,=32-bit_immidiate_value
Notice the = sign used in the syntax. The following pseudo-instruction loads R7
with 0x112233.
LDR R7, =0x112233
To load values less than 0xFF, “MOV Rd, #8-bit_immidiate_value” instruction is
used since it is a real instruction of ARM, therefore more efficient in code size.
In the big endian method, the high byte goes to the low address, whereas in the
little endian method, the high byte goes to the high address and the low byte to the
low address.
In von Neumann, there are no separate buses for code and data memory. In
Harvard, there are separate buses for code and data memory.
ARM Assembly Language Programming & Architecture by Mazidi, et al.
When the CPU wants to execute the “LDR Rd,[Rx]” instruction, it puts Rx on the
address bus of the data bus, and receives data through the
data bus. For example, to execute “LDR R2,[R5]”, assuming that R5 =
0x40000200, the CPU puts the value of R5 on the address bus. The Memory
puts the contents of location 0x40000200 on the data
bus. The CPU gets the contents of location 0x40000200 through the data bus
and brings into CPU and puts it in R2.
The “STR Rx,[Rd]” instruction is executed similarly. The CPU puts Rd on the
address bus and the contents of Rx on the data bus. The memory location
whose address is on the address bus receives the contents of data bus.
1. register
2. immediate
3. register indirect (indexed addressing mode)
Table 6-2 summarizes these addressing modes. Each of these indexed addressing mode can be used with offset
of fixed value or offset of a shifted register.
Table 6-2: Indexed Addressing in ARM ARM Assembly Language Programming & Architecture by Mazidi, et al.
Preindexed address mode with offset register:
LDR R0, [R1, R2] ; Load R0 from [R1 + R2]
Preindexed address mode with offset of a shifted register
LDR R1,[R2,R3,LSL#2] ;content of location R2+(R3×4) (i.e; R2 + R3 LSL by 2) is loaded to R1
WB in Preindexed address mode with offset of a shifted register
LDR R1,[R2,R3,LSL#2]! ;content of location R2+(R3×4) is loaded to R1
;R2 = R2 +(R3 × 4)
Program counter relative LDR R0, [PC, #offset]
• MOV Instruction
MOV RD, op2 ; op2 can be a register or 8 bit constant
• MOVT instruction that can load any value in the range 0x0000 to 0xFFFF into
the most significant half of a register, without altering the contents of the least
significant half.
• MOV32 instruction can also be used to load any 32-bit number.
MOV32 R2, #5
• The LDR Rd,=const pseudo-instruction generates the most efficient single
instruction to load any 32-bit number.
The instructions ADD and ADC are used to add two operands. The destination
operand must be a register. The Op2 operand can be a register or immediate.
Remember that memory-to-register or memory-to-memory arithmetic and logic
operations are never allowed in ARM Assembly language since it is a RISC
processor. The instruction could change any of the Z, C, N, or V bits of the status
flag register, as long as we use the ADDS or ADCS instead of ADD or ADC. ADC is
used in the addition of multiword data.
Show how to create 2’s complement of a 64-bit data in R0 and R1 register. The
R0 hold the lower 32-bit.
Solution:
LDR R0,=0xF62562FA ;R0 = 0xF62562FA
LDR R1,=0xF812963B ;R1 = 0xF812963B
RSB R5,R0,#0 ;R5 = 0 – R0
; = 0 – 0xF62562FA = 9DA9D06 and C = 0
RSC R6,R1,#0 ;R6 = 0 – R1 – 1 + C
; = 0 – 0xF812963B – 1 + 0 = 7ED69C4
Notice that multiply and add can produce a result greater than 32-bit, if the
MLA instruction is used, the destination register will hold the lower word
(32-bit) and the portion beyond 32-bit is dropped.
ARM Assembly Language Programming & Architecture by Mazidi, et al.
UMLAL RdLo,RdHi,Rn,Op2 ;RdHi:RdLo = Rn × Op2 + RdHi:RdLo
In multiplication and add, the operands must be in registers. Notice that the
addend and the high word of the destination use the same registers, the two left
most registers in the instruction. It means that the contents of the registers
which have the addend will be changed after execution of UMLAL instruction.
• The CMP instruction compares two operands and changes the flags according to the result of the
comparison. The operands themselves remain unchanged. The second source operands can be a
register or an immediate value not larger than 0xFF.
• It must be emphasized that “CMP Rn,Op2” instruction is really a subtract operation. Op2 is subtracted
from Rn (Rn – Op2) and the result is discarded and flags are set accordingly.
• Although all the C, S, Z, and V flags reflect the result of the comparison, only C and Z are used for
unsigned numbers, as shown below:
C Z
Rn > Op2 1 0
Rn = Op2 1 1
Rn < Op2 0 0
we can perform the shift and rotate operations as part of other instructions
such as MOV
MOV R0,#0x9A
MOV R2,#0x03
MOV R1,R0,LSR R2 ;shift R0 to right R2 times and move the result to R1
LDR R1,=0x0F000006
MOVS R2,R1,LSL #8
LDR R1,=0x0F000006
MOV R0,#0x08
MOV R2,R1,LSL R0
MOV R1,#0x36
MOVS R1,R1,ROR #1
MOV R1,#0x36
MOV R0,#3
MOVS R1,R1,ROR R0
There is no rotate left option in ARM since one can use the rotate right (ROR)
to do the job. That means instead of rotating left n bits we can use rotate right
32–n bits to do the job of rotate left. Using this method does not give us the
proper carry if actual instruction of ROL was available
LDR R0,=0x00000072
;R0 = 0000 0000 0000 0000 0000 0000 0111 0010
MOVS R0,R0,ROR #31
;R0 = 0000 0000 0000 0000 0000 0000 1110 0100 C=0
In RRX the LSB is moved to C and C is moved to the MSB. In reality, C flag acts as
if it is part of the operand. That means the RRX is like 33-bit register since the C
flag is 33rd bit. The RRX takes no arguments and the number of times an
operand to be rotated is fixed at one.
;assume C=0
MOV R2,#0x26
;R2 = 0000 0000 0000 0000 0000 0000 0010 0110
MOVS R2,R2,RRX
;R2 = 0000 0000 0000 0000 0000 0000 0001 0011 C=0
ARM Assembly Language Programming & Architecture by Mazidi, et al.
Rotate operations for unsigned numbers in ARM
In ARM Cortex M all the above shift and rotate instructions can be used as
independent instructions
LDR R2,=0x00000002
RRX R0,R2 ;R0=R2 is shifted right one bit
;now, R0=0x00000001
RRXS Rotate Right with extend (update the flags)
RRXS Rd,Rm ;Rd=rotate Rm right 1 bit position
Function: Each bit of Rm register is shifted from left to right one bit. The RRXS
updates the flags.
LDR R2,=0x00000002
RRXS R0,R2 ;R0=R2 is shifted right one bit
;now, R0=0x00000001
ARM Assembly Language Programming & Architecture by
Mazidi, et al.
{cond} Suffix Tested Status Flags Description
EQ Z set equal
NE Z clear not equal
CS/HS C set unsigned higher or same
CC/LO C clear unsigned lower
MI N set negative
PL N clear positive or zero
VS V set overflow
VC V clear no overflow
HI C set and Z clear unsigned higher
LS C clear or Z set unsigned lower or same
GE N equals V signed greater or equal
LT N not equal to V signed less than
GT Z clear AND (N equals V) signed greater than
LE Z set OR (N not equal to V) signed less than or equal
AL (ignored) always (usually omitted)
• In the ARM there is only one instruction for call and that is BL (branch and
link).
• To use BL instruction for call, we must leave the R14 register unused since
that is where the ARM CPU stores the address of the next instruction where
it returns to resume executing the program.
• ARM automatically saves in the link register (LR), the R14, the address of the
instruction immediately below the BL.
• To branch beyond the address space of 32M bytes, we use BX (branch and
exchange) instruction.
• The “BX Rn” instruction uses register Rn to hold target address. Since Rn can
be any of the R0–R14 registers and they are 32-bit registers, the “BX Rn”
instruction can land anywhere in the 4G bytes address space of the ARM.
• In the instruction “BX R2” the R2 is loaded into the program counter (R15)
and CPU starts to fetch instructions from the target address pointed to by
R15, the program counter.
TST Rn,Op2 ;Rn AND with Op2 and flag bits are updated
The TST instruction is used to test the contents of register to see if any bit is
set to HIGH.
After the operands are ANDed together the flags are updated.
After the TST instruction if result is zero, then Z flag is raised and one can use
BEQ (branch equal) to make decision.
example:
MOV R0,#0x04 ;R0=00000100 in binary
LDR R1,=myport ;port address
OVER LDRB R2,[R1] ;load R2 from myport
TST R2,R0 ;is bit 2 HIGH?
BEQ OVER ;keep checking
ARM Assembly Language Programming & Architecture by Mazidi, et al.
In TST, the Op2 can be an immediate value of less than 0xFF.
example:
LDR R1,=myport ;port address
OVER LDRB R2,[R1] ;load R2 from myport
TST R2,#0x04 ;is bit 2 HIGH?
BEQ OVER ;keep checking
• TEQ Rn,Op2 ;Rn EX-ORed with Op2 and flag bits are set
• The TEQ instruction is used to test to see if the contents of two registers are
equal.
• After the source operands are Ex-ORed together the flag bits are set according
to the result.
• After the TEQ instruction if result is 0, then Z flag is raised and one can use BEQ
(branch zero) to make decision.