Programs ARM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

@ Quadratic Equation f(x)=5x^2+3x+2

@ Assume x is in R0

MOV R1, #5 @ Load coe icient of x^2

MUL R2, R0, R0 @ x^2

MUL R2, R2, R1 @ 5x^2

MOV R1, #3 @ Load coe icient of x

MUL R3, R0, R1 @ 3x

ADD R2, R2, R3 @ 5x^2 + 3x

ADD R2, R2, #2 @ 5x^2 + 3x + 2

@ Result is in R2

@ Memory block transfer

@ Assume source address in R0, destination in R1, count in R2

LOOP:

LDR R3, [R0], #4 @ Load word from source, post-increment

STR R3, [R1], #4 @ Store word to destination, post-increment

SUBS R2, R2, #1 @ Decrement count

BNE LOOP @ Loop if not zero

@ Fibonacci sequence

@ Generate n Fibonacci numbers, n in R0

MOV R1, #0 @ First number

MOV R2, #1 @ Second number

MOV R3, #0 @ Counter

LOOP:

ADD R4, R1, R2 @ Next Fibonacci number

MOV R1, R2 @ Shift numbers


MOV R2, R4

ADD R3, R3, #1 @ Increment counter

CMP R3, R0

BLT LOOP @ Loop if counter < n

@ Last Fibonacci number in R2

@ Addition of 16-bit numbers

@ Assume numbers in R0 and R1

UXTH R0, R0 @ Zero-extend to 32 bits

UXTH R1, R1 @ Zero-extend to 32 bits

ADD R2, R0, R1 @ Add numbers

@ Result in R2

@ Factorial

@ Assume n in R0

MOV R1, #1 @ Initialize result to 1

LOOP:

MUL R1, R1, R0 @ Multiply result by current n

SUBS R0, R0, #1 @ Decrement n

BNE LOOP @ Loop if not zero

@ Factorial result in R1

@ Bubble sort (Ascending)

@ Assume array start address in R0, length in R1

OUTER:

MOV R2, #0 @ Swapped flag


MOV R3, R0 @ Current element pointer

MOV R4, R1 @ Counter

INNER:

LDR R5, [R3] @ Load current element

LDR R6, [R3, #4] @ Load next element

CMP R5, R6

BLE SKIP @ Skip if in order

STR R6, [R3] @ Swap elements

STR R5, [R3, #4]

MOV R2, #1 @ Set swapped flag

SKIP:

ADD R3, R3, #4 @ Move to next element

SUBS R4, R4, #1 @ Decrement counter

BNE INNER @ Continue inner loop

CMP R2, #0 @ Check if swapped

BNE OUTER @ Repeat if swapped

@ Addition of first 10 even numbers

MOV R0, #0 @ Sum

MOV R1, #0 @ Current even number

MOV R2, #10 @ Counter

LOOP:

ADD R0, R0, R1 @ Add to sum

ADD R1, R1, #2 @ Next even number

SUBS R2, R2, #1 @ Decrement counter

BNE LOOP @ Loop if not zero

@ Sum in R0
@ Count 1's and 0's in 32-bit number

@ Assume number in R0

MOV R1, #0 @ 1's count

MOV R2, #32 @ Bit counter

LOOP:

TST R0, #1 @ Test least significant bit

ADDNE R1, R1, #1 @ Increment 1's count if set

LSR R0, R0, #1 @ Shift right

SUBS R2, R2, #1 @ Decrement bit counter

BNE LOOP @ Loop if not zero

SUB R3, #32, R1 @ 0's count = 32 - 1's count

@ 1's count in R1, 0's count in R3

@ BCD to Binary Conversion

@ Assume BCD number in R0

MOV R1, #0 @ Result

MOV R2, #0 @ Digit counter

LOOP:

AND R3, R0, #0xF @ Extract least significant BCD digit

MUL R4, R1, #10 @ Multiply result by 10

ADD R1, R4, R3 @ Add new digit

LSR R0, R0, #4 @ Shift to next BCD digit

ADD R2, R2, #1 @ Increment digit counter

CMP R2, #8 @ Check if all 8 digits processed

BNE LOOP @ Loop if not done


@ Binary result in R1

@ Division of 32-bit number by 16-bit

@ Assume 32-bit dividend in R0, 16-bit divisor in R1

MOV R2, #0 @ Quotient

MOV R3, #0 @ Remainder

MOV R4, #32 @ Loop counter

LOOP:

LSLS R0, R0, #1 @ Shift dividend left, MSB to carry

ADC R3, R3, R3 @ Shift carry into remainder

CMP R3, R1 @ Compare remainder with divisor

SUBHS R3, R3, R1 @ Subtract if remainder >= divisor

ADDHS R2, R2, #1 @ Set quotient bit if subtraction occurred

SUBS R4, R4, #1 @ Decrement loop counter

BNE LOOP @ Loop if not done

@ Quotient in R2, Remainder in R3

You might also like