x86 - Even and odd numbers in assembly language -


i have code in assembly language, , show me if number odd or even. want if number show opposite of number, , if odd show number/2. can me ? thanks. sorry bad english

.model small .stack 100h .data msg db 10,13,'enter number=$' msg1 db 10,13,'number even$' msg2 db 10,13,'number 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 

if odd show number/2

after mov bl,al characters <= '9' doing sar bl,1 (shr make more sense me, unless coming java, , think normal have byte values signed).

so character '7' (value 37h) produce 1bh in bl, technically non-printable character.

if want print input '7' "3.5" on output, can add 24 calculate whole part digit in ascii (1bh + 24 = 33h = '3'), , output single char, output '.' , '5' ascii characters (as every odd_number/2 have ".5" decimal part).

if wonder why magic number +24 produce half number... because have on input ascii character '7', not number 7. value of digit glyphs in ascii defined 48 + digit_value, in case 48+7 = 55 = 37h.

so calculate 7/2 in "human way", '7' input 1 do:

x = input - '0'   ; x = '7' - '0' = 7 (conversion numeric value) x /= 2            ; x = 7/2 = 3 output = x + '0'  ; converting ascii displaying                   ; output = 3+48 = 51 = 33h = '3' 

now written single formula is:
output = (input - 48)/2 + 48 <=>
output = input/2 - 48/2 + 48 <=>
output = input/2 - 24 + 48 <=>
output = input/2 + 24 <=>

as @ label odd: have in bl input/2 value, instead of restoring original (adc bl,bl right after odd:), , doing -48 /2 +48 operation 1 one, can +24 result directly.

this power of understanding have on input, want on output, , doing simple math reasoning between, on high level, without touching cpu instruction yet.

after know want calculate, write cpu instructions it, in case whopping complex add bl,24 , char-output int 21h i'm lazy add, doing 1 upper/lower case conversion.


i'm somehow afraid not answer did expect, maybe think i'm sort of trolling ... maybe partly correct... again, if understand did , why, should resolve question easily.


unfortunately have no idea "opposite" number, can imagine many things under it, few reasonable display.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -