SECTION .data
msg db 'hello, brave new world!', 0Ah
SECTION .text
global _start
_start:
mov ebx,msg
mov eax,ebx
nextchar:
cmp byte[eax],0 ;将此地址上EAX所指向的字节与0进行比较(0是字符串分隔符的结尾)
jz finished;跳转到有标签‘finished’的地方 若ZF已经被设置了
inc eax ;增加一个字节的地址在EAX中(若没设ZF)
jmp nextchar ;跳转到代码中标着‘nextchar'的
finished:
sub eax, ebx ; address A - B
; 两个寄存器开始的时候指向的同一个地址
; 但是EAX在信息字符串里每个字符都加一字节
mov edx,eax;EAX 等于字节的数目在我们的字符串里
mov ecx,msg;
mov ebx,1
mov eax,4
int 80h
mov ebx,0
mov eax,1
int 80h
SECTION .data
msg db 'hello, brave new world!', 0Ah
SECTION .text
global _start
_start:
mov eax,msg
// move the address of our message string into EAX
call strlen
// call our function to calculate the length of the string
mov edx,eax
算的结果存在EAX里
mov ecx,msg
mov ebx,1
mov eax,4
int 80h
mov ebx,0
mov eax,1
int 80h
strlen: ; 这是第一个函数声明
push ebx ; 将EBX上的值压入栈中,当在这个函数中使用EBX的时候保护它
mov ebx, eax
nextchar :
cmp byte[eax],0
jz finished
inc eax
jmp nextchar
finished:
sub eax,ebx
pop ebx ;出栈 EBX
ret ;返回函数被调用的地方