下面给出一个求解字符串长度的函数的实现及其用法,这个函数已经在irvine32.inc中实现,但是仍然有必要去了解他的函数的是过程。其定义如下:
Str_length PROC USES edi
pString:PTR BYTE ;指向字符串
mov edi,pString ;字符计数器
mov eax,0 ;字符串结束?
L1:
cmp BYTE PTR[edi],0 ;
je L2 ;是?退出
inc edi ;否:指向下一个字符
inc eax ;计数器加一
jmp L1
L2:
ret
Str_Length ENDP
下面是这个函数的使用过程以及声明的细节
;String Length (Length.asm)
;This program tests the Str_length procedure
;which returns the length of a string
include irvine32.inc
Str_length PROTO,
pString :PTR BYTE ;函数声明
;关于函数的声明:
;①MASM要求被调用的每一个过程都有原型,PROTO必须在INVOKE之前
; 首先出现
;②假设已经编写了一个特定的过程,创建其原型的方式,复制PROC语句并作修改
; 将关键字PROC改为PROTO,如果有USE运算符,那么连同其寄存器列表一起删除
.data
string_1 BYTE "Hello",0
string_2 BYTE "#",0
string_3 BYTE "#",0
.code
main PROC
call Clrscr
invoke Str_length,addr string_1
call DumpRegs ;求解字符串长度函数的返回结果存放在EAX寄存器中
invoke Str_length,addr string_2
call DumpRegs
invoke Str_length,addr string_3
call DumpRegs
call WaitMsg
exit
main ENDP
END main