J S S ACADEMY OF TECHNICAL EDUCATION
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COURSE: MICROCONTROLLERS
COURSE CODE: BCS402
Programs List
Module – 1
1. Using Keil software, observe the various Registers, Dump, CPSR, with a simple Assembly Language
Programs (ALP).
Module – 2
2. Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations (Demonstrate with the
help of a suitable program).
3. Develop an ALP to multiply two 16-bit binary numbers.
4. Develop an ALP to find the sum of first 10 integer numbers.
5. Develop an ALP to find the largest/smallest number in an array of 32 numbers.
6. Develop an ALP to count the number of ones and zeros in two consecutive memory locations.
Module – 3
7. Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in ascending/descending
order using bubble sort.
8. Simulate a program in C for ARM microcontroller to find factorial of a number.
9. Simulate a program in C for ARM microcontroller to demonstrate case conversion of characters from upper to
lowercase and lower to uppercase.
Module – 4 and 5
10. Demonstrate enabling and disabling of Interrupts in ARM.
11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow exceptions in ARM.
ARM ASSEMBLER DIRECTIVES
AREA directive allows the programmer to specify the memory location to store code
and data.
CODE: Contains machine instructions. READONLY is the default.
DATA: Contains data, not instructions. READWRITE is the default.
READONLY: Indicates that this area should not be written to.
READWRITE: Indicates that this area may be read from or written to.
General form of ARM Assembly Program
AREA MyCode, CODE, READONLY
ENTRY ; Entry point for the application
Start ;usercode
END ; Informs the assembler about the end of a source file
ENTRY and END Directives
The first instruction to be executed within an application is marked by the ENTRY
directive. END indicates the end of the source program.
Data Reservation Directives (DCB, DCD, DCW)
ARM assembler supports different data definition directives to insert constants in
assembly code. This directive allows the programmer to enter fixed data into the
program memory and treats that data as a permanent part of the program. Different
variants of these directives are:
1. DCB (Define Constant Byte) to define constants of byte size.
2. DCW (Define Constant Word) allocates one or more half words of memory, aligned
on two-byte boundaries.
3. DCD (Define Constant Double word) allocates one or more words of memory, aligned
on four-byte boundaries.
Example: Data1 DCD 0, 0, 0 ; Reserves 3 words in memory
Note: if the data value is prefixed with 0x it is a hexadecimal number, If it is prefixed
with 0b it is a binary number, if it is not prefixed, by default it is treated as a decimal
number.
Steps for executing ARM Assembly Programs
Step 1: After installing Keil Microvision software, open the keil IDE and in the project
menu click on New Uvision project. Give name to the project.
Step 2: And it prompts for the device to be selected. Select NXP under that select
LPC2148. Also for the message add Startup file to the source group click on NO.
Step 3: In the file option select new file, an editor window is opened so type in the
source program and save it as <filename>.asm or <filename>.s.
Step 4: in the project window click right click on the source group and add the file that
was created and close. The file is now added to the source group.
Step 5: Right click on the file that was added in the source group and click on the option
built target. The file is now assembled and linked.
Step 6: If target is successfully created go to debug menu and click on Start/Stop debug.
Execution is done in single step. U can view the register window for any changes in the
register contents and also view memory by clicking in the View menu, view memory
option and give the memory address in hexadecimal as an 8 digit address.
//Program-2 Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical
operations (Demonstrate with the help of a suitable program).
AREA SAMPLE, CODE, READONLY
ENTRY
START LDR R9,=A ; LOAD ADDRESS OF A TO REGISTER R9
LDR R0, [R9] ;LOAD VALUE OF A TO REGISTER R0
LDR R9,=B ; LOAD ADDRESS OF B TO REGISTER R9
LDR R1, [R9] ; LOAD VALUE OF B TO REGISTER R1
ADD R2, R0, R1 ; ADD R0 & R1 AND STORE RESULT IN R2
SUB R3, R0, R1 ; SUBTRACT R0 & R1(R0-R1) AND STORE RESULT IN R3
RSB R4, R0, R1 ; SUBTRACT R0 & R1(R1-R0) AND STORE RESULT IN R4
MUL R5, R0, R1 ; MULTIPLY R0 & R1 AND STORE RESULT IN R5
AND R6, R0, R1 ; LOGICAL AND R0 & R1 AND STORE RESULT IN R6
ORR R7, R0, R1 ; LOGICAL OR R0 & R1 AND STORE RESULT IN R7
EOR R8, R0, R1 ; LOGICAL XOR R0 & R1 AND STORE RESULT IN R8
LDR R9, =C ; GET ADDRESS OF C TO REGISTER R9
STR R2, [R9] ; STORE ADDITION RESULT IN C LOCATION
STR R3, [R9] ;STORE SUBTRACTION RESULT IN C LOCATION
STR R4, [R9] ;STORE REVERSE SUBTRACTION RESULT IN C LOCATION
STR R5, [R9] ;STORE MULTIPLICATION RESULT IN C LOCATION
STR R6, [R9] ;STORE LOGICAL AND RESULT IN C LOCATION
STR R7, [R9] ;STORE LOGICAL OR RESULT IN C LOCATION
STR R8, [R9] ;STORE LOGICAL XOR RESULT IN C LOCATION
STOP B STOP
AREA DATA1, DATA, READONLY
A DCD 0X02
B DCD 0X03
AREA DATA2, DATA, READWRITE
C DCD 0X0
END
// Program 3- Program to multiply two 16 bit binary numbers
AREA FIRST, CODE, READONLY
ENTRY
START
LDR R3, =NUM1 ; Get the address of 1st number NUM1
LDRH R0, [R3] ; Get the value of NUM1 to register R0
LDR R3, =NUM2 : Get the address of 2nd number NUM2
LDRH R1, [R3] ; Get the value of NUM2 to register R1
MUL R2, R0, R1 ; Multiply NUM1 & NUM2 and store in register R2
LDR R3, =RESULT ; Get the address of RESULT memory Location in
; register R3
STR R2, [R3] ; Store the result in R2 to memory location RESULT
; pointed by R3
STOP B STOP
AREA DATA1, DATA, READONLY
NUM1 DCW 0x04
NUM2 DCW 0x03
AREA DATA2, DATA, READWRITE
RESULT DCD 0x0
END
**********************************************************************
// Program 4- Program to find the sum of first 10 integers
AREA SUM, CODE, READONLY
ENTRY : Mark 1st Instruction to execute
START
MOV R3, #11 ; Initialize R3 with 10
MOV R0, #0 ; Initialize R0 with 0
MOV R1, #1 : Initialize R1 with 1
LOOP ADD R0, R0, R1 : Add R0 with R1 and store sum in R0
ADD R1, R1, #1 ; Increment the number by 1
CMP R1, R3 ; Compare incremented value with 10 in R3
BNE LOOP ; If it’s not equal then loop
LDR R4, =SUM1 ; If yes, get address of SUM1 pointed by R4
STR R0, [R4] ; Store sum in Location SUM1
STOP B STOP
AREA DATA1, DATA, READWRITE
SUM1 DCD 0X0
END
------------------------------------------------------------------------------------------------------------------
// Sum of 1st 10 integers-Simple ones
AREA SUM10, CODE, READONLY
ENTRY ; Mark 1st instruction to execute
START
MOV R0, #10 ; Initialize R0 with last number 10
MOV R1, #0 ; Initialize R1 with 0
LOOP ADD R1, R1, R0 ; Add R1 with R0 and store sum in R1
SUBS R0, R0, #1 ; Decrement counter by 1
BNE LOOP ; If it’s not 0, then go to label LOOP
LDR R2, =SUM ; If it’s 0, get address of SUM pointed by
; R2
STR R1, [R2] ; Store the result in SUM location
STOP B STOP
AREA DATA1, DATA, READWRITE
SUM DCD 0X0
END
/* Program 5- PROGRAM TO FIND LARGEST NUMBER IN AN ARRAY & STORE IN
INTERNAL RAM */
AREA LARGEST, CODE, READONLY
ENTRY ; Mark first instruction to execute
START
MOV R5, #6 ; INTIALIZE COUNTER TO 6(i.e. N=7)
LDR R1, =VALUE1 ; LOADS THE ADDRESS OF FIRST VALUE
LDR R2, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT
LOOP LDR R4,[R1],#4 ; WORD ALIGN T0 ARRAY ELEMENT
CMP R2, R4 ; COMPARE NUMBERS
BHI LOOP1 ; IF THE FIRST NUMBER IS > THEN GOTO LOOP1
MOV R2, R4 ; IF THE FIRST NUMBER IS < THEN MOV
; CONTENTS OF R4 TO R2
LOOP1 SUBS R5, R5, #1 ; DECREMENT COUNTER
CMP R5, #0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS
LDR R4, =RESULT ; LOADS THE ADDRESS OF RESULT
STR R2, [R4] ; STORES THE RESULT IN RESULT mem from R2
XSS B XSS
; ARRAY OF 32 BIT NUMBERS (N=7)
VALUE1 DCD 0X44444444 , 0X22222222, 0X11111111, 0X33333333
DCD 0X0AAAAAAAA, 0X88888888, 0X99999999 ;
AREA DATA2, DATA, READWRITE ; TO STORE RESULT IN GIVEN ADDRESS
RESULT DCD 0X0
END ` ; Mark end of file
------------------------------------------------------------------------------------------------------------------
// PROGRAM -5 SMALLEST NUMBER IN AN ARRAY OF 32-BIT NUMBERS
AREA SMALLEST, CODE, READONLY
ENTRY ; Mark first instruction to execute
START
MOV R5, #6 ; INTIALIZE COUNTER TO 6(i.e. N=7)
LDR R1, =VALUE1 ; LOADS THE ADDRESS OF FIRST VALUE
LDR R2, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT
LOOP LDR R4, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT
CMP R2, R4 ; COMPARE NUMBERS
BLS LOOP1 ; IF THE FIRST NUMBER IS < THEN GOTO LOOP1
MOV R2, R4 ; IF THE FIRST NUMBER IS > THEN MOV
; CONTENT R4 TO R2
LOOP1 SUBS R5, R5, #1 ; DECREMENT COUNTER
CMP R5, #0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS
LDR R4, =RESULT ; LOADS THE ADDRESS OF RESULT
STR R2, [R4] ; STORES THE RESULT FROM R2 IN LOCATION
;RESULT
XSS B XSS
; ARRAY OF 32 BIT NUMBERS(N=7)
VALUE1 DCD 0X44444444, 0X22222222, 0X11111111, 0X33333333
DCD 0XAAAAAAA, 0X88888888, 0X99999999
AREA DATA2, DATA, READWRITE ; TO STORE RESULT IN GIVEN ADDRESS
RESULT DCD 0X0
END ; Mark end of file
;PROGRAM -6 COUNTING NUMBER OF ZEROES AND ONES IN 32 bit DATA
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 32- bit numbers
LDR R6, =VALUE ; LOADS THE ADDRESS OF VALUE
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 NEXT
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 ; 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 ; IF NOT EQUAL GOTO TO LOOP
XSS B XSS
VALUE DCD 0X3, 0X2 ; TWO VALUES IN AN ARRAY
END ; Mark end of file