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

1 Jun 2013

DLS file layout and comments



Following format shows, how to write DLS file.

Usually program start with ".start" and following line with subroutine name

Semicolon (;) used for comments and used in end of the statements

Label         Opcode       Operands      Comments
L1               add             r1,r0,r1           ;r1++


Memory address declaration

Label:            .word    exp1
r1backup       .word    4
r1backup   reference pointer for memory address 4

Allocating empty space on DLX program

Label:      .space     expr
Array:      .space       40      ;Allocate 10 words 

Setting the program start address
                 .start expr

Example
                 .start Main ;Jump to Main
Array: .space 10*4
Index: .word 0
Main: lw r1, Index      ;Jump to here



Tags:DLS file layout and comments,DLS file layout,DLS file commands,DLX file







Popular Posts