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:
0 Comments