Asm Language
Asm Language
Programming
ARE302-AMudhan AN
Assembly Language:
• Low-Level: Assembly language is a low-level programming language that is
specific to a particular computer architecture.
• Syntax: Uses mnemonics for instructions and operands, which correspond directly
to the machine code instructions executed by the CPU. For example, MOV, ADD,
SUB.
• Hardware Interaction: Provides direct control over hardware and processor
resources.
• Performance: Generally results in faster and more efficient code as it has minimal
abstraction.
• Memory Usage: More efficient use of memory, crucial for systems with limited
resources.
• Complexity: Writing and understanding assembly code requires a deep
understanding of the hardware. It's more complex and time-consuming to write
and debug
ARE302-AMudhan AN
C programming
• High-Level: C is a high-level programming language that provides abstractions
over the hardware.
• Syntax: Uses keywords and constructs like loops, conditionals, functions, and
variables that are more human-readable. For example, if, for, int, char.
• Portability: C code can be compiled and run on different types of hardware
with minimal changes.
• Development Time: Easier and faster to write, debug, and maintain compared
to assembly language.
• Standard Libraries: Rich set of standard libraries for various tasks, making it
versatile for application development.
• Performance: While not as low-level as assembly, C still allows for efficient
code close to hardware-level performance, making it suitable for systems
programming
ARE302-AMudhan AN
• Control vs. Abstraction: Assembly offers finer control over hardware,
while C provides higher abstraction for easier and faster development.
• Efficiency vs. Ease: Assembly is more efficient but complex, whereas C
strikes a balance between performance and ease of use.
ARE302-AMudhan AN
Advantages of Assembly Programming
1. Fine-Grained Control:
1. Direct access to the hardware, allowing precise control over system resources.
2. Can optimize critical sections of code for performance.
2. Performance:
1. Typically results in faster and more efficient code because you're writing instructions directly for the CPU.
2. Reduced overhead compared to high-level languages.
3. Memory Usage:
1. More efficient use of memory, which is crucial for systems with limited resources.
2. Greater control over data placement in memory.
4. Speed Optimization:
1. Possible to write highly optimized routines, especially for time-critical tasks.
2. Ability to use specific processor features and instructions not accessible through C.
5. Predictability:
1. Assembly language execution is highly predictable, making it easier to perform real-time operations.
6. Interrupt Handling:
1. More efficient handling of interrupts as you can write custom interrupt service routines with precise control
ARE302-AMudhan AN
Disadvantages:
• Complexity: More difficult to write and maintain.
• Development Time: Takes significantly longer to develop compared to hig
h-level languages like C.
• Portability: Assembly code is not portable across different architectures, un
like C code.
ARE302-AMudhan AN
Example
• MOV R0, #5 Move the value 5 into register R0
• ADD R1, R2, R3 (R1 = R2 + R3)
• SUB R4, R5, R6 (R4 = R5 - R6)
• LDR R7, [R8] Load the value at the address contained in R8 into R7
• STR R9, [R10] Store the value in R9 into the memory address contained
in R10
• B LOOP Branch to the label LOOP
• CMP R0, #10 Compare the value in R0 with 10
• BEQ EQUAL_LABEL Branch to EQUAL_LABEL if the comparison
was equal
ARE302-AMudhan AN
Sample code:
AREA MyCode, CODE, READONLY
ENTRY
START
MOV R0, #10 → Load 10 into R0
MOV R1, #20 → Load 20 into R1
ADD R2, R0, R1 → R2 = R0 + R1 (10 + 20)
LDR R3, =Value → Load the address of Value into R3
LDR R4, [R3] → Load the value at the address in R3 into R4
B END → Branch to END
Value
DCD 100 → Define a constant data value 100
END
END
ARE302-AMudhan AN
Rules for Labels:
• Uniqueness: Each label must be unique within the scope where it's used.
• Start with a letter: Labels typically start with a letter (A-Z or a-z) and can be
followed by letters, digits (0-9), or underscores (_).
ARE302-AMudhan AN
Number systems:
• MOV R3, #’B’ directly loads the ASCII value for the character B
into R3 (which is 66)
ARE302-AMudhan AN
• Assembler directives, also known as pseudo-operations or pseudo-
instructions, provide the assembler with instructions on how to process
the code. They don't translate directly into machine code but help in o
rganizing and managing assembly programs.
ARE302-AMudhan AN
AREA Assembler Directives
Defines a new section of code or data. ENTRY
Example: AREA MyCode, CODE, READO Indicates the entry point of the program.
NLY Example: ENTRY
EQU EXPORT
Defines a constant value. Exports a symbol to be used in other mod
Example: SIZE EQU 10 ules.
DCB Example: EXPORT MyFunction
Defines a byte constant. IMPORT
Example: DCB 0x1, 0x2, 0x3 Imports a symbol from another module.
DCD Example: IMPORT SomeFunction
Defines a word constant (32-bit). ALIGN
Example: DCD 0x12345678 Aligns data to a specific boundary.
DCW Example: ALIGN 4
Defines a Half word constant (16-bit).
SPACE
Example: DCD 0x1234
END Reserves a block of memory.
Marks the end of the source code file. Example: myArray SPACE 100 (100 bytes)
Example: END ARE302-AMudhan AN
• The AREA directive in ARM assembly language is used to
define a section of code or data. It provides important attributes
that help the assembler organize and manage these sections
effectively.
• AREA name, type, attributes
• Parameters
• name: The name of the section. This should be a unique identifier.
• type: Specifies the type of area, which can be either CODE or DATA.
• attributes: A combination of attributes that control various properties
of the section
ARE302-AMudhan AN
• Types
• CODE: This type is used to define a section of executable code.
• DATA: This type is used to define a section of data.
• Common Attributes
• READONLY: Marks the section as read-only.
• READWRITE: Marks the section as read/write.
• ALIGN=expression: Specifies the alignment boundary.
• For example, ALIGN=4 ensures the section starts at a 4-byte boundary.
ARE302-AMudhan AN
Example 1: Code Section
AREA MyCode, CODE, READONLY
ENTRY
_start
MOV R0, #0 Some instructions
B _start Infinite loop to demonstrate code
END
END
ARE302-AMudhan AN
AREA MyCode, CODE, READONLY
ENTRY
_start
LDR R0, =dataValue → Load address of dataValue into R0
LDR R1, [R0] → Load the value at dataValue into R1
B _start → Infinite loop
END
ARE302-AMudhan AN
AREA MyData, DATA, READWRITE
MyUnsignedWords DCWU 0xFFFF, 0x7FFF Define unsigned 16-bit words
END
AREA MyData, DATA, READWRITE
MyUnsignedDWords DCDU 0xFFFFFFFF, 0x80000000 Define unsigned 32-bit words
END
ARE302-AMudhan AN
Transferring data from 32 bit array (left) and 8 bit array (right)
_start _start
LDR R0, =myData Load address of myData into R0 LDR R0, =myData → Load address of myData into R0
LDR R1, [R0] → Load the first value (0x12345678) into R1 LDRB R1, [R0] → Load the first 8-bit value (0x12) into R1
LDR R2, [R0, #4] → Load the second value (0x9ABCDEF0) into LDRB R2, [R0, #1] → Load the second 8-bit value (0x34) into R2
R2
→ Endless loop to keep the program running
→ Endless loop to keep the program running B .
B .
AREA MyData, DATA, READWRITE
AREA MyData, DATA, READWRITE myData
myData DCB 0x12, 0x34 → Define two 8-bit values
DCD 0x12345678, 0x9ABCDEF0 → Define two 32-bit values END
END
ARE302-AMudhan AN
AREA MyCode, CODE, READONLY
ENTRY
_start
→ Load the address of MyBytes
LDR R0, =MyBytes
ARE302-AMudhan AN
Addressing modes
Immediate Addressing Mode
MOV R0, #10 Move the value 10 into R0
ARE302-AMudhan AN
AREA MyCode, CODE, READONLY
AREA MyCode, CODE, READONLY
ENTRY
ENTRY
_start
LDR R1, =data Load the address of data into R1 _start
LDR R0, [R1, #4] Load the value from address (R1 + 4) LDR R1, =data Load the address of data
into R0 into R1
Use pre-indexed with update LDR R0, [R1], #4 Load the value from
LDR R2, [R1, #4]! Load value from (R1 + 4) into R2 and address R1 into R0, then update R1 to (R1 + 4)
update R1 to (R1 + 4)
Endless loop to keep the program running
Endless loop to keep the program running B .
B .
AREA MyData, DATA, READWRITE
AREA MyData, DATA, READWRITE data
data DCD 0x12345678, 0x9ABCDEF0 Define two
32-bit words
DCD 0x12345678, 0x9ABCDEF0 Define two 32-bit
words END
END
ARE302-AMudhan AN
Instruction Condition Result/Example
B Unconditional B label – Branch to label
BL Unconditional BL function_label – Branch to function_label and link
BX Unconditional BX R0 – Branch to address in R0 and possibly change state
BEQ Zero flag set BEQ label – Branch if result is zero (equal)
BNE Zero flag clear BNE label – Branch if result is not zero (not equal)
BCS/BHS Carry flag set BCS label – Branch if carry set (higher or same, unsigned)
BCC/BLO Carry flag clear BCC label – Branch if carry clear (lower, unsigned)
BMI Negative flag set BMI label – Branch if result is negative
BPL Negative flag clear BPL label – Branch if result is positive or zero
BVS Overflow flag set BVS label – Branch if overflow occurs
BVC Overflow flag clear BVC label – Branch if no overflow
BHI Unsigned higher BHI label – Branch if unsigned higher
BLS Unsigned lower or same BLS label – Branch if unsigned lower or same
BGE Signed >= BGE label – Branch if signed greater than or equal
BLT Signed < BLT label – Branch if signed less than
BGT Signed > BGT label – Branch if signed greater than
BLE Signed <= BLE label – Branch
ARE302-AMudhan AN if signed less than or equal
AREA MyCode, CODE, READONLY
ENTRY
_start
MOV R0, #-5 → Load -5 into R0
MOV R1, #10 → Load 10 into R1
CMP R0, R1 → Compare R0 and R1
BLT less_than → Branch if R0 < R1 (i.e., -5 < 10)
BGT greater_than → Branch if R0 > R1 (not taken in this case)
less_than
MOV R2, #1 → If R0 < R1, set R2 to 1
B end
greater_than
MOV R2, #2 → If R0 > R1, set R2 to 2
end
B end
END
carry_set
MOV R3, #1 ; Set R3 to 1 to indicate carry
B end ; Branch to end to avoid executing no_carry
no_carry
MOV R3, #0 ; Set R3 to 0 to indicate no carry
end
B end ; Infinite loop
{Note: use BKPT #0 ; Breakpoint to simulate an end}
END ARE302-AMudhan AN
MOV instructions
Instruction Addressing Mode Description Example
MOV Rd, Rm Register to Register Move the value from one register to another MOV R0, R1 - Move R1 to R0
MOV Rd, #imm Immediate to Register Move an immediate value to a register MOV R0, #0x1F - Move 31 to R0
Move the value from the address relative to th MOV R0, [PC, #8] -
MOV Rd, [PC, #offset] PC-relative
e program counter Move value at address PC + 8 to R0
ARE302-AMudhan AN
MOVS R0, #0x1F -
MOVS Various Move the value and update the condition flags
Move 31 to R0 and update flags
Move the value if carry flag is clear (no overfl MOVCC R0, R1 -
MOVCC Rd, Rm Conditional (Carry Clear)
ow in unsigned arithmetic) Move R1 to R0 if carry clear
ARE302-AMudhan AN
Load instructions
Instruc Operation Description Example
tion
LDR Load Register Loads a word from memory into a register LDR R0, [R1] - Load from address in R1
Load Register
LDRB Loads a byte from memory into a register LDRB R0, [R1] - Load byte from address
Byte
Load Register
LDRH Loads a halfword from memory into a register LDRH R0, [R1] - Load halfword from addr
Halfword
Load Register LDRSB R0, [R1] -
LDRSB Loads a signed byte from memory into a register
Signed Byte Load signed byte from addr
Load Register
LDRSH R0, [R1] -
LDRSH Signed Halfw Loads a signed halfword from memory into a register
Load signed halfword from addr
ord
STR Store Register Stores a word from a register into memory STR R0, [R1] - Store to address in R1
Store Register
STRB Stores a byte from a register into memory STRB R0, [R1] - Store byte to address
Byte
Store Register
STRH Stores a halfword from a register into memory STRH R0, [R1] - Store halfword to addr
Halfword
ARE302-AMudhan AN
• Further it can follow all the addressing modes like :
• LDR R0,[R1]
• LDR R0,[R1,#4]
• LDR R0,[R1,R2]
• LDR R0,[R1,R2,LSL#2]
• LDR R0,[R1,#4]!
• LDR R0,[R1],#4
ARE302-AMudhan AN
• MOV: Works with immediate values and registers.
• LDR: Fetches data from memory addresses.
ARE302-AMudhan AN
Arithmetic instructions
Instructi
Description Operation Example
on
Add without updating Adds two values and stores the resul ADD R0, R1, R2
ADD
flags t in a register R0 = R1 + R2
ADDEQ Conditional add (if eq Adds two values if equal condition i ADDEQS R0, R1, #10
S ual) and update flags s met, updates flags R0 = R1 + 10 if equal, update flags
Adds two values and the carry flag, ADC R0, R1, R2
ADC Add with carry
stores the result R0 = R1 + R2 + Carry
ARE302-AMudhan AN
Instruc
Description Operation Example
tion
Subtracts the second operand from the first a SUB R0, R1, R2
SUB Subtract
nd stores the result in a register R0 = R1 - R2
Subtracts the first operand from the second a RSB R0, R1, R2
RSB Reverse Subtract
nd stores the result in a register R0 = R2 - R1
ARE302-AMudhan AN
Multiplication operations
Instruction Description Operation Example
MUL R0, R1, R2
MUL Multiply Multiplies two values and stores the result
R0 = R1 * R2
Multiplies two values, adds a third value, and st MLA R0, R1, R2, R3
MLA Multiply-Accumulate
ores the result R0 = (R1 * R2) + R3
Multiplies two unsigned values and stores the re
sult in two registers.
UMULL R0, R1, R2, R3
UMULL Unsigned Multiply Long (the lower 32 bits (LSB) of the product of R2
{R1, R0} = R2 * R3
and R3 are stored in R0, and the upper 32 bits
(MSB) are stored in R1)
Multiplies two signed values and stores the resu SMULL R0, R1, R2, R3
SMULL Signed Multiply Long
lt in two registers {R1, R0} = R2 * R3
Multiplies two unsigned values, adds to a 64-
Unsigned Multiply- UMLAL R0, R1, R2, R3
UMLAL bit accumulator, and stores the result in two regi
Accumulate Long {R1, R0} += R2 * R3
sters
Multiplies two signed values, adds to a 64-
Signed Multiply- SMLAL R0, R1, R2, R3
SMLAL bit accumulator, and stores the result in two regi
Accumulate Long {R1, R0} += R2 * R3
sters
ARE302-AMudhan AN
Instruction Description Operation Example
ARE302-AMudhan AN
Logical instructions
Bitwise Exclusive Performs bitwise XOR between two regis EOR R0, R1, R2
EOR
OR (XOR) ters R0 = R1 ^ R2
ARE302-AMudhan AN
Working of BIC
MOV R0, #0xFF ; R0 = 0x000000FF
MOV R1, #0x0F ; R1 = 0x0000000F
BIC R2, R0, R1
ARE302-AMudhan AN
Instruction Description Operation Example
ARE302-AMudhan AN
Working of CMP
ARE302-AMudhan AN
AREA SumArray, CODE, READONLY
ENTRY Sum of a given array
_start
LDR R0, =array ; Load base address of the array into R0
MOV R1, #5 ; Load the array length (5) into R1
MOV R2, #0 ; Initialize the sum in R2 to 0
sum_loop
LDR R3, [R0], #4 ; Load the current element into R3 and increment R0 by 4
ADD R2, R2, R3 ; Add the value in R3 to R2 (accumulating the sum)
SUBS R1, R1, #1 ; Decrement the loop counter
BNE sum_loop ; If loop counter is not zero, branch to sum_loop
; Result in R2 contains the sum of the array elements
B . ; Endless loop to keep the program running
ARE302-AMudhan AN
END
AREA FindMax, CODE, READONLY
ENTRY
Largest Element in an array
_start
LDR R0, =array ; Load base address of the array into R0
MOV R1, #5 ; Load the array length (5) into R1
LDR R2, [R0] ; Load the first element into R2 (initial max)
find_max
LDR R3, [R0], #4 ; Load the current element into R3 and increment R0 by 4
CMP R2, R3 ; Compare the current max (R2) with R3
MOVLT R2, R3 ; If R3 is greater, move R3 to R2
SUBS R1, R1, #1 ; Decrement the loop counter
BNE find_max ; If loop counter is not zero, branch to find_max
; Result in R2 contains the largest number
B . ; Endless loop to keep the program running
MSR SPSR_cxsf, R0 -
Transfers a value from a register Move R0 value to SPSR co
MSR Move to SPSR from Register/Immediate
or immediate value to the SPSR ntrol/extension/status/flags fi
eld
ARE302-AMudhan AN