0% found this document useful (0 votes)
14 views12 pages

MClabprgm

The document provides a series of assembly language programs (ALPs) for ARM microcontrollers using Keil software, covering various operations including data transfer, arithmetic, logical operations, finding sums, and identifying largest/smallest numbers in arrays. It also includes C programs for sorting arrays, calculating factorials, and case conversion of characters. Each section is structured with code examples and comments explaining the functionality.

Uploaded by

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

MClabprgm

The document provides a series of assembly language programs (ALPs) for ARM microcontrollers using Keil software, covering various operations including data transfer, arithmetic, logical operations, finding sums, and identifying largest/smallest numbers in arrays. It also includes C programs for sorting arrays, calculating factorials, and case conversion of characters. Each section is structured with code examples and comments explaining the functionality.

Uploaded by

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

1.

Using Keil software, observe the various Registers, Dump, CPSR, with a simple Assembly
Language Programs (ALP).

AREA reg, CODE, READONLY


ENTRY

MOV R0, #0x56 ; Move immediate number to R0


MOV R1, #0x89 ; Move immediate number to R1
LDR R5, =DATA ; Load address of data into R5
LDRH R3, [R5] ; Load first halfword from DATA into R3
LDRH R4, [R5, #2] ; Load second halfword from DATA into R4
MRS R6, CPSR ; Move CPSR content into R6

STOP B STOP ; Infinite loop

DATA DCW 0x1234, 0x6789 ; Data in memory

END
**********************************************************************************

This is how the code should look like:-


2. Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations
(Demonstrate with the help of a suitable program).

AREA PRG6, CODE, READONLY ; Define a logical area named PRG6


ENTRY ; Entry point where the code starts

LDR R0, =5 ; Data transfer – R0 = 5


LDR R1, =3 ; R1 = 3

ADD R2, R0, R1 ; Arithmetic: R2 = 8 (5 + 3)


SUB R3, R0, R1 ; SUB: R3 = 2 (5 - 3)
MUL R4, R0, R1 ; MUL: R4 = 15 (5 * 3)

AND R5, R0, R1 ; Logical AND: R5 = 1 (5 & 3)


ORR R6, R0, R1 ; Logical OR: R6 = 7 (5 | 3)
EOR R7, R0, R1 ; Logical XOR: R7 = 6 (5 ^ 3)

STOP B STOP ; Infinite loop to stop execution

END ; End of the program

**********************************************************************************
This is how the code should look like:-
3. Develop an ALP to multiply two 16-bit binary numbers.

AREA MULTIPLY,CODE, READONLY


ENTRY

START
MOV r1,#0X1900
MOV r2,#0X0C80
MUL r3,r1,r2

STOP B STOP
END
**********************************************************************************

This is how the code should look like :-


4. Develop an ALP to find the sum of first 10 integer numbers.

AREA SUM10, CODE, READONLY ; Define a logical code area


ENTRY ; Entry point of the program

MOV R0, #10 ; R0 = 10 (counter for 10 numbers)


MOV R1, #0 ; R1 = 0 (to store the sum)

LOOP ADD R1, R1, R0 ; Add current R0 to sum (R1 = R1 + R0)


SUBS R0, R0, #1 ; Decrement R0 by 1 and update flags
BNE LOOP ; If R0 ? 0, repeat the loop

STOP B STOP ; Infinite loop to stop execution

END ; End of the program

**********************************************************************************
This is how the code should look like:-
5. Develop an ALP to find the largest/smallest number in an array of 32 numbers.

Largest:-

AREA LARGEST, CODE, READONLY


ENTRY
MOV R5, #5 ; Initialize loop counter for 6 elements
LDR R0, =ARRAY_DATA ; Load base address of array
LDR R1, =RESULT ; Load address of result
LDR R2, [R0] ; Load first element into R2 (initial max)

LOOP
ADD R0, R0, #4 ; Move to next element
LDR R3, [R0] ; Load next element into R3
CMP R2, R3 ; Compare current max with next element
BHS SKIP_UPDATE ; If R2 >= R3, skip update
MOV R2, R3 ; Else update R2 with R3 (new max)

SKIP_UPDATE
SUBS R5, R5, #1 ; Decrement counter
BNE LOOP ; If not zero, repeat loop

STR R2, [R1] ; Store max in RESULT

B STOP ; Infinite stop loop

STOP
B STOP ; Halt

ARRAY_DATA
DCD 0x25, 0x68, 0x25, 0xFF, 0x99, 0x12 ; Word-aligned data

AREA Data1, DATA, READWRITE


RESULT
DCD 0

END

**********************************************************************************

This is how the code should look like(next page):-


Smallest :-

AREA SMALLEST, CODE, READONLY


ENTRY
MOV R5, #5 ; Initialize counter for 6 iterations (7 elements total)
LDR R0, =ARRAY_DATA ; Load address of array
LDR R1, =RESULT ; Load address to store result
LDR R2, [R0] ; Load the first element as initial minimum

LOOP
ADD R0, R0, #4 ; Move to the next element
LDR R3, [R0] ; Load next array element into R3
CMP R2, R3 ; Compare current min with the new element
BLS SKIP_UPDATE ; If R2 <= R3, skip update
MOV R2, R3 ; Update R2 to new minimum

SKIP_UPDATE
SUBS R5, R5, #1 ; Decrement loop counter
BNE LOOP ; Repeat loop until all elements are checked

STR R2, [R1] ; Store the smallest number in memory


B STOP ; Infinite loop to stop program
STOP
B STOP ; Halt execution

ARRAY_DATA
DCD 0x25, 0x68, 0x25, 0xFF, 0x99, 0x12, 0xAB ; Array of 7 elements

AREA Data1, DATA, READWRITE


RESULT
DCD 0 ; Memory location to store result

END

**********************************************************************************

This is how it should look like :-


6. Develop an ALP to count the number of ones and zeros in two consecutive memory
locations.

AREA ONEZERO, CODE, READONLY


ENTRY
MOV R2, #0 ; Counter for ones
MOV R3, #0 ; Counter for zeros
MOV R7, #2 ; Counter for 2 numbers
LDR R6, =LOOKUP ; Load starting address of numbers

LOOP MOV R1, #32 ; Number of bits in each number


LDR R0, [R6] ; Load 1st number into R0

NEXTBIT
MOVS R0, R0, ROR #1 ; Rotate right and update flags
BCS ONES ; If LSB was 1 (carry set), go to ONES
ZEROS ADD R3, R3, #1 ; R3 stores count of 0s
B REPEAT

ONES ADD R2, R2, #1 ; R2 stores count of 1s

REPEAT SUBS R1, R1, #1 ; Decrement bit counter


BNE NEXTBIT ; Repeat until R1 = 0

ADD R6, R6, #4 ; Move to next number (4 bytes)


SUBS R7, R7, #1 ; Decrement number counter
BNE LOOP ; Repeat for 2 numbers

STOP B STOP ; Infinite loop to end program

LOOKUP DCD 0x5, 0x7 ; Test data: 000...0101 and 000...0111

END

**********************************************************************************
This is how the code should look like :-
7. Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in
ascending/descending order using bubble sort.

Ascending :-

#include <lpc214x.h>
int main(void)
{
volatile int i, j, temp;
int arr[ ] = {4,1,3,5,2}; // the array to be sort
// bubble sort
for (i = 1; i < 5; i++)
{
for (j = 0; j < 5 - i; j++)
{
if (arr[j] > arr[j + 1]) // Compares first and second element of the array
{
temp = arr[j]; // If first element is greater, swap the elements of the array
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
while(1);
}

**********************************************************************************

This is how it should look like:-


Descending :-

#include <lpc214x.h>

int main(void)
{
volatile int i, j, temp;
int arr[] = {4, 1, 3, 5, 2}; // The array to be sorted (descending)

// Bubble sort in descending order


for (i = 1; i < 5; i++)
{
for (j = 0; j < 5 - i; j++)
{
if (arr[j] < arr[j + 1]) // Compare and swap if current is less than next
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

while(1); // Infinite loop to end main


}

**********************************************************************************

This is how the code should look like :-


8. Simulate a program in C for ARM microcontroller to find factorial of a number.

#include <lpc214x.h>

int main()
{
volatile int n, i, fact = 1;
n = 5; // initialize the number

for (i = 1; i <= n; ++i)


fact *= i; // factorial calculation

while (1); // Infinite loop to keep program running


}
**********************************************************************************

OR

**********************************************************************************
#include <lpc21xx.h>
int main(){

unsigned int num = 5, i;


unsigned long fact = 1;
for(i = 1; i<=num; i++)
fact = fact * i;

while(1);
}

**********************************************************************************

This is how the code should look like :-


9. Simulate a program in C for ARM microcontroller to demonstrate case conversion of
characters from upper to lowercase and lower to uppercase.

#include <lpc21xx.h>

char src[] = "Hello";


char dest[100]; // Allocate enough memory for output

void caseConvert() {
unsigned int i;
for (i = 0; src[i] != '\0'; i++) {
if (src[i] >= 'a' && src[i] <= 'z') {
dest[i] = src[i] - 32; // Lowercase to uppercase
} else if (src[i] >= 'A' && src[i] <= 'Z') {
dest[i] = src[i] + 32; // Uppercase to lowercase
} else {
dest[i] = src[i]; // Copy non-alphabet characters as-is
}
}
dest[i] = '\0'; // Null-terminate the output string
}

int main() {
caseConvert();
while (1); // Infinite loop to keep the program running
}
**********************************************************************************

This is how it should look like :-

You might also like