emu8086汇编输入输出有三个字符

时间:2016-07-12 14:31:43

标签: assembly emu8086

大家好的一天,我是这种语言的新手,这里修改的代码应该接受输入ABC并返回ABC,但它返回ABB。我用AH,BH和Ax试了一下,Bx它变得更糟。我该如何修改它(现在我们的老师希望我们坚持使用MOV,INT,LEA以及这里的代码)。感谢提示和答案

.MODEL SMALL
    .STACK 100H
    .CODE
     MOV AH, 01H ; Character input with echo
     INT 21H ; Character in AL
     MOV BL, AL ; Save in BL    

     MOV AH, 01H ; Character input with echo
     INT 21H ; Character in AL
     MOV CL, AL ; Save in CL

     MOV AH, 01H ; Character input with echo
     INT 21H ; Character in AL
     MOV DL, AL ; Save in DL

     MOV AH, 02H ; Display character function    

     MOV DL, 0DH ; carriage return
     INT 21H

     MOV DL, 0AH ; line feed
     INT 21H  

     MOV DL, BL ; Get character stored in BL and display
     INT 21H   
     MOV DL, CL ; Get character stored in CL and display
     INT 21H  
     MOV DL, DL ; Get character stored in DL and display
     INT 21H  

     MOV AH, 4CH
     INT 21H
     END

2 个答案:

答案 0 :(得分:1)

在第14行,您将第三个字符保存在DL中,但稍后,通过将ODH和OAH分配给DL,您将丢失此值。解决方案很简单:将第三个字符存储在另一个寄存器而不是DL中,例如CH,在底部显示CH而不是DL(箭头< ==============指向变化):

.MODEL SMALL
    .STACK 100H
    .CODE
     MOV AH, 01H ; Character input with echo
     INT 21H ; Character in AL
     MOV BL, AL ; Save in BL    

     MOV AH, 01H ; Character input with echo
     INT 21H ; Character in AL
     MOV CL, AL ; Save in CL

     MOV AH, 01H ; Character input with echo
     INT 21H ; Character in AL
;    MOV DL, AL ; Save in DL
     MOV CH, AL ; <============================

     MOV AH, 02H ; Display character function    

     MOV DL, 0DH ; carriage return
     INT 21H

     MOV DL, 0AH ; line feed
     INT 21H  

     MOV DL, BL ; Get character stored in BL and display
     INT 21H   
     MOV DL, CL ; Get character stored in BL and display
     INT 21H  
;    MOV DL, DL ; Get character stored in BL and display
     MOV DL, CH ; <============================
     INT 21H  

     MOV AH, 4CH
     INT 21H
     END

我选择了CH,因为它是一个未在代码中使用的寄存器。

答案 1 :(得分:-1)

有一种更有效的方法

主要过程

MOV BH,0
MOV BL,10D


   INPUT:
   MOV AH,1
   INT 21H
   CMP AL,13D
   JNE NUMBER 

   JMP EXIT


   NUMBER:
   SUB AL,30H
   MOV CL,AL
   MOV AL,BH
   MUL BL
   ADD AL,CL
   MOV BH,AL

   JMP INPUT

EXIT:

   AND AX,0
   MOV AL,BH
   MOV CL,10D

   MOV BX,0000H    

   STORE:
   DIV CL
   MOV [0000H+BX],AH
   ADD BX,2H
   MOV AH,0
   CMP AL,0
   JNE STORE

   MOV AH,2
   MOV  DL,0DH
   INT 21H
   MOV DL,0AH
   INT 21H

   PRINT: 
   SUB BX,2H
   MOV DL,[0000H+BX]
   ADD DL,30H
   INT 21H
   CMP BX,0
   JNE PRINT



MAIN ENDP

END MAIN