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

📄 markline.asm

📁 一个朋友写的操作系统源码
💻 ASM
字号:

;/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; we will read source file in 32k as a unit, insertnr subrountine will
; process it, it won't be back until all the lines has been processed,
; when it encounts 0ah, that's mean our line # will be insert after it.
; the line # must be given a special convertion to the right format,
; like this 01023 in decimal, so a modify verion of printf will be used
; to accomplish this job. it will generate the ascii character in the
; reversed order compara to the original verion of printf, and feed the
; rest space with 'zero char',after that it will call another modify of
; printn subroutine to insert the 5 'bit' line number to the position
; indicate by global variable inrtpos.
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/

include arg.inc

sseg segment stack

     db 128 dup (0)

sseg ends

dseg segment para public 'global'

     public inrtpos

     fhand      dw 0        ; file handler for destination
     fname      db lenofid dup (0)
     nfname     db 'markfile',0
     inrtpos    dw 0        ; address used by printnum
     linecount  dw 0        ; how many line encounted
     basecount  dw bascnt
     fbuf       db 32*1024 dup (0)
     padbuf     db 30*1024 dup (0)  ; hold the charater overflow

     ; global variable can't put after fbuf
     ; fbuf will be larger than current size

dseg ends

cseg segment para public 'code'

     assume cs:cseg,ds:dseg,es:dseg,ss:sseg

     extrn panic:near
     extrn printnm:near

start:
     cld
     mov si,80h            ; fetch command line argument len
     lodsb
     or al,al              ; comand line argument can't be NULL
     jnz getfid1
fiderr:
     mov ax,dseg
     mov ds,ax             ; setup environment to call panic
     mov ax,11
     push ax
     call panic
getfid1:
     mov cl,al
     xor ch,ch
getfid2:
     lodsb
     cmp al,space
     jz getfid3
     cmp al,tab
     jnz getfid4
getfid3:
     loop getfid2
     jmp fiderr
getfid4:
     dec si
     mov ax,dseg
     mov es,ax
     mov di,offset fname
     rep movsb
     xor al,al
     stosb           ; add 0 at file name tail

     mov ax,dseg
     mov ds,ax
     mov es,ax
     call insertnrf
     mov ah,4ch
     int 21h

;/============================================================================
;
;                             insertnrf
;
;============================================================================/


     insertnrf proc near

     push ds
     push es
     push bp
     mov bp,sp
     sub sp,12

     ; stack arranges like this
     ;
     ; [bp-2]  : file descriptor for source
     ; [bp-4]  : bytes of file read
     ; [bp-6]  : points to the end of fbuf
     ; [bp-8]  : next search point for scasb
     ; [bp-10] : total byte remain
     ; [bp-12] : file descriptor for destination

     ; create destination file for writing

     mov dx,offset nfname
     mov al,02h           ; read/write
     mov ah,3ch
     int 21h
     jnc destok
     push ax
     call panic
destok:
     mov [bp-12],ax       ; save destination file handler
     mov dx,offset fname
     mov al,0       ; prevent the source file been modified
     mov ah,3dh
     int 21h
     jnc inrtnext1
     push ax
     call panic
inrtnext1:
     mov [bp-2],ax  ; save source file handler

     ; read in 32kb

inrtw1loop:
     mov bx,[bp-2]
     mov cx,32*1024
     mov dx,offset fbuf
     mov ah,3fh
     int 21h
     jnc inrtnext2
     push ax
     call panic
inrtnext2:
     cmp ax,0
     jnz inrtnext3

     ; the file is just the multiplex of
     ; the 32k, so it is unnecessary to
     ; process again, close it and go back

     jmp goback
inrtnext3:
     mov [bp-4],ax       ; it won't change 
     mov [bp-10],ax      ; constant changed

     mov word ptr [bp-6],offset fbuf
     add [bp-6],ax       ; local offset
     mov word ptr [bp-8],offset fbuf
inrtw2loop:
     mov cx,linecount
     inc cx
     mov linecount,cx    ; increase the line count

     ; scan for a 0ah

     mov al,0ah
     mov di,[bp-8]
     mov cx,[bp-10]
     cld
     repnz scasb
     jz inrtnext4        ; got it

     ; if not found, read next 32k, and
     ; continue search if there's any
     ; bytes availabe

     ; calculate how many bytes shall be written

     mov cx,[bp-6]
     sub cx,offset fbuf

     mov bx,[bp-12]
     mov dx,offset fbuf
     mov ah,40h
     int 21h
     jnc wrtok
     push ax
     call panic
wrtok:

     ; we will try to determine if
     ; it is necessary to read another
     ; 32k block

     cmp word ptr [bp-4],32*1024
     jz inrtnext5

     ; it is the last of the block
     ; write back to the newfile
     ; and go back

goback:
     mov bx,[bp-2]
     mov ah,3eh
     int 21h
     mov bx,[bp-12]
     mov ah,3eh
     int 21h
     mov sp,bp
     pop bp
     pop es
     pop ds
     ret
inrtnext5:
     jmp inrtw1loop
inrtnext4:

     ; skip the mark number space
     ; specify the next search address

     add di,insertnr
     mov [bp-8],di
     mov [bp-10],cx      ; remain bytes for next search

     mov si,[bp-6]
     dec si
     add word ptr [bp-6],insertnr  ; one line is processed
     mov di,[bp-6]
     dec di
     std                 ; move string backward
     rep movsb
     mov inrtpos,di      ; save side effect
     mov ax,10           ; decimal
     push ax
     mov ax,basecount
     push ax
     inc ax
     mov basecount,ax
     call printnm
     pop cx
     pop cx
     jmp inrtw2loop

     insertnrf endp

cseg ends

end start

⌨️ 快捷键说明

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