汇编x86倒车语句程序

Assembly x86 reversing sentence program

我正在尝试制作一个用户必须输入的程序,例如:Hello World 并获得输出:'DLROw OLLEh'。这是我的程序

 org 100h
 include emu8086.inc

.DATA
   STR1 DB 0DH, 0AH, 'Input: $'
   STR2 DB 0DH, 0AH, 'Output: $'
   Nl DB 0Dh, 0Ah,'$' 


.CODE
START:
    MOV AX, @DATA
    MOV DS, AX


DISP:
    LEA DX,STR1
    MOV AH,09H
    INT 21H
    MOV CL,00
    MOV AH,01H


READ:
    INT 21H
    MOV BL, AL
    PUSH BX
    INC CX
    CMP AL, 0DH
    JZ DISPLAY
    CMP AL, 'A'                 ; < then A  
    JB  NotALetter
    CMP AL, 'Z'                 ; > then Z 
    JA  AGAIN                   ; repeat again
    JMP CONTINUE1


AGAIN:  
    CMP AL, 'a'                 ; < then a
    JB  NotALetter  
    CMP AL, 'z'                 ; > then z 
    JA  NotALetter       


CONTINUE1:
    JMP READ


DISPLAY:
    LEA DX, STR2
    MOV AH, 09h
    INT 21H
    LEA DX, NL
    MOV AH, 09h
    INT 21h
    POP BX                      ; pop enter key


ANS:
    MOV AH, 02h
    POP BX                      ; pop the character 
    CMP BL, 'a'                 ; check if its in upper case
    JB  toLower                 ; if yes then jmp to toLower 
    SUB BL, 32                  ; if not in upper case then convert to upper case
    JMP CONTINUE2


toLower:
    ADD BL, 32                  ; convert to lower case
    CMP BL, 20h
    ;SUB BL, 32


CONTINUE2:
    MOV DL, BL
    INT 21H
    LOOP ANS  
    JMP EXIT                    ; if everything is fine jmp to exit                 


NotALetter:        
    printn
    print "The input character is not a letter."    


EXIT:
    hlt 


.EXIT
END  START

我可以输入任何输入,但只要我输入任何符号,我就会收到一条消息,说这是一个符号,然后程序结束,而我想获得相同的输出,但仍然允许输入 space 字符。我真的是 Assembly 的新手,而且当我试图弄清楚所有事情时,我更加迷茫了。

如果我注释掉 JB NotALetterJA NotALetter,我的 space 字符变成 @ 可能是因为我在 ASCII hex 数字上加了 20。有人可以帮忙解决这个问题吗?

只需向您的 toLower 方法添加一个额外的比较,如下所示:

toLower: 
    CMP BL, 'A'
    JL CONTINUE2
    ADD BL, 32                  ; convert to lower case

完整代码:

org 100h
include emu8086.inc


.DATA
  STR1 DB 0DH, 0AH, 'Input: $'
  STR2 DB 0DH, 0AH, 'Output: $'
  Nl DB 0Dh, 0Ah,'$' 


.CODE
START:
    MOV AX, @DATA
    MOV DS, AX


DISP:
    LEA DX,STR1
    MOV AH,09H
    INT 21H
    MOV CL,00
    MOV AH,01H


READ:
    INT 21H
    MOV BL, AL
    PUSH BX
    INC CX
    CMP AL, 0DH
    JZ DISPLAY
    CMP AL, 'A'                 ; < then A  
    JB  CONTINUE1
    CMP AL, 'Z'                 ; > then Z 
    JA  AGAIN                   ; repeat again
    JMP CONTINUE1


AGAIN:  
    CMP AL, 'a'                 ; < then a
    JB  CONTINUE1  
    CMP AL, 'z'                 ; > then z 
    JA  CONTINUE1       


CONTINUE1:
    JMP READ


DISPLAY:
    LEA DX, STR2
    MOV AH, 09h
    INT 21H
    LEA DX, NL
    MOV AH, 09h
    INT 21h
    POP BX                      ; pop enter key


ANS:
    MOV AH, 02h
    POP BX                      ; pop the character 
    CMP BL, 'a'                 ; check if its in upper case
    JB  toLower                 ; if yes then jmp to toLower 
    SUB BL, 32                  ; if not in upper case then convert to upper case
    JMP CONTINUE2


toLower: 
    CMP BL, 'A'
    JL CONTINUE2
    ADD BL, 32                  ; convert to lower case


CONTINUE2:
    MOV DL, BL
    INT 21H
    LOOP ANS  
    JMP EXIT                    ; if everything is fine jmp to exit                 


;NotALetter:       
;    printn
;    print "The input character is not a letter."    


EXIT:
    hlt 


.EXIT
END  START

输入Hello World, 输出DLROw OLLEh

此外,您并不真的需要 NotALetter 方法,您会注意到,我只是注释掉了。

I can enter any input but as soon as I enter any symbol, I am getting a message that this is a symbol, then program ends whereas I want to get the same output but still allow to enter a space character.

因为 OP 想要捕获 space 而不会弄乱符号消息。 这可以通过以下方式实现:

READ 标签中比较输入键后添加:

CMP AL, ' '                  ; compare for space  
JZ CONTINUE1

然后在弹出 bx 后的 ANS 标签中添加:

CMP BL, ' '                 ; if equal to space
JZ  CONTINUE2               ; then print it by going to CONTINUE2 label