MovToVar Macro m_Reg,Asc_AX
mov bx,m_Reg
call ConvertToAsc
lea si,CAscii
lea di,Asc_AX
mov cx,4d
rep movsb
EndM
data segment
mAX dw 0
mBX dw 0
mCX dw 0
mDX dw 0
AscAX db 4 dup(?),0dh,0ah
AscBX db 4 dup(?),0dh,0ah
AscCX db 4 dup(?),0dh,0ah
AscDX db 4 dup(?),0dh,0ah
WriteBytes EQU $-AscAX
CAscii db 5 dup(?) ;临时存放转化结果
filename db 'temp.txt$',0h
filehandle dw ?
ferr_num1 db 'Error occurred when create file!$'
ferr_num2 db 'Write file error!$'
tssaveok db 'Write register value to file success.$'
tsexit db 'Press any key to exit...$'
data ends
Code segment
assume cs:code,ds:data,es:data
Main proc far
start:
push ds
sub ax,ax
mov ax,data
mov ds,ax
mov es,ax
sub ax,ax
mov ax,0ABCDh ;四个测试数据
mov bx,4645h
mov cx,4F5Bh
mov dx,0FFFFh
call SaveRegToMem
call MovAll
call SaveToFile
call NextLine
lea dx,tsexit
call ShowString
waittoexit:
mov ah,0h
int 16h
exitprogram:
mov ah,4ch
int 21h
Main Endp
MovAll proc near
push ax
push bx
push cx
push dx
MovToVar mAX,AscAX
MovToVar mBX,AscBX
MovToVar mCX,AscCX
MovToVar mDX,AscDX
pop dx
pop cx
pop bx
pop ax
ret
MovAll endp
SaveRegToMem proc near
mov mAX,ax
mov mBX,bx
mov mCX,cx
mov mDX,dx
ret
SaveRegToMem endp
ShowString proc near
;显示DX所指的字符串
push ax
mov ah,09h
int 21h
pop ax
ret
ShowString endp
NextLine proc near
;回车换行
mov ah,02h
mov dl,0dh
int 21h
mov ah,02h
mov dl,0ah
int 21h
ret
NextLine endp
ConvertToAsc proc near
;参数为BX
;将BX中的二进制数据转化成相应的ASCII码,
;并将结果保存到CAscii[]
PUSH AX
PUSH CX
PUSH DX
mov bp,0h
mov ch,4h
xh:
mov cl,4h
rol bx,cl
mov al,bl
and al,0fh
add al,30h
cmp al,3ah ;whether >'9'(Ascii:49)
jl toCascii ;if not then store to CAscii
add al,7h ;if yes then convert it to ascii
toCascii:
mov CAscii[bp],al
inc bp
dec ch
jnz xh
mov CAscii[bp],'$'
POP DX
POP CX
POP AX
ret
ConvertToAsc endp
SaveToFile PROC NEAR
;保存内存中的数据到文件中
mov ah,3ch
mov cx,0h
lea dx,filename
int 21h ;建立文件
jc ferr1 ;如果建立出错,跳转到显示相应信息
mov filehandle,ax ;文件代号存放入内存变量
mov ah,40h
mov bx,filehandle
mov cx,WriteBytes ;将要写入文件的字节数送入CX
lea dx,AscAX
int 21h ;向文件写入以记录首地址开始的AX个字节
jc ferr1 ;如果写入错误,则跳转到显示相关信息处
cmp ax,WriteBytes ;如果写入文件的实际字节数不是要求写入的字节数
jne ferr2 ;则显示跳转到显示出错信息
mov bx,filehandle
mov ah,3eh
int 21h ;关闭文件
lea dx,tssaveok
call showstring
call nextline
ret
ferr1:
lea dx,ferr_num1
call showstring
call nextline
ret
ferr2:
lea dx,ferr_num2
call showstring
call nextline
ret
SaveToFile ENDP
Code ends
end start
第二個:
.model small
.stack
;*******
.data
date_str db "Current date is: yyyy-mm-dd", 0Ah, 0Dh, "$"
time_str db "Current time is: hh.mm.ss:xx", 0Ah, 0Dh, "$"
min_size dw ?
padd_chr db ?
file db 'C:\masm5.0\l.txt' , 0
buf db 256 dup(0)
error_message db 0ah , 'error !' , '$'
handle dw ?
;*******
.code
main proc
mov ax ,seg @data
mov ds , ax
mov dx , offset file
mov al , 0
mov ah , 3dh
int 21h
jc error
mov handle , ax
mov bx , ax
mov cx , 255
mov dx , offset buf
mov ah , 3fh
int 21h
jc error
mov bx , ax
mov buf[bx] , '$'
mov dx , offset buf
mov ah , 9
int 21h
mov bx , handle
mov ah , 3eh
int 21h
jnc end1
error:
mov dx , offset error_message
mov ah , 9
int 21h
end1:
mov ax, seg @data ;First we get the data segment address
mov ds, ax ;and store it into ds
mov [min_size], 02h ;Results should always be at least two digits
mov [padd_chr], '0' ;Use '0' as padding-character
mov ah, 2Ah ;Then we call int 21h,2Ah, which will give
int 21h ;us the current date
lea di, date_str ;Then we load the address of the date_str string
add di, 17 ;and set si to point at the first y in yyyy-...
mov ax, cx ;Next we mov cx to ax and
call todec ;call todec
inc di ;We skip the '-' character...
xor ax, ax ;Then we empty ax
mov al, dh ;And set the low-byte of ax to dh
call todec
inc di ;Skip character in string...
xor ax, ax ;Empty ax
mov al, dl ;Set low-byte to dl
call todec ;Convert it to base10
lea di, time_str ;Now we load the time_str string
add di, 17 ;And set the correct pointer offset
mov ah, 2Ch ;And then we call int 21h,2Ch
int 21h ;which will give us the current time
xor ax, ax ;Empty ax
mov al, ch ;Set low-byte to ch
call todec ;Convert it to base10
inc di ;Skip character
mov al, cl ;Set low-byte to cl
call todec ;Convert to base10
inc di ;Skip character
mov al, dh ;Set low-byte to dh
call todec ;Convert to base10
inc di ;Skip character
mov al, dl ;Set low-byte to dl
call todec ;Convert to base10
mov dx, offset date_str ;Now load offset of the date_str string into dx
call print ;And print the (modified) string
mov dx, offset time_str ;Load offset of the time_str string into dx
call print ;And print the (modified) string
mov ax, 4C00h ;Do a clean exit(error code=00)
int 21h
todec proc
push ax ;Save all registers
push bx
push cx
push dx
xor cx,cx ;Empty the POP counter
mov bx,10 ;Base divisor
decloop:
xor dx,dx ;Set the high 16-bits to 0
div bx ;Preform division(dx=remainder, ax=quotient)
inc cx ;Increase the counter
push dx ;and save the remainder
cmp ax,0 ;If the quotient != 0
jnz decloop ;then get one more number
mov bx, [min_size] ;Load min_size value into bx
mov dl, [padd_chr] ;Load padd_chr value into dl
padd_result:
cmp cx, bx ;Is cx >= min_size?
jge poploop ;If so, proceed
mov byte ptr ds:[di], dl ;Else padd with padd_chr
inc di ;and increase string pointer
dec bx ;decrease bx
jmp padd_result ;and test for more padding
poploop:
pop dx ;Get the number of the stack
add dl,'0' ;and add '0' to it
mov byte ptr ds:[di], dl ;Modify the string at ds:di
inc di ;Increase the string pointer
dec cx ;Decrease the loop counter
jnz poploop
pop dx ;Restore all registers
pop cx
pop bx
pop ax
ret ;And return from call
todec endp
print proc
push ax ;Save ax
push ds ;and ds onto the stack
mov ax, @data ;Then get the address of the data segment
mov ds, ax ;and store it into ds
mov ax, 0900h
int 21h ;and then print the message pointed to by dx
pop ds ;Retrieve ds
pop ax ;and ax from stack
ret
print endp
main endp
end main