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

    14 Jul 2013

    Division in DLX programming

    When we multiply or divide unsigned numbers, we must use logical shifts, as we will now see.

    Example 1

      addui r1,r0,200   
      srli r2,r1,1 ;2^1  
      ;r2=200/2=100  
    
    Example 2
      addui r1,r0,200   
      srli r2,r1,2 ;2^2  
      ;r2=200/4=50  
    
    Example 3
      addui r1,r0,200   
      srli r2,r1,3 ;2^3  
      ;r2=200/8=25  
    
    Tags:Division in DLX programming,Divide instruction on DLX,DLX divide function

    Multiplication in DLX programming


    When we multiply or divide unsigned numbers, we must use logical shifts, as we will now see.We can multiply 2 the power values by just shifting bits in left 

    Example multiply by 2

     addui r1,r0,25  
     slli r2,r1,1  ;2^1
     ;r2=25X2=50  
    

    Example multiply by 4
     addui r1,r0,25  
     slli r2,r1,2  ;2^2
     ;r2=25X4=100  
    

    Example multiply by 8
     addui r1,r0,25  
     slli r2,r1,3  ;2^3
     ;r2=25X8=200  
    


    Tags:Multiplication on dlx, Multiply registers in DLX, DLX Multiplication

    Example DLX program to sum of integers


    Here is the Example DLX program to sum of integers 

    Java version

     sum= 0;  
     j= 100;  
     for( i=0; i<100; i++ ){  
     if( i>j ){  
     sum= sum+i;  
     }else{  
     sum= sum+j;  
     }  
     j--;  
     }  
    
    DLX Code

    In DLX program we need to assign registers for each variables.

    r4=temp register to store result
    r3=Sum
    r2=j
    r1=i
          add r3,r0,r0 ;sum= 0;  
          addi r2,r0,100 ;Init j  
          addi r1,r0,0 ;Init i  
     L1   slti r4,r1,100 ;i<100?  
          bf r4,L4 ;No  
          sgt r4,r1,r2 ;i>j?  
          bf r4,L2 ;No  
          add r3,r3,r1 ;Yes, sum= sum+i  
          j L3  
     L2   add r3,r3,r2 ;sum= sum+j  
     L3   subi r2,r2,1 ;j= j-1  
          addi r1,r1,1 ;i= i+1;  
          j L1  
     L4   halt
    Tags: sum of integers,DLX Example program,Simple DLX program

    DLX OR & OR-immediate instructions


    The or instruction takes the 32-bit data value in register source1 and performs a logical-or operation
    between it and the 32-bit value in register source2 , then puts the result in register destination register. 
    The operation iscarried out between corresponding bits of the two operands. The ori instruction is similar, except that the second operand is the 16-bit unsigned immediate value Kuns, zero-extended to 32 bits.


    Source 1 and Source 2 =Destination register

    Example:

    r1=Source1
    r2=Source2
    r3=Source3

    r1r2r3
    000
    011
    101
    111
    OR instruction used to set certain or all bits 1

    Example to make last 4 digits to 1 and leave the remaining to original values

    r111010011
    r200001111
    r311001111
    r2=We are interested in last 4 bits so we make last 4 digits to one and rest of them to zero
    r3=first 4 digits are followed from source and last 4 digits set to 1


    DLX And and ORI Instrunction

    or r3,r1,r2
    if r1 or r2 or equal to 1 then 1 else 0
    ori r3,r1,1
    r3 will be 1
    ori r3,r1,0
    r3 =r1

    Tags:OR  instruction, OR-immediate instructions,DLX or instruction, DLX ORI instruction


    DLX And & And-immediate instructions

    The and instruction takes the 32-bit data value in register source1 and performs a logical-and operation
    between it and the 32-bit value in register source2, then puts the result in register destination register. The and operation is carried out between corresponding bits of the two operands.

    Source 1 and Source 2 =Destination register

    Example:

    r1=Source1
    r2=Source2
    r3=Source3


    r1 r2 r3
    0 0 0
    0 1 0
    1 0 0
    1 1 1


    And instruction can be used for masking to allow only certain bits

    Example

    If we are interested in last 4 digits of r1 register then create marking like below

    r1 1 1 0 1 0 0 1 1
    r2 0 0 0 0 1 1 1 1
    r3 0 0 0 0 0 0 1 1


    r2=We are interested in last 4 bits so we make last 4 digits to one and rest of them to zero
    r3=first 4 digits are zero and last 4 digits followed r1 last 4 digits


    DLX And and ANDI Instrunction

    and r3,r1,r2
    if r1 and r2 or equal then r3 will be 1 else r3 will be 0
    andi  r3,r1,1
    if r1 is 1 then r3 will be 1 else r3 will be 0


    Tags:And instruction, And-immediate instructions,DLX and instruction, DLX Andi instruction

    2 Jun 2013

    Shift left and Shift right instruction on DLX Program



    Shift left and Shift right instruction are used to move bits in a register ,This will used for multiplication and division of registers.

    Example Shift left DLX program

    1:  ;before execution r1=3=0000 0011  
    2:  ;before execution r2=0=0000 0000  
    3:       .start main  
    4:  main:  
    5:       addi r1,r0,3 ;r1=3  
    6:       slai r2,r1,2 ;move 2 bits left from R1 and save it to R2  
    7:       halt  
    8:  ;After execution r1=3=0000 0011  
    9:  ;After execution r2=0=0000 1100  
    

    Example Shift Right DLX program

    1:  ;before execution r1=2=0000 0010  
    2:  ;before execution r2=0=0000 0000  
    3:       .start main  
    4:  main:  
    5:       addi r1,r0,2 ;r1=2  
    6:       srai r2,r1,1  ;move 2 bits right from R1 and save it to R2  
    7:       halt  
    8:  ;After execution r1=2=0000 0010  
    9:  ;After execution r2=1=0000 0001  
    


    Shift DLX program commands

    Shift right Arithmatic
    sra rk,ri,rj rk
    Shift right Arithmatic Immediate
    srai rj,ri,u rj
    Shift right Logical
    srl rk,ri,rj rk
    Shift right logical Immediate
    srli rj,ri,u rj
    Shift Left Arithmatic
    sla rk,ri,rj rk
    Shift Left Arithmatic Immediate
    slai rj,ri,u rj
    Shift light Logical
    sll rk,ri,rj rk
    Shift light Logical immediate
    slli rj,ri,u rj

    Tags:Shift left and Shift right instruction on DLX Program,Shift left DLX,Shift Right DLX,DLX program

    Popular Posts