Assembly language experiment----Encryption

Title Encryption program
;
-----------------------------------------------------------
;Discription: TO encrypt data from a file.
;Author: Dan Lee, Sun Yat
- sen University  All rights reserved
;
-----------------------------------------------------------
.model small, c
.
386
Encrypt proto cell:ptr 
byte           ; function protatype

count
= 30
keyboard  
struct    
   maxin    
byte  count
   incount  
byte   ?
   buf      
byte  count dup( ? )
keyboard  ends


.data  
   BufSize 
=   500
   kydbdata  keyboard 
<>
   prompt    
byte   " Please input file name under current directory... " , 10 , 13 , ' $ '
   fmsg      
byte   " file not found... " , 10 , 13 , ' $ '   
   Handle    WORD 
?
   buffer    BYTE BufSize DUP(
? )
   bytesRead WORD 
?
.code

.startup
 
again:
;Prompt to input file name
   mov  dx,offset Prompt
   mov  ah,9h
   
int   21h  
  
;Get file name from cosole
   mov ah,0Ah
   mov dx,offset kydbdata 
   
int  21h                           ; input file name 
   mov bl,kydbdata.incount
   cmp bl,
0
   je  quit                          ; 
if  no input filename, then exit the program
   mov di,offset kydbdata
   mov al,kydbdata.incount
   xor ah,ah
   add di,ax
   mov ax,
2
   add di,ax 
   mov 
byte  ptr [di], 0                ; To make the filename end with  0  
    
; Open the input file
   mov ax,716Ch                      ; extended create or open
   mov bx,
0                           ; mode  =  read - only
   mov cx,
0                           ; normal attribute
   mov dx,
1                           ; action: open
   mov si,OFFSET kydbdata.buf
   
int  21h                           ; call MS - DOS
   jnc succ                          ; quit 
if  error
   mov dx,offset fmsg
   mov ah,9h
   
int  21h
   jmp again
    

    
succ:
     mov  Handle,ax

; Read the input file
     mov  ah,3Fh                         ; read file or device
     mov  bx,Handle                       ; file handle
     mov  cx,BufSize                     ; max bytes to read
     mov  dx,OFFSET buffer               ; buffer pointer
     
int   21h
     jc   quit                           ; quit 
if  error
     mov  bytesRead,ax

; Display the buffer
     mov  ah,40h                         ; write file or device
     mov  bx,
1                            ; console output handle
     mov  cx,bytesRead                   ; number of bytes
     mov  dx,OFFSET buffer               ; buffer pointer
     
int   21h
     jc   quit                           ; quit 
if  error
  
; Close the file
     mov  ah,3Eh                       ; function: close file
     mov  bx,Handle                       ; input file handle
     
int   21h                            ; call MS - DOS
     jc   quit                           ; quit 
if  error
    
;Encrypttion procession
   mov  cx,bytesRead
   mov  di,OFFSET buffer
mark:
   invoke Encrypt,di
   Inc  di
   loop mark 
    
   call WriteSpace                   ; Write Spaces to seperate the output
   call WriteSpace
   call WriteSpace
; Display the buffer
     mov  ah,40h                         ; write file or device
     mov  bx,
1                            ; console output handle
     mov  cx,bytesRead                   ; number of bytes
     mov  dx,OFFSET buffer               ; buffer pointer
     
int   21h
     jc   quit      
    
; Reopen the input file 
for  write
     mov  ax,716Ch                        ; extended create or open
     mov  bx,
1                            ; mode  =  write
     mov  cx,
0                            ; normal attribute
     mov  dx,
1                             ; action: open
     mov  si,OFFSET kydbdata.buf
     
int   21h                            ; call MS - DOS
     jc  quit                             ; quit 
if  error
     mov Handle,ax                       ; save handle

; Write buffer to 
new  file
     mov ah,40h                           ; write file or device
     mov bx,Handle                       ; output file handle
     mov cx,bytesRead                     ; number of bytes
     mov dx,OFFSET buffer                 ; buffer pointer
     
int  21h
     jc  quit                             ; quit 
if  error

; Close the file
     mov  ah,3Eh                         ; function: close file
     mov  bx,Handle                       ; output file handle
     
int   21h                            ; call MS - DOS
    
quit:
.exit 
0

;
-----------------------------------------------------------
;Discription:This procedure implement Encrypting  data 
;             
using  the demand algorithm of the book
;             design by Dan Lee
;Recives:  one 
byte   char  of its address
;Return:   explicitly nothing, but 
in  fact a  string  of
;          characters by passing by reference
;
-----------------------------------------------------------
Encrypt proc  cell:ptr 
byte           ;passing by reference
   push  si
   push  ax
   mov   si,cell                     ; 
get  address 
   mov   al,[si]                     ; 
get  element
   .
if  (AL >= ' A ' ) &&  (AL <= ' Z ' )         ; process
     mov   al,
' Z '
     sub   al,[si]
     add   al,
' A '
     mov   [si],al
   .endif
   .
if  (AL >= ' a ' ) &&  (AL <= ' z ' )
     mov   al,
' z '
     sub   al,[si]
     add   al,
' a '
     mov   [si],al
   .endif
   pop  ax
   pop  si
   ret
Encrypt  endp

;
----------------------------------------------------------
WriteSpace PROC
; Write a single space to standard output.
; This procedure 
is  copied from Assembly Language  for  
; Intel
- based Computers fourth edition by Kip R.Irvine
;
----------------------------------------------------------
     push ax
     mov  ah,
2                            ; function: display character
     mov  dl,20h                         ; 20h 
=  space
     
int   21h
     pop  ax
     ret
WriteSpace ENDP
      end
 Assembly language experiment。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值