;----------------- Solution to Project 5 --------------------
; Author: Aneesh Akella
; Last Modified Date: 12/2/2021
; Summary: This program decrypt and encrypt a message (input) with a key using Vigenere Cipher, the results will be stored in output.

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
    input byte "JPUESZSBAPANNHTXRTLBVLL"  
    key byte "ABCXYZ"	
    options BYTE 0                          ; Variable to determine what procedure to execute. 1 for Encryption, otherwise for Decryption
    d byte 26                               ; variable to hold the devisor that will be used for division ( mod )
    output byte lengthof input dup(0)

.code
    main proc
        cmp options, 1                      ; compare value of options to 1
        je En                               ; if value equal to 1 go to encryption rotten
        jne De                              ; if not go to Decryption
        De:
            call Decrypt
            jmp MOVEON                      ; after returning from Decrypt procedure jump to "moveon" so we don't call Decrypt accidentally
        En:
            call Encrypt
            jmp MOVEON                      ; There is no need use jump but just in case is. 
		
        MOVEON:
            mov eax, 0
            ; OPTIONAL: Print Output to console: uncomment the next 2 lines to activate (Require irvine32.inc)
            ; lea EDX, output
             CALL writestring
            invoke ExitProcess, 0
    main endp
	
    Decrypt proc
        mov esi, 0                          ; index of both input and output
        mov edi, 0                          ; index of the key
        mov ecx, lengthof input             ; (1) loop L (LENGTHOF input) times

        L:
            mov eax, 0                      ; clear EAX
            mov al, input[esi]              ; move cypher character ASCII value into al
            sub al, 65                      ; get the "order" of the character ( A=0, B=1, C=2, ... , Z=25)
            mov bl, key[edi]                ; (2) move the key ASCII character into bl
            sub bl, 65                      ; (3) get the "order" of the character ( A=0, B=1, C=2, ... , Z=25)
            sub al, bl                      ; (4) subtract the indices to "decrypt" the character
            add al, 26                      ; add 26 in case subtraction result is a negative value
            div d                           ; (5) use divide to get "mod 26". AX will be divided, quotient in AL and reminder in AH. 
            add ah, 65                      ; (6) add 65 to convert reminder back from order to the proper ASCII value
            mov output[esi], ah             ; write ASCII value to output
            inc esi			
            inc edi
            cmp edi, lengthof key 
            jne Next
            mov edi, 0                      ; (7)if key index reached the end, reset the key index
        Next:
            loop L
            ret
    Decrypt endp

    Encrypt proc
        mov esi, 0
        mov edi, 0
        mov ecx, lengthof input             ; loop L (LENGTHOF input) times
	
        L:
            mov eax, 0
            mov al, input[esi]              ; move the input character into al
            sub al, 65                      ; get the order of the character ( A=0, B=1, C=2, ... , Z=25)
            mov bl, key[edi]                ; move the key character into bl
            sub bl, 65                      ; get the "index" of the character ( A=0, B=1, C=2, ... , Z=25)
            add al, bl                      ; (8) add the indices to "encrypt" the character
            div d                           ; divide so we can "mod 26"
            add ah, 65                      ; add 65 to convert from order back to the proper ASCII value
            mov output[esi], ah             ; write reminder into the output
            inc esi
            inc edi
            cmp edi, lengthof key           ; if we have reached the end of the key ...
            jne Next
            mov edi, 0                      ; ...reset the key index
        Next:
            loop L
            ret
    Encrypt endp
end main
 

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret