;============================================================================= ; crypt03.asm ; This program takes ciphertext from crypt.txt and then calculates the ; frequencies of the letters and outputs on screen. ; Algorithm ; C1: Open file ; C2: Find next word ; C3: Read symbols in word and put number into buffer ; C4: Show the buffer ; C5: Show total number of characters ;(C)I don't take any responsibility for the use of this program ; Zedr0n -- connection closed ;============================================================================= ;============================================================================= ; .Com declaration ;============================================================================= .MODEL TINY LOCALS @@ ;============================================================================= ; Data definition/declaration ;============================================================================= .DATA ;----------------------------------------------------------------------------- ; Equals ;----------------------------------------------------------------------------- SPACE EQU 20H ; ;----------------------------------------------------------------------------- ; Constants ;----------------------------------------------------------------------------- CRLF DB 0DH,0AH,'$' A_STR DB "A ",'$' FILENAME: DB "CIPHER.TXT",0 HEADER DB " F : I 2 3 - 3 2 E",'$' OFF_2 DW OFFSET FIRST,OFFSET SECOND,OFFSET THIRD,OFFSET OTHER,OFFSET THIRDL DW OFFSET SECONDL,OFFSET LAST ;----------------------------------------------------------------------------- ; File variables ;----------------------------------------------------------------------------- BUFFER DB 26 DUP(?) HFILE DW ? TOT DB ? ; FIRST DB 26 DUP(?) ; SECOND DB 26 DUP(?) ; THIRD DB 26 DUP(?) ; OTHER DB 26 DUP(?) ; THIRDL DB 26 DUP(?) ; SECONDL DB 26 DUP(?) ; LAST DB 26 DUP(?) ;----------------------------------------------------------------------------- ; Stack variables ;----------------------------------------------------------------------------- .CODE .386 ORG 100H ;============================================================================= ; Code segment ;============================================================================= CUR_BYT1 EQU [BP-1] START: ENTER 1,0 ; Allocate space for filename CALL SHOW_HEADER MOV DI,-1 ; DI is assumed to be 0 C1: MOV AH,3DH ; XOR AL,AL ; Set the read only mode XOR CL,CL ; LEA DX, FILENAME INT 21H OR AX,AX ; JZ EXIT MOV HFILE,AX CALL CLEAR C2: ; CALL FINDWORD ; C3: ; CALL GETSYM ; MOV AH,3FH ; MOV BX,HFILE ; MOV CX,1 ; LEA DX,CUR_BYT1 INT 21H ; MOV AH,BYTE PTR CUR_BYT1; CMP AH,'@' ; JZ C4 ; JMP C2 C4: ; CALL CALC_TOTAL ; MOV TOT,AL MOV BX,-1 C5: ; CALL SHOW_LETTER PUSH BX MOV AH,02H ; MOV DL,20H ; INT 21H ; MOV DL,':' ; INT 21H ; MOV DL,20H ; INT 21H CALL SHOW_FREQ POP BX CMP BX,25 ; JL C5 C6: ; MOV AH,09H MOV DX,OFFSET CRLF ; INT 21H ; MOV AH,02H ; MOV DL,20H ; INT 21H ; INT 21H ; INT 21H INT 21H MOV AL,TOT CALL HEXDEC EXIT: LEAVE RET ;=============================================================================== ; Procedures ;=============================================================================== ;----------------------------------------------------------------------------- ; FindWord ; Find the next word in the file ; Algorithm ; ; F1: Read the byte. ; F2: Check if it is space, or end of line, if it isn't then it was the ; beginning of the new word ; F3: Decrease pointer ;----------------------------------------------------------------------------- FindWord proc cur_byte equ [bp-1] enter 1,0 pusha F1: mov ah,3Fh mov bx,HFILE mov cx,1 lea dx,cur_byte int 21h F2: mov ah,cur_byte cmp ah,SPACE jz F1 cmp ah,0Dh jz F1 cmp ah,0Ah jz F1 F3: mov ah,42h mov al,1 xor cx,cx xor dx,dx int 21h mov dx,ax mov ah,42h xor al,al xor cx,cx dec dx int 21h popa leave ret FindWord endp ;----------------------------------------------------------------------------- ; GetSym ; G1: Read char ; G2: Is it space, or new line? If not read another! ;----------------------------------------------------------------------------- GETSYM PROC ; CUR_BYT EQU [BP-1] ; ENTER 1,0 ; PUSHA XOR SI,SI G1: ; MOV AH,3FH ; MOV CX,1 MOV BX,HFILE ; LEA DX,CUR_BYT ; INT 21H MOVZX BX,BYTE PTR CUR_BYT G2: ; CMP BX,SPACE ; JZ G3 CMP BX,0DH ; JZ G3 ; CMP BX,0AH ; JZ G3 AND BX,5FH ; CMP BX,'A' ; JL G1 ; CMP BX,'Z' ; JG G1 SUB BX,'A' ; INC BUFFER[BX] CALL CALC_FREQ INC SI JMP G1 G3: POPA LEAVE RET GETSYM ENDP ;----------------------------------------------------------------------------- ; CLEAR ; Clear the buffer ; Algorithm ; L1: ; L2: ;----------------------------------------------------------------------------- CLEAR PROC ; PUSHA MOV BX,26 ; L1: MOV BUFFER[BX-1],00 ; MOV FIRST[BX-1],00 ; MOV SECOND[BX-1],00 ; MOV THIRD[BX-1],00 MOV OTHER[BX-1],00 ; MOV THIRDL[BX-1],00 ; MOV SECONDL[BX-1],00 ; MOV LAST[BX-1],00 DEC BX ; JNZ L1 ; ; POPA RET CLEAR ENDP ;------------------------------------------------------------------------------- ; HEXDEC ; Convert the hexadecimal value in al to ascii and output it. ; H1: Get the number of 10's ; H2: Show the digit ; H3: Is it seconds? If not go H7 ; H4: Show colon! ; H5: exits ;------------------------------------------------------------------------------- HEXDEC proc mov dl,30h H1: push dx H2: cmp al,10 JL H3 sub al,10 inc dx ; shorter than inc dl, but in this case does just the same jmp H2 H3: mov ah,02h push ax int 21h pop ax pop dx add dl,al int 21h H4: ret HEXDEC endp ;----------------------------------------------------------------------------- ; CALC_TOTAL ; Calculate total number of chars ;----------------------------------------------------------------------------- CALC_TOTAL PROC ; MOV BX,26 ; XOR AL,AL T1: ADD AL,BUFFER[BX-1] DEC BX ; JNZ T1 RET ; CALC_TOTAL ENDP ;----------------------------------------------------------------------------- ; Show_Letter ; Show the letter and number of appearences ; Algorithm ; S1: Check if it appears at all. ; S2: Jump line ; S3: Update the string to show ; S4: Show the string ; S5: Convert and show the number of appearences ; ;----------------------------------------------------------------------------- SHOW_LETTER PROC ; @@S1: ; INC BX CMP BUFFER[BX],0 ; JZ @@S1 PUSH BX @@S2: MOV AH,09H ; MOV DX,OFFSET CRLF ; INT 21H @@S3: PUSH BX ADD BL,'A' MOV A_STR[0],BL POP BX ; @@S4: MOV DX,OFFSET A_STR ; INT 21H @@S5: MOV AL,BUFFER[BX] XOR AH,AH ; CALL HEXDEC @@EXIT: ; POP BX RET SHOW_LETTER ENDP ;----------------------------------------------------------------------------- ; Show_Freq ; Show the letter position frequencies ; Algorithm ; S1: Check if empty ; S2: Show value ; S3: ; ; ;----------------------------------------------------------------------------- SHOW_FREQ PROC ; MOV DI,BX PUSH BX MOV BX,-1 @@S1: ; INC BX PUSH BX SHL BX,1 MOV BX,OFF_2[BX] @@S2: MOVZX AX,[BX][DI] CALL HEXDEC ; MOV AH,02H ; MOV DL,20H ; INT 21H ; INT 21H ; INT 21H POP BX ; CMP BX,6 ; JL @@S1 @@EXIT: ; POP BX RET ; SHOW_FREQ ENDP ;----------------------------------------------------------------------------- ; Calc_freq ; Calculate the letter position ; Â× - index in the buffer ; SI - index of the letter ; Algorithm ; C1: Check if it is first,second or third letter ; C2: Save the current file pointer position ; C3: Check if these are last letters ; C4: Increment other[letter] ; C5: ; ;----------------------------------------------------------------------------- CALC_FREQ PROC ; @@CUR_BYTE EQU [BP-1] @@TEMP EQU [BP-3] @@SHORT EQU [BP-4] ENTER 4,0 ; MOV @@SHORT,0 @@C1: PUSH SI PUSH BX MOV BX,SI CMP BX,3 ; JGE @@C2 SHL BX,1 ; JMP OFF_1[BX] REACT_1: ; POP BX ; INC @@SHORT INC FIRST[BX] ; PUSH BX JMP @@C2 REACT_2: POP BX ; INC @@SHORT INC SECOND[BX] ; PUSH BX JMP @@C2 ; REACT_3: ; POP BX ; INC @@SHORT INC THIRD[BX] ; PUSH BX JMP @@C2 @@C2: MOV SI,-1 ; MOV AH,42H MOV BX,HFILE MOV AL,1H ; XOR CX,CX ; XOR DX,DX ; INT 21H ; MOV @@TEMP,AX @@C3: ; INC SI MOV AH,3FH ; MOV BX,HFILE ; MOV CX,1 ; LEA DX,@@CUR_BYTE ; INT 21H ; MOV AH,CUR_BYTE ; MOV BX,SI CMP AH,20H ; JZ JUMP CMP SI,2 ; JL @@C3 ; JMP @@C4 JUMP: ; SHL BX,1 JMP OFF_3[BX] REAC_1: ; POP BX INC LAST[BX] ; JMP @@C5 REAC_2: ; POP BX ; INC SECONDL[BX] ; JMP @@C5 REAC_3: POP BX ; INC THIRDL[BX] JMP @@C5 @@C4: ; ;MOV DX,@@TEMP ;MOV AH,42H ; ;MOV BX,HFILE ; ;XOR CX,CX ; ;XOR AL,AL ;INT 21H ; POP BX MOV AH,@@SHORT CMP AH,1 JZ @@C5 INC OTHER[BX] ;JMP @@EXIT @@C5: ; MOV DX,@@TEMP MOV AH,42H ; MOV BX,HFILE ; XOR CX,CX ; XOR AL,AL INT 21H @@EXIT: ; POP SI LEAVE RET OFF_1 DW OFFSET REACT_1,OFFSET REACT_2,OFFSET REACT_3 OFF_3 DW OFFSET REAC_1,OFFSET REAC_2,OFFSET REAC_3 CALC_FREQ ENDP ;----------------------------------------------------------------------------- ; Show_header ; Show header ; ;----------------------------------------------------------------------------- SHOW_HEADER PROC ; MOV AH,09H ; MOV DX,OFFSET HEADER ; INT 21H MOV DX,OFFSET CRLF ; INT 21H RET SHOW_HEADER ENDP END START