简体   繁体   English

汇编x86倒转句子程序

[英]Assembly x86 reversing sentence program

I am trying to make a program where the user have to enter an input, for example: Hello World and get an output: 'DLROw OLLEh'. 我正在尝试创建一个用户必须输入输入的程序,例如: Hello World并获得输出:'DLROw OLLEh'。 Here is my program 这是我的计划

 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

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. 我可以输入任何输入但是只要我输入任何符号,我收到一条消息,这是一个符号,然后程序结束,而我想获得相同的输出,但仍允许输入空格字符。 I am really new in Assembly and moreover while I was trying to figure everything out I got even more lost. 我在大会上真的很新,而且在我试图解决所有问题时我失去了更多。

If I comment out JB NotALetter and JA NotALetter , my space character becomes @ probably because I am adding 20 to the ASCII hex number. 如果我注释掉JB NotALetterJA NotALetter ,我的空格字符变为@可能是因为我在ASCII hex数中添加了20。 Can someone please help to figure out this problem? 有人可以帮忙找出这个问题吗?

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. 我可以输入任何输入但是只要我输入任何符号,我收到一条消息,这是一个符号,然后程序结束,而我想获得相同的输出,但仍允许输入空格字符。

As OP wants to capture the space without messing with symbol message. 因为OP希望捕获空间而不会弄乱符号消息。 This can be achieved with the following: 这可以通过以下方式实现:

In the READ label after you compare for enter key add this: 在比较输入键后的READ标签中添加以下内容:

CMP AL, ' '                  ; compare for space  
JZ CONTINUE1

And in the ANS label after you pop bx add this: 弹出bx后在ANS标签中添加:

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

Just add an extra comparison to your toLower method as follows: 只需为您的toLower方法添加一个额外的比较,如下所示:

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

Complete code: 完整代码:

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

Input Hello World , Output DLROw OLLEh 输入Hello World ,输出DLROw OLLEh

Also, you don't really need NotALetter method as you can notice, I just commented out. 另外,你真的不需要NotALetter方法,因为你可以注意到,我只是注释掉了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM