Search code examples
assemblyencryptionrot13

Rot13 obfuscation in Assembly language doesn't work correctly


I'm trying to do the rot13 in assembly wherein you have to input 10 lowercase letters and outputs it after rot13 encryption. However, it's not working correctly. I don't know how else to fix it because I just started learning assembly recently. Please help me.

.model small
.data
   arr dd 10 dup(?), '$'
.stack 100h
.code
main proc

mov ax, @data
mov ds, ax

mov si, 0

    scan:                       ; gets input from user

        mov ah, 01h                 ; get input
        int 21h 
        mov bx, offset arr
        mov byte ptr [bx+si], al
        add si, 1                       ; increment si <-- array index

    cmp si, 9                   ; compare si to 9
    jle scan                    ; (loop) if si <= 9, jump to scan

mov si, 0

    print:                      ; display the input of user
        mov al, [si]

        cmp al, 'n'
        jb plus

        cmp al, 'n'
        jae minus

        plus:
            mov al, [si]
            add al, 13
            jmp alwaysRun

        minus:
            mov al, [si]
            sub al, 13

        alwaysRun:
            mov dl, al              
            mov ah, 02h                 ; print
            int 21h
            add si, 1                       

    cmp si, 9
    jle print

mov ax, 4c00h
int 21h 

main endp
end main

Solution

  • The main error is mov al, [si]. It is not guaranteed that arr starts at 0 - and actually it doesn't. Change it to [bx+si] (offset of arr plus index) like in the input block.