0% found this document useful (0 votes)
39 views15 pages

MCs Lab Programs - No Errors

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)
39 views15 pages

MCs Lab Programs - No Errors

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/ 15

Experiment No.

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

AREA OBSERVEREGS, CODE, READONLY


ENTRY ; Mark first instruction to execute
MOV R5, #5 ; Get the value 5 into R5
MOV R10, #8 ; Get the value 8 into R10
MOV R7, R5 ; Copy the content of R5 into R7
MOV R12, R10 ; Copy the content of R10 into R12
MRS R1, CPSR ; Copy CPSR content into R1
BIC R1, R1, #0x80 ; 0b0100000
MSR CPSR_C, R1 ; Copy the content of R1 into CPSR
HERE B HERE
END ; MARK END OF FILE
Experiment No. 2(a) : Develop and simulate ARM ALP for Data Transfer, Arithmetic
and Logical operations (Demonstrate with the help of a suitable program).

AREA DATATRANSFER, CODE, READONLY


ENTRY
LDR R0, =0x40000000
LDR R1, =0x40000080
LDR R2, =0x0000000A
UP LDRB R3, [R0]
STRB R3, [R1]
ADD R0, R0, #1
ADD R1, R1, #1
ADD R2, R2, #-1
CMP R2, #0
BEQ NEXT
B UP
NEXT MOV R0, #0x18
LDR R1, =0x20026
SVC #0x123456
END
Experiment No. 2(b) : Develop and simulate ARM ALP for Data Transfer, Arithmetic
and Logical operations (Demonstrate with the help of a suitable program).

AREA MULTIPLY, CODE, READONLY


