0% found this document useful (0 votes)
24 views

Asm Language

Uploaded by

ajjualmighty4955
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Asm Language

Uploaded by

ajjualmighty4955
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

ARM Assembly 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.

• Assembly programming is ideal for scenarios where performance and contr


ol are critical, like embedded systems or certain performance-
critical applications. But for most applications, the balance of development
efficiency and performance offered by C is often preferable

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 (_).

• No spaces: Labels cannot contain spaces or special characters like @, #, $,


etc.

• Case-sensitive: Labels are case-sensitive, so LABEL and label would be


considered different.

ARE302-AMudhan AN
Number systems:

• MOV R0, #10


• MOV R0, #0xA Hexadecimal for decimal 10

• MOV R1, #2_10101010


• MOV R1, #0b10101010  Using '0b' prefix
• MOV R1, #%10101010 Using '%' prefix

• 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

•MyCode: Name of the code section.


•CODE: Section type indicating executable code.
•READONLY: Attribute specifying the code sectio
n as read-only.
ARE302-AMudhan AN
Example 2: Data Section
AREA MyData, DATA, READWRITE
dataValue DCD 0x12345678  Define a word with a specific value
moreData DCB "Hello", 0  Define a string with a null terminator

END

•MyData: Name of the data section.


•DATA: Section type indicating data.
•READWRITE: Attribute specifying the data section as read/write.

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

AREA MyData, DATA, READWRITE


ALIGN=4
dataValue DCD 0x12345678 → Define a word with a specific
value
END
ARE302-AMudhan AN
AREA MyCode, CODE, READONLY
ENTRY
_start
LDR R0, =dataValue → Load the address of dataValue into R0
LDR R1, [R0] → Load the value at dataValue into R1
→ Load a constant directly into R2
LDR R2, =0x12345678
→ Load the address of a label into R3
LDR R3, =myLabel
→ Load the value at R3 + 4 into R4
LDR R4, [R3, #4]
→ Endless loop to keep the program running
B .

AREA MyData, DATA, READWRITE


dataValue DCD 0x89ABCDEF → Define a word with specific value
myLabel DCD 0x11223344 → Define another word
DCD 0x55667788 → Define yet another word
END ARE302-AMudhan AN
Examples for defining constants
AREA MyData, DATA, READWRITE
MyBytes DCB 0x12, 0x34, 0x56, 0x78 Define bytes 0x12, 0x34, 0x56, 0x78
DCB "Hello", 0  Define a string with a null terminator
END

AREA MyData, DATA, READWRITE


MyWords DCW 0x1234, 0x5678 Define 16-bit words 0x1234, 0x5678
END

AREA MyData, DATA, READWRITE


MyDWords DCD 0x12345678, 0x9ABCDEF0 → Define 32-bit words 0x12345678, 0x9ABCDEF0
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)

AREA MyCode, CODE, READONLY AREA MyCode, CODE, READONLY


ENTRY ENTRY

_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

→ Calculate the offset for the string "Hello"


ADD R1, R0, #4 → The string starts 4 bytes a er MyBytes

→ Example: Load the first character of the string into R2


LDRB R2, [R1] → Load the first byte ('H') into R2

→ Endless loop to keep the program running


B .

AREA MyData, DATA, READWRITE


MyBytes
DCB 0x12, 0x34, 0x56, 0x78 → Define bytes
DCB "Hello", 0 → Define a string with a null terminator
END

ARE302-AMudhan AN
Addressing modes
Immediate Addressing Mode
MOV R0, #10  Move the value 10 into R0

Register Addressing Mode


MOV R0, R1  Move the value in R1 to R0

Direct Addressing Mode


LDR R0, =0x20000000 This instruction loads the address 0x20000000 into R0

Register Indirect Addressing Mode


LDR R0, [R1]  Load the value from the memory address in R1 into R0
ARE302-AMudhan AN
Indexed Addressing Mode
LDR R0, [R1, #4] ; Load the value from the address R1 + 4 into R0

Pre-Indexed Addressing Mode


LDR R0, [R1, #4]! ; Pre-indexed, Load the value from the address R1 + 4 into R0 and
update R1

Post-Indexed Addressing Mode


LDR R0, [R1], #4 ; Post-indexed, Load the value from the address in R1 into R0 then
add 4 to R1

Relative Addressing Mode


B label ; Branch to the address specified by 'label', relative to the current PC
ARE302-AMudhan AN
Scaled Index Addressing Mode
LDR R0, [R1, R2, LSL #2] ; Load value from address (R1 + (R2 << 2)) into R0

Base Index Indirect Addressing Mode


LDR R0, [R1, R2] ; Load value from address (R1 + R2) 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

BLT will branch to less_than because -5 is less than 10.


BGT will not be taken because -5 is not greaterAN than 10.
ARE302-AMudhan
AREA MyCode, CODE, READONLY
ENTRY
Carry based example
_start
MOV R0, #0xFFFFFFFF ; Load maximum 32-bit unsigned value into R0
MOV R1, #1 ; Load 1 into R1
ADDS R2, R0, R1 ; Add R0 and R1, result goes to R2, sets flags
BCC no_carry ; Branch to no_carry if carry flag is clear
BCS carry_set ; Branch to carry_set if carry flag is set

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

MOV R0, [R1] -


MOV Rd, [Rn] Register Indirect Move the value from the address in a register
Move value at address in R1 to R0

MOV R0, [R1, #4] -


Move the value from the address in a register
MOV Rd, [Rn, #offset] Register Indirect with Offset Move value at address in R1 + 4 to R
plus an offset
0

MOV R0, [R1, R2, LSL #2] -


Move the value from the address in a register
MOV Rd, [Rn, Rm, LSL #shift] Register Indirect with Shift Move value at address in R1 + (R2 <
plus a shifted register value
< 2) 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

Move the value if carry flag is set (overflow i MOVCS R0, R1 -


MOVCS Rd, Rm Conditional (Carry Set)
n unsigned arithmetic) Move R1 to R0 if carry set

Move the value if higher (unsigned compariso MOVHI R0, R1 -


MOVHI Rd, Rm Conditional (Higher)
n) Move R1 to R0 if higher

Move the value if lower (unsigned compariso MOVLO R0, R1 -


MOVLO Rd, Rm Conditional (Lower)
n) Move R1 to R0 if lower

MOVSEQ R0, #0x1F -


Move immediate value if equal and update fla
MOVSEQ Rd, #imm Conditional (Equal + Update Flags) Move 31 to R0 if equal and update
gs
flags

Check what is MVN R0, R1

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

Adds two values, stores the result, a ADDS R0, R1, R2


ADDS Add and update flags
nd updates flags  R0 = R1 + R2, updates flags

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 second operand and the comple


Subtract with Borrow SBC R0, R1, R2
SBC ment of the carry flag from the first and store
(Carry)  R0 = R1 - R2 - !Carry
s the result

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

Subtracts the first operand and the compleme


Reverse Subtract with RSC R0, R1, R2
RSC nt of the carry flag from the second and store
Borrow (Carry)  R0 = R2 - R1 - !Carry
s the result

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

Multiplies two values, stores the re MULS R0, R1, R2


MULS Multiply and update flags
sult, and updates flags R0 = R1 * R2, updates flags

Multiplies two values if the carry f MULCC R0, R1, R2


MULCC Conditional Multiply (if Carry Clear)
lag is clear  R0 = R1 * R2 if carry clear

MULCCS R0, R1, R2


Conditional Multiply (if Carry Clear) an Multiplies two values if the carry f
MULCCS  R0 = R1 * R2 if carry clear,
d update flags lag is clear, updates flags
updates flags
ARE302-AMudhan AN
UMLAL working

• Multiplies the 32-bit values in R2 and R3.

• Adds the 64-bit result of the multiplication to the combined 64-bit


value in {R1, R0}.

• Stores the final 64-bit result back into {R1, R0}

ARE302-AMudhan AN
Logical instructions

Instruction Description Operation Example

Performs bitwise AND between two regis AND R0, R1, R2


AND Bitwise AND
ters  R0 = R1 & R2

Performs bitwise OR between two registe ORR R0, R1, R2


ORR Bitwise OR
rs R0 = R1 | R2

Bitwise Exclusive Performs bitwise XOR between two regis EOR R0, R1, R2
EOR
OR (XOR) ters R0 = R1 ^ R2

Bitwise Clear (AN BIC R0, R1, R2


BIC Clears specific bits in a register
D NOT)  R0 = R1 & (~R2)

ARE302-AMudhan AN
Working of BIC
MOV R0, #0xFF ; R0 = 0x000000FF
MOV R1, #0x0F ; R1 = 0x0000000F
BIC R2, R0, R1

; R2 = R0 & ~R1 = 0x000000FF & 0xFFFFFFF0 = 0x000000F0

ARE302-AMudhan AN
Instruction Description Operation Example

Subtracts the second operand from the


CMP R0, R1
CMP Compare first and updates flags based on the resul
 Updates flags based on (R0 - R1)
t (No need to give S command)

Adds the first and second operands and CMN R0, R1


CMN Compare Negative
updates flags based on the result  Updates flags based on (R0 + R1)

Performs a bitwise AND between two


TST R0, R1
TST Test (AND) operands and updates flags based on the
 Updates flags based on (R0 & R1)
result

Performs a bitwise XOR between two


Test Equivalence TEQ R0, R1
TEQ operands and updates flags based on the
(XOR)  Updates flags based on (R0 ^ R1)
result

ARE302-AMudhan AN
Working of CMP

MOV R0, #5 ; Load 5 into R0 The CMP (Compare)


instruction in ARM assembly
MOV R1, #10 ; Load 10 into R1 is used to compare two values
CMP R0, R1 ; Compare R0 with R1 (5 - 10) by performing a subtraction
(without storing the result)
and updating the condition
; The flags after CMP R0, R1 will be: flags based on the outcome.
; N = 1 (negative result)
; Z = 0 (result is not zero) This allows subsequent
instructions to make
; C = 0 (there was a borrow, 5 is less than 10) decisions based on these
; V = 0 (no signed overflow) flags.
ARE302-AMudhan AN
AREA MyCode, CODE, READONLY
ENTRY Data Transfer code
_start
; Load immediate values into registers
MOV R0, #0x12345678 ; Load R0 with 0x12345678
MOV R1, #0x87654321 ; Load R1 with 0x87654321
; Store the values from registers into memory
LDR R2, =0x20000000 ; Load base address 0x20000000 into R2
STR R0, [R2] ; Store the value of R0 at address 0x20000000
STR R1, [R2, #4] ; Store the value of R1 at address 0x20000004
; Load the values back from memory into different registers
LDR R3, [R2] ; Load the value at address 0x20000000 into R3
LDR R4, [R2, #4] ; Load the value at address 0x20000004 into R4
; Endless loop to keep the program running
B .
END

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

AREA MyData, DATA, READWRITE


array
DCD 1, 2, 3, 4, 5 ; Define the array
END ARE302-AMudhan AN
AREA MyCode, CODE, READONLY
ENTRY
Addition of Arrays
_start
LDR R0, =array1 ; Load base address of array1 into R0
LDR R1, =array2 ; Load base address of array2 into R1
LDR R2, =resultArray ; Load base address of resultArray into R2
MOV R3, #5 ; Load array length (5) into R3
loop
LDR R4, [R0], #4 ; Load element from array1 into R4 and increment R0
LDR R5, [R1], #4 ; Load element from array2 into R5 and increment R1
ADD R6, R4, R5 ; Add elements R4 and R5, store result in R6
STR R6, [R2], #4 ; Store result in resultArray and increment R2 AREA MyData, DATA, READWRITE
SUBS R3, R3, #1 ; Decrement loop counter array1
BNE loop ; If loop counter not zero, branch to loop DCD 1, 2, 3, 4, 5 ; Define array1
array2
B .
DCD 6, 7, 8, 9, 10 ; Define array2
resultArray
SPACE 20 ; Reserve space for resultArray
(5 elements, 4 bytes each)
ARE302-AMudhan AN
END
AREA Factorial, CODE, READONLY
ENTRY
Factorial
_start
MOV R0, #5 ; Load the number for which factorial is to be found into R0 (e.g., 5)
MOV R1, #1 ; Initialize result register R1 to 1
MOV R2, R0 ; Copy the value of R0 into R2 for loop counter
factorial_loop
CMP R2, #1 ; Compare R2 with 1
BLE end_factorial ; If R2 <= 1, branch to end_factorial
MUL R1, R1, R2 ; Multiply R1 by R2 and store result in R1
SUB R2, R2, #1 ; Decrement R2 by 1
B factorial_loop ; Branch back to factorial_loop

end_factorial ; Result in R1 contains the factorial of the number in R0

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

AREA MyData, DATA, READWRITE


array
DCD 10, 25, 15, 32, 7 ; Define the array
END
ARE302-AMudhan AN
Flag instruction based commands
Instruction Description Operation Example

Transfers the value from the CPS MRS R0, CPSR -


MRS Move from Status Register to Register
R or SPSR to a register Move CPSR value to R0

Transfers a value from a register MSR CPSR_c, R0 -


Move to Status Register from Register/Im
MSR or immediate value to the CPSR Move R0 value to CPSR co
mediate
or SPSR ntrol field

Transfers the value from the SPS MRS R0, SPSR -


MRS Move from SPSR to Register
R to a register Move SPSR value to R0

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

You might also like