if is odd to show the number/2
After mov bl,al
for characters <= '9' you are doing sar bl,1
(shr
would make more sense to me, unless you are coming from Java, and you think that it is normal to have byte values signed).
So for character '7'
(value 37h
) you will produce 1Bh
in bl
, which is technically non-printable character.
If you want to print for input '7'
"3.5"
on output, you can add 24
to calculate the whole part digit in ASCII (1Bh + 24 = 33h
= '3'), and then do
output that as single char, then output '.'
and '5'
ASCII characters (as every odd_number/2 will have ".5"
decimal part).
If you wonder why magic number +24
does produce the half number... because you have on input ASCII character '7'
, not number 7
. Value of digit glyphs in ASCII are defined as 48 + digit_value
, in this case 48+7 = 55 = 37h
.
So to calculate 7/2 in "human way", from '7'
input one would do:
x = input - '0' ; x = '7' - '0' = 7 (conversion to numeric value)
x /= 2 ; x = 7/2 = 3
output = x + '0' ; converting it back to ASCII for displaying
; output = 3+48 = 51 = 33h = '3'
Now written as single formula that is:
output = (input - 48)/2 + 48 <=>
output = input/2 - 48/2 + 48 <=>
output = input/2 - 24 + 48 <=>
output = input/2 + 24 <=>
As at label odd:
you already have in bl
the input/2
value, instead of restoring the original (adc bl,bl
right after odd:
), and doing the -48 /2 +48
operation one by one, you can simply do the +24
to get the result directly.
This is the power of understanding what you have on input, what you want on output, and doing simple math reasoning between, all on the high level, without even touching any CPU instruction yet.
After you know what you want to calculate, you write the CPU instructions to do it, in my case the whopping complex add bl,24
and then char-output with int 21h
which I'm too lazy to add, as you are already doing that one with upper/lower case conversion.
I'm somehow afraid this is not the answer you did expect, maybe you will think I'm sort of trolling you ... maybe you will be partly correct... Then again, if you will fully understand what I did and why, you should resolve your question easily.
Unfortunately I have no idea what is "opposite" number, I can imagine many things under it, but only few would be reasonable to display.