Advertisement
Guest User

Untitled

a guest
May 24th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. include 'emu8086.inc'
  2.  
  3. org 100h
  4.  
  5. jmp     start   ; Skip data.
  6.  
  7. Buffer  db 81 dup(00h)            
  8. Buffer2 db 81 dup(00h)
  9. msg1 db 0ah,0dh, 'choose 1-encrypt 2-decrypt: $',0ah,0dh
  10. msg2 db 0ah,0dh, 'Enter your text: $'  
  11. msg3 db 0ah,0dh, 'text after encryption is: $'
  12. msg4 db 0ah,0dh, 'text after decryption is: $'
  13. msg5 db 0ah,0dh, 'Enter your number: $'
  14.  
  15. ;                       'abcdefghijklmnopqrstuvwxyz'
  16. table1 db 97 dup (' '),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
  17. table2 db ' ','abcdefghijklmnopqrstuvwxyz'
  18.  
  19. start:
  20. ; Print first message: choose what to do
  21.     mov  DX,offset msg1
  22.     mov  AH,09h ;DOS function: Write string to STDOUT (output string)
  23.     int  21h
  24.  
  25. ; take user's  choise
  26.     mov  AH,01h ;DOS function: Read character from STDIN
  27.     int  21h
  28.  
  29.     CMP     AL,'1'
  30.     JZ      encrypt
  31.  
  32.     CMP     AL,'2'
  33.     JZ      decrypt
  34.  
  35. encrypt:
  36. ; Print second message
  37.     mov  DX,offset msg2
  38.     mov  AH,09h ;DOS function: Write string to STDOUT (output string)
  39.     int  21h
  40.  
  41. ;Take string from user and store it in buffer                              
  42.  
  43. ;Tell service 0Ah that we want to allow up to 80 chars in our string.Done by putting 81 in the
  44. ;first byte of the buffer.(80 chars plus the <Enter> key).
  45.     mov  BX,offset Buffer
  46.     mov  byte [BX],81
  47.    
  48.     mov  DX,bx
  49.     mov  AH,0Ah ;DOS function: Buffered input (input string)  input of a string to DS:DX, fist byte is buffer size, second byte is
  50.                 ;number of chars actually read.
  51.     int  21h
  52.    
  53.     mov  AL,[BX+1]   ;Copy entered data length to AL
  54.     add  AL,02       ;offsets are zero based and [bx+1] is the second position in the buffer
  55.                      ;the string doesn't start at the beginning of our buffer.
  56.                      ;It starts at offset 02h or at the third position.So we need to add 2 to
  57.                      ;the length to get to the end of the string.
  58.    
  59.     mov  AH,00h      ;can not put an 8-bit register into a 16 bit register,so we must clear out AH first
  60.     mov  SI,AX       ;Then we need another index register to hold this value.
  61.     mov  byte [BX+SI],'$' ; Now we can put the '$' at this position.String in assembly is dollar-terminated string
  62. ;end of string entering
  63.  
  64.  
  65. ;encrypt
  66.     lea BX, table1    ;encrypt using table1
  67.     lea SI, Buffer+2
  68.     call encryption      
  69.  
  70.  
  71. ; print third message
  72.     mov dx, offset msg3
  73.     mov ah, 9
  74.     int 21h
  75.  
  76.  
  77. ; show result:
  78.     mov si, 2
  79.     mov cl, [Buffer+1]
  80.     mov ch,0
  81.     lea bx, Buffer
  82. again:
  83.     mov al, [bx+si]
  84.     mov ah,0
  85.     CALL   PRINT_NUM_UNS
  86.     inc si
  87.     loop again
  88.     jmp start
  89.  
  90.  
  91. decrypt:
  92. ; Print second message
  93.     mov  DX,offset msg5
  94.     mov  AH,09h ;DOS function: Write string to STDOUT (output string)
  95.     int  21h
  96.  
  97. ;Tell service 0Ah that we want to allow up to 80 chars in our string.Done by putting 81 in the
  98. ;first byte of the buffer.(80 chars plus the <Enter> key).
  99.     mov  BX,offset Buffer
  100.     mov  byte [BX],81
  101. ;Take string from user and store it in buffer                    
  102.     mov  DX,bx          
  103.     mov  AH,0Ah ;DOS function: Buffered input (input string)
  104.     int  21h
  105.    
  106.     mov  AL,[BX+1]   ;Copy entered datae length to AL
  107.     add  AL,02       ;offsets are zero based and [bx+1] is the second position in the buffer
  108.                      ;the string doesn't start at the beginning of our buffer.
  109.                      ;It starts at offset 02h or at the third position.So we need to add 2 to
  110.                      ;the length to get to the end of the string.
  111.    
  112.     mov  AH,00h      ;can not put an 8-bit register into a 16 bit register,so we must clear out AH first
  113.     mov  SI,AX       ;Then we need another index register to hold this value.
  114.     mov  byte [BX+SI],'$' ; Now we can put the '$' at this position.String in assembly is dollar-terminated string
  115. ;end of string entering
  116.  
  117.     mov cl,[BX+1]
  118.     mov ch,0
  119.     mov si,2
  120.     lea DX,Buffer2
  121.     mov di,2
  122. redo:
  123.     CLC
  124.     mov al,[BX+SI]
  125.     sub al,30h
  126.     mov ah,10
  127.     mul ah
  128.     inc si
  129.     mov ah,[BX+SI]
  130.     sub ah,30h  
  131.     add ah,al
  132.     XCHG BX,DX
  133.     mov [BX+DI],ah
  134.     XCHG BX,DX
  135.     inc si
  136.     inc di
  137.     cmp [BX+si], '$'
  138.     jnz redo
  139.    
  140.     XCHG BX,DX
  141.     mov  byte [BX+DI],'$' ; Now we can put the '$' at this position.String in assembly is dollar-terminated string
  142. ;end of string entering
  143.  
  144.  
  145. ;decrypt
  146.     lea BX, table2
  147.     lea SI, Buffer2+2
  148.     call decryption  
  149.  
  150. ; print third message
  151.     mov dx, offset msg4
  152.     mov ah, 9
  153.     int 21h
  154.  
  155. ; show result:
  156. mov  DX,offset Buffer2+2
  157. mov  AH,09h ;DOS function: Write string to STDOUT (output string)
  158. int  21h
  159.  
  160.  
  161. jmp start
  162.  
  163.  
  164. ; wait for any key...
  165. exit:
  166. mov ah, 0
  167. int 16h
  168.  
  169.  
  170.        
  171.        
  172.            
  173. ;mov  DX,offset buffer+2
  174. ;mov  AH,09h
  175. ;int  21h
  176.          
  177.            
  178.  
  179. ret
  180.  
  181. encryption proc near
  182.  
  183. next_char:
  184.     cmp [si], '$'      ; end of string?
  185.     je end_of_string
  186.    
  187.     mov al, [si]
  188.     cmp al, 'a'
  189.     jb  skip
  190.     cmp al, 'z'
  191.     ja  skip     
  192.     xlatb     ; encrypt using table2.  
  193.     mov [si], al
  194. skip:
  195.     inc si 
  196.     jmp next_char
  197.  
  198. end_of_string:
  199.  
  200.  
  201. ret
  202. encryption endp
  203.  
  204. ;DEFINE_PRINT_NUM
  205.  
  206. ; this procedure prints out an unsigned
  207. ; number in AX (not just a single digit)
  208. ; allowed values are from 0 to 65535 (FFFF)
  209. PRINT_NUM_UNS   PROC    NEAR
  210.         PUSH    AX
  211.         PUSH    BX
  212.         PUSH    CX
  213.         PUSH    DX
  214.  
  215.         ; flag to prevent printing zeros before number:
  216.         MOV     CX, 1
  217.  
  218.         ; (result of "/ 10000" is always less or equal to 9).
  219.         MOV     BX, 10000       ; 2710h - divider.
  220.  
  221.         ; AX is zero?
  222.         CMP     AX, 0
  223.         JZ      print_zero ,
  224.        
  225.         CMP     AX,20h
  226.         JZ      print_space
  227.        
  228.         CMP     AX,10
  229.         JB      print_zero
  230.  
  231.        
  232.         ;CMP     AX,9
  233. ;        Ja      begin_print
  234.         jmp     begin_print
  235.        
  236.         print_zero:
  237.         PUTC    '0'
  238.        
  239.         jmp     begin_print    
  240.            
  241.        ; CMP     AX,9
  242. ;        JB      begin_print
  243.        
  244. print_space:
  245.         PUTC    ' '
  246.         Jmp      end_print
  247.                        
  248. begin_print:    
  249.        
  250.         ; check divider (if zero go to end_print):
  251.         CMP     BX,0
  252.         JZ      end_print
  253.  
  254.         ; avoid printing zeros before number:
  255.         CMP     CX, 0
  256.         JE      calc
  257.         ; if AX<BX then result of DIV will be zero:
  258.         CMP     AX, BX
  259.         JB      skip2
  260. calc:
  261.         MOV     CX, 0   ; set flag.
  262.  
  263.         MOV     DX, 0
  264.         DIV     BX      ; AX = DX:AX / BX   (DX=remainder).
  265.  
  266.         ; print last digit
  267.         ; AH is always ZERO, so it's ignored
  268.         ADD     AL, 30h    ; convert to ASCII code.
  269.         PUTC    AL
  270.  
  271.  
  272.         MOV     AX, DX  ; get remainder from last div.
  273.  
  274. skip2:
  275.         ; calculate BX=BX/10
  276.         PUSH    AX
  277.         MOV     DX, 0
  278.         MOV     AX, BX
  279.         DIV     CS:ten  ; AX = DX:AX / 10   (DX=remainder).
  280.         MOV     BX, AX
  281.         POP     AX
  282.  
  283.         JMP     begin_print
  284.        
  285.      
  286.  
  287. end_print:
  288.  
  289.         POP     DX
  290.         POP     CX
  291.         POP     BX
  292.         POP     AX
  293.         RET
  294. PRINT_NUM_UNS   ENDP
  295.  
  296.  
  297.  
  298. decryption proc near
  299.  
  300. next_num:
  301.     cmp [si] , '$'    ; end of string?
  302.     je end_of_string_1
  303.  
  304.     mov al, [si]
  305.     cmp al, 1
  306.     jb  skip_1
  307.     cmp al, 26
  308.     ja  skip_1  
  309.     ; xlat algorithm: al = ds:[bx + unsigned al]
  310.     xlatb     ; encrypt using table2.  
  311.     mov [si], al
  312.     ;mov ah,0
  313. skip_1:
  314.     inc si
  315.     jmp next_num
  316.  
  317. end_of_string_1:
  318.  
  319.  
  320. ret
  321. decryption endp
  322.  
  323.  
  324.  
  325.  
  326. ten             DW      10      ; used as multiplier/divider by PRINT_NUM_UNS.
  327.  
  328.  
  329. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement