0% found this document useful (0 votes)
4 views10 pages

ARM Assembly Number Algorithms

its a code for lpc2148 microcontollers for experiment of lab i practiced some few questions and i got the code

Uploaded by

amulyarathod496
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

ARM Assembly Number Algorithms

its a code for lpc2148 microcontollers for experiment of lab i practiced some few questions and i got the code

Uploaded by

amulyarathod496
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//PRIME NUMBER

AREA PROGRAM, CODE, READONLY


ENTRY

LDR R0, =0x40000000 ; Input numbers (10 bytes)


LDR R1, =0x40000040 ; Prime numbers store address
LDR R2, =0x40000080 ; Non-prime numbers store address
MOV R5, #10 ; Loop counter for 10 numbers

LOOP:
LDRB R6, [R0], #1 ; Load next number into R6
MOV R7, R6 ; Copy for checking

; ===== Numbers less than 2 are NOT prime =====


CMP R7, #2
BLT STORE_NONPRIME

; ===== Prime checking loop =====


MOV R8, #2 ; Start divisor = 2

CHECK_DIV:
MUL R9, R8, R8 ; R9 = divisor^2
CMP R9, R7
BGT STORE_PRIME ; If divisor^2 > number → prime

; ===== Remainder check without division =====


MOV R10, R7 ; temp = number
REM_LOOP:
CMP R10, R8
BLT NOT_DIVISIBLE ; If temp < divisor → not divisible
SUB R10, R10, R8
BEQ STORE_NONPRIME ; If remainder == 0 → not prime
B REM_LOOP

NOT_DIVISIBLE:
ADD R8, R8, #1 ; divisor++
B CHECK_DIV

; ===== Store Prime =====


STORE_PRIME:
STRB R6, [R1], #1 ; Store prime
B NEXT_NUM

; ===== Store Non-prime =====


STORE_NONPRIME:
STRB R6, [R2], #1 ; Store non-prime

; ===== Next number =====


NEXT_NUM:
SUBS R5, R5, #1
BNE LOOP

STOP:
B STOP
END
///ARMSTRONG NUMBER
AREA PROGRAM, CODE, READONLY
ENTRY

LDR R0, =0x40000000 ; Input dataset start address


LDR R1, =0x40000040 ; Armstrong numbers storage
LDR R2, =0x40000080 ; Non-Armstrong numbers storage
MOV R5, #10 ; Loop counter for 10 numbers

MAIN_LOOP
LDR R3, [R0], #4 ; Load number
MOV R4, R3 ; Copy for digit count
MOV R6, #0 ; Sum = 0

; ===== PASS 1: Count number of digits =====


MOV R9, #0 ; Digit count

COUNT_LOOP
CMP R4, #0
BEQ COUNT_DONE

; Divide R4 by 10 (manual)
MOV R7, R4
MOV R8, #0 ; quotient = 0
DIV1_LOOP
CMP R7, #10
BLT DIV1_DONE
SUB R7, R7, #10
ADD R8, R8, #1
B DIV1_LOOP
DIV1_DONE
MOV R4, R8 ; R4 = quotient
ADD R9, R9, #1 ; digit count++
B COUNT_LOOP

COUNT_DONE
MOV R4, R3 ; Restore original number for processing

; ===== PASS 2: Extract digits and sum power =====


DIGIT_LOOP
CMP R4, #0
BEQ DIGIT_DONE

; Divide R4 by 10 (manual)
MOV R7, R4
MOV R8, #0
DIV2_LOOP
CMP R7, #10
BLT DIV2_DONE
SUB R7, R7, #10
ADD R8, R8, #1
B DIV2_LOOP
DIV2_DONE
; R7 = remainder (digit), R8 = quotient
; Power = digit ^ (digit_count)
MOV R10, #1 ; result = 1
MOV R11, R9 ; counter = digit_count
POWER_LOOP
CMP R11, #0
BEQ POWER_DONE
MUL R10, R10, R7
SUB R11, R11, #1
B POWER_LOOP
POWER_DONE

