1

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?

John E.
  • 321
  • 4
  • 11
  • 5
    Umm, instead of printing an error message maybe go to `CONTINUE2` so it will be passed through unchanged?Have you written this code yourself? You don't seem to understand how it works... – Jester Dec 21 '17 at 17:58
  • @Jester You are right. I've already asked similar question a week ago [link](https://stackoverflow.com/questions/47706414/assembly-emu8086-reversing-a-string). I am trying to figure out how it works but I just don't understand how assembly works. Basically, I am learning by doing something and failing... Thank you for your time – John E. Dec 21 '17 at 18:00
  • Maybe I don't understand the question. Exactly what input did you use, what output did you expect, and what output did you get instead? – interjay Dec 21 '17 at 18:09
  • @interjay Currently, this program can except an input similar to this: `Hello` and return `OLLEh`. As soon as you enter any symbol, the output will be: `The input character is not a letter`. I want to allow to enter a `space` character so the input can be `Hello World` and an output `DLROw OLLEh` in other words I want to reverse my `string`. – John E. Dec 21 '17 at 18:14
  • @Jester Can you please explain more? – John E. Dec 21 '17 at 19:11
  • 2
    Actually your first loop is storing the input unchanged, you can remove the check from there. Add it into the second loop, where you are doing the case conversion so it skips that step. – Jester Dec 21 '17 at 19:13
  • 1
    @Jester He just copy pasted my code that i have answered to his other question. https://stackoverflow.com/questions/47706414/assembly-emu8086-reversing-a-string/47739462#47739462 – Ahtisham Dec 23 '17 at 10:50

2 Answers2

2

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
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
1

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.

Anton S.
  • 969
  • 1
  • 11
  • 29