⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab4.asm

📁 汇编实验,用汇编语言实现一些简单操作,包括文件读写
💻 ASM
字号:

        
DSEG    SEGMENT

Inputmsg    db " ",0ah,0dh
            db "$"
Exitmsg     db  'Press any key to exit...$'       
            
Filename1   db  '1.txt',0
Filename2   db  '2.txt',0
Filename3   db  'new.txt',0
handle1     dw   ?
handle2     dw   ?
handle3     dw   ?

buf         db  ?
bufcmp      db  ?
DSEG    ENDS


        
SSEG    SEGMENT STACK   
        DB      100H    DUP(?) 
SSEG    ENDS

        
CSEG    SEGMENT		

        ASSUME  CS:CSEG, DS:DSEG, SS:SSEG   

START:        

        MOV  AX,DSEG
        MOV DS,AX
        MOV  ES,AX
OPEN1:       
        clc
        mov   ax,offset Filename1
        push  ax
        lea   ax, handle1
        push  ax
        call  fopen
        jc    EXIT 
        
OPEN2:
       clc
       mov ax,offset Filename2
       push ax
       lea ax,handle2
       push ax
       call fopen       
       jc   EXIT
     
OPEN3:    
        clc   
        mov bx,offset Filename3
        push bx
        lea  bx,handle3
        push bx
        call fcreate
        jc   EXIT

READ2:  
        clc      
        mov ax,handle2
        push  ax
        mov  ax,0001h
        push  ax
        mov  ax, offset bufcmp
        push  ax 
        call fread
	jc  CLOSE
	cmp  ax,0
	je  CLOSE
        
READ1:
        clc      
        mov ax,handle1
        push  ax
        mov  ax,0001h
        push  ax
        mov  ax, offset buf
        push  ax 
        call fread
    
	jc  CLOSE
	cmp  ax,0
	je  WRITE
	mov dl,bufcmp
        cmp dl,buf
        je  TOHEAD
        jmp  READ1

WRITE:
        clc
        mov ax,handle3
        push ax
        mov  ax,0001h
        push  ax
        mov   ax,offset bufcmp
        push  ax
        
        call fwrite   
        jc  CLOSE
       	
 
TOHEAD:
        clc
        mov ah,42h
        xor al,al
        mov  bx,handle1
        xor cx,cx
        xor dx,dx
        int 21h
        jc  CLOSE       
       

SHOW:    
       mov ah,02h
       mov  dl,bufcmp
       int 21h      
       jmp  READ2
       
CLOSE:    
       mov ax,handle1
       push ax
       call fclose
       
       mov ax,handle2
       push ax
       call fclose
       
       mov ax,handle3
       push ax
       call  fclose
      
       
       call jtnl
        
       
EXIT:
       ;mov dx,offset Exitmsg
       ;mov ah,09h
       ;int 21h
       ;mov ah,07h
       ;int 21h
        MOV     AX, 4C00H  
        INT     21H

fopen  proc near ;文件打开,堆栈传递参数,push  filename, push filehandle
         ;如果 文件不存在,則致位cf,并在ax中返回錯誤代號
          push   bx
          push   cx
          push   dx
          push   bp
          push   di
          mov    bp,sp ;;8BYTES+2BYTES
          
          mov   ah,3dh
          mov   al,02h
          mov   dx,[bp+14]
          mov  di,[bp+12]
          int 21h
          ;jc  bl
          mov  [di],ax   
          
          pop   di
          pop   bp
          pop  dx
          pop  cx
          pop  bx
          ;pop  ax
         ret 4
fopen  endp

fclose  proc  near   ;(handle)

	push bx
	push bp
	mov bp,sp

	
	mov ah,3eh
	mov bx,[bp+6]
	int 21h
	
	pop bp
	pop bx
	
	ret 2
fclose endp

fread   proc  near ;文件读,堆栈传递参数,push filehandle,push readbytes,push buffer
	  ;讀成功則清除CF,并 將實際讀如的字節數放入ax中
	  ;不成功則致位CF,并 在 AX中返回錯誤代號
	push  bx
	push  cx
	push  dx
	push  bp
	mov bp,sp ;10+2
	
	
	mov  ah,3fh
	mov  bx,[bp+14]
	mov  cx,[bp+12]
	mov  dx, [bp+10]
	int 21h
	
	
	
	
	pop  bp
	pop  dx
	pop  cx
	pop  bx

	
	ret 6

fread endp

fcreate   proc near  ;(filename,handle)
	  ;建立文件成功則 清除CF位,
	  push cx
	  push dx
	  push  bp
	  mov  bp,sp ;8+2
	  push di
	  
	  mov ah,3ch
	  xor cx,cx
	  mov dx,[bp+10]
	  mov  di,[bp+8]
	  int 21h
	
	  mov [di],ax
	  
	  
	  pop  di
	  pop bp
	  pop dx
	  pop cx
	 
	ret  4
fcreate endp

fwrite    proc  near  ;filehandle,bytes,buf
	 ;成功CF=0
	 
	 push bx
	 push cx
	 push dx
	 push bp
	 mov bp,sp  ;8+2
	
	 mov  ah,40h
	 mov  bx,[bp+14]
	 mov  cx,[bp+12]
	 mov   dx,[bp+10]
	 int  21h
	
	pop bp
	pop dx
	pop cx
	pop bx
	
	ret  6
fwrite endp
jtnl   proc  near
       push   dx
       push  ax       
       mov dl,0dh
       mov  ah,02h
        int 21h
       mov dl,0ah
       mov  ah,02h
      int 21h
      pop ax
      pop dx
      ret
jtnl   endp





CSEG    ENDS
	
END START      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -