|
[code:1]
.section .data
new_line_char:
.byte 0x0a
.section .text
.globl _start
.align 4
_start:
movl %esp, %ebp # store %esp in %ebp //指向argc
again:
addl $4, %esp # %esp ---> next parameter on stack//指向程序名字本身
movl (%esp), %eax # move next parameter into %eax //%esp所指向内存单元里的值(这个值是也是一个地址)放入eax中
testl %eax, %eax # %eax (parameter) == NULL pointer?
jz end_again # get out of loop if yes
call putstring # output parameter to stdout.
jmp again # repeat loop
end_again:
xorl %eax, %eax # %eax = 0
incl %eax # %eax = 1, system call _exit ()
xorl %ebx, %ebx # %ebx = 0, normal program exit.
int $0x80 # execute _exit () system call
## prints string to stdout
putstring: .type @function
pushl %ebp
movl %esp, %ebp //第一次执行到此时,我觉得%esp指向程序名字本身,现在ebp指向程序的名字所在的地址,在下一行中还要加个8然后放到ecx中,我就不解了
[/code:1]
movl 8(%ebp), %ecx //%ecx是write调用中第二个参数buf的地址,我觉得ebp现在就是指向参数的,为何还要把%ebx加8后才放入%ecx中
[code:1]
xorl %edx, %edx
count_chars:
movb (%ecx,%edx,$1), %al
testb %al, %al
jz done_count_chars
incl %edx
jmp count_chars
done_count_chars:
movl $4, %eax
xorl %ebx, %ebx
incl %ebx //%ebx = 1, fd = stdout
int $0x80//4号是write调用write(int fd,const void *buf, size_t count); %ebx=fd, %ecx=buf, %edx=count.
movl $4, %eax
leal new_line_char, %ecx //标号new_line_char处的字符是一个换行符,0x0a
xorl %edx, %edx
incl %edx
int $0x80 //调用write()写入换行符
movl %ebp, %esp
popl %ebp
ret
[/code:1]
不解的是红色的那行代码:movl 8(%ebp), %ecx
这行将%ebp里的地址加8后移到ecx中,为什么要加8呢,我觉得%ebp现在就是指向程序的参数的地址了.
程序引自:
http://database.sarang.net/study/linux/asm/linux-asm.txt |
|