Decimal to Binary Conversion ASM 3 digits

筅森魡賤 提交于 2020-06-01 05:22:30

问题


I was given a task to convert decimal input numbers and output its binary equivalent via an "array" using assembly. I've the code complete and working, trouble is it only accepts numbers 0-99 for conversion.

Ideally, the program should be able to convert any number at least up to 255 decimal so that the code can be reusable for future practices where I might need to handle different 16 bit values within registers.

I appreciate all advice given, the code is as follows:

.model small
.stack
.data
                             ;Variables used:

   cad  db 9 dup (' '),'$'   ;Cad will contain the chain of bits
   var1 db ?                 ;Var1 will be used to conver number
   num  db ?                 ;variable for input number
   aux  db ?                 ;auxiliary  variable

   msg db 10,13, "Enter decimal number 0-99: $", 10, 13

.code
.startup

   mov ah,9
   lea dx,msg
   int 21h      ;Shows first message

   mov var1,0   ;Initializes var1 value to 0
   mov ah,01h   ;Int to obtain input
   int 21h      
   sub al,30h   ; Ascii code value to real decimal value conversion (subtracts 48d)
   mov num,al   ;Input number from AL is moved to variable num

   mov al,num   
   mov bl,10    ;10 is stored in bl
   mul bl       ;Number to convert is multiplied by 10
   mov aux,al   ;aux variable is assigned the result

   mov var1,0   ;We obtain the second user number input
   mov ah,01h
   int 21h      
   sub al,30h   


   add aux,al   ;We add aux to the previous number multiplied by 10
   mov bl,aux   ;Doesn't need to be multiplied
   mov num,bl   ;result is stored in BL

   mov ah,02h   ;Prints '=' sign symbol after decimal input 
   mov dl,'='
   int 21h

   mov SI,6     ;Cycles where we use long division (Divide by 2)
   L1:          ;L1  label

      xor Ah,Ah    ;Resets AH 
      mov Al,num
      mov Bl,2
      div Bl
      mov var1,Ah
      mov num,Al

      mov dl,var1
      add dl,30h

      mov cad[SI],dl;Concatenates results

      cmp num,1 ;Compares num with 1
      dec SI
      jne L1   ;L1 loops until it goes through the all numbers
       je exit   ;exits loop



      cmp num,0 ;Compares num with 0
       jne L1
        je exit

   exit:          ;exit label

      mov dl,num    ;prints the chain in binary
      add dl,30h

      mov cad[SI],dl

      mov ah,09h
      lea Dx,cad
      int 21h

      mov ah,4ch
      int 21h
      end  

来源:https://stackoverflow.com/questions/52547515/decimal-to-binary-conversion-asm-3-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!