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

arm -programming Arithmetic operations

The document provides a series of ARM assembly language programs demonstrating basic arithmetic operations such as addition, subtraction, multiplication, and factorial calculation, as well as sorting and counting bits in memory. Each program is structured with defined areas for code and data, utilizing registers for computation and control flow. It serves as a practical guide for implementing fundamental programming concepts in ARM architecture.

Uploaded by

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

arm -programming Arithmetic operations

The document provides a series of ARM assembly language programs demonstrating basic arithmetic operations such as addition, subtraction, multiplication, and factorial calculation, as well as sorting and counting bits in memory. Each program is structured with defined areas for code and data, utilizing registers for computation and control flow. It serves as a practical guide for implementing fundamental programming concepts in ARM architecture.

Uploaded by

saisharan510
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ARM PROGRAMMING

Addition of two 32 bit numbers

AREA add1, CODE, READONLY

ENTRY

START

MOV R1,#0x00000020 ;MOVE 20 VALUE TO R1 REGISTER

MOV R2,#0x00000020 ; MOVE 20 VALUE TO R2 REGISTER

ADD R3,R1,R2 ; add R1 & R2 AND STORE RESULT IN R3 EGISTER

END
Sub of two 32 bit numbers

AREA SUB, CODE, READONLY

ENTRY

START

MOV R1,#0x00000022 ;MOVE 20 VALUE TO R1 REGISTER

MOV R2,#0x00000020 ; MOVE 20 VALUE TO R2 REGISTER

SUB R3,R1,R2 ; SUB R1 & R2 AND STORE RESULT IN R3 EGISTER

END
32 BIT REVERSE SUBTRACTION

AREA RSB, CODE, READONLY

ENTRY

START

MOV R1,#0x00000022 ;MOVE 20 VALUE TO R1 REGISTER

MOV R2,#0x00000020 ; MOVE 20 VALUE TO R2 REGISTER

RSB R3,R1,R2 ; REVERSE SUBTRACTION R2 & R1 AND STORE RESULT IN R3 EGISTER

END
32 BIT MULTIPLY

AREA MUL, CODE, READONLY

ENTRY

START

MOV R1, #0x00000022 ; MOVE 20 VALUE TO R1 REGISTER

MOV R2, #0x00000020 ; MOVE 20 VALUE TO R2 REGISTER

MUL R3,R1,R2 ; MUL R1 & R2 AND STORE RESULT IN R3 EGISTER

END
32 BIT MULTIPLY ACCUMULATE
AREA ACC, CODE, READONLY

ENTRY

START

MOV R1,#0x00000005 ;MOVE 5 VALUE TO R1 REGISTER

MOV R2,#0x00000003 ; MOVE 3 VALUE TO R2 REGISTER

MOV R3,#0x00000018 ;MOVE 18 VALUE TO R2 REGISTER

MLA R0,R1,R2,R3 ; MUL R1 & R2 AND ADD WITH VALUE OF R3=18 AND STORE RESULT INTO R0 5*3+18=27

END
32 BIT LOGICAL SHIFT LEFT

AREA LOGICAL LEFT, CODE, READONLY

ENTRY

START

MOV R5 , #0x00000005 ;MOVE 20 VALUE TO R1 REGISTER

MOV R7, #0x00000008 ; MOVE 20 VALUE TO R2 REGISTER

MOV R7, R5, LSL #2 ; LET R7=R5*4= =(R5<<2)

END
2) Write a program to find the sum of first 10 integer numbers
AREA INT, CODE, READONLY

ENTRY

START

MOV R1,#10 ; LOAD 10 TO REGISTER

MOV R2,#0 ; EMPTY THE REGISTER TO STORE RESULT

LOOP

ADD R2, R2, R1 ; ADD THE CONTENT OF R1 WITH RESULT AT R2

SUBS R1,#0x01 ; DECREMENT R1 BY 1

BNE LOOP ; REPEAT TILL R1 GOES 0

BACK B BACK ; JUMPS BACK TO C CODE

END
Program to find factorial of a given number.

AREA FACTORIAL, CODE, READONLY

ENTRY ; MARK FIRST INSTRUCTION TO EXECUTE

START

MOV R0, #7 ; STORE FACTORIAL NUMBER IN R0

MOV R1, R0 ; MOVE THE SAME NUMBER IN R1

FACT SUBS R1, R1, #1 ; SUBTRACTION

MUL R3, R0,R1 ; MULTIPLICATION

MOV R0, R3 ; RESULT R3 MOVED TO R0

CMP R1, #1 ; COMPARISON R1 WITH 1

