How to convert String to Number in 8086 assembly?

后端 未结 2 1751
独厮守ぢ
独厮守ぢ 2020-12-12 06:52

I have to build a Base Converter in 8086 assembly .

The user has to choose his based and then put a number, after then , the program will show him his number in 3 mo

相关标签:
2条回答
  • 2020-12-12 07:20

    the algorighm to decode ascii strings from ANY base to integer is the same:

    result = 0
    for each digit in ascii-string
       result *= base
       result += value(digit)
    

    for { bin, oct, dec } value(digit) is ascii(digit)-ascii('0')
    hex is a bit more complicated, you have to check if the value is 'a'-'f', and convert this to 10-15

    converting integer to ascii(base x) is similar, you have to divide the value by base until it's 0, and add ascii representation of the remainder at the left

    e.g. 87/8= 10, remainder 7 --> "7"
         10/8=  1, remainder 2 --> "27"
          1/8=  0, remainder 1 --> "127"
    
    0 讨论(0)
  • 2020-12-12 07:33

    Copy-paste next little program in EMU8086 and run it : it will capture a number as string from keyboard, then convert it to numeric in BX. To store the number in "The_numer", you have to do mov The_numer, bl :

    .stack 100h
    ;------------------------------------------
    .data
    ;------------------------------------------
    msj1   db 'Enter a number: $'
    msj2   db 13,10,'Number has been converted',13,10,13,10,'$'
    string db 5 ;MAX NUMBER OF CHARACTERS ALLOWED (4).
           db ? ;NUMBER OF CHARACTERS ENTERED BY USER.
           db 5 dup (?) ;CHARACTERS ENTERED BY USER. 
    ;------------------------------------------
    .code          
    ;INITIALIZE DATA SEGMENT.
      mov  ax, @data
      mov  ds, ax
    ;------------------------------------------        
    ;DISPLAY MESSAGE.
      mov  ah, 9
      mov  dx, offset msj1
      int  21h
    ;------------------------------------------
    ;CAPTURE CHARACTERS (THE NUMBER).
      mov  ah, 0Ah
      mov  dx, offset string
      int  21h
    ;------------------------------------------
      call string2number
    ;------------------------------------------        
    ;DISPLAY MESSAGE.
      mov  ah, 9
      mov  dx, offset msj2
      int  21h
    ;------------------------------------------
    ;STOP UNTIL USER PRESS ANY KEY.
      mov  ah,7
      int  21h
    ;------------------------------------------
    ;FINISH THE PROGRAM PROPERLY.
      mov  ax, 4c00h
      int  21h           
    ;------------------------------------------
    ;CONVERT STRING TO NUMBER IN BX.
    proc string2number         
    ;MAKE SI TO POINT TO THE LEAST SIGNIFICANT DIGIT.
      mov  si, offset string + 1 ;<================================ YOU CHANGE THIS VARIABLE.
      mov  cl, [ si ] ;NUMBER OF CHARACTERS ENTERED.                                         
      mov  ch, 0 ;CLEAR CH, NOW CX==CL.
      add  si, cx ;NOW SI POINTS TO LEAST SIGNIFICANT DIGIT.
    ;CONVERT STRING.
      mov  bx, 0
      mov  bp, 1 ;MULTIPLE OF 10 TO MULTIPLY EVERY DIGIT.
    repeat:         
    ;CONVERT CHARACTER.                    
      mov  al, [ si ] ;CHARACTER TO PROCESS.
      sub  al, 48 ;CONVERT ASCII CHARACTER TO DIGIT.
      mov  ah, 0 ;CLEAR AH, NOW AX==AL.
      mul  bp ;AX*BP = DX:AX.
      add  bx,ax ;ADD RESULT TO BX. 
    ;INCREASE MULTIPLE OF 10 (1, 10, 100...).
      mov  ax, bp
      mov  bp, 10
      mul  bp ;AX*10 = DX:AX.
      mov  bp, ax ;NEW MULTIPLE OF 10.  
    ;CHECK IF WE HAVE FINISHED.
      dec  si ;NEXT DIGIT TO PROCESS.
      loop repeat ;COUNTER CX-1, IF NOT ZERO, REPEAT.
    
      ret 
    endp    
    

    The proc you need is string2number. Pay attention inside the proc : it uses a variable named "string", you have to change it by the name of your own variable. After the call the result is in BX: if the number is less than 256, you can use the number in BL.

    By the way, the string is ALWAYS converted to a DECIMAL number.

    0 讨论(0)
提交回复
热议问题