Consider the following high-level program:
Uint_8 NUM1 = 10, NUM2 = 0, NUM3;
While (NUM1 != NUM2)
{
NUM1 --;
NUM2 ++;
NUM3 = NUM1 +NUM2;
}
Convert the above code into 8086 assembly code.
MOV AL, 10 ; Initialize NUM1 to 10
MOV AH, 0 ; Initialize NUM2 to 0
WHILE_LOOP:
CMP AL, AH ; Compare NUM1 and NUM2
JE EXIT_LOOP ; If NUM1 is equal to NUM2, exit the loop
DEC AL ; Decrement NUM1 by 1
INC AH ; Increment NUM2 by 1
ADD BL, AL ; Add NUM1 and NUM2 and store the result in NUM3
JMP WHILE_LOOP ; Jump back to WHILE_LOOP
EXIT_LOOP:
; NUM1 equals NUM2, exit the loop
; Your code after the loop ends
To convert the given high-level program into 8086 assembly code, we need to follow these steps:
1. Declare variables: Declare three variables NUM1, NUM2, NUM3 as DB (Byte variables).
2. Initialize variables: Load the value 10 into NUM1, 0 into NUM2, and leave NUM3 uninitialized.
3. Create a loop: Use a loop to repeatedly execute the code until NUM1 is equal to NUM2.
4. Increment NUM2: Increment the value of NUM2 by 1.
5. Decrement NUM1: Decrement the value of NUM1 by 1.
6. Perform addition: Add the values of NUM1 and NUM2 and store the result in NUM3.
7. Terminate the program: Once NUM1 becomes equal to NUM2, exit the loop and terminate the program.
Here's the 8086 assembly code conversion:
```
MOV NUM1, 10 ; Initialize NUM1 to 10
MOV NUM2, 0 ; Initialize NUM2 to 0
; NUM3 is left uninitialized
LOOP_START:
; Check if NUM1 is equal to NUM2
CMP NUM1, NUM2
JE EXIT_LOOP ; If NUM1 == NUM2, exit the loop
; Increment NUM2 by 1
INC NUM2
; Decrement NUM1 by 1
DEC NUM1
; Perform the addition NUM3 = NUM1 + NUM2
ADD NUM1, NUM2
MOV NUM3, NUM1 ; Store the result in NUM3
JMP LOOP_START ; Repeat the loop
EXIT_LOOP:
; The program ends here
; Declare variables
NUM1 DB 0
NUM2 DB 0
NUM3 DB 0
```
Please note that in this assembly conversion, we have assumed NUM1, NUM2, and NUM3 to be of type `DB` (Byte variables). If they are different data types, make the necessary adjustments in the code.