Course Code : BCSL-022

Course Title : Assembly Language Programming Lab

Assignment Number : BCA(II)/L-022/Assignment/2024-25
Maximum Marks : 50
Weightage : 25%
Last Dates for Submission : 31st October, 2024 (For July Session)
30th April, 2025 (For January Session)


This assignment has two questions of total of 40 marks. Rest 10 marks are for viva voce.Please go through the guidelines regarding assignments given in the programme guide for the format of presentation. 


Q1. Design a two bit counter circuit that counts from 0 to 2. It should have states 00, 01 and 10. The initial state of the counter may be assumed to be 00. The counter will be in following successive states: 00, 01, 10, 00, 01, 10, 00, 01, 10, 00 ... Use J-K flip flop to design the circuit.You must design the circuit using state transition diagram and Karnaugh's maps.

Ans:-  To design a two-bit counter that counts from 0 to 2 (states 00, 01, and 10) using J-K flip-flops, we'll follow these steps:

1. **Define the state transitions**
2. **Create the state transition table**
3. **Design the Karnaugh maps for the J and K inputs**
4. **Draw the circuit diagram**

---

1. **State Transition Diagram**

The counter has three states: 

- **State 0:** `00`
- **State 1:** `01`
- **State 2:** `10`

The counter should follow this sequence:
```
00 → 01 → 10 → 00 → 01 → 10 → ...
```

We can represent these states as:

- Present state `Q1 Q0` (Q1 is the most significant bit)

- Next state based on the current state

2. **State Transition Table**

We need a table that shows the current state and the corresponding next state:

| Present State (Q1 Q0) | Next State (Q1' Q0') |
|-----------------------|----------------------|
|         00            |         01           |
|         01            |         10           |
|         10            |         00           |

Now, for each flip-flop, determine what the inputs `J` and `K` should be.

The characteristic equation for a J-K flip-flop is:
- \( Q_{next} = JQ' + K'Q \)

We will calculate the inputs for each flip-flop (for both `Q1` and `Q0`).

3. **Karnaugh Maps**

For Q0 (Least significant bit):

