俺来用C RunTimer试一下。C用惯了。
不过感觉MASM 6以后的语法、语义已经和C的很接近了(感觉不像在写汇编,倒像在用C)。呵呵。
程序代码:
.586
.model flat,stdcall ;内存平坦方式,stdcall调用
option casemap:none ;大小写敏感
include windows.inc
include kernel32.inc
include msvcrt.inc ;C RunTime 定义
includelib kernel32.lib
includelib msvcrt.lib ;C RunTime 导入
CTEXT MACRO y:VARARG ;字符串宏
LOCAL sym
CONST segment dword private 'DATA'
IFIDNI <y>,<>
sym db 0
ELSE
sym db y,0
ENDIF
CONST ends
EXITM <OFFSET sym>
ENDM
PlayInfo proto :DWORD,:DWORD ;封装3个功能函数
SwitchLine proto :DWORD
Suspension proto
.data
strdemo db "pid=BC5844B4B3DBF3E193CD8DF26A7D63239A2E29C074E" ;演示字符串1
db "44BFFC15BBD919912C99A5FFD935A95FF6452D4D2EA24B3"
db "5738C0B4302260EB549FB6BD9D879974FDAECC0090EF82E"
db "DE33060717A054DA9F1D3079F0870B92F341A7D1C515D10"
db "&jid=/000001.jpg",0
strdemo2 db "<input type=",22h,"hidden",22h, "id=",22h,"titleb0",22h ;演示字符串2
db "name=",22h,"titleb0",22h,"value=",22h,"虎式坦克",22h," />",0
strnewline db 0dh,0ah,0 ;回车、换行
.data
hInstance dd ?
lpBuffer db 1024 * sizeof(BYTE) dup (?)
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke crt_system,CTEXT("title 字符串处理演示") ;修改演示窗口标题
invoke crt_system,CTEXT("color 3c") ;修改背景、前景色
invoke PlayInfo,CTEXT("____%s____"),CTEXT("利用C RuntimeLibrary 处理字符串演示") ;秀
invoke SwitchLine,3
invoke crt_memset,offset lpBuffer,0,1024 * sizeof(BYTE)
mov edx,'/'
invoke crt_strrchr,offset strdemo,edx
lea ecx,strdemo
sub eax,ecx ;offset2 - offset1
invoke crt_strncpy,offset lpBuffer,offset strdemo,eax
invoke PlayInfo,CTEXT("原字符串: %s"),offset strdemo
invoke SwitchLine,2
invoke PlayInfo,CTEXT("字符串切分结果: %s"),offset lpBuffer
invoke SwitchLine,4
invoke crt_memset,offset lpBuffer,0,1024 * sizeof(BYTE)
mov edx,'='
invoke crt_strrchr,offset strdemo2,edx
lea ecx,strdemo2
push eax ;保存搜索到的偏移
sub eax,ecx
push eax ;保存搜索到的偏移前长度
invoke crt_strlen,offset strdemo2
pop edx ;搜索到的偏移前长度出栈
sub eax,edx ;原串长度减去搜索到的偏移前长度
dec eax ;减去一个 '='
sub eax,3 ;减去 ' />'
pop edx ;搜索到的偏移出栈
inc edx ;让过 '='
invoke crt_strncpy,offset lpBuffer,edx,eax
invoke PlayInfo,CTEXT("原字符串: %s"),offset strdemo2
invoke SwitchLine,2
invoke PlayInfo,CTEXT("字符串提取结果: %s"),offset lpBuffer
invoke Suspension
invoke ExitProcess,0
SwitchLine proc NullLineNum
mov ecx,NullLineNum
.while ecx
push ecx
invoke PlayInfo,CTEXT("%s"),offset strnewline ;输出需要数量的空行
pop ecx
dec ecx
.endw
ret
SwitchLine endp
Suspension proc
invoke crt__getch ;暂停
ret
Suspension endp
PlayInfo proc lpszFormat,lpszText
invoke crt_printf,lpszFormat,lpszText ;格式化输出
ret
PlayInfo endp
end start