ARM CODES

Basic ARM LPC218 ASSEMBLY CODES-I

6. To write and simulate ARM assembly language programs for data transfer, arithmetic and logical operations (Demonstrate with the help of a suitable program).

– A - PROGRAM :

area prg1,code,readonly
entry
start
ldr r1,=value
ldr r2,[r1]
ldr r4,=value
str r3,[r4]
bx lr
value
dcd 0x22222222
end

 – B - PROGRAM :

area pgm,code,readonly
entry
start
mov r0,#0
mov r1,#1
and r2,r1,r0
orr r3,r1,r0
eor r4,r1,r0
bx lr
end

 – C - PROGRAM :

area pgm3,code,readonly
entry
start
ldr r0,=0x00000002
ldr r1,=0x00000003
add r2,r1,r0
muls r3,r1,r0
bx lr
end

Output: Check Registers


PROGRAM 1.a:

ADDITION OF TWO 32 BIT NUMBERS.

area addition,code,readonly
entry
            mov r0,#00000009
mov r1,#00000005
add r2,r1,r0
end



PROGRAM 1.b:


ADDITION OF TWO 64 BIT NUMBERS.


        area addition,code,readonly
entry
             ldr r0,=0x11111111
ldr r1,=0x22222222
ldr r2,=0x33333333
ldr r3,=0x44444444
adds r4,r0,r2
adcs r5,r1,r3

end


PROGRAM 1.c:

ADDITION OF 'N' 32 BIT NUMBERS.

       area nadd,code,readonly
entry
             ldr r0,=0x40000000
             mov r2,#05             // number of 32 bit no. to be added

             ldr r3,[r0],#04
back      ldr r4,[r0],#04
            add r3,r3,r4
            subs r2,r2,#01
            cmp r2,#00
             bne back
stop   b  stop

             end



PROGRAM 2.a:

DATA EXCHANGE


   area data_exch,code,readonly
entry
      ldr r0,=0x40000000
      ldr r1,=0x40000044
      mov r4,#09

loop  ldr r2,[r0]
      mov r5,r2
      ldr r6,[r1]
      str r6,[r0],#04
      str r5,[r1],#04
      subs r4,#01
      cmp r4,#00
      bne loop
stop  b stop

      end



PROGRAM 2.b:

BLOCK OF DATA TRANSFER


     area data_trans,code,readonly
entry
             ldr r0,=0x40000000
     ldr r1,=0x40000044
     mov r4,#09

loop     ldr r2,[r0],#04
            str r2,[r1],#04
    subs r4,#01
    cmp r4,#00
    bne loop
stop  b stop

          end




PROGRAM 3.a:

COVERTING ASCII TO HEX

      area ascii_hex,code,readonly
entry
       mov r0,#&41
   mov r1,#&46
   mov r3,r0
   bl next
   mov r4,r3,lsl#04
   mov r3,r1
   bl next
   add r5,r4,r3
   b stop

next   cmp r3,#&39
   subls r3,r3,#&30
   subgt r3,r3,#&37
   mov pc,lr
stop   b stop
    end



PROGRAM 3.b:


CONVERTING HEX NUMBER TO ASCII NUMBER

     area hex_ascii,code,readonly
entry
      mov r0,#&af
      mov r1,r0
      and r2,r1,#&0f
      bl next
      mov r3,r2
      and r2,r0,#&f0
      mov r2,r2,lsr#04
      bl next
      b stop

next  cmp r2,#09
      addcs r2,r2,#&37
      addcc r2,r2,#&30
      mov pc,lr
stop  b stop

      end



PROGRAM 3.c:

CONVERTING BINARY TO GRAY

     area binary_gray,code,readonly
entry
     mov r0,#0x06
     mov r1,#08
     lsr r2,r2,#01
     eor r2,r2,r1
stop b stop

     end



PROGRAM 3.d:


CONVERTING GRAY TO BINARY

        area gray_binary,code,readonly
entry
                mov r0,#0x58
mov r1,r0
mov r2,#07
loop   mov r1,r1,lsr#01
eor r0,r0,r1
subs r2,r2,#01
cmp r2,#00
bne loop
stop    b  stop

 end



PROGRAM 4:

DIVISION OF TWO NUMBERS BY SUCCESSIVE SUBTRACTION 

 area division,code,readonly
entry
     mov r0,#25
     mov r1,#04
     cmp r0,r1
     movge r3,r0
     movls r3,r1
     movcs r4,r1
     movcc r4,r0

loop subs r3,r3,r4
     add r2,r2,#01      //divisible
     cmp r3,r4
     bge loop
     mov r5,r3          //r3 holds quotient
stop b stop

     end




PROGRAM 5:

FINDING FACTORIAL OF NUMBER WITHOUT LOOKUP TABLE

      area fact_wout_lt,code,readonly
entry
      mov r0,#10
      mov r1,#01
back  mul r2,r0,r1
      mov r1,r2
      subs r0,r0,#01
      cmp r0,#00
      bne back
stop  b  stop

      end

PROGRAM 1:


PROGRAM TO FIND FIBONACCI SERIES     
  
       area fbnc,code,readonly
entry
         mov r5,#10
         ldr r0,=0x40000000
                    mov r1,#01
                    mov r2,#00

back     add r3,r1,r2
                    str r3,[r0],#04
                    mov r2,r1
                    mov r1,r3
                    subs r5,r5,#01
                    cmp r5,#00
                    bne back
stop     b  stop
         end

PROGRAM 2:

PROGRAM TO FIND MAXIMUM OF GIVEN NUMBERS

      area max,code,readonly
entry
      ldr r0,=numbers
            mov r1,#06
            ldr r2,[r0],#04
loop  ldr r3,[r0],#04
            cmp r2,r3
            bhi next
            mov r2,r3

next  subs r1,r1,#01
           cmp r1,#00
            bne loop
stop  b  stop
    
         area max1,code
numbers dcd 0x04, 0x08, 0x15, 0x07, 0x24, 0x74, 0x18
      end

PROGRAM 3:

PROGRAM TO FIND MINIUM OF GIVEN NUMBER

      area minimum,code,readonly
Entry
      ldr r0,=numbers
            mov r1,#06
            ldr r2,[r0],#04
loop  ldr r3,[r0],#04
      cmp r2,r3
            bls next
            mov r2,r3

next  subs r1,r1,#01
      cmp r1,#00
            bne loop
stop  b  stop
    
         area minimum,code
numbers dcd 0x04, 0x08, 0x15, 0x07, 0x24, 0x74, 0x18
      end   
       
 PROGRAM 4:

PROGRAM TO FIND PRIME NUMBERS

       area prim,code,readonly
entry
main    
      mov r0,#01
            ldr r6,=0x40000000
            mov r8,#0x64

start mov r1,r0
            add r0,r0,#01
            lsr r9,r1,#01
            mov r5,r9
            mov r4,#02
            bl subb

back   cmp r3,#01
       bge prime
      
goo    subs r8,r8,#01
       cmp r8,#00
             bne start
             b stop

prime  str r1,[r6],#04
       subs r8,r8,#01
       cmp r8,#00
             bne start
             b stop

subb   mov r3,r1
loop   subs r3,r3,r4
             cmp r3,r4
             bge loop
             cmp r3,#00
             beq goo
             cmp r4,r5
             beq back
             add r4,r4,#01
             subs r9,r9,#01
             cmp r9,#00
             bgt subb
             mov pc,lr
stop   b  stop
       end

PROGRAM 5:

PROGRAM TO IMPLEMENT MULTIPLICATION OF TWO 8bit NUMBERS

          area multiplication,code,readonly
entry
    mov r0,#06
          mov r1,#02
          mul r2,r1,r0
          end

PROGRAM 6:

PROGRAM TO IMPLEMENT MULTIPLICATION OF TWO NUMBERS WITHOUT USING mul INSTRUCTION

          area multiplication,code,readonly
entry
    mov r0,#13
          mov r1,#04
loop
    add r3,r3,r0
          add r2,#01
          cmp r2,r1
          bne loop
          mov r4,r3
          end

PROGRAM 7:

PROGRAM TO SORT POSITIVE AND NEGATIVE NUMBERS

       area positive_negative,code,readonly
entry
       ldr r0,=0x40000000
             ldr r1,=0x40000038
             ldr r2,=0x40000054
       mov r3,#10

back   ldr r4,[r0],#04
             movs r5,r4,lsl#01
             movcs r6,r4;
             strcs r6,[r1],#04;negative no stored in memory location ;
             addcs r8,r8,#01;no of negative no's;          
             movcc r7,r4;
             strcc r7,[r2],#04;positive no stored in memory location;
             addcc r9,r9,#01;no of positive no's;                              
             subs r3,r3,#01
             cmp r3,#00
             bne back
stop   b stop
             end

PROGRAM 8:

PROGRAM TO DISPLAY 0-9 ON 7 SEGMENT

             area seven_segment,code,readonly
entry
main  
       mov r0,#07
             mov r4,#04
             ldr r1,=value
             mul r3,r0,r4
             add r1,r1,r3
             ldr r2,[r1]
       b stop
   
             area seven_segment,code,readonly
value  dcd 0xbf,0x86,0xbb,0x8f,0xe6,0xab,0xfb
       dcd 0x87,0xff,0xef
stop   b stop
       end

PROGRAM 1:


PROGRAM TO IMPLEMENT  SIMPLE TASK SWITCHING

#include <rtl.h>
#include <lpc21xx.h>
#include <stdio.h>
void init_serial (void);
OS_TID tsk1,tsk2;
char    cnt1,cnt2,test2;
__task void job1 (void);
__task void job2 (void);
char arr1[20],arr2[20];
unsigned int i = 0;
__task void job1 (void)
 {
  os_tsk_prio_self (2);
  while (1)
   {
 os_tsk_create (job2, 1);
             os_evt_wait_or (0x0001, 0x0000);
             cnt1++;
             sprintf(arr1,"counter1 :%d",cnt1);
          while (arr1[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr1[i];
                   i++;
          }
          i=0;
          while (!(U0LSR & 0x20));
          U0THR = '\r';
          if (cnt1==0x05)
          cnt1=0;
          os_dly_wait(1);                                           
    }
}

__task void job2 (void)
 {
  while (1)
   {
          cnt2++;
sprintf(arr2,"counter2 :%d",cnt2);
while (arr2[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr2[i];
                   i++;
          }
          os_dly_wait(3);
          i=0; 
          while (!(U0LSR & 0x20)); 
          U0THR = '\n';       
          if(cnt2==0x0f)
{
 cnt2=0;
os_evt_set (0x0001, tsk1);        

  }
test2=22;
           os_dly_wait(3);
          test2=0;
          os_dly_wait(3);
  }
}
int main (void)
{
cnt1=0;
cnt2=0;
   init_serial ();
  os_sys_init (job1);
  while (1);
}
void init_serial (void) {
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD0             
  U0LCR = 0x83;                         // 8 bits, no Parity, 1 Stop bit    
  U0DLL = 0x61;                           // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                         // DLAB = 0                         
}

PROGRAM 2:

PROGRAM TO IMPLEMENT SIMPLE ROUND ROBIN TASK SWITCHING

#include <rtl.h>
#include <lpc21xx.h>
#include <stdio.h>
void init_serial (void);
int counter1;
int counter2;
char arr1[20],arr2[20];
int i=0;
__task void job1 (void);
__task void job2 (void);
__task void job1 (void) {
  os_tsk_create (job2, 0); 
  while (1) {              
    counter1++; 
          sprintf(arr1,"counter1 :%d",counter1);
          while (arr1[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr1[i];
                   i++;
          }
          i=0;
          while (!(U0LSR & 0x20));
          U0THR = '\n';
                    
  }
}
__task void job2 (void) {
  while (1) {               
    counter2++;             

sprintf(arr2,"counter2 :%d",counter2);
          while (arr2[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr2[i];
                   i++;
          }
          i=0;
          while (!(U0LSR & 0x20));
          U0THR = '\n';
  }
}

void main (void)
 {
init_serial();
  os_sys_init (job1);       
  for (;;);
}

void init_serial (void)
{
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD0             
  U0LCR = 0x83;                         // 8 bits, no Parity, 1 Stop bit     
  U0DLL = 0x61;                           // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                         // DLAB = 0                         
}

PROGRAM 3:

PROGRAM TO IMPLEMENT  MAIL_BOX

#include <rtl.h>
#include<stdio.h>
#include<lpc214x.h>
void init_serial(void);
os_mbx_declare (MsgBox, 100);              
U32 mpool[8*sizeof(U32) ];       
unsigned int cnt1,cnt2;
char arr1[20],arr2[20];
char arr3[20]="task2deleted";
int i=0;
__task void task2 (void);
__task void task1 (void)
 {
  U32 *mptr;
  os_tsk_create (task2, 0);
  os_mbx_init (MsgBox, sizeof(MsgBox));
  mptr = _alloc_box (mpool);               
while(1)
 {
           while(!(cnt1==15))
                             {
                             cnt1++;

                             sprintf(arr1,"counter1 :%d",cnt1);
                             while (arr1[i] != '\0')
                             {        os_dly_wait(1);
                                       while (!(U0LSR & 0x20));
                                       U0THR = arr1[i];
                                      i++;
                             }
                             i=0;
                             while (!(U0LSR & 0x20));
                             U0THR = '\n';
                              os_dly_wait(5);
                             }
 if (cnt1==15)
                   {
                   mptr[0] = cnt1;
                   cnt1=0;
                   os_mbx_send (MsgBox, mptr, 0xffff); /* Send the count value to a 'Mailbox' */
                   os_dly_wait(5);
    
                    }
           }
   }

__task void task2 (void)
{
  U32 *rptr  ;
  cnt2=0;
           while(1)
           {
           os_mbx_wait (MsgBox, (void**)&rptr, 0xffff);     
         cnt2 = *rptr;    
          while(!(cnt2==30))
          {       
                             cnt2++;
                   sprintf(arr2,"counter2 :%d",cnt2);
                   os_dly_wait(2);
                             while (arr2[i] != '\0')
                             {
                             while (!(U0LSR & 0x20));
                             U0THR = arr2[i];
                             i++;
                             }
                             i=0;
                             while (!(U0LSR & 0x20));
                             U0THR = '\n';
                             os_dly_wait(5);
                      }
                    cnt2=0;
           }
           }

int main (void)
{
 init_serial();
  _init_box (mpool, sizeof(mpool), sizeof(U32));
  os_sys_init(task1);
}
void init_serial (void)
{
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD0             
  U0LCR = 0x83;                               // 8 bits, no Parity, 1 Stop bit    
  U0DLL = 0x61;                              // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                             // DLAB = 0                         
}

PROGRAM 1:

PROGRAM TO IMPLEMENT SIMPLE SEMAPHORE

#include <RTL.h>                     
#include <LPC21xx.h>               
#include <stdio.h>

void init_serial (void);      
unsigned char msg1[]="task1\r\n", msg2[]="task2\r\n";
unsigned int i=0;
OS_TID tsk1, tsk2;
OS_SEM semaphore1;

__task void task1 (void) {
  OS_RESULT ret;

  while (1) {
    os_dly_wait(3);
    ret = os_sem_wait (semaphore1, 1);
    if (ret != OS_R_TMO) {
       while (msg1[i] != '\0')
            {
                    while (!(U0LSR & 0x20));
                    U0THR = msg1[i];
                   i++;
            }
            i=0;
          os_sem_send (semaphore1);
    }
  }
}

__task void task2 (void) {
  while (1)
  {
  
    os_sem_wait (semaphore1, 0xFFFF);
     while (msg2[i] != '\0')
            {
                    while (!(U0LSR & 0x20));
                    U0THR = msg2[i];
                   i++;
            }
            i=0;
    os_sem_send (semaphore1);
  }
}

__task void init (void) {
   init_serial ();
   os_sem_init (semaphore1, 1);
  tsk1 = os_tsk_create (task1, 10);
  tsk2 = os_tsk_create (task2, 0);
   os_tsk_delete_self ();
}

void init_serial (void) {
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD1             
  U0LCR = 0x83;                         // 8 bits, no Parity, 1 Stop bit    
  U0DLL = 0x61;                           // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                         // DLAB = 0                         
}
int main (void) {
  init_serial();
  os_sys_init (init);
}

PROGRAM 2:


PROGRAM TO IMPLEMENT HARDWARE INTERRUPT

#include<lpc21xx.h>
#include<rtl.h>
OS_TID tsk1;
__task void job1(void);

int p=0;
void delay(unsigned int j)
{        unsigned int i;
          for(i=0;i<j;i++);
}

void extint0_isr(void)__irq
{       
unsigned int c[16]={0xF03f0200,0xF0060200,0xF05b0200,0x004f0200,0x00660200,0x006d0200,0x007d0200,0x00070200,0x007f0200,0x006f0200,0x00770200,0x007f0200,0x00390200,0x003f0200,0x00790200,0x00710200};
          unsigned int i;
          PINSEL0=0x00000000;
          IODIR0=0xF0FF0200;
          IOCLR0=0x00ff0000;
          IOSET0=c[p];
          for(i=0;i<200;i++)
          delay(10000);
          IOCLR0=0x00000200;
          p++;
          if(p>15)
          p=0;
          EXTINT|=0x00000001;
          VICVectAddr=0;
          os_evt_set(0x0001,tsk1);
          }
__task void job1(void)
{        tsk1=os_tsk_self();
     while(1)
           {   
          PINSEL1=0x00000001;
          EXTMODE=0x00000001;
          EXTPOLAR=0x00000000;
VICVectAddr0=(unsigned long)extint0_isr;
          VICVectCntl0=0x20|14;
          VICIntEnable|=0x00004000;
          delay(1000);
            }
 }
int main(void)
{          IODIR0=0xF0FF0200;
          os_sys_init(job1);
          for(;;);
}

PROGRAM 3:

PROGRAM TO INTERFACE SOFTWARE INTERRUPT

#include<RTL.H>
 #include<stdio.h>
 #include<lpc21xx.h>
 OS_TID tsk1,tsk2;
 U32 i;
 __task void job1(void);
 __task void job2(void);
 void __swi(9) ONLED (void);
 void __swi(8) OFFLED (void);

 void __SWI_8 (void)
 {
          IODIR0=0x00FF0200;
          IOSET0=0x00FF0200;
          for(i=0;i<100000;i++);
 }

 void __SWI_9 (void)
 {
          for(i=0;i<100000;i++);
          IOCLR0=0x00FF0200;
          for(i=0;i<100000;i++);
          for(i=0;i<100000;i++);
 }

 __task void job1(void)
 {
          tsk1=os_tsk_self();
          tsk2=os_tsk_create(job2,2);
          while(1)
          {
                   ONLED();
          }
 }

 __task void job2(void)
 {
          while(1)
          {
                   OFFLED();
                   os_dly_wait(1);
          }
 }

 int main(void)
 {
          os_sys_init(job1);

 }

No comments:

Post a Comment