0

我必须在 8086 程序集中构建一个基本转换器。

用户必须选择他的基数,然后输入一个数字,然后,程序将在 3 个基数中显示他的数字[他带来一个十进制数字,之后他将看到他的十六进制、八进制和 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

它应该是这样的:在此处输入图像描述

谢谢!

שלולוי

4

2 回答 2

3

将 ascii 字符串从任何基数解码为整数的算法是相同的:

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 有点复杂,你必须检查值是否是 'a'-'f',然后转换到 10-15

将整数转换为 ascii(base x) 类似,您必须将值除以基数直到它为 0,然后在左侧添加余数的 ascii 表示

e.g. 87/8= 10, remainder 7 --> "7"
     10/8=  1, remainder 2 --> "27"
      1/8=  0, remainder 1 --> "127"
于 2016-05-02T12:07:17.780 回答
0

在 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    

您需要的过程是string2number. 注意 proc 内部:它使用了一个名为“string”的变量,你必须用你自己的变量名来改变它。结果在 BX后call:如果数字小于 256,则可以使用 BL 中的数字。

顺便说一句,字符串总是转换为十进制数字。

于 2016-05-02T14:13:49.917 回答