ENTRY
LDR R0, =0x40000000
LDRB R1, [R0]
LDRB R2, [R0, #1]
ADD R3, R1, R2
STRH R3, [R0, #2]
MUL R4, R1, R2
STRH R4, [R0, #4]
SUB R5, R1, R2
STR R5, [R0, #6]
MOV R0, #0x18
LDR R1, =0x20026
SVC #0x123456
END
Experiment No. 2(c) : Develop and simulate ARM ALP for Data Transfer, Arithmetic
and Logical operations (Demonstrate with the help of a suitable program).

AREA LOGIC, CODE, READONLY


ENTRY
LDR R0, =0x40000000
LDR R1, =0x40000010
LDRB R2, [R0]
LDRB R3, [R0, #1]
AND R4, R2, R3
STRB R4, [R1]
ORR R4, R2, R3
STRB R4, [R1, #1]
MOV R0, #0x18
LDR R1, =0x20026
SVC #0x123456
END
Experiment No. 3 : Develop an ALP to multiply two 16-bit binary numbers.

AREA MULTIPLY, CODE, READONLY


ENTRY ; Mark first instruction to execute
MOV R1, #0x6400 ; STORE FIRST NUMBER IN R0
MOV R2, #0x3200 ; STORE SECOND NUMBER IN R1
MUL R3, R1, R2 ; MULTIPLICATION
HERE B HERE
END ; MARK END OF FILE
Experiment No. 4 : Develop an ALP to find the sum of first 10 integer numbers.

AREA INTSUM, CODE, READONLY


ENTRY ; Mark first instruction to execute
MOV R1, #10 ; LOAD 10 TO REGISTER
MOV R2, #0 ; EMPTY R2 REGISTER TO STORE RESULT
LOOP ADD R2, R2, R1 ; ADD THE CONTERNT OF R1 WITH RESULT AT R2
SUBS R1, #0x01 ; DECREMENT R1 BY 1
BNE LOOP ; REPEAT TILL R1 GOES TO ZERO
HERE B HERE
END
;/* PROGRAM TO FIND LARGEST NUMBER IN AN ARRAY & STORE IN */
;/* INTERNAL RAM */
;/* ARRAY OF 7 NUMBERS 0X44444444, 0X22222222, 0X11111111, 0X33333333, */
;/* 0XAAAAAAAA, 0X88888888, 0X99999999 */
;/* RESULT CAN BE VIEWED IN LOCATION 0X40000000 & ALSO IN R2 */
;/* SET A BREAKPOINT AT NOP INSTRUCTION, RUN THE PROGRAM & CHECK */
;/* THE RESULT */
AREA LARGEST , CODE , READONLY
ENTRY ; Mark first instruction to execute
START
MOV R5, #6 ; INTIALISE 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 MOVE
; 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 IN R2
STOP B STOP
; ARRAY OF 32 BIT NUMBERS (N=7)
VALUE1
DCD 0X44444444 ;
DCD 0X22222222 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0XAAAAAAAA ;
DCD 0X88888888 ;
DCD 0X99999999 ;
AREA DATA2, DATA, READWRITE ; TO STORE RESULT IN
; GIVEN ADDRESS
RESULT DCD 0X0
END ; Mark end of file
;/* PROGRAM TO FIND SMALLEST NUMBER IN AN ARRAY & STORE IN */
;/* INTERNAL RAM */
;/* ARRAY OF 7 NUMBERS 0X44444444, 0X22222222, 0X11111111, 0X22222222, */
;/* 0XAAAAAAAA, 0X88888888, 0X99999999 */
;/* RESULT CAN BE VIEWED IN LOCATION 0X40000000 & ALSO IN R2 */
;/* SET A BREAKPOINT AT NOP INSTRUCTION, RUN THE PROGRAM & CHECK */
;/* THE RESULT */
;/* PROGRAM WRITTEN BY ALS R&D TEAM BENGALURU DATE:08/08/2011 */
AREA SMALLEST , CODE , READONLY
ENTRY ; Mark first instruction to execute
START
MOV R5, #6 ; INTIALISE 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 MOVE
; 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 IN R1
XSS B XSS
; ARRAY OF 32 BIT NUMBERS (N=7)
VALUE1
DCD 0X44444444 ;
DCD 0X22222222 ;
DCD 0X11111111 ;
DCD 0X22222222 ;
DCD 0XAAAAAAAA ;
DCD 0X88888888 ;
DCD 0X99999999 ;
AREA DATA2, DATA, READWRITE ; TO STORE RESULT IN GIVEN
; ADDRESS
RESULT DCD 0X0
END ; Mark end of file
Experiment No. 6:
Develop an ALP to count the number of ones and zeros in two consecutive memory
locations.

;/* PROGRAM TO COUNT THE NUMBER OF ONES & ZEROS IN TWO


CONSECUTIVE MEMORY LOCATIONS */
;/* WE TOOK TWO NUMBERS i.e. 0X11111111,0XAA55AA55 (R0) */
;/* CHECK THE RESULT IN R2 FOR ONES & R3 FOR ZEROS */
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK
THE RESULT */
AREA ONEZERO , CODE , READONLY
ENTRY ;Mark first instruction to execute
LDR R0, MEMORY ;Load the address of memory
LDR R1, [R0] ;Load the 32-bit number
MOV R4, #32 ;Load rotation count
ROTATE RORS R1, #1 ;Rotate right by one bit, update CPSR
BCS ONES ;Is carry is=1
ADD R3, R3, #1 ;Increment zero’s counter
B NEXT ;Branch to next rotation
ONES ADD R2, R2, #1 ;Increment one’s counter
NEXT ADD R4, R4, #-1 ;Decrement the rotation count
CMP R4, #0 ;Is rotation count is zero
BNE ROTATE ;If No, go to rotate
ADD R0, R0, #4 ;Load the address of memory for no. of one’s
STRB R2, [R0] ;store no. of one’s
ADD R0, R0, #1 ;Load the address of memory for no. of zero’s
STRB R3, [R0] ;store no. of zero’s
STOP B STOP
MEMORY DCD 0x40000000 ;Memory address
END
Experiment No. 7a : Simulate a program for ARM microcontroller using KEIL to sort
the numbers in ascending order.
;/* PROGRAM TO sort in ascending order */
;/* ARRAY OF 4 NUMBERS 0X44444444 ,0X11111111,0X33333333,0X22222222 */
;/* SET A BREAKPOINT AT START1 LABLE & RUN THE PROGRAM */
;/* CHECK THE UNSORTED NUMBERS AT LOCATION 0X40000000 NEXT */
;/* SET A BREAKPOINT AT NOP INSTRUCTION, RUN THE PROGRAM & CHECK THE */
;/* RESULT */
;/* RESULT CAN BE VIEWED AT LOCATION 0X40000000 */
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 TO 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
XSS B XSS
; ARRAY OF 32 BIT NUMBERS (N=4) IN CODE REGION
CVALUE
DCD 0X44444444;
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
Experiment No. 7b : Simulate a program for ARM microcontroller using KEIL to sort
the numbers in descending order.

;/* PROGRAM TO sort in Descending order */


;/* ARRAY OF 4 NUMBERS 0X44444444 , 0X11111111 , 0X33333333 , 0X22222222 */
;/* SET A BREAKPOINT AT START1 LABLE & RUN THE PROGRAM */
;/* CHECK THE UNSORTED NUMBERS AT LOCATION 0X40000000 NEXT */
;/* SET A BREAKPOINT AT NOP INSTRUCTION, RUN THE PROGRAM & CHECK THE */
;/* RESULT */
;/* RESULT CAN BE VIEWED AT LOCATION 0X40000000 */
AREA DESCENDING , 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
BGT 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
XSS B XSS
; ARRAY OF 32 BIT NUMBERS (N=4) IN CODE REGION
CVALUE
DCD 0X44444444 ;
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
; PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER
; In this example we have taken n=7
; Check the result in R2/R1 register =13B0H (5040) */
AREA FACTORIAL , CODE , READONLY
START
MOV R0, #7
MOV R1, #1
LOOP
MUL R2, R1, R0
MOV R1, R2
SUBS R0, R0, #1
BNE LOOP
STOP B STOP
END
Experiment No. 9:
Simulate a program in C for ARM microcontroller to demonstrate case conversion of
characters from upper to lowercase and lower to uppercase.

#include <lpc214x.h>

//Function to convert character to lowercase


char toLower(char ch);

//Function to convert character to lowercase


char toUpper(char ch);

void UART0_Init(void){
//UART Initialization
PINSEL0 |= 0x00000005; //Enable UART0 pins P0.0(TXD0) and P0.1(RXD0)
U0LCR = 0x83; //8-bit data, 1 stop bit, Enable DLAB
U0DLL = 0x61; //Set baud rate to 9600 (for PCLK = 15MHz)
U0DLM = 0x00; //DLM = 0 for baud rate 9600
U0LCR = 0x03; //Disable DLAB, 8-bit data, 1 stop bit
}
void UART0_SendChar(char c) {
while(!(U0LSR & 0x20)); //Wait until the UART0 to transmit
U0THR = c; //Transmit character
}
char UART0_ReceiveChar(void) {
while(!(U0LSR & 0x01)); //Wait until data is received
return U0RBR; //Read and return received character
}
void UART0_SendString(const char *str) {
while(*str) {
UART0_SendChar(*str++);
}
}
void UART0_ReceiveString(char *str, int maxLength){
char c;
int i=0;
while(i<maxLength-1){
c= UART0_ReceiveChar();
if(c=='\r'||c=='\n'){ //End of input on newline or carriage return
break;
}
str[i++]=c;
UART0_SendChar(c); //Echo the received character
}
str[i]='\0'; //Null-terminate the string
}
void toLowerString(char *str) {
while(*str) {
*str=toLower(*str);
str++;
}
}
void toUpperString(char *str) {
while(*str) {
*str=toUpper(*str);
str++;
}
}

int main() {
char str[100];
UART0_Init();
//Prompt and convert string to lowercase
UART0_SendString("Enter a string in Uppercase: ");
UART0_ReceiveString(str, sizeof(str));
UART0_SendString("\r\nLowercase conversion: ");
toLowerString(str);
UART0_SendString(str);
UART0_SendString("\r\n");

//Prompt and convert string to uppercase


UART0_SendString("Enter a string in Lowercase: ");
UART0_ReceiveString(str, sizeof(str));
UART0_SendString("\r\nUppercase conversion: ");
toUpperString(str);
UART0_SendString(str);
UART0_SendString("\r\n");

return 0;
}
//Function definitions
//Function to convert character to lowercase
char toLower(char ch){
if(ch>='A' && ch<='Z'){
return ch+('a'-'A');
} else{
return ch; //Return unchanged if not uppercase
}
}
//Function to convert character to uppercase
char toUpper(char ch){
if(ch>='a' && ch<='z'){
return ch-('a'-'A');
} else{
return ch;
}
}

Enter a string in Uppercase: KARNATAKA


Lowercase conversion: karnataka
Enter a string in Lowercase: welcome
Uppercase conversion: WELCOME
Enter a string in Uppercase:
Experiment No. 10: Demonstrate enabling and disabling of Interrupts in ARM

AREA ENIRQ , CODE , READONLY


ENTRY
MRS R1, CPSR;
BIC R1, R1, #0x80;
MSR CPSR_C, R1;
HERE B HERE
END

AREA ENFIQ , CODE , READONLY


ENTRY
MRS R1, CPSR;
BIC R1, R1, #0x40;
MSR CPSR_C, R1;
HERE B HERE
END

AREA DISIRQ , CODE , READONLY


ENTRY
MRS R1, CPSR;
ORR R1, R1, #0x80;
MSR CPSR_C, R1;
HERE B HERE
END

AREA DISFIQ , CODE , READONLY


ENTRY
MRS R1, CPSR;
ORR R1, R1, #0x40;
MSR CPSR_C, R1;
HERE B HERE
END

You might also like