Assembly Language Program to convert BCD to binary, binary to BCD and multiply two ASCII no’s

 1.Develop and execute an assembly language program to perform the

conversion from BCD to binary


Here's an assembly language program for the EMU8086 compiler that converts a BCD (Binary-Coded Decimal) number to a binary number. The program takes a BCD number in the AL register and converts it to a binary number which is stored in the AX register.


```assembly

; BCD to Binary Conversion Program for EMU8086

; Input: BCD number in AL

; Output: Binary number in AX


ORG 100h  ; Set origin


START:

    MOV AX, 0       ; Clear AX register to store the result

    MOV BL, 10      ; BL will be used to multiply the higher nibble by 10


    ; Isolate the higher nibble (most significant digit)

    MOV BH, AL      ; Copy AL to BH to isolate higher nibble

    SHR BH, 4       ; Shift right 4 bits to get the higher nibble in BH

    MUL BL          ; Multiply the higher nibble by 10 (stored in BL)

    

    ; Add the lower nibble (least significant digit) to the result

    AND AL, 0FH     ; Mask the higher nibble to get the lower nibble in AL

    ADD AX, AX      ; Add the result of the multiplication to AL


    ; AX now contains the binary equivalent of the BCD number in AL

    ; Output the result (for demonstration purposes, not part of conversion)

    MOV DX, AX      ; Move the result to DX for output

    CALL PRINT_NUM  ; Call the print number subroutine


    ; Terminate program

    MOV AH, 4CH

    INT 21H


; Subroutine to print the number in AX

PRINT_NUM:

    PUSH AX         ; Save AX

    PUSH DX         ; Save DX

    MOV CX, 0       ; Clear CX to count digits

    MOV BX, 10      ; Divisor for division by 10


NEXT_DIGIT:

    XOR DX, DX      ; Clear DX for division

    DIV BX          ; Divide AX by 10

    PUSH DX         ; Save remainder (digit)

    INC CX          ; Increment digit count

    TEST AX, AX     ; Check if AX is 0

    JNZ NEXT_DIGIT  ; Repeat if AX is not zero


PRINT_LOOP:

    POP DX          ; Get digit

    ADD DL, '0'     ; Convert to ASCII

    MOV AH, 02H     ; Function to print character

    INT 21H         ; Print character

    LOOP PRINT_LOOP ; Loop to print all digits


    POP DX          ; Restore DX

    POP AX          ; Restore AX

    RET


END START

```



 Explanation:


1. Initialization:

   - `ORG 100h`: This sets the start address of the program to 0100h, which is the typical start address for COM files in DOS.

   - `MOV AX, 0`: Clears the AX register to store the result.


2. Isolate Higher Nibble and Multiply by 10:

   - `MOV BH, AL`: Copy the BCD number in AL to BH.

   - `SHR BH, 4`: Shift right 4 bits to isolate the higher nibble.

   - `MUL BL`: Multiply the higher nibble by 10.


3. Add Lower Nibble:

   - `AND AL, 0FH`: Mask the higher nibble to isolate the lower nibble.

   - `ADD AX, AX`: Add the isolated lower nibble to the result in AX.


4. Output the Result:

   - `MOV DX, AX`: Move the result to DX for output.

   - `CALL PRINT_NUM`: Call the subroutine to print the number.


5. Print Subroutine (`PRINT_NUM`):

   - This subroutine prints the number in AX as decimal characters.

   - `PUSH AX` and `PUSH DX` to save the current state of registers.

   - The number is divided by 10 repeatedly to extract each digit.

   - Digits are stored on the stack and printed in reverse order to display the number correctly.

   - `POP` the digits and print them using `INT 21H` DOS interrupt.


6. Terminate Program:

   - `MOV AH, 4CH` and `INT 21H` to terminate the program and return control to DOS.


 How to Use:


1. Input:Place the BCD number in the AL register.

2. Execution: Run the program.

3. Output: The binary equivalent of the BCD number will be stored in the AX register and printed on the screen.


You can assemble and run this code using the EMU8086 emulator.



2.Write an ALP to convert binary to BCD


To convert a binary number to a BCD (Binary-Coded Decimal) format in assembly language for the EMU8086, we can use the "Double Dabble" algorithm, which is efficient for this purpose. The algorithm involves shifting the binary number left, adding to BCD digits if they are 5 or greater before the next shift, and repeating until all bits have been processed.


Here's an assembly language program for the EMU8086 compiler that converts a binary number in the AX register to a BCD number and stores it in memory:


```assembly

; Binary to BCD Conversion Program for EMU8086

; Input: Binary number in AX

; Output: BCD digits in memory starting at offset 200h


ORG 100h  ; Set origin


START:

    MOV CX, 16       ; We will process 16 bits of the binary number

    MOV BX, 0       ; Clear BX to use as BCD digit container

    MOV DI, 200h    ; Destination index for BCD digits in memory


NEXT_BIT:

    SHL AX, 1       ; Shift left the binary number in AX

    RCL BX, 1       ; Rotate through carry to BX


    ; Adjust BCD digits in BX if any of them are 5 or greater

    MOV DX, BX

    AND DX, 000Fh   ; Isolate least significant nibble

    CMP DX, 5

    JB SKIP1

    ADD BX, 0003h


SKIP1:

    MOV DX, BX

    AND DX, 00F0h   ; Isolate next nibble

    SHR DX, 4

    CMP DX, 5

    JB SKIP2

    ADD BX, 0030h


SKIP2:

    MOV DX, BX

    AND DX, 0F00h   ; Isolate next nibble

    SHR DX, 8

    CMP DX, 5

    JB SKIP3

    ADD BX, 0300h


SKIP3:

    MOV DX, BX

    AND DX, F000h   ; Isolate most significant nibble

    SHR DX, 12

    CMP DX, 5

    JB SKIP4

    ADD BX, 3000h


SKIP4:

    LOOP NEXT_BIT   ; Repeat for all 16 bits


    ; Store BCD digits in memory

    MOV CX, 4       ; We have 4 BCD digits to store

STORE_BCD:

    MOV DL, BL      ; Move the least significant byte of BX to DL

    AND DL, 0Fh     ; Isolate the least significant digit

    ADD DL, '0'     ; Convert to ASCII

    MOV [DI], DL    ; Store in memory

    INC DI          ; Increment memory pointer

    SHR BX, 4       ; Shift right to get the next digit

    LOOP STORE_BCD  ; Repeat for all 4 digits


    ; Terminate program

    MOV AH, 4CH

    INT 21H


END START

```


 Explanation:


1. Initialization:

   - `ORG 100h`: Sets the start address of the program to 0100h.

   - `MOV CX, 16`: We will process 16 bits (a word) from AX.

   - `MOV BX, 0`: BX will store the intermediate BCD result.

   - `MOV DI, 200h`: The memory offset to store the BCD digits.


2. Double Dabble Algorithm:

   - `NEXT_BIT` loop processes each bit of the binary number.

   - `SHL AX, 1` and `RCL BX, 1` shift left AX and rotate through carry into BX.

   - Adjust BCD digits in BX: For each nibble, check if it's 5 or greater and add 3 to it.


3. Store BCD Digits:

   - `MOV CX, 4`: We need to store 4 BCD digits.

   - In the `STORE_BCD` loop, each BCD digit is isolated, converted to ASCII, and stored in memory at DI.

   - `SHR BX, 4` shifts BX right to process the next BCD digit.


4. Terminate Program:

   - `MOV AH, 4CH` and `INT 21H` terminate the program and return control to DOS.


 How to Use:


1. Input: Place the binary number in the AX register.

2. Execution:Run the program.

3. Output: The BCD digits will be stored in memory starting at offset 200h. Each digit is stored as an ASCII character.


This program can be assembled and run using the EMU8086 emulator. The BCD result can be viewed in the memory window starting at address 0200h.


3.Write an ALP to multiply two ASCII no’s

To write an assembly language program that multiplies two ASCII numbers, we need to follow these steps:


1. Convert the ASCII numbers to their binary (integer) equivalents.

2. Perform the multiplication on the integer values.

3. Convert the result back to ASCII for output.


Here's a complete assembly program for the EMU8086 to multiply two ASCII numbers:


```assembly

; Program to multiply two ASCII numbers in EMU8086

; Input: Two ASCII numbers in memory

; Output: Result of multiplication in ASCII


ORG 100h  ; Set origin


START:

    ; Initialize data

    MOV SI, 200h      ; Source index for input ASCII numbers

    MOV DI, 210h      ; Destination index for output ASCII result


    ; Read first ASCII number

    MOV AL, [SI]      ; Load the first ASCII number from memory

    SUB AL, '0'       ; Convert ASCII to binary

    MOV BL, AL        ; Store the first number in BL


    ; Read second ASCII number

    INC SI            ; Move to the next byte in memory

    MOV AL, [SI]      ; Load the second ASCII number from memory

    SUB AL, '0'       ; Convert ASCII to binary

    MOV BH, AL        ; Store the second number in BH


    ; Multiply the numbers

    MOV AL, BL        ; Move first number to AL

    MUL BH            ; Multiply AL by BH, result in AX


    ; Convert binary result to ASCII

    MOV CX, 0         ; Clear CX to count digits

    MOV BX, 10        ; Divisor for division by 10


NEXT_DIGIT:

    XOR DX, DX        ; Clear DX for division

    DIV BX            ; Divide AX by 10

    ADD DL, '0'       ; Convert remainder to ASCII

    PUSH DX           ; Save remainder (digit)

    INC CX            ; Increment digit count

    TEST AX, AX       ; Check if AX is 0

    JNZ NEXT_DIGIT    ; Repeat if AX is not zero


    ; Output the result

PRINT_LOOP:

    POP DX            ; Get digit from stack

    MOV [DI], DL      ; Store ASCII digit in memory

    INC DI            ; Move to the next memory location

    LOOP PRINT_LOOP   ; Loop to print all digits


    ; Terminate program

    MOV AH, 4CH

    INT 21H


END START

```



Explanation:


1. Initialization:

   - `ORG 100h`: Set the start address of the program to 0100h.

   - `MOV SI, 200h`: Set the source index to the location of the input ASCII numbers.

   - `MOV DI, 210h`: Set the destination index to the location where the output will be stored.


2. Convert ASCII to Binary:

   - `MOV AL, [SI]`: Load the first ASCII number from memory.

   - `SUB AL, '0'`: Convert the ASCII character to its integer equivalent.

   - `MOV BL, AL`: Store the first number in BL.

   - `INC SI`: Move to the next byte in memory for the second number.

   - `MOV AL, [SI]`: Load the second ASCII number from memory.

   - `SUB AL, '0'`: Convert the ASCII character to its integer equivalent.

   - `MOV BH, AL`: Store the second number in BH.


3. Multiplication:

   - `MOV AL, BL`: Move the first number to AL.

   - `MUL BH`: Multiply AL by BH; the result is in AX.


4. Convert Binary Result to ASCII:

   - Initialize `CX` to 0 to count digits and `BX` to 10 for division.

   - In `NEXT_DIGIT`, repeatedly divide AX by 10 and push the remainders (digits) onto the stack, converting them to ASCII.

   - The loop continues until AX is 0.


5. Output the Result:

   - In `PRINT_LOOP`, pop each digit from the stack and store it in memory, converting each to an ASCII character.

   - `LOOP PRINT_LOOP` ensures all digits are stored.


6. Terminate Program:

   - `MOV AH, 4CH` and `INT 21H` terminate the program and return control to DOS.


How to Use:


1. Input: Place the two ASCII numbers at memory locations 0200h and 0201h.

2. Execution: Run the program.

3. Output: The ASCII result of the multiplication will be stored starting at memory location 0210h.


You can assemble and run this code using the EMU8086 emulator. Place your ASCII numbers in memory before running the program, and check the output in memory after execution.

Post a Comment

0 Comments