;
push si
lodsw
;
@43_5:
lodsw ;[si]-->ax
mov [si-4],ax
cmp si,bp
jbe @43_5
;
pop si
add word ptr [si-2],4 ;[0004]+=4
dec bp
dec bp
;cx still is 0
;si still point to 6
@44:
;inner loop header
mov word ptr ax,[si]
mul bx ; *= i
add ax,cx ; +=carry
adc dx,0
@45:
div di ;save current digital
mov word ptr [si],dx ;save current digtial to buff
inc si
inc si
mov cx,ax ;move carry to cx
xor dx,dx
cmp si,bp
jbe @44
or ax,ax
jnz @45
@46:
lea bp,[si-2] ;calc end address of buff
@46_5:
dec bx ;i--
jnz @43
;calulation is completed
;convert number to string
@47:
;init
std
pop bx ;restore file handle to bx
mov si,bp ;si, the end address of buff, si will decrease
dec bp
dec bp ;di: it always point to address of buff-2
@48: ;out loop header
lodsw ;move [si] to ax
mov cl,4
mov di,3 ;overwrite code
@49:
;inner loop header ;convert a word to 4 digital sign
xor dx,dx ;covert a word to 4 dec digital
push di
mov di,10
div di
pop di
add dl,'0'
mov byte ptr [di],dl
dec di
loop @49
@50:
mov cl,5
@51: ;do while loop,at least be excuted one times
inc di
dec cx
cmp si,bp
jnz @52
cmp byte ptr [di],'0'
jz @51
@52:
mov dx,di
;bx: ,file handle
mov ah,40h ;Write file dos call
int 21h
cmp si,6
jae @48
@53:
mov cx,word ptr [si] ;mov [0004] to cx
or cx,cx
jz @54
xor di,di
cld
mov al,'0' ;di is end address of buff
push cx
rep stosb
pop cx
xor dx,dx
mov ah,40h
int 21h
@54: ;
; close files
;Function (ah): 3Eh
;Entry parameters: bx- File Handle
;Exit parameters: If the carry flag is set, ax contains 6, the only possible error, which is an invalid handle error
mov ah, 3eh ;close file dos call
int 21h
@60:
int 20h
xor ax,ax ;ax return value
mov bp,10
push si ;save address of buff+2
@101:
mov cl,byte ptr [si]
sub cl,'0'
cmp cl,9
ja @120
cmp ax,MAX_N/10
ja @60
@110:
mul bp
add ax,cx; register ch should be zero
inc si
mov di,si
jmp @101
@120:
or ax,ax
jz @60 ;invalid value, teminal program
@130:
pop dx ;the address number string
push ax ;save value of n to stack
;di: end of number string and shift 1
mov si,offset DGROUP:_fileName
mov cl,6 ;cx is file length ,ch always equ to 0
rep movsb ;dx is end address of number string
; create a file
; dx: fileName
; cx should be zero
mov ah, 3ch ; create a file
int 21h ; ax:file handle
pop bx ; restore value of n from stack
jc @60
;
push ax ;ax: _fp
;
mov ax,ds
add ax,1000h
push ax
pop ds
push ax
pop es
;
mov di, 6 ;di: start address of buff, it should align 2 byte
mov bp, di
mov word ptr [di-2],cx ;[0004]=0
inc cx
mov word ptr [di],cx ;[0006]=1;
mov di,10000
jmp @43
_main endp
_buff label byte
db 'n'
db '='
db '?'
db '('
db '0'
db '<'
db 'n'
db '<'
db '3'
db '4'
db '0'
db '5'
db '0'
db ')'
db '$'
_fileName label byte
db 33
db 46
db 116
db 120
db 116
db 0
_TEXT ends