Chapter 03 - Assembly Language Programming
Chapter 03 - Assembly Language Programming
Chapter 3
Assembly programming language
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 1 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 2 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 3 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 4 / 272
3. Assembly programming language 3.1 Introduction to Assembly
High-level
language
Assembly
language
Low-level
language
Machine language
Hardware
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 5 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Assembler syntax
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 6 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Assembler syntax
Symbols:
⋄ Used as labels, constants, and substitution values and stored in
a symbol table.
⋄ A symbol name is a string of up to 200 alphanumeric characters
(A-Z, a-z, 0-9, $, and ), cannot contain embedded blanks, is
case sensitive.
⋄ The first character cannot be a number.
Labels:
⋄ Labels are symbols.
⋄ Begined in column 1 and is optionally followed by a colon.
⋄ The value of a label is the current value of the Location
⋄ Counter (address within program).
⋄ A label on a line by itself is a valid statement.
⋄ Labels used locally within a file must be unique.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 7 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Assembler syntax
mnemonic:
⋄ Contains one of the following items: Instruction, directive, pseudo-
instruction.
⋄ Instructions and pseudo-instructions make up the code a pro-
cessor uses to perform tasks.
⋄ Directives provide important information to the assembler that
either affects the assembly process or affects the final output
image.
⋄ Cannot start in column 1. If it does, it is interpreted as a label.
Operands:
⋄ Contains one or more operands.
⋄ An operand may consist of: symbols, constants, expressions.
⋄ Operands are separated with commas.
Comment: Optional.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 8 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 9 / 272
3. Assembly programming language 3.1 Introduction to Assembly
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 10 / 272
3. Assembly programming language 3.2 Assembly source code
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 11 / 272
3. Assembly programming language 3.2 Assembly source code
Literals
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 12 / 272
3. Assembly programming language 3.2 Assembly source code
Literals
Assembly language source code can contain numeric, string, Boolean, and single
character literals. Literals can be expressed as
Decimal numbers, for example 123.
Hexadecimal numbers, for example 0x7B.
Numbers in any base from 2 to 9, for example 5 204 is a number in base 5.
Floating point numbers, for example 123.4.
Boolean values TRUE or FALSE.
Single character values enclosed by single quotes, for example ’w’.
Strings enclosed in double quotes, for example "This is a string".
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 13 / 272
3. Assembly programming language 3.2 Assembly source code
Common directives
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 14 / 272
3. Assembly programming language 3.2 Assembly source code
Common directives
Directives are not instructions. They are used to divert the manner of the assembling
code.
The common directives include:
DCB directive allocates one or more bytes of memory, and defines the initial
runtime contents of the memory.
ELF (Executable and Linkable Format) sections are independent, named, indi-
visible sequences of code or data.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 15 / 272
3. Assembly programming language 3.2 Assembly source code
Common directives
END directive informs the assembler that it has reached the end of a source
file.
ENTRY directive declares an entry point to a program.
EQU directive gives a symbolic name to a numeric constant, a register-relative
value or a PC-relative value.
EXPORT or GLOBAL directive declares a symbol that can be used by the linker
to resolve symbol references in separate object and library files. GLOBAL is a
synonym for EXPORT.
INCLUDE or GET directive includes a file within the file being assembled. The
included file is assembled at the location of the GET directive. INCLUDE is a
synonym for GET.
RN directive defines a name for a specified register.
THUMB directive instructs the assembler to interpret subsequent instructions as
Thumb instructions.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 16 / 272
3. Assembly programming language 3.2 Assembly source code
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 17 / 272
3. Assembly programming language 3.2 Assembly source code
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 18 / 272
3. Assembly programming language 3.2 Assembly source code
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 19 / 272
3. Assembly programming language 3.3 Move instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 20 / 272
3. Assembly programming language 3.3 Move instructions
MOV instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 21 / 272
3. Assembly programming language 3.3 Move instructions
MOV instruction
Syntax
MOV{S}{cond} Rd, Operand2
MOV{cond} Rd, #imm16
where:
S is an optional suffix.
cond is an optional condition code.
Rd is the destination register.
Operand2 is a flexible second operand.
imm16 is any value in the range 0-65535 (0x0000 - 0xFFFF).
Operation
Rd ← Operand2
Description
Copy the value of Operand2 into Rd.
Status bits
If S is specified, the N, Z, and C (during the calculation of Operand2) flags according
to the result but the V flag is not affected.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 22 / 272
3. Assembly programming language 3.3 Move instructions
MOV instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 23 / 272
3. Assembly programming language 3.3 Move instructions
MOV instruction
Solution:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 24 / 272
3. Assembly programming language 3.3 Move instructions
MOV instruction
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 25 / 272
3. Assembly programming language 3.3 Move instructions
MOVT instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 26 / 272
3. Assembly programming language 3.3 Move instructions
MOVT instruction
Syntax
MOVT{cond} Rd, #imm16
where:
cond is an optional condition code.
Rd is the destination register.
imm16 is a 16-bit immediate value.
Operation
Rd ← imm16
Description
MOVT writes imm16 to Rd[31:16], without affecting Rd[15:0]
Status bits
This instruction does not change the flags.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 27 / 272
3. Assembly programming language 3.3 Move instructions
MOVT instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 28 / 272
3. Assembly programming language 3.3 Move instructions
MOVT instruction
Solution:
ALIGN
END
MOV32 pseudo-instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 30 / 272
3. Assembly programming language 3.3 Move instructions
MOV32 pseudo-instruction
Syntax
MOV32{cond} Rd, expr
where:
cond is an optional condition code.
Rd is the destination register. Rd must not be SP or PC.
expr can be any one of the following:
⋄ symbol: A label in this or another program area.
⋄ #constant: Any 32-bit immediate value.
⋄ symbol + constant: A label plus a 32-bit immediate value.
Operation
Rd ← expr
Description
Load any 32-bit immediate, or to access the whole 32-bit address space.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 31 / 272
3. Assembly programming language 3.3 Move instructions
MOV32 pseudo-instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 32 / 272
3. Assembly programming language 3.3 Move instructions
MOV32 pseudo-instruction
Solution:
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 34 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 35 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
Syntax
LDR{type}{cond} Rt, [Rn {, #offset}] ; immediate offset
LDR{type}{cond} Rt, [Rn, #offset]! ; pre-indexed
LDR{type}{cond} Rt, [Rn], #offset ; post-indexed
LDRD{cond} Rt, Rt2, [Rn {, #offset}] ; immediate offset,
; doubleword
LDRD{cond} Rt, Rt2, [Rn, #offset]! ; pre-indexed, doubleword
LDRD{cond} Rt, Rt2, [Rn], #offset ; post-indexed, doubleword
where:
type can be any one of:
⋄ B: unsigned Byte (Zero extend to 32 bits on loads.)
⋄ SB: signed Byte (LDR only. Sign extend to 32 bits.)
⋄ H: unsigned Halfword (Zero extend to 32 bits on loads.)
⋄ SH: signed Halfword (LDR only. Sign extend to 32 bits.)
⋄ -: omitted, for Word.
cond is an optional condition code.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 36 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
Operation
Register ← Memory
Description
Load value from memory to a register.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 37 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 38 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
Solution:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 39 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 40 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR instruction
Registers Memory
R0 0xXXXXXXXX 0xC0F9A4B0 0xXXXXXXXX
R1 0xXXXXXXXX
R2 0xC0F9A4B0
R3 0xXXXXXXXX
R4 0xXXXXXXXX
R5 0xXXXXXXXX
... ...
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 41 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 42 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
Syntax
LDR{cond}{.W} Rt, =expr
LDR{cond}{.W} Rt, =label_expr
where:
cond is an optional condition code.
.W is an optional instruction width specifier.
Rt is the register to be loaded.
expr evaluates to a numeric value.
label expr is a PC-relative or external expression of an address in the form
of a label plus or minus a numeric value.
Operation
Register ← Memory
Description
Load a register with either a 32-bit immediate value or an address.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 43 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
Copy the value of the memory LED codes at the location 2 into register R3.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 44 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
Solution:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 45 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
ALIGN
END
LDR pseudo-instruction
Remark 3.1
The value of register R3 will be 0x9299B0A4.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 47 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
Copy the value at the address GPIOA BASE+MODER into register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 48 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
Solution:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 49 / 272
3. Assembly programming language 3.4 Load/Store Instructions
LDR pseudo-instruction
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 50 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 51 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Syntax
STR{type}{cond} Rt, [Rn {, #offset}] ; immediate offset
STR{type}{cond} Rt, [Rn, #offset]! ; pre-indexed
STR{type}{cond} Rt, [Rn], #offset ; post-indexed
STRD{cond} Rt, Rt2, [Rn {, #offset}] ; immediate offset,
; doubleword
STRD{cond} Rt, Rt2, [Rn, #offset]! ; pre-indexed, doubleword
STRD{cond} Rt, Rt2, [Rn], #offset ; post-indexed, doubleword
where:
type can be any one of:
⋄ B: Byte.
⋄ H: Halfword.
⋄ -: omitted, for Word.
cond is an optional condition code.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 52 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Operation
Register → Memory.
Description
Store with immediate offset, pre-indexed immediate offset, or post-indexed imme-
diate offset.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 53 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 54 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Solution:
Code 3.7 (STR instruction)
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 55 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 56 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 57 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 58 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Solution:
Reset_Handler
MOV R1,#2
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2,R1]
NOP
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 59 / 272
3. Assembly programming language 3.4 Load/Store Instructions
STR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 60 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Pre-indexed addressing
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 61 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Pre-indexed addressing
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 62 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Pre-indexed addressing
Solution:
Reset_Handler
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2,#2]!
NOP
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 63 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Pre-indexed addressing
The offset value 2 was added to the address specified by register R2 (address = R2 +
0x02 = 0x20000000 + 0x02 = 0x20000002). After the STR instruction was ex-
ecuted, the calculated address is written back into register R2 (R2 = 0x20000002).
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 64 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Post-indexed addressing
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 65 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Post-indexed addressing
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 66 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Post-indexed addressing
Solution:
Reset_Handler
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2],#2]
NOP
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 67 / 272
3. Assembly programming language 3.4 Load/Store Instructions
Post-indexed addressing
The offset value 2 was not added to the address specified by register R2 (address
= R2 = 0x20000000). After the STR instruction was executed, the calculated
address is written back into register R2 (R2 = 0x20000002).
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 68 / 272
3. Assembly programming language 3.5 Arithmetic instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 69 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 70 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Syntax
ADD{S}{cond} {Rd}, Rn, Operand2
ADD{cond} {Rd}, Rn, #imm12 ; Thumb, 32-bit encoding only
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand
Operand2 is a flexible second operand.
imm12 is any value in the range 0-4095.
Operation
Rd ← Rn + {Operand2} {imm12}.
Description
Add without Carry.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 71 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Status bits
If S is specified, these instructions update the N, Z, C and V flags according to the
result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 72 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 73 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Solution:
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADD R1,R2
ADD R1,R3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 74 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 75 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Solution:
Reset_Handler
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADDS R1,R2
ADDS R1,R3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 76 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADC instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 77 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADC instruction
Syntax
ADC{S}{cond} {Rd}, Rn, Operand2
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand
Operand2 is a flexible second operand.
Operation
Rd ← Rn + Operand2 + Carry.
Description
Add with Carry.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 78 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADC instruction
Example 3.13
Repeat Example 3.11 but using ADCS instructions and compare the results.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 79 / 272
3. Assembly programming language 3.5 Arithmetic instructions
ADC instruction
Solution:
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADCS R1,R2
ADCS R1,R3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 80 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SUB instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 81 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SUB instruction
Syntax
SUB{S}{cond} {Rd}, Rn, Operand2
SUB{cond} {Rd}, Rn, #imm12 ; Thumb, 32-bit encoding only
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand
Operand2 is a flexible second operand.
imm12 is any value in the range 0-4095.
Operation
Rd ← Rn - Operand2.
Description
Subtract without carry.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 82 / 272
3. Assembly programming language 3.5 Arithmetic instructions
Example 3.14
Let R1 = 0xE0F5. Calculate the subtraction R1-1 and store the result back into
register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 83 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SUB instruction
Solution:
Reset_Handler
MOV R1,#0xE0F5
SUBS R1,R1,#1 ; R1--
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 84 / 272
3. Assembly programming language 3.5 Arithmetic instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 85 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SUB instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 86 / 272
3. Assembly programming language 3.5 Arithmetic instructions
MUL instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 87 / 272
3. Assembly programming language 3.5 Arithmetic instructions
MUL instruction
Syntax
MUL{S}{cond} {Rd}, Rn, Rm
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn, Rm are registers holding the values to be multiplied.
Operation
Rd ← Rn × Rm.
Description
Multiply with signed or unsigned 32-bit operands, giving the least significant 32 bits
of the result
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 88 / 272
3. Assembly programming language 3.5 Arithmetic instructions
MUL instruction
Example 3.16
Calculate Y = 0xE0F5 × 0x765C using core registers and store the result into
register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 89 / 272
3. Assembly programming language 3.5 Arithmetic instructions
MUL instruction
Solution:
Code 3.15 (MUL instruction)
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
MUL R1,R1,R2 ; R1 = R1 * R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 90 / 272
3. Assembly programming language 3.5 Arithmetic instructions
MUL instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 91 / 272
3. Assembly programming language 3.5 Arithmetic instructions
MUL instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 92 / 272
3. Assembly programming language 3.5 Arithmetic instructions
UDIV instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 93 / 272
3. Assembly programming language 3.5 Arithmetic instructions
UDIV instruction
Syntax
UDIV{cond} {Rd}, Rn, Rm
where:
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the value to be divided.
Rm is a register holding the divisor.
Operation
Rd ← Rn / Rm.
Description
Unsigned divide.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 94 / 272
3. Assembly programming language 3.5 Arithmetic instructions
UDIV instruction
Example 3.18
Calculate Y = 0xE0F5239B / 0x765C using core registers and store the result into
register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 95 / 272
3. Assembly programming language 3.5 Arithmetic instructions
UDIV instruction
Solution:
Reset_Handler
MOV32 R1,#0xE0F5239B
MOV R2,#0x765C
UDIV R1,R1,R2 ; R1 = R1/R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 96 / 272
3. Assembly programming language 3.5 Arithmetic instructions
UDIV instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 97 / 272
3. Assembly programming language 3.5 Arithmetic instructions
UDIV instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 98 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SDIV instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 99 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SDIV instruction
Syntax
SDIV{cond} {Rd}, Rn, Rm
where:
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the value to be divided.
Rm is a register holding the divisor.
Operation
Rd ← Rn / Rm.
Description
Signed divide.
Status flags
The condition flags are not affected.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 100 / 272
3. Assembly programming language 3.5 Arithmetic instructions
SDIV instruction
Overflow
If the signed integer division 0x80000000/0xFFFFFFFF is performed, the pseu-
docode produces the intermediate integer result +231 , that overflows the 32-bit
signed integer range. No indication of this overflow case is produced, and the 32-
bit result written to Rd must be the bottom 32 bits of the binary representation of
+231 . So the result of the division is 0x80000000.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 101 / 272
3. Assembly programming language 3.6 Logical instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 102 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 103 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
Syntax
MVN{S}{cond} Rd, Operand2
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Operand2 is a flexible second operand.
Operation
Rd ← Bitwise NOT Operand2.
Description
The MVN instruction takes the value of Operand2, performs a bitwise logical NOT
operation on the value, and places the result into Rd.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 104 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
Example 3.20
Let R1 = 0xE0F5. Invert bits of register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 105 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
Solution:
Reset_Handler
MOV R1,#0xE0F5
MVN R1,R1
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 106 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 107 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 108 / 272
3. Assembly programming language 3.6 Logical instructions
MVN instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 109 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 110 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Syntax
AND{S}{cond} Rd, Rn, Operand2
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand.
Operand2 is a flexible second operand.
Operation
Rd ← Rn AND Operand2.
Description
Logical AND.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 111 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Example 3.22
Let R1 = 0xE0F5 and R2 = 0x765C. Calculate (R1 AND R2) and store the result
into register R3.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 112 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Solution:
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
AND R3,R1,R2 ; R3 = R1 AND R
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 113 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 114 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Example 3.23
Let R1 = 0xE0F5. Reset bit 5th of register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 115 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Solution:
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0xFFDF
AND R1,R1,R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 116 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 117 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 118 / 272
3. Assembly programming language 3.6 Logical instructions
AND instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 119 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 120 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Syntax
ORR{S}{cond} Rd, Rn, Operand2
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand.
Operand2 is a flexible second operand.
Operation
Rd ← Rn OR Operand2.
Description
Logical OR.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 121 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Example 3.25
Let R1 = 0xE0F5 and R2 = 0x765C. Calculate (R1 OR R2) and store the result
into register R3.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 122 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Solution:
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
ORR R3,R1,R2 ; R3 = R1 OR R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 123 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 124 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Example 3.26
Let R1 = 0xE0F5. Set bit 10th of register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 125 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Solution:
Code 3.21 (ORR instruction one bit)
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x0400
ORR R3,R1,R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 126 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 127 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 128 / 272
3. Assembly programming language 3.6 Logical instructions
ORR instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 129 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 130 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
Syntax
BIC{S}{cond} Rd, Rn, Operand2
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand.
Operand2 is a flexible second operand.
Operation
Rd ← Rn AND NOT Operand2.
Description
Bitwise clear.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 131 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
Example 3.28
Let R1 = 0xE0F5. Clear bits 6th and 15th of register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 132 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
Solution:
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x8040
BIC R1,R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 133 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 134 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 135 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 136 / 272
3. Assembly programming language 3.6 Logical instructions
BIC instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 137 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 138 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Syntax
EOR{S}{cond} Rd, Rn, Operand2
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
cond is an optional condition code.
Rd is the destination register.
Rn is the register holding the first operand.
Operand2 is a flexible second operand.
Operation
Rd ← Rn EOR Operand2.
Description
Logical Exclusive OR.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 139 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Example 3.30
Let R1 = 0xE0F5 and R2 = 0x765C. Calculate (R1 EOR R2) and store the result
into register R3.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 140 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Solution:
Code 3.23 (EOR instruction)
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
ORR R3,R1,R2 ; R3 = R1 OR R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 141 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 142 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Example 3.31
Let R1 = 0xE0F5. Toggle bits 4th and 9th of register R1.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 143 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Solution:
Code 3.24 (EOR instruction toggle bits)
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x0210
EOR R3,R1,R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 144 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
We have
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 145 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 146 / 272
3. Assembly programming language 3.6 Logical instructions
EOR instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 147 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 148 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSL instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 149 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSL instruction
Syntax
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
Rd is the destination register.
Rm is the register holding the first operand. This operand is shifted left.
Rs is a register holding a shift value to apply to the value in Rm. Only the
least significant byte is used.
sh is a constant shift. The range of values permitted is 0-31.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 150 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSL instruction
Operation
C ...
Description
LSL provides the value of a register multiplied by a power of two, inserting zeros
into the vacated bit positions.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 151 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSL instruction
Example 3.33
Let R1 = 0x617EC3A8. Specify the value of register R1 as well as the Carry after
the following code is executed
LSLS R1,#3
and check the result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 152 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSL instructions
Solution:
Code 3.25 (LSL instruction)
Reset_Handler
MOV32 R1,#0x617EC3A8
LSLS R1,R1,#3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 153 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSL instruction
Shifting left register R1 by 3 bits.
Carry
x 0 1 1 0 0 0 0 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 1 0 0 0
... ...
0 0 0
1 0 0 0 0 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 1 0 0 0 0 0 0
Carry
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 154 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 155 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSR instruction
Syntax
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
Rd is the destination register.
Rm is the register holding the first operand. This operand is shifted left.
Rs is a register holding a shift value to apply to the value in Rm. Only the
least significant byte is used.
sh is a constant shift. The range of values permitted is 0-31.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 156 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSR instruction
Operation
C ...
Description
LSR provides the unsigned value of a register divided by a variable power of two,
inserting zeros into the vacated bit positions.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 157 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSR instruction
Example 3.34
Let R1 = 0x617EC3A8. Specify the value of register R1 as well as the Carry after
the following code is executed
LSRS R1,#6
and check the result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 158 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSR instructions
Solution:
Reset_Handler
MOV32 R1,#0x617EC3A8
LSRS R1,R1,#6
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 159 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
LSR instruction
Shifting right register R1 by 6 bits.
Carry
x 0 1 1 0 0 0 0 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 1 0 0 0
... ...
0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0
Carry
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 160 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ASR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 161 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ASR instruction
Syntax
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
Rd is the destination register.
Rm is the register holding the first operand. This operand is shifted left.
Rs is a register holding a shift value to apply to the value in Rm. Only the
least significant byte is used.
sh is a constant shift. The range of values permitted is 0-31.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 162 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ASR instruction
Operation
C ...
Description
Arithmetic shift right by n bits moves the left-hand 32-n bits of a register to the
right by n places, into the right-hand 32-n bits of the result. It copies the original
bit[31] of the register into the left-hand n bits of the result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 163 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ASR instruction
Example 3.35
Let R1 = 0xB17EC3A8. Specify the value of register R1 as well as the Carry after
the following code is executed
ASRS R1,#4
and check the result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 164 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ASR instruction
Solution:
Reset_Handler
MOV32 R1,#0xB17EC3A8
ASRS R1,R1,#4
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 165 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ASR instruction
Arithmetic shift right register R1 by 4 bits.
Carry
x 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 1 0 0 0
... ...
1 1 1 1 1 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 0
Carry
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 166 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ROR instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 167 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ROR instruction
Syntax
where:
S is an optional suffix. If S is specified, the condition flags are updated on the
result of the operation.
Rd is the destination register.
Rm is the register holding the first operand. This operand is shifted left.
Rs is a register holding a shift value to apply to the value in Rm. Only the
least significant byte is used.
sh is a constant shift. The range of values permitted is 0-31.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 168 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ROR instruction
Operation
C ...
Description
Rotate right by n bits moves the left-hand 32-n bits of a register to the right by n
places, into the right-hand 32-n bits of the result. It also moves the right-hand n
bits of the register into the left-hand n bits of the result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 169 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ROR instruction
Example 3.36
Let R1 = 0xB17EC3A8. Swap the two high bytes for the two low bytes and check
the result.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 170 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ROR instruction
Solution:
Reset_Handler
MOV32 R1,#0xB17EC3A8
RORS R1,R1,#16
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 171 / 272
3. Assembly programming language 3.7 Shift and rotating instructions
ROR instruction
We obtain R1 = 11000011 10101000 10110001 01111110 = 0xC3A8 B17E.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 172 / 272
3. Assembly programming language 3.8 Compare instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 173 / 272
3. Assembly programming language 3.8 Compare instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 174 / 272
3. Assembly programming language 3.8 Compare instructions
where:
cond is an optional condition code.
Rn is the register holding the first operand.
Operand2 is a flexible second operand.
Operation
CMP instruction: Condition flags are updated on the result of (Rn - Operand2).
CMN instruction: Condition flags are updated on the result of (Rn + Operand2).
Description
These instructions compare the value in a register with Operand2. They update
the condition flags on the result, but do not place the result in any register.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 175 / 272
3. Assembly programming language 3.8 Compare instructions
Remark 3.2
The CMP instruction is the same as a SUBS instruction, except that the result is
discarded.
The CMN instruction is the same as an ADDS instruction, except that the result is
discarded.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 176 / 272
3. Assembly programming language 3.8 Compare instructions
Example 3.37
Let R1 = 0xE0F5 and R2 = 0x765C. Compare R1 with R2 using CMP and CMN
instructions and check the results.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 177 / 272
3. Assembly programming language 3.8 Compare instructions
Solution:
Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
CMP R1,R2
CMN R1,R2
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 178 / 272
3. Assembly programming language 3.8 Compare instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 179 / 272
3. Assembly programming language 3.8 Compare instructions
where:
cond is an optional condition code.
Rn is the register holding the first operand.
Operand2 is a flexible second operand.
Operation
Condition flags are updated on the result of testing the value in a register against
Operand2.
Description
The TST instruction performs a bitwise AND operation on the value in Rn and the
value of Operand2. This is the same as an ANDS instruction, except that the result
is discarded.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 180 / 272
3. Assembly programming language 3.8 Compare instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 181 / 272
3. Assembly programming language 3.9 Conditional instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 182 / 272
3. Assembly programming language 3.9 Conditional instructions
Condition codes
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 183 / 272
3. Assembly programming language 3.9 Conditional instructions
Condition codes
ARM instructions can be executed conditionally by appending a two letter suffix to
the mnemonic. Almost all ARM instructions can be done this way.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 184 / 272
3. Assembly programming language 3.9 Conditional instructions
Condition codes
Codes Suffix Flags Meaning
0000 EQ Z set Equal
0001 NE Z clear Not equal
0010 CS or HS C set Higher or same (unsigned >= )
0011 CC or LO C clear Lower (unsigned < )
0100 MI N set Negative
0101 PL N clear Positive or zero
0110 VS V set Overflow
0111 VC V clear No overflow
1000 HI C set and Z clear Higher (unsigned >)
1001 LS C clear or Z set Lower or same (unsigned <=)
1010 GE N and V the same Signed >=
1011 LT N and V differ Signed <
1100 GT Z clear, N and V the same Signed >
1101 LE Z set, N and V differ Signed <=
1110 AL Any Always (normally omitted)
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 185 / 272
3. Assembly programming language 3.10 Flow control instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 186 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 187 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Syntax
B{cond}{.W} label
where:
cond is an optional condition code.
.W is an optional instruction width specifier to force the use of a 32-bit B
instruction in Thumb.
label is a PC-relative expression.
Operation
PC ← [label]
Description
The B instruction causes a branch to label.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 188 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Condition flags
The B instruction does not change the flags.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 189 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Example 3.38
5
X
Calculate Y = n
n=1
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 190 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Solution:
Code 3.30 (B instructions)
Reset_Handler
MOV R1,#1
MOV R2,#0
Loop
ADD R2,R2,R1
ADD R1,R1,#1
CMP R1,#5
BLE Loop
Stop
B Stop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 191 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Remark 3.3
In Program 3.30, we can see that the instructions in the block
Loop
...
BLE Loop
are iterated 5 times. Since we know in advance these instructions should be executed
how many times, the block of instructions like this is called a counting or for loop.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 192 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 193 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 194 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 195 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 196 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Find the sum of all these bytes and store the result into register R0.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 197 / 272
3. Assembly programming language 3.10 Flow control instructions
B instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 198 / 272
3. Assembly programming language 3.10 Flow control instructions
BL instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 199 / 272
3. Assembly programming language 3.10 Flow control instructions
BL instruction
Syntax
BL{cond}{.W} label
where:
cond is an optional condition code.
.W is an optional instruction width specifier to force the use of a 32-bit B
instruction in Thumb.
label is a PC-relative expression.
Operation
PC ← [label]
Description
The BL instruction causes a branch to label, and copies the address of the next
instruction into LR (R14, the link register).
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 200 / 272
3. Assembly programming language 3.10 Flow control instructions
BL instruction
Condition flags
The BL instruction does not change the flags.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 201 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 202 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Syntax
BX{cond} Rm
where:
cond is an optional condition code (cond is not available on all forms of this
instruction).
Rm is a register containing an address to branch to.
Operation
PC ← [Rm]
Description
The BX instruction causes a branch to the address contained in Rm and exchanges
the instruction set.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 203 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Condition flags
The BX instruction does not change the flags.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 204 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Example 3.42
Let R1, R2, R3, R4 contain arbitrary values. Store the maximum value of R1 and
R2 into R1 and store the maximum value of R3 and R4 into R3.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 205 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Solution:
Code 3.31 (FindMaxSubroutine)
Reset_Handler
MOV32 R1,#0xB17EC3A7
MOV32 R2,#0xB17EC3A8
MOV32 R3,#0x706892AF
MOV32 R4,#0x706892AE
MOV R5,R1
MOV R6,R2
BL FindMax
MOV R1,R5
MOV R5,R3
MOV R6,R4
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 206 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
BL FindMax
MOV R3,R5
Stop
B Stop
FindMax
CMP R5,R6
BLT MaxR6
BX LR
MaxR6
MOV R5,R6
BX LR
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 207 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
BL FindMax
...
FindMax
...
BX LR
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 208 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
The instruction
BL FindMax
is a call to execute the sub-routine named FindMax.
The instruction
BX LR
causes a branch to the address contained in link register LR (R14).
By decomposing a program into subroutines, the codes can be restated within the
program or reused by other programmers. Thus, it helps to reduce the cost of
developing and maintaining the program.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 209 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 210 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 211 / 272
3. Assembly programming language 3.10 Flow control instructions
BX instruction
FindFactorial
MUL R2,R2,R3
ADD R3,#1
CMP R3,R4
BHI Done
B FindFactorial
Done BX LR
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 212 / 272
3. Assembly programming language 3.11 Stack instructions
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 213 / 272
3. Assembly programming language 3.11 Stack instructions
PUSH instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 214 / 272
3. Assembly programming language 3.11 Stack instructions
PUSH instruction
Syntax
PUSH{cond} reglist
where:
cond is an optional condition code (cond is not available on all forms of this
instruction).
reglist is a non-empty list of registers, enclosed in braces. It can contain
register ranges. It must be comma separated if it contains more than one
register or register range.
Description
Push registers onto a full descending stack.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 215 / 272
3. Assembly programming language 3.11 Stack instructions
POP instruction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 216 / 272
3. Assembly programming language 3.11 Stack instructions
POP instruction
Syntax
POP{cond} reglist
where:
cond is an optional condition code (cond is not available on all forms of this
instruction).
reglist is a non-empty list of registers, enclosed in braces. It can contain
register ranges. It must be comma separated if it contains more than one
register or register range.
Description
Pop registers off a full descending stack.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 217 / 272
3. Assembly programming language 3.12 If-Then block
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 218 / 272
3. Assembly programming language 3.12 If-Then block
Flowchart symbols
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 219 / 272
3. Assembly programming language 3.12 If-Then block
Flowchart symbols
A flowchart consists of function blocks representing input/output data, execution
blocks, and condition checking. The direction of data movement is represented by
arrows.
Starting or
Oval
ending
Parallelogram Input/output
Rectangular Process
Diamond Condition
Flow direction
Line
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 220 / 272
3. Assembly programming language 3.12 If-Then block
Flowchart symbols
Note that the diamond block has 02 output arrows indicating the direction of the
processes corresponding to the test results. If the condition is true, it will be
indicated by a ”+” sign. Otherwise, if the condition is false, it will be indicated by
a ”-” sign.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 221 / 272
3. Assembly programming language 3.12 If-Then block
Flowchart symbols
An example of a flow chart
Start
R1, R2
R1 = R1 - R2
R1 >= 0 -
max = R1 max = R2
End
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 222 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 223 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Syntax
IT{x{y{z}}} {cond}
where:
x specifies the condition switch for the second instruction in the IT block.
y specifies the condition switch for the third instruction in the IT block.
z specifies the condition switch for the fourth instruction in the IT block.
cond specifies the condition for the first instruction in the IT block.
The condition switch for the second, third and fourth instruction in the IT
block can be either:
T Then. Applies the condition cond to the instruction.
E Else. Applies the inverse condition of cond to the instruction.
Description
The If-Then (IT) instruction.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 224 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Status flags
This instruction does not change the flags.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 225 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Example 3.44
Assign the value of register R1 - R2 to register to register R3 if the value of register
R1 bigger than that of register R2. Otherwise, store the result of R1 + R2 to register
R3.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 226 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Solution:
Not using IT block
Reset_Handler
MOV R1,#5
MOV R2,#12
CMP R1,R2
BLT Result
SUB R3,R1,R2
B Stop
Result
ADD R3,R1,R2
Stop
B Stop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 227 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Using IT block
Reset_Handler
MOV R1,#5
MOV R2,#12
CMP R1,R2
ITE GT
SUBGT R3,R1,R2
ADDLE R3,R1,R2
Stop
B Stop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 228 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 229 / 272
3. Assembly programming language 3.12 If-Then block
IT block
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 230 / 272
3. Assembly programming language 3.13 Types of loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 231 / 272
3. Assembly programming language 3.13 Types of loop
Types of loop
In the context of programming, a process in which a set of instructions is executed
in a repeated manner is called a loop. The repetition of instructions is also called
iteration.
There are three major types of loops
For loop,
While loop,
and Do-while loop.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 232 / 272
3. Assembly programming language 3.13 Types of loop
For loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 233 / 272
3. Assembly programming language 3.13 Types of loop
Start
Condition
false
- End
Condition
Condition +
true
Set of
instructions
Increment/Decrement
a counter
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 234 / 272
3. Assembly programming language 3.13 Types of loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 235 / 272
3. Assembly programming language 3.13 Types of loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 236 / 272
3. Assembly programming language 3.13 Types of loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 237 / 272
3. Assembly programming language 3.13 Types of loop
Start
Condition
false
Condition -
Condition +
true
Instruction block
End
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 238 / 272
3. Assembly programming language 3.13 Types of loop
Example 3.46
Suppose that there exist some bytes of memory named Array including zero that
is declared as
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 239 / 272
3. Assembly programming language 3.13 Types of loop
Solution:
Code 3.34 (Non zero sum)
Reset_Handler
MOV R0,#0 ; Array index
MOV R1,#0 ; Sum
LDR R2,=Array
CalcSum
LDR R3,[R2,R0] ; Array elements
BIC R3,R3,#0xFFFFFF00
CMP R3,#0
BEQ Done
ADD R1,R1,R3
ADD R0,#1
B CalcSum
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 240 / 272
3. Assembly programming language 3.13 Types of loop
Done
B Done
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 241 / 272
3. Assembly programming language 3.13 Types of loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 242 / 272
3. Assembly programming language 3.13 Types of loop
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 243 / 272
3. Assembly programming language 3.13 Types of loop
Start
Instruction block
Condition
true
+
Condition
- Condition
false
End
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 244 / 272
3. Assembly programming language 3.13 Types of loop
Example 3.47
Suppose that there exist some bytes of memory named Array including zero that
is declared as
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 245 / 272
3. Assembly programming language 3.13 Types of loop
Solution:
Code 3.35 (Non zero sum)
Reset_Handler
MOV R0,#0 ; Array index
MOV R1,#0 ; Sum
LDR R2,=Array
CalcSum
LDR R3,[R2,R0]
BIC R3,R3,#0xFFFFFF00
ADD R1,R1,R3
ADD R0,#1
CMP R3,#0
BEQ Done
B CalcSum
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 246 / 272
3. Assembly programming language 3.13 Types of loop
Done
B Done
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 247 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 248 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 249 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 250 / 272
3. Assembly programming language 3.14 Floating-point unit
FPU registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 251 / 272
3. Assembly programming language 3.14 Floating-point unit
FPU registers
The FPU provides an extension register file containing 32 single-precision registers.
These can be viewed as:
Sixteen 64-bit double-word registers, D0-D15.
Thirty-two 32-bit single-word registers, S0-S31.
A combination of registers from the above views.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 252 / 272
3. Assembly programming language 3.14 Floating-point unit
FPU registers
S0
D0
S1
S2
D1
S3
S4
D2
S5
S6
D3
S7
S28
D14
S29
S30
D15
S31
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 253 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 254 / 272
3. Assembly programming language 3.14 Floating-point unit
CP10
CP11
Reserved Reserved
r r r r
CPn[2n+1:2n](rw) for n values 10 and 11. Access privileges for coprocessor n. The
possible values of each field are:
0b00: Access denied. Any attempted access generates a NOCP UsageFault.
0b01: Privileged access only. An unprivileged access generates a NOCP fault.
0b10: Reserved. The result of any access is Unpredictable.
0b11: Full access.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 255 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 256 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 257 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 258 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 259 / 272
3. Assembly programming language 3.14 Floating-point unit
where:
fpliteral is a single-precision floating-point literal.
Description
The DCFS directive allocates memory for word-aligned single-precision floating-point
numbers, and defines the initial runtime contents of the memory. DCFSU is the same,
except that the memory alignment is arbitrary.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 260 / 272
3. Assembly programming language 3.14 Floating-point unit
where:
fpliteral is a double-precision floating-point literal.
Description
The DCFD directive allocates memory for word-aligned double-precision floating-
point numbers, and defines the initial runtime contents of the memory. vebtDCFDU
is the same, except that the memory alignment is arbitrary.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 261 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 262 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 263 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 264 / 272
3. Assembly programming language 3.14 Floating-point unit
The <dt> field normally contains one data type specifier. This indicates the
data type contained in
The second operand, if any.
The operand, if there is no second operand.
The result, if there are no operand registers.
The F32 data type can be abbreviated to F.
The F64 data type can be abbreviated to D.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 265 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 266 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 267 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 268 / 272
3. Assembly programming language 3.14 Floating-point unit
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 269 / 272
3. Assembly programming language 3.14 Floating-point unit
Example 3.48
Find the area of a circle with radius R = 20m.
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 270 / 272
3. Assembly programming language 3.14 Floating-point unit
Solution:
Reset_Handler
LDR R0,=0xE000ED88
LDR R1,[R0]
ORR R1,R1,#(0xF<<20)
STR r1,[R0] ; Enable FPU
LDR R0,=pi
VLDR.F S0,[R0]
LDR R0,=Radius
VLDR.F S1,[R0]
VMUL.F S2,S1,S1
VMUL.F S2,S2,S0
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 271 / 272
3. Assembly programming language 3.14 Floating-point unit
Stop
B Stop
pi DCFS 3.14159
Radius DCFS 20.0
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2024-2025 272 / 272