| Present State (Q1 Q0) | Next State for Q0 (Q0') |
|-----------------------|-------------------------|
| 00                    | 1                       |
| 01                    | 0                       |
| 10                    | 1                       |

The K-map for Q0' will help us find J0 and K0:

| Q1\Q0 | 0   | 1   |
|-------|-----|-----|
| 0     | 1   | 0   |
| 1     | 1   | X   |

**From the K-map:**
- \( J_0 = Q1' = Q1 \)
- \( K_0 = 1 \)

For Q1 (Most significant bit):

| Present State (Q1 Q0) | Next State for Q1 (Q1') |
|-----------------------|-------------------------|
| 00                    | 0                       |
| 01                    | 1                       |
| 10                    | 0                       |

The K-map for Q1' will help us find J1 and K1:

| Q1\Q0 | 0   | 1   |
|-------|-----|-----|
| 0     | 0   | 1   |
| 1     | X   | 0   |

**From the K-map:**
- \( J_1 = Q0 \)
- \( K_1 = Q0 \)

4. **Circuit Design Using J-K Flip-Flops**

Now that we have the equations for the flip-flops, the next step is to design the circuit using these values:

- For **Q0**:
  - \( J_0 = 1 \)
  - \( K_0 = 1 \)
 
- For **Q1**:
  - \( J_1 = Q0 \)
  - \( K_1 = Q0 \)

**Final Circuit Diagram:**

1. **Two J-K Flip-Flops**:

   - The first flip-flop (for Q0) has both J0 and K0 tied to logic 1 (constant HIGH).

   - The second flip-flop (for Q1) has both J1 and K1 connected to Q0.

2. **Clock Input**: Both flip-flops will share the same clock signal to toggle simultaneously.

---

This design creates a counter that will cycle through the states 00 → 01 → 10, and then reset to 00, repeatedly. The use of J-K flip-flops ensures correct state transitions based on the input values from the Karnaugh maps.





Q2. Write and run following programs using 8086 assembly language.

(a) Assembly Language Program to Convert Packed 4-Digit BCD to Binary

Ans:- This program converts a packed BCD (Binary Coded Decimal) stored in two consecutive memory locations into its binary equivalent and stores the result in the DX register.

Program Explanation:

- The packed BCD number consists of two digits in each byte.

- We will first unpack the two BCD digits.

- Then, convert the unpacked digits into their binary equivalent.

```asm
; Assuming the BCD is stored in memory location 500h and 501h
section .data
    packed_bcd dw 0x1234     ; Packed BCD stored in two consecutive locations

section .text
    org 100h

start:
    ; Load the packed BCD number into AX
    mov ax, [packed_bcd]

    ; Unpack the digits
    mov cx, 0     ; Initialize CX register to store binary result

    ; Process lower byte (BCD digits 34h)
    mov bl, al     ; Move the lower byte (34h) to BL
    and bl, 0Fh    ; Mask the lower nibble to get the BCD '4'
    mov bh, al     ; Move the lower byte to BH
    shr bh, 4      ; Shift right to get the upper nibble, BCD '3'
    mov ax, 10     ; Multiply the first digit by 10
    mul bh         ; AX = 3 * 10 = 30
    add cx, ax     ; Add it to CX, CX = 30

    ; Add the lower digit (BCD '4')
    add cx, bl     ; CX = 34

    ; Process higher byte (BCD digits 12h)
    mov bl, ah     ; Move the higher byte (12h) to BL
    and bl, 0Fh    ; Mask the lower nibble to get BCD '2'
    mov bh, ah     ; Move the higher byte to BH
    shr bh, 4      ; Shift right to get the upper nibble, BCD '1'
    mov ax, 1000   ; Multiply the first digit by 1000
    mul bh         ; AX = 1 * 1000 = 1000
    add cx, ax     ; Add it to CX, CX = CX + 1000

    ; Add the lower digit (BCD '2') multiplied by 100
    mov ax, 100
    mul bl         ; AX = 2 * 100 = 200
    add cx, ax     ; CX = 1234 (Binary value of 1234)

    ; Store the result in DX register
    mov dx, cx     ; DX = 1234

    ; Exit the program
    mov ah, 4Ch
    int 21h
```

(b) Assembly Language Program for a Near Procedure

Ans :- This program contains a near procedure that checks if the input parameter is less than 5 and displays an appropriate message.

Program Explanation:

- We'll pass the input parameter in a register (e.g., AL).

- If AL < 5, it will print "Parameter value is less than 5", otherwise, it will print "Parameter value is >= 5".

```asm
section .data
    less_msg db 'Parameter value is less than 5', 0Dh, 0Ah, '$'
    greater_msg db 'Parameter value is >= 5', 0Dh, 0Ah, '$'

section .text
    org 100h

start:
    ; Input parameter
    mov al, 4   ; You can change the value for testing

    ; Call the procedure to check the parameter
    call check_parameter

    ; Exit the program
    mov ah, 4Ch
    int 21h

; Near Procedure to Check Parameter
check_parameter:
    cmp al, 5      ; Compare AL with 5
    jl less_than   ; If AL < 5, jump to less_than label
    jmp greater_than  ; Otherwise, jump to greater_than label

less_than:
    ; Print the message for less than 5
    mov ah, 09h
    lea dx, [less_msg]
    int 21h
    ret

greater_than:
    ; Print the message for >= 5
    mov ah, 09h
    lea dx, [greater_msg]
    int 21h
    ret
```


(c) Assembly Language Program to Calculate Factorial

Ans:- This program calculates the factorial of a value stored in the BH register (maximum 8) and stores the result in AX.

Program Explanation:

- The factorial will be calculated using a loop.

- The value is stored in the BH register, and the result will be stored in AX.

```asm
section .text
    org 100h

start:
    ; Initialize BH with the number whose factorial is to be calculated
    mov bh, 5   ; Change the value for testing

    ; Initialize AX with 1 (as factorial starts from 1)
    mov ax, 1

factorial_loop:
    cmp bh, 1          ; Check if BH == 1 (end condition)
    jle end_factorial  ; If BH <= 1, exit the loop

    ; Multiply AX (result) with BH
    mul bh             ; AX = AX * BH

    ; Decrement BH for the next iteration
    dec bh
    jmp factorial_loop ; Repeat the loop

end_factorial:
    ; AX contains the result of the factorial

    ; Exit the program
    mov ah, 4Ch
    int 21h
```

**Summary of Each Program:**

1. **Program (a)** converts a packed 4-digit BCD number into binary and stores the result in the DX register.

2. **Program (b)** is a near procedure that checks if a parameter is less than 5 and displays a corresponding message.

3. **Program (c)** calculates the factorial of a value stored in the BH register using a loop and stores the result in AX.

Make sure you have an assembler such as MASM or TASM to assemble and run these programs.

No comments: