Monday, February 28, 2011

Lexer, First-Follow, Left Recursion


 
Covers :

1) Lexical Analyzer (C)

2) Left Recursion (Java, GUI)

3) First and Follow (Java, GUI)












The codes of the above three assignments are provided at:
https://docs.google.com/leaf?id=0B-mmfn2DeWvJMGE4OGU3YTAtNDYxZi00MDc3LWIxMDQtZGQ0MGZjMjdhMzZm&hl=en


Thursday, February 10, 2011

VLSI Laboratory


VHDL Codes for FPGA implementation
  1. Full Adder (1-bit)
  2. Full Adder (4-bit)
  3. 7-Segment Decoder (3-8)
  4. 4-bit ALU
  5. Comparator (4-bit)
  6. Multiplexer (8 X 1)
  7. Demultiplexer (1 X 8)
  8. Binary to Gray Code Conversion
  9. (Even) Parity Generator
  10. D Flip Flop
  11. T flip-flop
  12. Binary Coded Decimal Adder(1 bit)
  13.  MULTIPLIER(2 bits)
  14.  Decoder(3-8)
  15.  Encoder(0-9)
  • In Viva you have to implement and run a full program (obtained by card picking).
Complete Source Codes, with UCF file and circuit diagram are provided here:
https://docs.google.com/leaf?id=0B-mmfn2DeWvJZDdkYjAxZTUtODQ5Yy00NzY5LWE1YjEtY2ZiMDk4ZmJiODYy&hl=en

Binary to Hexadecimal



%OUT-----------------------------------------------------------------------------------------
%OUT- Prints a 8-bit binary number in its equivalent hexadecimal form                     
%out- input: 8-bit binary string accepted from the user                                                  
%out- output: 1 byte Hexadecimal equivalent of the binary string is printed 
%out – author: Ayon and Ariyam                                
%OUT-----------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack

.data
count equ 08h                                                                                            ;for 8-bit binary string
ip db "Enter a 8-bit binary number: ","$"                                              ;a request to prompt the user for entering the binary no.
op db "  Hexadecimal form:  ","$"                                                           ;output message
err db " Invalid Entry!","$"                                                             ;error message
heading db "***** BINARY to HEXADECIMAL *****","$"                     ;a heading

.code


main proc
            ;clrscrn                                                           ;clearing the screen
            mov dx,0105h
            ;cursor                                                            ;setting up the cursor
            showmsg heading                                     ;displaying heading
           
            mov dx,0303h                                             ;pointing to next line
            ;cursor                                                            ;setting up the cursor
            showmsg ip                                                  ;prompt the user to enter the binary string
                       
            xor bh,bh                                                      ;clearing register Bh
            mov cx,count                                             ;loading cx with length of the binary string

           
continue:      rol bh,01h  ;rotate BH left, 0 will come in the LSB of BH, as intially BH was cleared
                        mov ah,01h
                        int 21h
                        sub al,30h                             ;getting the binary digit (bit 0 or 1) in AL
                        cmp al,01h                           ;when bit is greater than 0 or 1
                        ja WRONG
                        add bh,al                             ;add the input bit (0 or 1) to BH
                        loop continue                     ;iterate till 8 bits are accepted from the user
           
            showmsg op                                    ;display output message, content of BH not hampered
            output                                              ;calling output macro
            jmp FINISH
           
WRONG:       showmsg err                        ;displaying error message           
FINISH:                       exit
           
main endp
end main
           

Merge Sort



%OUT------------------------------------------------------------------------------------------------------------------------------------
%OUT- Accept two arrays, merge them and then sort them in ascending order using any SORTING algorithm
%out- input: Each term of the each of the two arrays is accepted from the user        
                                               ; Sizes of the arrays are mentioned in the program itself
%out- output: Print the arrays after merging them and reprint after sorting them    
%out – author: Ayon and Ariyam                                            
%OUT---------------------------------------------------------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack
.data

array_size1 equ 4                                                         ;assuming array_size1 to be equal to 3
array1 db array_size1 dup(?)                                       ;reserving bytes = array_size1 for the array1, uninitialised
array_size2 equ 3                                                         ;assuming array_size2 to be equal to 3
array2 db array_size2 dup(?)                                       ;reserving bytes = array_size2 for the array2, uninitialised
merge_size equ 7                                                        ;this will be equal to array_size1+array_size2

ip1 db "Enter terms for 1st array: ","$"               ;a request to prompt the user for entering terms of the 1st array
ip2 db "Enter terms for 2nd array: ","$"             ;a request to prompt the user for entering terms of the 2nd array
op1 db "After merging: ","$"                                         ;output message        
op2 db "After sorting: ","$"                                           ;output message        
heading db "***** MERGE & THEN SORT *****","$"         ;a heading

.code

main proc
            clrscrn                                                  ;clearing the screen
            mov dx,0105h
            cursor                                                   ;setting up the cursor
            showmsg heading                              ;displaying heading
           
            mov dx,0303h                                      ;pointing to next line
            cursor                                                   ;setting up the cursor
            input_array array1,array_size1,ip1        ;accept array from user
           
            mov dx,0503h                                      ;pointing to next line
            cursor                                                   ;setting up the cursor
            input_array array2,array_size2,ip2        ;accept array from user
                       
; ============= begin merging ==================================

            mov si,array_size1
            mov di,00h
            mov cx,array_size2

merge: mov ah,array2[di]
            mov array1[si],ah                                ;merge the element from second array into first at the end
            inc di
            inc si
            loop merge
                                   
            mov dx,0703h                                      ;pointing to next line
            cursor                                                   ;setting up the cursor
            display_array array1,merge_size,op1   ;print the merged array

; ============== begin sorting (bubble sort) ===================       
           
            mov cx,merge_size
            sub cx,01h       
           
            outer:               push cx                        ;saving the current iteration number
                                    mov bx,0001h             
;loading bx (displacement for based indexed addressing with displacement)
                                    mov di,00h                  ;pointer to first array element
            inner:               mov al,array1[di]       
                                    cmp al,array1[di+bx]  ;comparing 2 consecutive array elements
                                    jbe continue
            swap:              mov ah,array1[di+bx]
;performing swap by using two registers ah and al, when succeeding array element is smaller
                                    mov array1[di],ah
                                    mov array1[di+bx],al
            continue:         inc di                           ;point to next array element
                                    loop inner                    ;iterate till the maximum is bubbled down
                                    pop cx                         ;loader count for outer loop
                                    dec cx
                                    cmp cx,0000h              ;short jump not possible
                                    jne outer
                                                                                   
            mov dx,0903h                                      ;pointing to next line
            cursor                                                   ;setting up the cursor
            display_array array1, merge_size, op2             ;print the merged array after sorting

            exit

main endp
end main

Bubble Sort



%OUT-----------------------------------------------------------------------------------------
%OUT- Arranges an array in ascending order using BUBBLE SORT                     
%out- input: Each term of the array is accepted from the user                         
; Size of the array is mentioned in the program itself
%out- output: Accepted array printed in ASCENDING order                            
%out – author: Ayon and Ariyam                    
%OUT-----------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack
.data
array_size equ 6                              ;assuming array_size to be equal to 6
array db array_size dup(?)                      ;reserving bytes = array_size for the array, uninitialised
ip db "Enter terms of the array: ","$"                
;a request to prompt the user for entering terms of the array
op1 db "Array in ascending order: ","$"                       ;output message
op2 db "Next iteration: ","$"                                            ;intermediate output message            
heading db "***** BUBBLE SORT *****","$"          ;a heading

.code


main proc
            clrscrn                                                                                    ;clearing the screen
            mov dx,0105h
            cursor                                                                                     ;setting up the cursor
            showmsg heading                                                 ;displaying heading
           
            mov dx,0303h                                                         ;pointing to next line
            push dx                                                         ;saving current cursor position
            cursor                                                                         ;setting up the cursor
            input_array array,array_size,ip               ;accept array from user

            mov cx,array_size
            sub cx,01h   
           
            outer:             pop dx                                              ;retrieving the current cursor position
                                    add dh,02h                          ;pointing the cursor to the next line
                                    push dx                                 ;saving current cursor position
                                    cursor
                                    push cx                                             ;saving the current iteration number
                                    mov bx,0001h                     ;loading bx (displacement for based indexed
;addressing with displacement)
                                    mov di,00h               ;pointer to first array element
            inner:              mov al,array[di]    
                                    cmp al,array[di+bx]          ;comparing 2 consecutive array elements
                                    jbe continue
            swap:             mov ah,array[di+bx]         ;performing swap by using two registers ah and al,
;when succeeding array element is smaller
                                    mov array[di],ah
                                    mov array[di+bx],al
            continue:      inc di                          ;point to next array element
                                    loop inner                 ;iterate till the maximum is bubbled down
                                    display_array array,array_size,op2        ;display the result of this iteration
                                    pop cx                                                           ;loader count for outer loop
                                    dec cx
                                    cmp cx,0000h                                             ;short jump not possible
                                    jne outer
                                                                                   
            pop dx
            add dh,02h                                                  ;pointing the cursor to the final line
            cursor
            display_array array,array_size,op1                    ;printing array after sorting

            exit

main endp
end main
           

Second Maximum & Second Minimum



%OUT---------------------------------------------------------------------------------------------------------
%OUT- Find second maximum & second minimum from an array accepted from user                             
%out- input: Each term of the array is accepted from the user                                                 
                     ; Size of the array is mentioned in the program itself
%out- output: Print second maximum & second minimum                                 
%out – author: Ayon and Ariyam                               
%OUT---------------------------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack
.data
array_size equ 6                              ;assuming array_size to be equal to 6
array db array_size dup(?)          ;reserving bytes = array_size for the array, uninitialised
ip db "Enter terms of the array: ","$"    
;a request to prompt the user for entering terms of the array
op1 db "Second Maximum: ","$"                                    ;output message
op2 db "Second Minimum: ","$"                                     ;output message  
heading db "** SECOND MAX - SECOND MIN **","$"           ;a heading

.code


main proc
            clrscrn                                                                        ;clearing the screen
            mov dx,0105h
            cursor                                                                         ;setting up the cursor
            showmsg heading                                                 ;displaying heading
           
            mov dx,0303h                                                         ;pointing to next line
            cursor                                                                         ;setting up the cursor
            input_array array,array_size,ip                           ;accept array from user
           
            mov dx,0503h                                                         ;advancing to next line
            cursor                                                                                    

           

            mov dh,00h                         ;dh will store first maximum
            mov dl,00h                           ;dl will store second maximum
            mov cx,array_size              ;loading cx with number of times iteration to take place
            mov si,00h                            ;setting up the index to point to the array element
           
outer:             cmp array[si],dl
                        jbe done
                        cmp array[si],dh
                        jae replace
                        mov dl,array[si]
                        jmp done

replace:        mov dl,dh
                        mov dh,array[si]
done:             inc si
                        loop outer
                       
            mov bh,dl                             ;storing 2nd maximum in BH to call 'output' macro
            showmsg op1
            output
                       
            mov dx,0703h                     ;advancing to next line
            cursor                                                                                    

            mov dh,0FFh                       ;dh will store first minimum
            mov dl,0FFh                         ;dl will store second minimum
            mov cx,array_size              ;loading cx with number of times iteration to take place
            mov si,00h                            ;setting up the index to point to the array element
           
outer1:          cmp array[si],dl
                        jae done1
                        cmp array[si],dh
                        jbe replace1
                        mov dl,array[si]
                        jmp done1

replace1:      mov dl,dh
                        mov dh,array[si]
done1:                       inc si
                        loop outer1
                       
            mov bh,dl                                         ;storing 2nd maximum in BH to call 'output' macro
            showmsg op2
            output

            exit
main endp
end main

Linear Search



%OUT-------------------------------------------------------------------------
%OUT- Performs linear Search on an array accepted from user
%out- input: Enter the key to find                                                        
%out- output: Array index(index starts from 00)[if found]     
%out – author: Ayon and Ariyam                                
%OUT-------------------------------------------------------------------------

if1
include macrolib.lby                     ;contains common macros
endif

.model small
.stack
.data
array db 11 dup(?)                        ;array to be sorted
count dw 1 dup(?)                        ;array length
msg db " Enter No. ","$"
i_msg db "How many elements? ","$"
yes db "Data found at array index:","$"
no db "Data not present!","$"
find db " Enter element to search: ","$"
i_p db "Original array: ","$"
.code

main proc
;accept data
            mov ax,@data
            mov ds,ax
            showmsg i_msg
            input
            lea bx,count
            mov [bx],al
            input_array array,count,msg
            showmsg i_p
            mov cx,count
            mov si,0000h

lp:       
            mov bh,array[si]
            output
            space
            inc si
            loop lp
            showmsg find
            input                          ;call input macro, accept the data to search
            mov cx,count
            mov si,00h

lp1:
            cmp al,array[si]
            jz found
            inc si
            loop lp1
;not found
            mov bh,-1h
            space
            showmsg no
            jmp op_display

found:
            push si
            space
            showmsg yes
            pop si
            mov bx,si
            mov bh,bl
           
op_display:
            space
            output                                  ;macro output

            exit
main endp
end main