Assembly language program for addition, substraction,divition,multiplication

Write an ALP to add 2 hexadesimal number   for emu 8086


To add two hexadecimal numbers using the 8086 microprocessor in an assembly language program, we need to handle the arithmetic carefully, particularly for multibyte numbers due to the limited size of the registers. Here's a straightforward example to add two 2-digit hexadecimal numbers stored in memory locations and display the result.


In this example, we'll assume the two hexadecimal numbers are 2-digit (1 byte each) and are stored in memory at known locations. We'll add them and store the result in another memory location.


```assembly

; Program to add two 2-digit hexadecimal numbers in 8086 assembly language

; Assume the numbers are stored in memory as follows:

; Number 1: 2000h

; Number 2: 2001h

; Result:  2002h


org 100h


.data

num1 db 0x1A  ; First hexadecimal number (26 decimal)

num2 db 0x2F  ; Second hexadecimal number (47 decimal)

result db 0   ; To store the result


.code

start:

    ; Initialize the Data Segment register

    mov ax, seg num1

    mov ds, ax


    ; Load the first number

    mov al, [num1]


    ; Add the second number

    add al, [num2]


    ; Store the result

    mov [result], al


    ; Terminate the program

    mov ah, 4Ch

    int 21h


end start

```




 Explanation:

1. Initialization:

   - Set the `DS` register to point to the segment containing the numbers.


2. Addition:

   - Load the first number into `AL`.

   - Add the second number to `AL`.

   - Store the result in the designated memory location.


3. Program Termination:

   - Use DOS interrupt `INT 21h` with `AH = 4Ch` to terminate the program.


Note:

- This example assumes the hexadecimal numbers are 1 byte each. If you need to handle multibyte numbers (e.g., 2 bytes or more), you'll need to handle the addition with carry for each byte, similar to the previous example provided for multibyte addition.

- The numbers and the result are stored in the data segment. Adjust the memory locations as needed for your specific use case.





Write an ALP to add 2 Multibyte no.


In Assembly Language Programming (ALP) for the 8086 microprocessor, adding two multibyte numbers involves a few steps due to the limited size of the registers and the need to handle carries between the bytes. Here's an example program to add two 4-byte (32-bit) numbers stored in memory.


```assembly

; Program to add two 4-byte (32-bit) numbers in 8086 assembly language

; Assume the numbers are stored in memory as follows:

; Number 1: 4000h - 4003h

; Number 2: 4004h - 4007h

; Result:  4008h - 400Bh


org 100h


.data

num1 dw 1234h, 5678h  ; First number (lower word, higher word)

num2 dw 9ABCh, DEF0h  ; Second number (lower word, higher word)


.code

start:

    ; Load DS with segment address of the data segment

    mov ax, seg num1

    mov ds, ax


    ; Initialize pointers to the numbers

    lea si, num1      ; Source index to the first number

    lea di, num2      ; Destination index to the second number

    lea bx, 4008h     ; BX points to the result location


    ; Clear the carry flag

    clc


    ; Add lower words (first 2 bytes)

    mov ax, [si]      ; AX = lower word of num1

    adc ax, [di]      ; AX = AX + lower word of num2 + CF

    mov [bx], ax      ; Store the result lower word

    add si, 2

    add di, 2

    add bx, 2


    ; Add higher words (next 2 bytes)

    mov ax, [si]      ; AX = higher word of num1

    adc ax, [di]      ; AX = AX + higher word of num2 + CF

    mov [bx], ax      ; Store the result higher word


    ; Terminate program

    mov ah, 4Ch

    int 21h


end start

```


 Explanation:

1. Initialization:

   - Set the `DS` register to point to the data segment containing the numbers.

   - Initialize `SI` and `DI` to point to the memory locations of the two numbers.

   - Initialize `BX` to point to the memory location where the result will be stored.


2. Addition with Carry:

   - Clear the carry flag using `CLC`.

   - Load the lower word (first 2 bytes) of the first number into `AX`.

   - Use `ADC` (Add with Carry) to add the lower word of the second number to `AX` and include any carry from the previous addition.

   - Store the result in the designated memory location.

   - Move the pointers to the next part of the numbers (higher words).

   - Repeat the addition for the higher words.


3. Program Termination:

   - Use DOS interrupt `INT 21h` with `AH = 4Ch` to terminate the program.


 Note:

- The numbers are stored in little-endian format, meaning the least significant byte comes first.

- The program assumes the numbers and the result memory location are within the same segment.

- This program only adds two 4-byte numbers, but it can be extended for larger numbers by adding more segments and handling carries accordingly.



Write an ALP to subtract two Multibyte numbers


Below is an example of an Assembly Language Program (ALP) for the 8086 microprocessor to subtract two hexadecimal numbers, each 2 bytes (16 bits) in size. The program will assume the numbers are stored in memory and will store the result in another memory location.


 Subtracting Two 2-Byte Hexadecimal Numbers

; Program to subtract two 2-byte (16-bit) hexadecimal numbers in 8086 assembly language

; Assume the numbers are stored in memory as follows:

; Number 1: 2000h, 2001h (stored as word)

; Number 2: 2002h, 2003h (stored as word)

; Result:  2004h, 2005h (stored as word)

.model small

 .stack 100h


 .data

num1 dw 1234h  ; First hexadecimal number (lower byte, higher byte)

num2 dw 0ABCh  ; Second hexadecimal number (lower byte, higher byte)

result dw 0    ; To store the result


 .code

start:

    ; Load DS with segment address of the data segment

    mov ax, seg num1

    mov ds, ax


    ; Subtract the numbers

    mov ax, [num1]     ; Load first number into AX

    sub ax, [num2]     ; Subtract second number from AX

    mov [result], ax   ; Store the result in memory


    ; Terminate program

    mov ah, 4Ch

    int 21h


end start






 Explanation:


1. Initialization:

   - Set the `DS` register to point to the data segment containing the numbers.


2. Subtraction:

   - Load the first number (2 bytes) into `AX`.

   - Subtract the second number from `AX` using the `SUB` instruction.

   - Store the result back into the memory location designated for the result.


3. Program Termination:

   - Use DOS interrupt `INT 21h` with `AH = 4Ch` to terminate the program.


Note:

- This example assumes the numbers are 2 bytes (16 bits) each. For single-byte numbers, you can modify the program accordingly by using the `AL` register instead of `AX`.

- The program uses a simple subtraction without considering borrow handling since it's just a 2-byte subtraction. For larger numbers or multiple bytes, similar logic to handling carries/borrows as shown in the multibyte examples should be applied.

- Adjust the memory locations as needed for your specific use case.


Write an ALP to multiply two 16-bit numbers

Multiplying two 16-bit numbers using the 8086 microprocessor involves utilizing the `MUL` instruction, which is designed for unsigned multiplication. Here’s an Assembly Language Program (ALP) that multiplies two 16-bit numbers stored in memory and stores the 32-bit result in memory.


 Example: Multiplying Two 16-bit Numbers




 Explanation:

1. Initialization:

   - Set the `DS` register to point to the data segment containing the numbers.


2. Multiplication:

   - Load the first 16-bit number into `AX`.

   - Load the second 16-bit number into `BX`.

   - Use the `MUL` instruction to multiply the contents of `AX` by `BX`. The result will be stored in the `DX:AX` pair (`DX` holds the upper 16 bits and `AX` holds the lower 16 bits).


3. Storing the Result:

   - Store the lower 16 bits of the result from `AX` into the memory location for the result.

   - Store the upper 16 bits of the result from `DX` into the subsequent memory location for the result.


4. Program Termination:

   - Use DOS interrupt `INT 21h` with `AH = 4Ch` to terminate the program.


 Notes:

- The `MUL` instruction performs unsigned multiplication. If you need to perform signed multiplication, you can use the `IMUL` instruction.

- Ensure that the memory locations for the numbers and the result are correctly defined and that they don't overlap to avoid any data corruption.

- Adjust the memory addresses and values as needed for your specific use case.



program:

   ; Program to multiply two 16-bit numbers in 8086 assembly language

; Assume the numbers are stored in memory as follows:

; Number 1: 2000h (16-bit)

; Number 2: 2002h (16-bit)

; Result:  2004h (32-bit result, lower 16-bit) and 2006h (upper 16-bit)

 .model small

.stack 100h


 .data

num1 dw 1234h  ; First 16-bit number

num2 dw 5678h  ; Second 16-bit number

result dw 0, 0 ; To store the 32-bit result


.code

start:

    ; Load DS with the segment address of the data segment

    mov ax, seg num1

    mov ds, ax


    ; Load the first number into AX

    mov ax, [num1]

    

    ; Multiply by the second number (num2)

    ; The result will be in DX:AX (DX = high word, AX = low word)

    mov bx, [num2]

    mul bx


    ; Store the result

    mov [result], ax  ; Store the lower 16 bits of the result

    mov [result+2], dx  ; Store the upper 16 bits of the result


    ; Terminate the program

    mov ah, 4Ch

    int 21h


end start


Output:



Write an ALP to divide two numbers

Dividing two numbers using the 8086 microprocessor involves using the `DIV` instruction for unsigned division or the `IDIV` instruction for signed division. Below is an Assembly Language Program (ALP) that divides two 16-bit unsigned numbers stored in memory and stores the quotient and remainder in memory.

 Example: Dividing Two 16-bit Unsigned Numbers

; Program to divide two 16-bit unsigned numbers in 8086 assembly language
; Assume the numbers are stored in memory as follows:
; Dividend: 2000h (16-bit)
; Divisor:  2002h (16-bit)
; Quotient: 2004h (16-bit)
; Remainder: 2006h (16-bit)
  .model small
.stack 100h

.data
dividend dw 1234h   ; 16-bit dividend
divisor  dw 0010h   ; 16-bit divisor
quotient dw 0       ; To store the 16-bit quotient
remainder dw 0      ; To store the 16-bit remainder

 .code
start:
    ; Load DS with the segment address of the data segment
    mov ax, seg dividend
    mov ds, ax

    ; Load the dividend into AX
    mov ax, [dividend]
    
    ; Load the divisor into BX
    mov bx, [divisor]
    
    ; Clear DX before division (DX:AX is the 32-bit dividend)
    xor dx, dx

    ; Perform the division (AX / BX)
    div bx

    ; Store the quotient in memory
    mov [quotient], ax

    ; Store the remainder in memory
    mov [remainder], dx

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

end start




 Explanation:
1. Initialization:
   - Set the `DS` register to point to the segment containing the numbers.

2. Division:
   - Load the 16-bit dividend into `AX`.
   - Load the 16-bit divisor into `BX`.
   - Clear `DX` by setting it to 0 because `DX:AX` is used as the 32-bit dividend for the division operation.
   - Use the `DIV` instruction to divide `AX` by `BX`. The quotient will be stored in `AX`, and the remainder will be stored in `DX`.

3. Storing the Result:
   - Store the quotient from `AX` into the designated memory location.
   - Store the remainder from `DX` into the designated memory location.

4. Program Termination:
   - Use DOS interrupt `INT 21h` with `AH = 4Ch` to terminate the program.

 Notes:
- The `DIV` instruction performs unsigned division. If you need to perform signed division, you can use the `IDIV` instruction.
- Ensure that the divisor is not zero to avoid a division error.
- Adjust the memory addresses and values as needed for your specific use case.





Write an ALP to multiply two ASCII no’s

Below is an Assembly Language Program (ALP) to multiply two ASCII numbers using the 8086 assembly language, which can be run on the EMU8086 emulator. The program reads two ASCII characters, converts them to integers, multiplies them, and then displays the result.

```assembly
.model small
.stack 100h
.data
    num1 db '6'         ; First ASCII number
    num2 db '3'         ; Second ASCII number
    result dw 0         ; Variable to store the result
    msg db 'Result: $'  ; Message to display the result
    res db '00', '$'    ; String to store the ASCII result for display

.code
main:
    ; Initialize data segment
    mov ax, @data
    mov ds, ax

    ; Convert num1 from ASCII to integer
    mov al, num1
    sub al, '0'         ; AL = num1 - '0'

    ; Convert num2 from ASCII to integer
    mov bl, num2
    sub bl, '0'         ; BL = num2 - '0'

    ; Multiply the numbers
    mul bl              ; AX = AL * BL

    ; Store the result
    mov [result], ax

    ; Convert result to ASCII string
    mov ax, [result]
    mov bx, 10          ; Base 10
    xor cx, cx          ; Clear CX

convert_loop:
    xor dx, dx          ; Clear DX
    div bx              ; AX = AX / 10, DX = AX % 10
    add dl, '0'         ; Convert remainder to ASCII
    push dx             ; Push ASCII digit onto stack
    inc cx              ; Increment count of digits
    cmp ax, 0
    jne convert_loop    ; Repeat if quotient is not zero

    ; Pop digits from stack and store in result string
    lea di, res         ; Load address of result string
    add di, cx          ; Move DI to the end of the string
    mov byte ptr [di], '$' ; Null-terminate the string

pop_loop:
    pop dx              ; Pop digit from stack
    dec di
    mov [di], dl        ; Store digit in result string
    loop pop_loop

    ; Display the message and result
    lea dx, msg
    mov ah, 09h
    int 21h

    lea dx, res
    mov ah, 09h
    int 21h

    ; Exit program
    mov ah, 4Ch
    int 21h

end main




```

 Explanation:
1. Data Section:
    - `num1` and `num2` hold the ASCII characters of the numbers to be multiplied.
    - `result` is a 16-bit word to store the multiplication result.
    - `msg` is a message to be displayed before the result.
    - `res` is a string to store the converted result in ASCII format for display.

2. Code Section:
    - `main`: The entry point of the program.
    - Initialize the data segment.
    - Convert the ASCII values to integers by subtracting '0'.
    - Multiply the two integers. The result is stored in the `AX` register.
    - Convert the multiplication result to an ASCII string by repeatedly dividing by 10 and storing the remainders.
    - Display the result by using DOS interrupt 21h, function 09h.

 Running the Program:
1. Copy the code into an EMU8086 code editor.
2. Assemble and run the program.
3. The output will display the message "Result:" followed by the result of the multiplication.

This example handles only single-digit multiplication and conversion to an ASCII string for display. Adjustments would be necessary for handling larger numbers or different requirements.

Post a Comment

0 Comments