3) Write a simple 8086 assembly program that:
- Load AL by an arbitrary number,
- Check whether it is an odd/even number,
- If it is an odd number, it will be converted to its next larger even number and will be shown in port 0 in the trainer kit.
- If it is an even number, it will be converted to its previous smaller odd number and will be shown in port 0 in the trainer kit.
Here is a possible solution:
.MODEL SMALL
.STACK 100h
.DATA
number DB 17 ; the arbitrary number to work with
.CODE
MOV AX, @DATA ; initialize DS
MOV DS, AX
MOV AL, number ; load AL by the arbitrary number
AND AL, 01h ; check the least significant bit (LSB)
JZ even ; if it is zero, AL is even
; if it is one, AL is odd
ADD AL, 1 ; convert AL to the next larger even number
JMP show_result
even:
SUB AL, 1 ; convert AL to the previous smaller odd number
show_result:
MOV AH, 0 ; clear AH (not needed, but good practice)
MOV DX, 0 ; prepare DX for the OUT instruction
MOV DX, 8 ; select port 0 in the trainer kit (use your own value)
MOV BL, AL ; move the result to a safe place (optional)
OUT DX, AL ; send the result to port 0
MOV AH, 4Ch ; return control to DOS
INT 21h
END
Here is a step-by-step breakdown of the assembly program:
Step 1: Load AL with an arbitrary number
- Use the MOV instruction to load an arbitrary number into AL
Step 2: Check whether AL is odd or even
- Use the TEST instruction to check the least significant bit (LSB) of AL
- If the LSB is set (1), then AL is odd. If the LSB is not set (0), then AL is even.
Step 3: Convert odd number to the next larger even number
- If AL is odd, use the ADD instruction to add 1 to AL, converting it to the next larger even number.
Step 4: Convert even number to the previous smaller odd number
- If AL is even, use the SUB instruction to subtract 1 from AL, converting it to the previous smaller odd number.
Step 5: Show the final number in Port 0 of the trainer kit
- Write the value in AL to Port 0 to display it on the trainer kit.
Here's the assembly code implementing the above steps:
```
.MODEL small
.stack 100h
.DATA
port0_message DB "The final number is: $"
.CODE
.STARTUP
MOV AL, 13 ; Step 1: Load AL with an arbitrary number
TEST AL, 1 ; Step 2: Check whether AL is odd or even
JNZ odd_number ; Jump to odd_number label if AL is odd
even_number: ; AL is even
SUB AL, 1 ; Step 4: Convert even number to the previous smaller odd number
JMP show_result
odd_number: ; AL is odd
ADD AL, 1 ; Step 3: Convert odd number to the next larger even number
show_result:
MOV AH, 0
MOV DX, OFFSET port0_message
MOV AH, 9
INT 21h ; Display the message in Port 0
MOV AH, 2 ; Display the value in AL in Port 0
MOV DL, AL
INT 21h
.EXIT
END
```