简体   繁体   中英

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'. 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. 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. This can be achieved with the following:

In the READ label after you compare for enter key add this:

CMP AL, ' '                  ; compare for space  
JZ CONTINUE1

And in the ANS label after you pop bx add this:

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: 
    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

Also, you don't really need NotALetter method as you can notice, I just commented out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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