汇编源代码之读寄存器内容
2009-10-12 09:36:43 来源:WEB开发网我的环境是WINXP+MASM5.0通过编译生成可执行文件,双击,提示写入文件成功,按任意键推出。
在程序的同一目录下的TEMP.TXT中已经写入了:
ABCD
4645
4F5B
FFFF
四行用来测试而显示送入寄存器的值。
以下是完整的代码,
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
更多精彩
赞助商链接