Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include 'emu8086.inc'
- org 100h
- jmp start ; Skip data.
- Buffer db 81 dup(00h)
- Buffer2 db 81 dup(00h)
- msg1 db 0ah,0dh, 'choose 1-encrypt 2-decrypt: $',0ah,0dh
- msg2 db 0ah,0dh, 'Enter your text: $'
- msg3 db 0ah,0dh, 'text after encryption is: $'
- msg4 db 0ah,0dh, 'text after decryption is: $'
- msg5 db 0ah,0dh, 'Enter your number: $'
- ; 'abcdefghijklmnopqrstuvwxyz'
- 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
- table2 db ' ','abcdefghijklmnopqrstuvwxyz'
- start:
- ; Print first message: choose what to do
- mov DX,offset msg1
- mov AH,09h ;DOS function: Write string to STDOUT (output string)
- int 21h
- ; take user's choise
- mov AH,01h ;DOS function: Read character from STDIN
- int 21h
- CMP AL,'1'
- JZ encrypt
- CMP AL,'2'
- JZ decrypt
- encrypt:
- ; Print second message
- mov DX,offset msg2
- mov AH,09h ;DOS function: Write string to STDOUT (output string)
- int 21h
- ;Take string from user and store it in buffer
- ;Tell service 0Ah that we want to allow up to 80 chars in our string.Done by putting 81 in the
- ;first byte of the buffer.(80 chars plus the <Enter> key).
- mov BX,offset Buffer
- mov byte [BX],81
- mov DX,bx
- mov AH,0Ah ;DOS function: Buffered input (input string) input of a string to DS:DX, fist byte is buffer size, second byte is
- ;number of chars actually read.
- int 21h
- mov AL,[BX+1] ;Copy entered data length to AL
- add AL,02 ;offsets are zero based and [bx+1] is the second position in the buffer
- ;the string doesn't start at the beginning of our buffer.
- ;It starts at offset 02h or at the third position.So we need to add 2 to
- ;the length to get to the end of the string.
- mov AH,00h ;can not put an 8-bit register into a 16 bit register,so we must clear out AH first
- mov SI,AX ;Then we need another index register to hold this value.
- mov byte [BX+SI],'$' ; Now we can put the '$' at this position.String in assembly is dollar-terminated string
- ;end of string entering
- ;encrypt
- lea BX, table1 ;encrypt using table1
- lea SI, Buffer+2
- call encryption
- ; print third message
- mov dx, offset msg3
- mov ah, 9
- int 21h
- ; show result:
- mov si, 2
- mov cl, [Buffer+1]
- mov ch,0
- lea bx, Buffer
- again:
- mov al, [bx+si]
- mov ah,0
- CALL PRINT_NUM_UNS
- inc si
- loop again
- jmp start
- decrypt:
- ; Print second message
- mov DX,offset msg5
- mov AH,09h ;DOS function: Write string to STDOUT (output string)
- int 21h
- ;Tell service 0Ah that we want to allow up to 80 chars in our string.Done by putting 81 in the
- ;first byte of the buffer.(80 chars plus the <Enter> key).
- mov BX,offset Buffer
- mov byte [BX],81
- ;Take string from user and store it in buffer
- mov DX,bx
- mov AH,0Ah ;DOS function: Buffered input (input string)
- int 21h
- mov AL,[BX+1] ;Copy entered datae length to AL
- add AL,02 ;offsets are zero based and [bx+1] is the second position in the buffer
- ;the string doesn't start at the beginning of our buffer.
- ;It starts at offset 02h or at the third position.So we need to add 2 to
- ;the length to get to the end of the string.
- mov AH,00h ;can not put an 8-bit register into a 16 bit register,so we must clear out AH first
- mov SI,AX ;Then we need another index register to hold this value.
- mov byte [BX+SI],'$' ; Now we can put the '$' at this position.String in assembly is dollar-terminated string
- ;end of string entering
- mov cl,[BX+1]
- mov ch,0
- mov si,2
- lea DX,Buffer2
- mov di,2
- redo:
- CLC
- mov al,[BX+SI]
- sub al,30h
- mov ah,10
- mul ah
- inc si
- mov ah,[BX+SI]
- sub ah,30h
- add ah,al
- XCHG BX,DX
- mov [BX+DI],ah
- XCHG BX,DX
- inc si
- inc di
- cmp [BX+si], '$'
- jnz redo
- XCHG BX,DX
- mov byte [BX+DI],'$' ; Now we can put the '$' at this position.String in assembly is dollar-terminated string
- ;end of string entering
- ;decrypt
- lea BX, table2
- lea SI, Buffer2+2
- call decryption
- ; print third message
- mov dx, offset msg4
- mov ah, 9
- int 21h
- ; show result:
- mov DX,offset Buffer2+2
- mov AH,09h ;DOS function: Write string to STDOUT (output string)
- int 21h
- jmp start
- ; wait for any key...
- exit:
- mov ah, 0
- int 16h
- ;mov DX,offset buffer+2
- ;mov AH,09h
- ;int 21h
- ret
- encryption proc near
- next_char:
- cmp [si], '$' ; end of string?
- je end_of_string
- mov al, [si]
- cmp al, 'a'
- jb skip
- cmp al, 'z'
- ja skip
- xlatb ; encrypt using table2.
- mov [si], al
- skip:
- inc si
- jmp next_char
- end_of_string:
- ret
- encryption endp
- ;DEFINE_PRINT_NUM
- ; this procedure prints out an unsigned
- ; number in AX (not just a single digit)
- ; allowed values are from 0 to 65535 (FFFF)
- PRINT_NUM_UNS PROC NEAR
- PUSH AX
- PUSH BX
- PUSH CX
- PUSH DX
- ; flag to prevent printing zeros before number:
- MOV CX, 1
- ; (result of "/ 10000" is always less or equal to 9).
- MOV BX, 10000 ; 2710h - divider.
- ; AX is zero?
- CMP AX, 0
- JZ print_zero ,
- CMP AX,20h
- JZ print_space
- CMP AX,10
- JB print_zero
- ;CMP AX,9
- ; Ja begin_print
- jmp begin_print
- print_zero:
- PUTC '0'
- jmp begin_print
- ; CMP AX,9
- ; JB begin_print
- print_space:
- PUTC ' '
- Jmp end_print
- begin_print:
- ; check divider (if zero go to end_print):
- CMP BX,0
- JZ end_print
- ; avoid printing zeros before number:
- CMP CX, 0
- JE calc
- ; if AX<BX then result of DIV will be zero:
- CMP AX, BX
- JB skip2
- calc:
- MOV CX, 0 ; set flag.
- MOV DX, 0
- DIV BX ; AX = DX:AX / BX (DX=remainder).
- ; print last digit
- ; AH is always ZERO, so it's ignored
- ADD AL, 30h ; convert to ASCII code.
- PUTC AL
- MOV AX, DX ; get remainder from last div.
- skip2:
- ; calculate BX=BX/10
- PUSH AX
- MOV DX, 0
- MOV AX, BX
- DIV CS:ten ; AX = DX:AX / 10 (DX=remainder).
- MOV BX, AX
- POP AX
- JMP begin_print
- end_print:
- POP DX
- POP CX
- POP BX
- POP AX
- RET
- PRINT_NUM_UNS ENDP
- decryption proc near
- next_num:
- cmp [si] , '$' ; end of string?
- je end_of_string_1
- mov al, [si]
- cmp al, 1
- jb skip_1
- cmp al, 26
- ja skip_1
- ; xlat algorithm: al = ds:[bx + unsigned al]
- xlatb ; encrypt using table2.
- mov [si], al
- ;mov ah,0
- skip_1:
- inc si
- jmp next_num
- end_of_string_1:
- ret
- decryption endp
- ten DW 10 ; used as multiplier/divider by PRINT_NUM_UNS.
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement