20 Aug 2013

Perrin function Java to DLX Conversion



In this post we will look at how to convert java code into DLX code. We will create two functions Perrin and gensum .

The function P(n) computes the nth Perrin number. Perrin numbers are defined by the recursive rules:

P(0) = 3
P(1) = 0
P(2) = 2
P(n) = P(n-2) + P(n-3) for n > 2

So for example: -


  • P(0) returns 3
  • P(1) returns 0
  • P(2) returns 2
  • P(3) returns 3

  • The gensum function will return the sum of Perrin numbers from P(a) to P(b) using an increment of step.
    The range of the parameters are as follows:
    • 0 <= a <= b
    • step >=1
    If any of the parameters are out of range, the function must return -1.
    For example:
    • gensum(0, 5, 1) = P(0) + P(1) + P(2) + P(3) + P(4) + P(5) = 3 + 0 + 2 + 3 + 2 + 5 = 15
    • gensum(2, 11, 2) = P(2) + P(4) + P(6) + P(8) + P(10) = 2 + 2 + 5 + 10 + 17 = 36
    • gensum(5, 2, 1) = -1
    • gensum(2, 10, -1) = -1
    Java code for perrin and gensum


    1:  public class Perrin {  
    2:    private static int P(int n) {  
    3:      int a = 3, b = 0, c = 2; // the first three P(n)  
    4:      if (n==0)  
    5:      {  
    6:           return a;   
    7:      }  
    8:      else if (n==1){    
    9:        return b;  
    10:      }  
    11:      else if (n==2){   
    12:        return c;  
    13:      }  
    14:      else{  
    15:        int m = 0; //r2  
    16:        while (n > 2) {  
    17:          // System.out.println(n+" a"+a+" b"+b+" c"+c);  
    18:          m = a + b; //r2=r3+r4  
    19:          a = b;//r3=r4  
    20:          b = c;//r4=r5  
    21:          c = m;//r5=r2  
    22:          n=n-1;//r1=r1-1  
    23:         // System.out.println(n+" a"+a+" b"+b+" c"+c);  
    24:        }  
    25:        return m;  
    26:      }  
    27:    }  
    28:    private static int gensum(int a,int b,int step)  
    29:    {  
    30:         int sum=0;  
    31:         int p=0;  
    32:         for(int i=a;i<=b;i=i+step)  
    33:         {  
    34:              p=P(i);  
    35:              sum=sum+p;  
    36:              System.out.print(" i"+i+" p"+p+" "+sum+" ");  
    37:         }  
    38:         return sum;  
    39:    }  
    40:    public static void main(String[] args) {  
    41:      for (int i = 0; i < 21; i++)  
    42:        System.out.println("p(" + i + ")=" + P(i));  
    43:      System.out.println(gensum(0, 7, 1) );  
    44:    }  
    45:  }  
    

    DLX code Main DLX code
    1:  lights      .equ    16#FFFFFFF8  
    2:  switches    .equ    16#FFFFFFFC  
    3:       .start main  
    4:  main:       
    5:       add r1,r0,r0    ;a=0  
    6:       addi r3,r0,1    ;c=1  
    7:       lw r2,switches  ;b=switches input  
    8:       jal gensum      ;gensum(a,b,c)  
    9:       sw lights,r1    ;lights=result  
    10:      halt  
    
    Perrin function DLX code
    1:       P:                 ;P(x)       
    2:       sw r2backup,r2     ;result  
    4:       sw r3backup,r3     ;a  
    5:       sw r4backup,r4     ;b  
    6:       sw r5backup,r5     ;c  
    7:       sw r6backup,r6     ;If Result  
    8:                          ;Backup Registers End       
    9:        sequi r6,r1,0     ;x==0?  
    10:       bt r6,case0       ;if x=0 case0   
    11:       sequi r6,r1,1     ;x==1?  
    12:       bt r6,case1       ;if x=1 case1  
    13:       sequi r6,r1,2     ;x==2?  
    14:       bt r6,case2       ;if x=2 case2  
    15:       addi r2,r0,0      ;if x>2 result=0  
    16:       addi r3,r0,3      ;a=3  
    17:       addi r4,r0,0      ;b=0  
    18:       addi r5,r0,2      ;c=2  
    19: while sgti r6,r1,2      ;while(x>2)  
    20:       bf r6,case3       ;if while not true case3  
    21:        add r2,r3,r4     ;y = a + b  
    22:        add r3,r4,r0     ;a = b  
    23:        add r4,r5,r0     ;b = c  
    24:        add r5,r2,r0     ;c = y  
    25:        subi r1,r1,1     ;x=x-1  
    26:       j while           ;continue while loop  
    27:  case3   
    28:       addi r1,r2,0      ;result=y  
    29:       j endp       
    30:  case0   
    31:      addi r1,r0,3       ;r1=result=3  
    32:       j endp  
    33:  case1   
    34:      addi r1,r0,0       ;r1=result=0  
    35:      j endp  
    36:  case2   
    37:      addi r1,r0,2       ;r1=result=2  
    38:      j endp       
    39:  endp                   ;end of P Subroutine  
    40:                         ;Restore Registers Start  
    41:       lw r2,r2backup    
    42:       lw r3,r3backup  
    43:       lw r4,r4backup  
    44:       lw r5,r5backup  
    45:       lw r6,r6backup    
    46:                           ;Restore Registers End  
    47:         jr r31            ; end P            
    48:  r2backup .word 1000  
    49:  r3backup .word 1004  
    50:  r4backup .word 1008  
    51:  r5backup .word 1012  
    52:  r6backup .word 1016  
    
    Gensum function in DLX code
    1:  gensum:                  ;gensum(a,b,c)  
    2:                           ;Backup Registers Start  
    3:       sw r2backupG,r2     ;b  
    4:       sw r3backupG,r3     ;c  
    5:       sw r4backupG,r4     ;result of IF   
    6:       sw r5backupG,r5     ;sum  
    7:       sw r6backupG,r6     ;result of P Subroutine  
    8:       sw r7backupG,r7     ;i  
    9:       sw r8backupG,r8     ;backup r31 register  
    10:                           ;Backup Registers End  
    11:       slt r4,r1,r0      ;a<0?  
    12:       bt r4,errorcase   ;if true,errorcase  
    13:       slt r4,r2,r0      ;b<0?  
    14:       bt r4,errorcase   ;if true,errorcase  
    15:       slti r4,r3,1      ;step<1?  
    16:       bt r4,errorcase   ;if true,errorcase  
    17:       sle r4,r2,r1      ;a<b?  
    18:       bt r4,errorcase   ;if true,errorcase  
    19:       addi r5,r0,0      ;sum=0  
    20:       addi r6,r0,0      ;result of P=0  
    21:       add r7,r0,r1      ;i=a  
    22:  for  slt r4,r2,r7      ;i<b for (i=a;i<b;i=i+step)  
    23:       bt r4,endfor      ;end for loop  
    24:       add r1,r0,r7      ;a=i  
    25:       add r8,r31,r0     ;backup r31 register  
    26:       jal P             ;P(i)  
    27:       add r31,r8,r0     ;restore r31 register  
    28:       add r5,r5,r1      ;sum=sum+P(i)  
    29:       add r7,r7,r3      ;i=i+step  
    30:       j for                 
    31:  endfor  
    32:       add r1,r5,r0     ;a=sum  
    33:       sw result,r1     ;result=sum  
    34:       j endgensum  
    35:  errorcase  
    36:    addi r1,r0,-1       ;a=-1  
    37:       j endgensum  
    38:  endgensum  
    39:                        ;Restore Registers Start  
    40:       lw r2,r2backupG  
    41:       lw r3,r3backupG                      
    42:       lw r4,r4backupG  
    43:       lw r5,r5backupG  
    44:       lw r6,r6backupG   
    45:       lw r7,r7backupG       
    46:       lw r8,r8backupG  
    47:                           ;Restore Registers End  
    48:       jr r31              ;end gensum()  
    49:  r2backupG .word 2000  
    50:  r3backupG .word 2004  
    51:  r4backupG .word 2008  
    52:  r5backupG .word 2012  
    53:  r6backupG .word 2016  
    54:  r7backupG .word 2020  
    55:  r8backupG .word 2024  
    56:  result      .word      2028  
    
    Tags:Example DLX program,Perrin function,gensum function,DLX code

    3 comments:

    1. In your programming, you did
      Backup Registers Start
      Backup Registers End
      Restore Registers Start
      Restore Registers End
      I have not seen this syntax. Could you please explain me why they need?

      ReplyDelete
    2. Those are comments. They are not syntax

      ReplyDelete

    Popular Posts