MICROCONTROLLER AND EMBEDDED SYSTEMS LABORATORY Part A

1. Write a program to multiply two 16 bit binary numbers.

Program: 

area multiply, code, readonly

entry

ldr r5,=value1

ldrh r0,[r5] 

ldr r6,=value2

ldrh r1,[r6]

mul r3,r1,r0

stop b stop

value1 DCD 0x08

value2 DCD 0x03

end

Program explanation and Demo: MCES Lab 1



2. Write a program to find the sum of first 10 integer numbers.

Program:

AREA ADDITION, CODE, READONLY

ENTRY ;MARK FIRST INSTRUCTION TO EXECUTE

MOV r0, #10 ; STORE INTEGER NUMBER IN R0

MOV r1,r0 

ADDIT SUBS r1, r1, #1 ; SUBTRACTION

CMP r1, #0 ; COMPARISON

BEQ DONE

ADD r3,r0,r1 ; ADDITION

MOV r0,r3 ; RESULT    

BNE ADDIT ; BRANCH TO THE LOOP IF NOT EQUAL

DONE LDR R4,=RES

STR R0,[R4]

  LDR R5,[R4]

STOP B STOP

AREA DATA1,DATA,READWRITE

RES DCD 0X000

END ;MARK END OF FILE

Program explanation and Demo: MCES Lab 2

    


3. Write a program to find factorial of a number.

Program : 

AREA FACT,READONLY,CODE ;Write a program to find factorial of a number.

ENTRY ;n=5   n!=5*4*3*2*1 = 120

START

MOV R0,VALUE

MOV R1,R0

LOOP

SUB R1,#1

MUL R2,R0,R1

MOV R0,R2

CMP R1,#1

BNE LOOP

STOP B STOP

VALUE DCD 0x06

END

Program explanation and Demo: MCES Lab 3



4. Write a program to add an array of 16 bit numbers and store the 32 bit result in internal RAM

Program: 

AREA ADDITION , CODE, READONLY

ENTRY ;Mark first instruction to execute

START

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

MOV R0,#0 ; INTIALISE SUM TO ZERO

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

LOOP

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

LDR R3,MASK ; MASK TO GET 16 BIT

AND R2,R2,R3 ; MASK MSB

ADD R0,R0,R2 ; ADD THE ELEMENTS

SUBS R5,R5,#1 ; DECREMENT COUNTER

CMP R5,#0

BNE LOOP ; LOOK BACK TILL ARRAY ENDS

LDR R4,=RESULT ; LOADS THE ADDRESS OF RESULT

STR R0,[R4] ; STORES THE RESULT IN R1

NOP

NOP

NOP

MASK DCD 0X0000FFFF ; MASK MSB

; ARRAY OF 16 BIT NUMBERS (N=6)

VALUE1 DCW 0X01, 0X0002, 0X0003, 0X0004, 0X0005, 0X0006

AREA DATA2, DATA, READWRITE ; TO STORE RESULT IN GIVEN ADDRESS 

RESULT DCD 0X0

END ; MARK END OF FILE

Program explanation and Demo: MCES Lab 4


5. Write a program to find the square of a number (1 to 10) using look-up table.

 ;Write a program to find the square of a number (1 to 10) using look-up table.

AREA LOOKUP,CODE,READONLY

ENTRY

START

LDR R0,=TABLE

MOV R1,#8

MOV R1,R1,LSL#0x2

ADD R0,R0,R1

LDR R3,[R0]

STOP B STOP

TABLE DCD 0X00000000  

       DCD 0X00000001   

   DCD 0X00000004  

   DCD 0X00000009

   DCD 0X00000010

   DCD 0X00000019

   DCD 0X00000024

   DCD 0X00000031

   DCD 0X00000040

   DCD 0X00000051

   DCD 0X00000064 

END

Program explanation and Demo: MCES Lab 5



6. Write a program to find the largest/smallest number in an array of 32 numbers .

Program 6a: 

;Program to find largest in an array

AREA LARGEST,CODE,READONLY

  ENTRY

START

LDR R1,=VALUE1

MOV R5,#6      ;LENGTH OF REMAINING ARRAY

LDR R2,[R1],#4

LOOP

LDR R4,[R1],#4 

cmp r2,r4

MOVLT R2,R4 

    SUBS R5,R5,#1

CMP R5,#1

BNE LOOP

  

LDR R6,=RESULT

str R2,[R6]

STOP B STOP

VALUE1 DCD 0X00000027

       DCD 0X00000008

   DCD 0X00000006

   DCD 0X00000002

   DCD 0X00000010

   DCD 0X00000011

   DCD 0X00000007

AREA DATA2,DATA,READWRITE

RESULT DCD 0,0,0,0,0,0,0,0

END

Program explanation and Demo: MCES Lab 6a


Program 6b: 

area smallest,code,readonly

entry

start

    mov r5,#7

    ldr r1,=value1

ldr r2,[r1],#4

loop

    ldr r4,[r1],#4

cmp r2,r4

bls loop1

    mov r2,r4

loop1

sub r5,r5,#1   

    cmp r5,#1

bne loop

    ldr r7,=result

str r2,[r7]

stop b stop   


value1 DCD 0x00000003

       DCD 0x00000010

   DCD 0x00000020

   DCD 0x00000030

   DCD 0x0000003a

   DCD 0x00000001

   DCD 0x0000005c

   DCD 0x00000033

area data1,data,readwrite

result dcd 0x0

    end

Program explanation and Demo: MCES Lab 6b



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

Program: 

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

area sort1,code,readonly

entry

start

    mov r0,#4

ldr r1,=arr

ldr r2,=sort

loop

ldr r3,[r1],#4

str r3,[r2],#4

sub r0,r0,#1

cmp r0,#0

bne loop

mov r5,#3 ;n-1

mov r6,#3

loop2

    mov r1,r6    ;n-i-1

    ldr r2,=sort

loop1

    ldr r3,[r2]

add r2,r2,#4

ldr r4,[r2]

cmp r3,r4

blt next

str r3,[r2]

sub r2,r2,#4

str r4,[r2]

add r2,r2,#4

next

    sub r1,r1,#1

cmp r1,#0

bne loop1

sub r5,r5,#1

cmp r5,#0

sub r6,r6,#1

bne loop2

mov r0,#4

ldr r2,=sort

loop3

ldr r3,[r2],#4

sub r0,r0,#1

cmp r0,#0

bne loop3

stop b stop

arr DCD 0x4

    DCD 0x3

DCD 0x5

DCD 0x1


area data2,data,readwrite

sort DCD 0x00000000

end

Program explanation and Demo: MCES Lab 7



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

Program :

; program to count the number of ones and zeros in two consecutive memory locations.

area zeroone,code,readonly

entry

start

    mov r0,#2

mov r3,#0   ; counting zero

mov r4,#0   ;counting 1

ldr r1,=input

mem ldr r2,[r1],#4

sub r0,r0,#1

mov r5,#32

loop

rors r2,r2,#1

ADDCC r3,r3,#1        ;0 

ADDCS r4,r4,#1                 ;1 

sub r5,r5,#1

cmp r5,#0

bne loop

cmp r0,#0

bne mem

stop b stop  

area data1,data,readonly

input dcd 0x13072021   ;00010011000001110010000000100001 0=23 0=0x17 1=9

      dcd 0x01234567   ;00000001001000110100010101100111 0=20 0=0x14 1=12 

end   ;0=43->0x2B 1= 21 hex->0x15

Program explanation and Demo: MCES Lab 8



9. Open Ended experiment

Consider a situation where the prices of Ten Cloths have been stored in continuous memory locations from lowest to highest. Write ARM assembly logic to store the price from highest to lowest in the same memory locations. 

AREA STACK,CODE,READONLY

            ENTRY

START

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

            MOV R6,#6 ; INTIALISE COUNTER TO 6(i.e. N=6)

            MOV R7,#6 ; INTIALISE COUNTER TO 6(i.e. N=6)

            LDR R3,=REV ; LOADS THE ADDRESS OF FIRST VALUE

            MOV R9,#1

; Following code to initilize the memory locations

INIT

    STR R9,[R3],#4

            SUBS R7,R7,#1 ; DECREMENT COUNTER

            ADD R9,R9,#1

            CMP R7,#0

            BNE INIT ; LOOK BACK TILL ARRAY ENDS

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

; Following code to push array values to stack memory

LOOP

 

            LDR R2,[R1],#4

            STMFD R13!, {R2}

            SUBS R5,R5,#1 ; DECREMENT COUNTER

            CMP R5,#0

            BNE LOOP ; LOOK BACK TILL ARRAY ENDS

            LDR R3,=REV ; LOADS THE ADDRESS OF FIRST VALUE

;Following code to pop item from stack and store it in the array

LOOP1

 

    LDMFD R13!,{R4}

            STR R4,[R3],#4 ; STORES THE RESULT IN R1

            SUBS R6,R6,#1 ; DECREMENT COUNTER

            CMP R6,#0

            BNE LOOP1 ; LOOK BACK TILL ARRAY ENDS

            LDR R3,=REV ; LOADS THE ADDRESS OF FIRST VALUE

 ;Following code is to verify the values

 MOV R7,#6 ; INTIALISE COUNTER TO 6(i.e. N=6)

LOOP2

 

    LDR R5,[R3],#4 ; STORES THE RESULT IN R1

            SUBS R7,R7,#1 ; DECREMENT COUNTER

            CMP R7,#0

            BNE LOOP2 ; LOOK BACK TILL ARRAY ENDS

STOP B STOP

 

            AREA DATA1,DATA,READWRITE

 

REV DCD  0X00, 0X00, 0X00, 0X00, 0X00, 0X00

            END

Explanation and demo Reverse an array



Comments

Popular posts from this blog

IoT Lab 8-11: Controlling Intensity of Led using Potentiometer, Traffic Control System, Interfacing of Ultrasonic sensor, IR sensor, Temperature, LDR

DBMS Lab 15-16: Design and implementation of COMPANY database. Design and implementation of MOVIE DATABASE.