BNE FACT ; BRANCH TO THE FACT IF NOT EQUAL

NOP

NOP

NOP

END ; MARK END OF FILE


Write a program to arrange a series of 32 bit numbers in ascending/descending order.

AREA ASCENDING , CODE, READONLY

ENTRY ;Mark first instruction to execute

START

MOV R8,#4 ; INTIALISE COUNTER TO 4(i.e. N=4)

LDR R2,=CVALUE ; ADDRESS OF CODE REGION

LDR R3,=DVALUE ; ADDRESS OF DATA REGION

LOOP0

LDR R1,[R2],#4 ; LOADING VALUES FROM CODE REGION

STR R1,[R3],#4 ; STORING VALUES TO DATA REGION

SUBS R8,R8,#1 ; DECREMENT COUNTER

CMP R8,#0 ; COMPARE COUNTER TO 0

BNE LOOP0 ; LOOP BACK TILL ARRAY ENDS

START1

MOV R5,#3 ; INTIALISE COUNTER TO 3(i.e. N=4)

MOV R7,#0 ; FLAG TO DENOTE EXCHANGE HAS OCCURED

LDR R1,=DVALUE ; LOADS THE ADDRESS OF FIRST VALUE

LOOP

LDR R2,[R1],#4 ; WORD ALIGN T0 ARRAY ELEMENT

LDR R3,[R1] ; LOAD SECOND NUMBER

CMP R2,R3 ; COMPARE NUMBERS

BLT LOOP2 ; IF THE FIRST NUMBER IS < THEN GOTO LOOP2

STR R2,[R1],#-4 ; INTERCHANGE NUMBER R2 & R3

STR R3,[R1] ; INTERCHANGE NUMBER R2 & R3


MOV R7,#1 ; FLAG DENOTING EXCHANGE HAS TAKEN PLACE

ADD R1,#4 ; RESTORE THE PTR

LOOP2

SUBS R5,R5,#1 ; DECREMENT COUNTER

CMP R5,#0 ; COMPARE COUNTER TO 0

BNE LOOP ; LOOP BACK TILL ARRAY ENDS

CMP R7,#0 ; COMPARING FLAG

BNE START1 ; IF FLAG IS NOT ZERO THEN GO TO START1 LOOP

NOP

NOP

NOP

; ARRAY OF 32 BIT NUMBERS (N=4) IN CODE REGION

CVALUE

DCD 0X11111111 ;

DCD 0X33333333 ;

DCD 0X22222222 ;

AREA DATA1, DATA, READWRITE ; ARRAY OF 32 BIT NUMBERS IN DATA REGION

DVALUE

DCD 0X00000000

END ; Mark end of file


Write a program to count the number of ones and zeros in two consecutive memory locations

AREA ONEZERO , CODE, READONLY

ENTRY ;Mark first instruction to execute

START

MOV R2,#0 ; COUNTER FOR ONES

MOV R3,#0 ; COUNTER FOR ZEROS

MOV R7,#2 ; COUNTER TO GET TWO WORDS

LDR R6, =VALUE ; LOADS THE ADDRESS OF VALUE

LDR R4, =RES1

LOOP MOV R1,#32 ; 32 BITS COUNTER

LDR R0,[R6],#4 ; GET THE 32 BIT VALUE

LOOP0 MOVS R0,R0,ROR #1 ; RIGHT SHIFT TO CHECK CARRY BIT (1's/0's)

BHI ONES ; IF CARRY BIT IS 1 GOTO ONES BRANCH OTHERWISE N EXT

ZEROS ADD R3,R3,#1 ; IF CARRY BIT IS 0 THEN INCREMENT THE COUNTER BY 1(R3)

B LOOP1 ; BRANCH TO LOOP1

ONES ADD R2,R2,#1 ; IF CARRY BIT IS 1 THEN INCREMENT THE COUNTER BY 1(R2)

LOOP1 SUBS R1,R1,#1 ; COUNTER VALUE DECREMENTED BY 1

BNE LOOP0

STR R2,[R4],#4

STR R3,[R4],#4 ; IF NOT EQUAL GOTO TO LOOP0 CHECKS 32BIT

SUBS R7,R7,#1 ; COUNTER VALUE DECREMENTED BY 1

CMP R7,#0 ; COMPARE COUNTER R7 TO 0

BNE LOOP

STR R2,[R4],#4
STR R3,[R4],#4

JMP b JMP

VALUE DCD 0X3, 0X2 ; TWO VALUES IN AN ARRAY

AREA DATA2,DATA,READWRITE

RES1 DCD 0X0

RES2 DCD 0X0

END

You might also like