Assuming that the input number is provided in the AX register, the following program can perform the given task:
; Initialize data segment
MOV AX, @data
MOV DS, AX
; Load AL by input number
MOV AL, AH
; Check odd/even
AND AL, 00000001B ; check the least significant bit
JZ is_even ; if it is zero, the number is even
; Convert odd to even
INC AL ; add 1 to make it even
JMP show_result
is_even:
; Convert even to odd
DEC AL ; subtract 1 to make it odd
show_result:
; Output the result to port 0
MOV DX, 0
MOV AH, 02h ; select output mode for DX
OUT DX, AL ; send the result to port 0
; Terminate program
MOV AH, 4Ch
INT 21h
The program starts by initializing the data segment to the location where the code and data segments are stored in memory. Then, it loads the input number from the AX register into the AL register.
Next, the program checks whether the number is odd or even by ANDing the least significant bit (LSB) of AL with a bit mask that has all bits set to 0 except the LSB. If the result of the AND operation is zero, the number is even and the program branches to the is_even label. Otherwise, it proceeds to the next instruction, which converts the odd number to the next larger even number by incrementing it.
If the number is already even, the program jumps to the show_result label directly after the LSB check and subtracts 1 from AL to make it the previous smaller odd number.
In either case, the program proceeds to the show_result label, where it sets the DX register to 0 to select port 0 and uses the OUT instruction to send the result in AL to that port.
Finally, the program terminates by setting the AH register to 4Ch (the exit function code) and calling interrupt 21h to exit to the operating system.
3) Write a 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.
1 answer