ADD R6, R6, R10 ; sum += power

MOV R4, R8 ; number = quotient


B DIGIT_LOOP

DIGIT_DONE
CMP R6, R3
BEQ STORE_ARMSTRONG

STORE_NONARM
STR R3, [R2], #4 ; Non-Armstrong
B CONTINUE

STORE_ARMSTRONG
STR R3, [R1], #4 ; Armstrong number

CONTINUE
SUBS R5, R5, #1
BNE MAIN_LOOP

STOP
B STOP
END

////GEOMETRIC PROGRRESION
AREA PROGRAM, CODE, READONLY
ENTRY

; Load inputs
LDR R0, =0x40000000 ; Address of a (first term)
LDR R1, =0x40000004 ; Address of r (common ratio)
LDR R2, =0x40000008 ; Address of n (number of terms)
LDR R3, =0x40000040 ; Output storage address for GP terms
LDR R8, =0x40000080 ; Address to store GP sum

LDR R4, [R0] ; R4 = a (current term)


LDR R5, [R1] ; R5 = r (ratio)
LDR R6, [R2] ; R6 = n (counter)
MOV R7, #0 ; R7 = sum accumulator

GP_LOOP:
STR R4, [R3], #4 ; Store current term
ADD R7, R7, R4 ; Add to sum
SUBS R6, R6, #1 ; Decrement counter
BEQ STORE_SUM ; If done, store sum

MUL R4, R4, R5 ; Next term = current term * ratio


B GP_LOOP

STORE_SUM:
STR R7, [R8] ; Store total sum

STOP:
B STOP
END

///EVEN ODD REPETION


AREA PROGRAM, CODE, READONLY
ENTRY

; Addresses
LDR R0, =0x40000000 ; Input dataset start
LDR R1, =0x40000040 ; Odd numbers storage
LDR R2, =0x40000080 ; Even numbers storage
LDR R8, =0x400000C0 ; Repetition count storage
LDR R9, =0x40000100 ; Fibonacci series storage

MOV R5, #10 ; Loop counter for 10 numbers


MOV R10, #0 ; Odd flag (to store first odd number later)

; ===== Separate Odd & Even =====


SEP_LOOP:
LDR R6, [R0], #4 ; Load next number
MOV R7, R6
AND R4, R6, #1 ; Check LSB (odd/even)
CMP R4, #1
BEQ STORE_ODD

STORE_EVEN:
STR R6, [R2], #4
B CONT_SEP

STORE_ODD:
STR R6, [R1], #4
CMP R10, #0 ; If first odd not stored yet
BNE CONT_SEP
MOV R10, R6 ; Store first odd number for Fibonacci

CONT_SEP:
SUBS R5, R5, #1
BNE SEP_LOOP

; ===== Count Repetitions =====


LDR R0, =0x40000000 ; Reset input pointer
MOV R5, #10

COUNT_LOOP:
LDR R6, [R0], #4 ; Current number
MOV R7, #0 ; Reset repetition counter
LDR R3, =0x40000000 ; Compare with entire dataset
MOV R11, #10

COUNT_INNER:
LDR R4, [R3], #4
CMP R4, R6
BNE NO_MATCH
ADD R7, R7, #1

NO_MATCH:
SUBS R11, R11, #1
BNE COUNT_INNER

STR R7, [R8], #4 ; Store repetition count for current number


SUBS R5, R5, #1
BNE COUNT_LOOP

; ===== Fibonacci from first odd =====


MOV R4, #0 ; Fib(0)
MOV R5, #1 ; Fib(1)
STR R4, [R9], #4
STR R5, [R9], #4

SUB R10, R10, #2 ; Remaining terms = first odd number - 2


FIB_LOOP:
CMP R10, #0
BEQ STOP

ADD R6, R4, R5 ; Next term = prev1 + prev2


STR R6, [R9], #4
MOV R4, R5
MOV R5, R6

SUBS R10, R10, #1


BNE FIB_LOOP

STOP:
B STOP
END

//ARITHEMATIC PROGRESSIOM
AREA PROGRAM, CODE, READONLY
ENTRY

LDR R0, =0x40000000 ; Start address to store AP


MOV R1, #2 ; First term (a)
MOV R2, #3 ; Common difference (d)
MOV R3, #10 ; Number of terms (n)
MOV R4, #0 ; R4 = sum

AP_LOOP
STRB R1, [R0], #1 ; Store current term (byte)
ADD R4, R4, R1 ; Add term to sum
ADD R1, R1, R2 ; Next term = term + d
SUBS R3, R3, #1 ; Decrement counter
BNE AP_LOOP ; Repeat

; Store sum (as word) after AP sequence


; R0 is already pointing to memory after the sequence
STR R4, [R0] ; Store sum at the end

STOP
B STOP
END

//COPRIME
AREA PROGRAM, CODE, READONLY
ENTRY

; Base addresses
LDR R0, =0x40000000 ; Input array base (10 bytes)
LDR R1, =0x40000040 ; Output base for coprime pairs (bytes)

; Constants / counters
MOV R2, #10 ; N = 10 inputs
MOV R3, #0 ; i = 0 (outer index)
MOV R10, #0 ; pair_count = 0

; ===============================
; Outer loop: i = 0..8
; ===============================
OUTER_LOOP
CMP R3, #9 ; while i <= 8
BGT ALL_DONE

; Load a = A[i]
ADD R4, R0, R3 ; addr of A[i]
LDRB R6, [R4] ; R6 = a

; j = i + 1
ADD R5, R3, #1

; ===============================
; Inner loop: j = i+1..9
; ===============================
INNER_LOOP
CMP R5, #10
BGE NEXT_I

; Load b = A[j]
ADD R4, R0, R5 ; addr of A[j]
LDRB R7, [R4] ; R7 = b

; ---------- GCD(a,b) via subtraction ----------


; Make working copies: x = a, y = b
MOV R8, R6 ; x
MOV R9, R7 ; y

; Handle zeros quickly to avoid infinite loop


CMP R8, #0
BEQ GCD_X_ZERO
CMP R9, #0
BEQ GCD_Y_ZERO
GCD_LOOP
CMP R8, R9
BEQ GCD_DONE ; gcd found in R8 (== R9)
BGT X_BIGGER
; y > x -> y = y - x
SUB R9, R9, R8
B GCD_LOOP

X_BIGGER
; x > y -> x = x - y
SUB R8, R8, R9
B GCD_LOOP

; If x==0 => gcd = y


GCD_X_ZERO
MOV R8, R9
B GCD_DONE

; If y==0 => gcd = x


GCD_Y_ZERO
; R8 already = x
B GCD_DONE

GCD_DONE
; If gcd == 1 -> (a,b) are coprime
CMP R8, #1
BNE NOT_COPRIME

; Store the pair as two bytes at output, advance pointer


STRB R6, [R1], #1
STRB R7, [R1], #1

; pair_count++
ADD R10, R10, #1

NOT_COPRIME
; j++
ADD R5, R5, #1
B INNER_LOOP

; i++
NEXT_I
ADD R3, R3, #1
B OUTER_LOOP

; ===============================
; Finish: store pair_count as a word after the last pair
; ===============================
ALL_DONE
; Align R1 to word boundary if needed (optional, pairs are bytes)
; Here we just store immediately after the last byte written.
STR R10, [R1] ; store total number of coprime pairs

STOP
B STOP
END
//. Read 10 numbers.

* If the number is divisible by 3, shift it left by 1 and store in one block.


* If divisible by 5, shift it right by 2 and store in another block.

AREA PROGRAM, CODE, READONLY


ENTRY

LDR R0, =0x40000000 ; Input numbers (10 bytes)


LDR R3, =0x40000040 ; Divisible by 3 → store shifted left
LDR R4, =0x40000080 ; Divisible by 5 → store shifted right
MOV R5, #10 ; Loop counter for 10 numbers

LOOP:
LDRB R1, [R0], #1 ; Load next number
MOV R2, R1 ; Copy for modulo checks

; ===== Check divisible by 3 =====


MOD3_LOOP:
CMP R2, #3
BLT MOD3_DONE
SUB R2, R2, #3
B MOD3_LOOP
MOD3_DONE:
CMP R2, #0
BNE CHECK5 ; If remainder ≠ 0, not divisible by 3
LSL R1, R1, #1 ; Shift left by 1
STRB R1, [R3], #1
B CHECK5

; ===== Check divisible by 5 =====


CHECK5:
MOV R2, R1 ; Copy original number
MOD5_LOOP:
CMP R2, #5
BLT MOD5_DONE
SUB R2, R2, #5
B MOD5_LOOP
MOD5_DONE:
CMP R2, #0
BNE CONTINUE
LSR R1, R1, #2 ; Shift right by 2
STRB R1, [R4], #1

CONTINUE:
SUBS R5, R5, #1
BNE LOOP

STOP:
B STOP
END

2. Input a number and check if it is a *perfect square*. Store result as 1 (true)


or 0 (false).
AREA PROGRAM, CODE, READONLY
ENTRY
; Load input number
LDR R0, =0x40000000 ; Address of input number
LDR R1, [R0] ; R1 = input number

; Special case: 0 or 1 are perfect squares


CMP R1, #0
BEQ STORE_TRUE
CMP R1, #1
BEQ STORE_TRUE

; Start checking from i = 1


MOV R2, #1 ; i = 1

CHECK_LOOP
MUL R3, R2, R2 ; R3 = i * i
CMP R3, R1
BEQ STORE_TRUE ; Found perfect square
BGT STORE_FALSE ; Passed number → not perfect square

ADD R2, R2, #1 ; i++


B CHECK_LOOP

; ----- Store result -----


STORE_TRUE
LDR R4, =0x40000004 ; Address to store result
MOV R5, #1
STR R5, [R4]
B STOP

STORE_FALSE
LDR R4, =0x40000004
MOV R5, #0
STR R5, [R4]

STOP
B STOP
END

4. Take 10 numbers.

* Store all numbers divisible by both 2 and 3 in one memory block.


* Store others in another block.
* Shift all stored divisible numbers left by 2 bits.

AREA PROGRAM, CODE, READONLY


ENTRY

; Base addresses
LDR R0, =0x40000000 ; Input numbers (word each)
LDR R1, =0x40000040 ; Divisible-by-6 storage
LDR R2, =0x40000080 ; Non-divisible storage
MOV R5, #10 ; Loop counter (10 numbers)
MOV R6, R1 ; Save start of divisible block for later
shifting

; ======== Main Loop ========


MAIN_LOOP
LDR R3, [R0], #4 ; Load next number
MOV R4, R3 ; Copy for checking

; Check divisibility by 6
MOV R7, #6
UDIV R8, R4, R7 ; R8 = number / 6
MLS R9, R8, R7, R4 ; R9 = number - (R8 * 6)
CMP R9, #0
BNE STORE_NON_DIV ; Not divisible by 6

; ===== Store divisible number =====


STR R3, [R1], #4
B CONTINUE

; ===== Store non-divisible number =====


STORE_NON_DIV
STR R3, [R2], #4

CONTINUE
SUBS R5, R5, #1
BNE MAIN_LOOP

; ======== Shift all divisible numbers left by 2 bits ========


; R6 holds start of divisible block
LDR R10, =0x40000040 ; Start of divisible block
CMP R1, R10
BEQ STOP ; No divisible numbers

SHIFT_LOOP
LDR R3, [R10] ; Load number
LSL R3, R3, #2 ; Shift left by 2 bits
STR R3, [R10], #4 ; Store back and move to next
CMP R10, R1 ; Check if end reached
BNE SHIFT_LOOP

STOP
B STOP
END

You might also like