-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_The_final.asm
825 lines (639 loc) · 19 KB
/
project_The_final.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
data segment
; add your data here!
arr db 40 dup (?)
arr2 db 2 dup(?)
arr3 db 40 dup (?)
arr4 db 40 dup (?)
var1 db "Enter your message: $"
var2 db "Enter cipher-decipher method: $"
; 1= method 1
; 2= method 2
; 3= method 3
var3 db "Enter secret key: $"
var4 db "The secret message is: $"
var5 db "the encrypted text is: $"
pick db ?
decide db "Enter 'C' for Cipher or 'D' for Decipher: $"
choose db "Do you want to decipher this message (Y for yes): $"
;var4 db "Enter message length: $"
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
; add your code here
;load array
;lea dx, var4
;mov ah, 9
;int 21h
;mov ah, 1
;int 21h
;mov ah, 0
;sub ax, 30h ; e.g = '3' of hex is 33h
;mov di, ax ;len of array
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var2
mov ah, 9
int 21h
mov ah, 1
int 21h
mov bl, al
cmp bl, '1'
je vigenere
cmp bl, '2'
je rrs
cmp bl, '3'
je caesar
cmp bl, '4'
je stacks
vigenere:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, decide ; deciding the mode of action
mov ah, 9
int 21h
mov ah,1
int 21h
cmp al, 'C'
je vignere_cipher:
cmp al, 'D'
je vignere_decipher:
vignere_cipher:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var3
mov ah, 9
int 21h
mov cx, 2 ;initializing the secret letter array
mov si, 0
input2:
mov ah, 1 ;input for taking 2 secret keys
int 21h
mov arr2[si], al ;array for two secret keys
inc si
loop input2
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
mov cx,40 ; initialising the message array
mov si,0
lea dx, var1
mov ah, 9
int 21h
input1:
mov ah,1
int 21h
cmp al, '$' ;checking if the last character is $ sign
jne next_step
jmp algo_end ;stopping taking input when $sign is found
next_step:
mov bl, al
mov ax, si
mov bh, 2
div bh ; dividing the si(position) checking if its even or odd
mov arr[si], bl ; storing the message
cmp ah, 0
je even
jmp odd
even: ; the even position letters gets processed
mov dx, si
mov si, 0 ; for not taking 0 as a char.
mov bh, arr2[si]
add bl, bh
mov si, dx
jmp next_phase
odd: ; the odd position letters gets processed
mov dx, si
mov si, 1
mov bh, arr2[si]
add bl, bh
mov si, dx
jmp next_phase
next_phase:
sub bl, 115
mov arr3[si], bl ;the value gets stored in array
add si, 1
mov di,si ;storing the number of input characters
loop input1
algo_end:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var5
mov ah, 9
int 21h
mov cx, 40
mov si, 0
vig_view:
mov dl, arr3[si]
mov ah, 2
int 21h
inc si
loop vig_view
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, choose
mov ah, 9
int 21h
mov cx, di ;initializing for deciphering (the number of input characters)
mov si, 0
mov ah, 1
int 21h
cmp al, 'Y' ; if user wants to see the message to be deciphered
je decipher_text
jmp exit2
decipher_text:
mov bl, arr3[si]
add bl, 115
mov ax, si
mov bh, 2
div bh
cmp ah, 0
je even2 ;checking if the position is even or odd
jmp odd2
even2:
mov dx, si
mov si, 0
mov bh, arr2[si]
sub bl, bh
mov si, dx
jmp vig_next_phase2
odd2:
mov dx, si
mov si, 1
mov bh, arr2[si]
sub bl, bh
mov si, dx
jmp vig_next_phase2
vig_next_phase2:
mov arr4[si], bl
add si, 1
loop decipher_text
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var4
mov ah, 9
int 21h
mov cx, 40
mov si, 0
view2:
mov dl, arr4[si]
mov ah, 2
int 21h
inc si
loop view2
jmp exit2
vignere_decipher: ;only decipher the text when the user
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var3
mov ah, 9
int 21h
mov cx, 2 ;initializing the secret letter array
mov si, 0
decipher_input2:
mov ah, 1 ;input for taking 2 secret keys
int 21h
mov arr2[si], al ;array for two secret keys
inc si
loop decipher_input2
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
mov cx,40 ;initializing the array iteration
mov si,0
lea dx, var1
mov ah, 9
int 21h
decipher_input1: ; taking the cipher text as input
mov ah,1
int 21h
cmp al, '$'
jne next_step2
jmp algo_end2
next_step2:
mov bl, al
add bl, 115
mov ax, si
mov bh, 2
div bh
mov arr[si], bl
cmp ah, 0
je even3
jmp odd3
even3:
mov dx, si
mov si, 0
mov bh, arr2[si] ;accessing the 1st secret character
sub bl, bh
mov si, dx
jmp vig_next_phase3
odd3:
mov dx, si
mov si, 1
mov bh, arr2[si] ;accessing the 2nd secret character
sub bl, bh
mov si, dx
jmp vig_next_phase3
vig_next_phase3:
mov arr3[si], bl
add si, 1
mov di,si
loop decipher_input1
algo_end2:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var5
mov ah, 9
int 21h
mov cx, 40 ;initializing the array iteration
mov si, 0
view3: ;viewing the deciphered text
mov dl, arr3[si]
mov ah, 2
int 21h
inc si
loop view3
jmp exit2
stacks:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, decide ; to cipher or decipher
mov ah, 9
int 21h
mov ah,1
int 21h
cmp al, 'C'
je cipher9
cmp al, 'D'
je decipher9
cipher9:
mov cx, 40 ;random limit
mov si, 0
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var1
mov ah, 9
int 21h
push1:
mov ah, 1 ;taking input
int 21h
cmp al, '$'
je pop1_init ;instruction to go to pop
jmp keep_up ; push the input in stack
keep_up:
push ax
inc si ; forward indexing that will be used as range later
loop push1
pop1_init:
mov bp,1 ; condition that we are doing cipher bp=1
mov cx, si
mov si, 0 ; restart indexing for new loop
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
pop1:
pop bx ;popping value and storing it on bl
mov dl, bl
add dl, 10h ; adding 10h with each val
mov ah, 2
int 21h
mov arr[si], dl ; storing the value in array
inc si
loop pop1
cmp bp, 1 ; condition check
je choice2
jmp exit2
choice2: ; choice for deciphering what we ciphered before
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, choose
mov ah, 9
int 21h
mov ah, 1
int 21h
cmp al, 'Y' ; if user wants to see the message to be deciphered
je decipher9 ; goes to decipher func
jmp exit2
;do_decipher2:
;loop decipher9
;jmp exit2
decipher9:
cmp bp, 1
je ciphered ;if we have the generated a cipher text
jmp not_ciphered ;if not
ciphered:
mov cx, si ;si that we last pushed in keep_up funtion
mov si, 0
loop_push: ; loop for pushing the values that was stored reversely in array
mov ax, 0
mov al, arr[si]
push ax
inc si
loop loop_push
jmp pop2_init
not_ciphered:
mov cx, 40 ;random limit
mov si, 0
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var1
mov ah, 9
int 21h
push2:
mov ah, 1
int 21h
cmp al, '$'
je pop2_init
jmp keep_up2
keep_up2:
push ax
inc si
loop push2
pop2_init:
mov bp,0 ;initializing to its previous state
mov cx, si
mov si, 0
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
pop2:
pop bx ;the popped value present in bl
mov dl, bl
sub dl, 10h
mov ah, 2
int 21h
inc si
loop pop2
jmp exit2
rrs:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, decide
mov ah, 9
int 21h
mov ah,1
int 21h
cmp al, 'C'
je cipher
cmp al, 'D'
je decipher
cipher:
mov cx, 40 ;random limit
mov si, 0
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var1
mov ah, 9
int 21h
jmp next_phase2
next_phase2:
mov ah, 1
int 21h
cmp al, '$'
jne inserting1
jmp formula_part1
inserting1:
mov arr[si], al ; inserting original text in an array to traverse
inc si
loop next_phase2
formula_part1:
mov di, si ; here di is the number of texts typed
mov cx, 40
mov si, 0
formula_loopC:
mov ax, 0
mov al, arr[si]
inc al ; A/a as the first position of respective alphabatical order
sub al, 65 ; since A(65)-A = 0 so
;for multiplication purpose and
;defining A as first letter
;we are subtracting by 65
;the value of v
;(1st, 2nd, 3rd letter... the ASCII serial from "A")
;stored in ax for multiplication
mov bx, 0
mov bx, di ; t = len of array
mul bl ; here, s = vt where, v==al; t==bl
dec al ; since indexing starting
;from 0 we are substracting find the
;actual alphabetical/ascii code
;sequencial position
add al, 65
mov arr3[si], al
inc si ;forward indexing
loop formula_loopC
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
mov cx, di
mov si, 0
mov bp, 1 ; this indicates that we ciphered a text
jmp view
decipher:
mov cx, 40
mov si, 0
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var1
mov ah, 9
int 21h
jmp next_phase3
next_phase3:
mov ah, 1
int 21h
cmp al, '$'
jne inserting2
jmp formula_part2
inserting2:
mov arr[si], al ; inserting original text in an array to traverse
inc si ;forward indexing
loop next_phase3
formula_part2:
mov di, si
mov cx, 40
mov si, 0
formula_loopD:
mov al, arr[si]
inc al
sub al, 65
mov ah, 0 ; cuz we have ah=1 cuz of input instruction
mov bx, 0
mov bx, di
div bl ; v = s/t
dec al ; to find the right ASCII val
add al, 65
mov arr3[si], al ;storing the value of v (the original text)
inc si
loop formula_loopD
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
mov cx, di ; the correct message length
mov si, 0
mov bp, 0
jmp view
view:
mov dl, arr3[si]
mov ah, 2
int 21h
inc si
loop view
cmp bp, 1 ; checking if we ciphered first
je choice
jmp exit2
choice:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, choose
mov ah, 9
int 21h
mov ah, 1
int 21h
cmp al, 'Y' ; if user wants to see the message to be deciphered
je do_decipher
jmp exit2
do_decipher:
mov cx, di ;last pushed in formula_part1
mov si, 0
mov ah, 0
decipher_loop:
mov al, arr3[si] ;previously stored (an alt for deciphering if inputs cant be typed on emulator)
mov arr[si], al
inc si
loop decipher_loop
jmp formula_part2
caesar:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
; Get user's choice (Cipher or Decipher)
lea dx, decide ; Load the address of the 'decide' string into DX
mov ah, 9
int 21h
mov ah, 1
int 21h
mov pick, al ; Store the user's choice in the 'pick' variable
; load array
mov cx, 40 ; Set CX to 5 (number of characters to read)
mov si, 0 ; Set SI to 0 (index for the array)
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
lea dx, var1 ; Load the address of the 'var1' string into DX
mov ah, 9
int 21h
L1:
mov ah, 1
int 21h
cmp al, '$'
jne next
jmp output
next:
mov bl, al
; Shift the character based on user's choice
cmp pick, 'C' ; Compare the user's choice with 'C' (Cipher)
je cipher2 ; Jump to 'cipher' if the user chose Cipher
cmp pick, 'D' ; Compare the user's choice with 'D' (Decipher)
je decipher2 ; Jump to 'decipher' if the user chose Decipher
cipher2:
add bl, 3 ; Shift the character by 3 bits forward for Cipher
jmp store_character ; Jump to 'store_character' to store the modified character
decipher2:
sub bl, 3 ; Shift the character by 3 bits backward for Decipher
jmp store_character ; Jump to 'store_character' to store the modified character
store_character:
mov arr[si], bl ; Store the modified character in the array
inc si ; Increment SI to move to the next position in the array
mov di, si
loop L1 ; Repeat the loop until CX becomes zero
; Output a newline
output:
mov ah, 2
mov dl, 0Dh
int 21h
mov dl, 0Ah
int 21h
; Output the modified string
mov cx, di ; Set CX to 40 (number of characters to print)
mov si, 0 ; Set SI to 0 (index for the array)
L2:
mov dl, arr[si]
mov ah, 2
int 21h
inc si ; Increment SI to move to the next position in the array
loop L2 ; Repeat the loop until CX becomes zero
exit2:
mov ax, 4c00h ; exit to operating system.
int 21h
ends
end start ; set entry point and stop the assembler.