-4

I have this code in assembly language, and it only show me if a number is odd or even. I also want if a number is even to show the opposite of the number, and if is odd to show the number/2. Can you help me with this ? Thanks. Sorry for my bad english

.model small
.stack 100h
.data
msg db 10,13,'Enter number=$'
msg1 db 10,13,'Number is even$'
msg2 db 10,13,'Number is odd$'
msg3 db 10,13, 'Case Convertion=$'






.code

   mov ax,@data
   mov ds,ax
   lea dx,msg
   mov ah,9
   int 21h

   mov ah,1
   int 21h


   mov bl,al

   cmp bl,'9'
   ja cc 


   sar bl,1  
   jc odd


   lea dx,msg1
   mov ah,9
   int 21h


   jmp exit

   odd:

   lea dx,msg2
   mov ah,9
   int 21h


   jmp exit



   cc:  

   lea dx,msg3
     mov ah,9
     int 21h  

   cmp bl,'A'
   jnle next



   next:

    cmp bl,'Z'
    jnge con

    jmp lower




   con:

     add bl,32d
     mov dl,bl
     mov ah,2
     int 21h

     jmp exit 


   lower:

      cmp bl,'a'
      jnle ln



    ln:
       cmp bl,'z'
       jnge conl



   conl:
      sub bl,32d
     mov dl,bl
     mov ah,2
     int 21h


     jmp exit 




   exit:
    .exit
    end
Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
user7435782
  • 1
  • 1
  • 1
  • 1

1 Answers1

1

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.

Ped7g
  • 16,236
  • 3
  • 26
  • 63