assembly 如何在8086汇编中将字符串转换为数字?

fjaof16o  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(130)

我必须建立一个基地转换器在8086大会。
用户必须选择他的基数,然后输入一个数字,然后,程序将显示他的数字在3个基地[他带来了一个十进制数,在此之后,他将看到他的数字在十六进制,oct和bin。
第一个问题是,我怎么把他给我的数字,从字符串,转换成一个数字?
证交会的问题是,我如何转换?通过RCR,然后adc一些变量?
下面是我的代码:

data segment

  N=8

       ERROR_STRING_BASE DB ,10,13, "               THIS IS NOT A BASE!",10,13, "               TRY AGINE" ,10,13," $"     
        OPENSTRING DB "                      Welcome, to the Base    Convertor",10,13,"                     Please enter your base to convert     from:",10,13,"                   <'H'= Hex, 'D'=Dec, 'O'=oct, 'B'=bin>: $"

  Hex_string DB "(H)" ,10,13, "$"
  Octalic_string DB "(O) ",10,13, "$" 
  Binar_string DB "(B)",10,13, "$"
  Dece_string DB "(D)",10,13, "$"

  ENTER_STRING DB ,10,13, "      Now, Enter Your Number (Up to 4 digits) ",10,13, "$"
     Illegal_Number DB ,10,13, "      !!!  This number is illegal, lets Start     again" ,10,13,"$"

  BASED_BUFFER  DB N,?,N+1  DUP(0)  
  Number_buffer  db N, ? ,N+1 DUP(0)

  TheBase DB N DUP(0)                           
  The_numer DB N DUP(0)
  The_binNumber DB 16 DUP(0)
  data ends

   sseg segment stack
   dw   128  dup(0)
   sseg ends

    code segment
    assume ss:sseg,cs:code,ds:data

    start:  mov ax,data
    mov ds,ax

      MOV DX,OFFSET OPENSTRING ;PUTS THE OPENING SRTING 
      MOV AH,9
      INT 21H

    call EnterBase 

     CALL  CheckBase



     HEXBASE: CALL PRINTtheNUMBER 
     MOV DX,OFFSET Hex_string
     MOV AH,9
     INT 21h 
     JMP I_have_the_numberH 

     oCTALICbASE: CALL PRINTtheNUMBER 
     MOV DX,OFFSET Octalic_string 
     MOV AH,9
     INT 21h  
     JMP I_have_the_numberO          

     BINBASE:CALL PRINTtheNUMBER 
     MOV DX,OFFSET Binar_string
     MOV AH,9
     INT 21h  
     JMP I_have_the_numberB

     DECBASE: CALL PRINTtheNUMBER   
     MOV DX,OFFSET Dece_string
     MOV AH,9
     INT 21h 
     JMP I_have_the_numberD



    I_have_the_numberH: CALL BINcalculation
                CALL OCTcalculation
                CALL DECcalculation 



    I_have_the_numberO: CALL BINcalculation
                CALL DECcalculation
                CALL HEXcalculation

    I_have_the_numberB: CALL OCTcalculation
                CALL DECcalculation
                CALL HEXcalculation

    I_have_the_numberD: CALL BINcalculation
                CALL OCTcalculation
                CALL HEXcalculation

     exit:  mov ax, 4c00h
      int 21h  


     EnterBase PROC

     MOV DX,OFFSET BASED_BUFFER  ; GETS THE BASE 
     MOV AH,10
     INT 21H 

     LEA DX,BASED_BUFFER[2]
     MOV BL,BASED_BUFFER[1]
     MOV BH,0
     MOV BASED_BUFFER[BX+2],0

     LEA SI, BASED_BUFFER[2]
     XOR CX, CX
     MOV CL, BASED_BUFFER[1]

     LEA DI, TheBase

     LOL_OF_BASE:   MOV DL, [SI]
           MOV [DI], DL
           INC SI
           INC DI
           INC AL

        RET

       EnterBase  ENDP   


       CheckBase proc


       CMP  TheBase,'H' 
       JE HEXBASE

       CMP  TheBase,'h'
         JE HEXBASE

       CMP TheBase,'O'
        JE oCTALICbASE

       CMP TheBase,'o'
       JE oCTALICbASE

        CMP TheBase,'B'
         JE BINBASE 

         CMP TheBase,'b'
       JE BINBASE

        CMP TheBase,'D'
       JE DECBASE

        CMP TheBase,'d'
        JE DECBASE
        CMP TheBase, ' ' 
        je ERRORoFBASE 

      ERRORoFBASE: MOV DX,OFFSET  ERROR_STRING_BASE ;PUTS WORNG BASE Illegal_Number 
      MOV AH,9
      INT 21H 
      JMP START      


    CheckBase  ENDP



   PRINTtheNUMBER  PROC 

    MOV DX,OFFSET ENTER_STRING
     MOV AH,9
     INT 21h 

    MOV DX,OFFSET Number_buffer  ; GETS THE number
    MOV AH,10
    INT 21H 

     LEA DX,Number_buffer[2]
     MOV BL,Number_buffer[1]
     MOV BH,0
     MOV Number_buffer[BX+2],0

     LEA SI, Number_buffer[2]
     XOR CX, CX
     MOV CL, Number_buffer[1]

     LEA DI, The_numer 
     xor AL,AL

     LOL_OF_NUMBER_CHECK:   MOV DL, [SI]
                   MOV [DI], DL
                   INC SI
                   INC DI
                   INC AL 
                   CMP AL,5 
                   JE ERRORofNUMBER 
                   LOOP LOL_OF_NUMBER_CHECK 

    RET 

      ERRORofNUMBER: MOV DX,OFFSET  Illegal_Number ;PUTS WORNG BASE         Illegal_Number 
      MOV AH,9
      INT 21H 
      JMP START        

     PRINTtheNUMBER ENDP





      PROC BINcalculation  
          XOR CX,CX
          XOR AX,AX
          MOV CX,4
          MOV AX,16
          LEA SI, The_binNumber[0]
       TheBinarLoop: RCL  The_numer,1
          ADC [SI],0
          INC SI
          LOOP TheBinarLoop

      ENDP

      PROC OCTcalculation


      ENDP

      PROC DECcalculation

      ENDP

      PROC  HEXcalculation

      ENDP

     code  ends

     end start

字符串
它应该看起来像这样:x1c 0d1x
谢谢!2谢谢!
toggle

ulydmbyx

ulydmbyx1#

将ASCII字符串从ANY基解码为整数的算法是相同的:

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

字符串
for { bin,oct,dec } value(digit)is ascii(digit)-ascii('0 ')
十六进制有点复杂,你必须检查值是否为'a'-'f',并将其转换为10-15
将整数转换为ascii(以x为基数)也是类似的,你必须将该值除以基数,直到它为0,然后在左边添加表示余数的ascii

e.g. 87/8= 10, remainder 7 --> "7"
     10/8=  1, remainder 2 --> "27"
      1/8=  0, remainder 1 --> "127"

lqfhib0f

lqfhib0f2#

在EMU8086中复制粘贴下一个小程序并运行它:它将从键盘捕获一个数字作为字符串,然后在BX中将其转换为数字。要将数字存储在“The_numer”中,您必须执行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

字符串
您需要的proc是string2number。请注意过程中:它使用了一个名为“string”的变量,你必须用你自己的变量名来改变它。在call之后,结果在BX中:如果数字小于256,则可以使用BL中的数字。
顺便说一下,字符串总是转换为十进制数。

相关问题