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

Popular Posts