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

25-ARM 7 Assembly Programming-22-03-2024

Uploaded by

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

25-ARM 7 Assembly Programming-22-03-2024

Uploaded by

AYUSH OM MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ARM7 Programming

MODULE 7
AREA
▪The AREA directive tells the assembler to define a new section of
memory.
▪The memory can be code (instruction) or data and can have attributes such
as ReadOnly,
▪ReadWrite, and so on.
▪This is widely used to define one or more blocks of indivisible memory
for code or data to be used by the linker.
▪Every Assembly language program has at least one AREA.
▪The following is the format:
▪AREA sectionname, attribute, attribute, …

DR. SHELJA VIT VELLORE


Attributes
▪Among widely used attributes are CODE, DATA, READONLY, READWRITE, COMMON, and
ALIGN. The following describes these widely used attributes:
▪READWRITE is an attribute given to an area of memory which can be read from and written to.
Since it is READWRITE section of the program it is by default for DATA.
▪READONLY is an attribute given to an area of memory which can only be read from. Since it is
READONLY section of the program it is by default for CODE.
▪CODE is an attribute given to an area of memory used for executable machine instruction. Since
it is used for code section of the program it is by default READONLY memory.
▪DATA is an attribute given to an area of memory used for data and no instruction (machine
instructions) can be placed in this area. Since it is used for data section of the program it is by
default a READWRITE memory.

DR. SHELJA VIT VELLORE


Attributes
▪COMMON is an attribute given to an area of DATA memory section which can be used commonly by several
program codes. We do not initialize the COMMON section of the memory since it is used by compiler exclusively.

▪ALIGN is another attribute given to an area of memory to indicate how memory should be allocated according to
the addresses. When the ALIGN is used for CODE and READONLY it aligned in 4-bytes address boundary by
default since the ARM instructions are all 32-bit (4-bytes) word. The ALIGN attribute of AREA has a number after
like ALIGN=3 which indicates the information should be placed in memory with addresses of23, that is 0x50000,
0x50008, 0x50010, 0x50020, and so on.

▪Examples:

▪AREA OUR_VARIABLES, DATA, READWRITE

▪AREA OUR_CONSTS, DATA, READONLY

▪AREA PROG_2_1, CODE, READONLY

DR. SHELJA VIT VELLORE


ENTRY
▪Another important pseudocode is the ENTRY directive.
▪This indicates to the assembler the beginning of the executable code.
▪The ENTRY directive is the first line of the ARM Assembly language code
section of the program, meaning that anything after the ENTRY directive
in the source code is considered actual machine instruction to be executed
by the CPU.
▪For a given ARM Assembly language program we can have only one
ENTRY point.
▪Having multiple ENTRY directive in an Assembly language program will
give you an error by assembler.

DR. SHELJA VIT VELLORE


Addition of two numbers
AREA PROG_1, CODE, READONLY
ENTRY
MOV R0,#0x1 ;Loading Register R0 with 0x1;
MOV R1, #0xA ;Loading Register R1 with 0xA;
ADD R3,R0,R1 ;Adding them together to get the result stored in R3
HBH ;Looping. BL can also be used in place of B. ;wait here
END

DR. SHELJA VIT VELLORE


VAL1 RN R1 ;define VAL1 as a name for R1
VAL2 RN R2 ;define VAL2 as a name for R2
SUM RN R3 ;define SUM as a name for R3
AREA PROG_2, CODE, READONLY
ENTRY
MOV VAL1, #0x25 ;R1 = 0x25
MOV VAL2, #0x34 ;R2 = 0x34
ADD SUM, VAL1,VAL2 ;R3 = R2 + R1
HERE B HERE
END

DR. SHELJA VIT VELLORE


Addition through lookup table
AREA LOOKUP_EXAMPLE,READONLY,CODE
ENTRY
LDR R2,=OUR_FIXED_DATA ;point to OUR_FIXED_DATA
MOV R3,#0x0C
LOOP: LDRB/LDRH/LDR R0,[R2] ;load R0 with the contents of 8-bit
ADD R1,R1,R0 ;add R0 to R1
ADD R2,R2,#0x01
SUBS R3,R3,#0x01
BNE LOOP
OUR_FIXED_DATA
DCB 0x55,0x33,1,2,3,4,5,6 ; define 8-bit data
DCH 0x4540,0x50 ; define 16-bit data
DCD 0x23222120,0x30 ; define 32-bit data
END

DR. SHELJA VIT VELLORE


Addition of multiword numbers
Analyze the following program which adds 0x35F62562FA to 0x21F412963B:

LDR R0,=0xF62562FA ;R0 = 0xF62562FA


LDR R1,=0xF412963B ;R1 = 0xF412963B
MOV R2,#0x35 ;R2 = 0x35
MOV R3,#0x21 ;R3 = 0x21
ADDS R5,R1,R0 ;R5 = 0xF62562FA + 0xF412963B ;now C = 1
ADC R6,R2,R3 ;R6 = R2 + R3 + C ; = 0x35 + 21 + 1 = 0x57

DR. SHELJA VIT VELLORE


Subtraction of unsigned numbers
Example 1:
MOV R2,#0x4F ;R2 = 0x4F
MOV R3,#0x39 ;R3 = 0x39
SUBS R4,R2,R3 ;R4 = R2 – R3
Example 2:
MOV R2,#0x4F ;R2 = 0x4F
SUBS R4,R2,#0x05 ;R4 = R2 – 0x05

DR. SHELJA VIT VELLORE


SBC (subtract with borrow)
Analyze the following program which subtracts 0x21F62562FA from
0x35F412963B:

LDR R0,=0xF62562FA ;R0 = 0xF62562FA, ; notice the syntax for LDR


LDR R1,=0xF412963B ;R1 = 0xF412963B
MOV R2,#0x21 ;R2 = 0x21
MOV R3,#0x35 ;R3 = 0x35
SUBS R5,R1,R0
SBC R6,R3,R2

DR. SHELJA VIT VELLORE


Multiplication
Example 1:
MOV R1,#0x25 ;R1=0x25
MOV R2,#0x65 ;R2=0x65
MUL R3,R1,R2 ;R3 = R1 × R2 = 0x65 × 0x25
Example 2:
LDR R1,=100000 ;R1=100,000
LDR R2,=150000 ;R2=150,000
UMULL R3,R4,R2,R1 ;0x54000000 × 0x10000002
Example 3:
MOV R1,#100 ;R1 = 100
MOV R2,#5 ;R2 = 5
MOV R3,#40 ;R3 = 40
MLA R4,R1,R2,R3 ;R4 = R1 × R2 + R3 = 100 × 5 + 40 = 540
DR. SHELJA VIT VELLORE
Logical instructions
Example 1: MOV R1,#0x04
Logical OR and set the flags
ORRS R2,R1,#0x68
Example 2: MOV R1,#0x04
Logical OR
ORR R2,R1,#0x68
Example 3: MOV R1,#0x54
Logical EXOR
EOR R2,R1,#0x78
Example 4: MOV R1,#0x0F
Logical AND and NOT
MOV R2,#0xAA
BIC R3,R2,R1 ;now R3 = 0xAA ANDed with 0xF0 = 0xA0
Example 5: MOV R1,#0x23
Logical NOR
ORR R2, R1, #0x54
MVN R3, R2
DR. SHELJA VIT VELLORE
Barrel Shifter
Example1:
MOV R0,#0x9A ;R0 = 0x9A
MOVS R1,R0,LSR #3 ;shift R0 to right 3 times
Example2:
MOV R0,#0x9A
MOV R2,#0x03
MOV R1,R0,LSRS R2 ;shift R0 to right R2 times and raise the flags
Example3:
LDR R1,#0x7 ;R1=0x7
MOV R2,#0x05 ;R2=0x05
MOV R1,R1,LSL R2 ;shift R1 left R2 number of times

DR. SHELJA VIT VELLORE


Rotate Right
MOV R1,#0x36
MOV R1,#0x36
;R1 = 0000 0000 0000 0000 0000 0000 0011 0110
;R1 = 0000 0000 0000 0000 0000 0000
MOVS R1,R1,ROR #1
0011 0110
;R1 = 0000 0000 0000 0000 0000 0000 0001 1011
C=0 MOV R0,#3
MOVS R1,R1,ROR #1 or ;R0 = 3 number of times to rotate
;R1 = 1000 0000 0000 0000 0000 0000 0000 1101 MOVS R1,R1,ROR R0
C=1
;R1 = 1100 0000 0000 0000 0000 0000
MOVS R1,R1,ROR #1
0000 0110 C=1
;R1 = 1100 0000 0000 0000 0000 0000 0000 0110
C=1
DR. SHELJA VIT VELLORE
Rotate Left
MOV R1,#0x36
MOV R1,#0x36
MOVS R1,R1,ROR #31
MOVS R1,R1,ROR #31 or MOV R0,#29
MOVS R1,R1,ROR #31 ;R0 = 3 number of times to rotate
MOVS R1,R1,ROR R0

DR. SHELJA VIT VELLORE


Rotate Right with carry
MOV R1,#0x26
MOVS R1,R1,RRX #1 MOV R1,#0x36

MOVS R1,R1, RRX #1 or MOVS R1,R1, RRX #3


MOVS R1,R1, RRX #1

DR. SHELJA VIT VELLORE


Rotate Left with carry
MOV R1,#0x36
MOV R1,#0x36
MOVS R1,R1,RRX #31
MOVS R1,R1, RRX #31 or MOV R0,#29
MOVS R1,R1, RRX #31 ;R0 = 3 number of times to rotate
MOVS R1,R1, RRX R0

DR. SHELJA VIT VELLORE


Looping and
Branch
Instructions

DR. SHELJA VIT VELLORE


Add the value 9 to the R0 a 1000 times
AREA EXAMPLE4_1, CODE, READONLY
ENTRY
LDR R2,=1000 ;R2 = 1000 (decimal) for counter
MOV R0,#0 ;R0 = 0 (sum)
AGAIN ADD R0,R0,#9 ;R0 = R0 + 9 (add 09 to R1, R1 = sum)
SUBS R2,R2,#1 ;R2 = R2 - 1 and set the flags. Decrement counter
BNE AGAIN ;repeat until COUNT = 0 (when Z = 1)
MOV R4,R0 ;store the sum in R4
HERE B HERE ;stay here
END

DR. SHELJA VIT VELLORE


Branching and compare Instructions

DR. SHELJA VIT VELLORE


Assume that there is a class of five people with the following
grades: 69, 87, 96, 45, and 75. Find the highest grade.
AREA PROG_4_1D, DATA, READONLY
MYDATA DCB 69,87,96,45,75
AREA PROG_4_1, CODE, READONLY
ENTRY
MOV R0,#5 ;COUNT = 5
MOV R1,#0 ;MAX = 0
LDR R2,=MYDATA ;POINTER = MYDATA ( address of first data )
AGAIN LDRB R4,[R2] ;load contents of R2 location to R4
CMP R1,R4 ;compare R1 and R4
BHS NEXT ;if R1 ≥ R4 branch to CTNU
MOV R1,R4 ;MAX = NEXT
NEXT ADD R2,R2,#1 ; point to next data
SUBS R0,R0,#1 ;decrement counter
BNE AGAIN ;branch AGAIN if counter is not zero
HERE B HERE
END
DR. SHELJA VIT VELLORE
Delay calculation for ARM
DELAY MOV R0,#255 1
AGAIN NOP 1 Crystal Frequency = 100 MHz
NOP 1
Time for one instruction cycle =
SUBS R0,R0,#1 1 1/100MHz = 10 ns
BNE AGAIN 3
MOV PC,LR ;return 1 Total delay Time=
[1+(1+1+1+3)x255+1] x 10ns =
❖Notice that BNE takes three instruction cycles if it
15320 ns
jumps back, and takes only one cycle when falling
through the loop.

DR. SHELJA VIT VELLORE


Problems
Write an ALP for do the following by using ARM7 instruction set:
1. In a class there are 16 students, they scored 32, 43, 57, 89, 68, 99 , 100,
20, 10, 92, 78, 56, 65, 12, 08, 80 out of 100 in their FAT exam. Find the
class average for FAT Result.
2. Rotate left this data 100070 with carry 10000 times.
3. R0 = 100. Do this : R7 = [(R0^9)x9] by using:
a) Branch and barrel shifter.
b) Branch and Booth multiplier
4. Find out lowest marks in FAT exam marks given in q1.
5. Write an ALP by using ARM7 to evaluate x3 +2x2 +5x for the range of x=
5to 10.
6. 32 LEDs are connected register R10, toggle theses LEDs with a delays of
4.5 seconds. While crystal frequency is 66 MHz.

DR. SHELJA VIT VELLORE

You might also like