Duplicate Question
The question on this page has been marked as a duplicate question.
Original Question
Write an assembly program that: - Load AL by an arbitrary number, - Check whether it is an odd/even number, - If it is an odd n...Asked by Anonymous
Write an 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.
- 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.
Answers
There are no human answers yet.
Answered by
Bot
Assuming we are using an ASCII code representation for the numbers to be shown in port 0, here's an example program:
```
; The arbitrary number is loaded in AL by the BIOS or other program.
start:
; Check whether AL is odd or even.
mov bl, 2 ; BL = 2 (the divisor for even/odd check)
xor ah, ah ; Clear AH to prepare for division
div bl ; Divide AL by BL, remainder is in AH
; Check the remainder: if it's zero, AL is even; otherwise, it's odd.
cmp ah, 0
je even
; AL is odd - convert to next larger even number.
add al, 1
odd:
; Convert the number to ASCII and show it in port 0.
add al, 30h ; Convert to ASCII digit
mov ah, 1
mov cx, 1
int 21h ; Use DOS interrupt for serial port output
jmp end
even:
; Convert the number to previous smaller odd number.
sub al, 1
; Skip output if AL is negative (no odd number below 1).
cmp al, 0
jl end
; Convert the number to ASCII and show it in port 0.
add al, 30h ; Convert to ASCII digit
mov ah, 1
mov cx, 1
int 21h ; Use DOS interrupt for serial port output
end:
; End of program.
ret
```
```
; The arbitrary number is loaded in AL by the BIOS or other program.
start:
; Check whether AL is odd or even.
mov bl, 2 ; BL = 2 (the divisor for even/odd check)
xor ah, ah ; Clear AH to prepare for division
div bl ; Divide AL by BL, remainder is in AH
; Check the remainder: if it's zero, AL is even; otherwise, it's odd.
cmp ah, 0
je even
; AL is odd - convert to next larger even number.
add al, 1
odd:
; Convert the number to ASCII and show it in port 0.
add al, 30h ; Convert to ASCII digit
mov ah, 1
mov cx, 1
int 21h ; Use DOS interrupt for serial port output
jmp end
even:
; Convert the number to previous smaller odd number.
sub al, 1
; Skip output if AL is negative (no odd number below 1).
cmp al, 0
jl end
; Convert the number to ASCII and show it in port 0.
add al, 30h ; Convert to ASCII digit
mov ah, 1
mov cx, 1
int 21h ; Use DOS interrupt for serial port output
end:
; End of program.
ret
```
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.