GNU-汇编

#汇编编译
as hello.S -o hello.o -gstabs
ld hello.o -o hello -g
gdb hello


#示例程序

#  
# 汇编语言写的 hello word  
#  
.code32  
.data  
msg:  
        .ascii"Hello word!\n"  
        len=.-msg  
	  
	  
	.text  
#.global_start  
	  
	  
	_start:  
	movl $len,     %edx    # 显示字符数  
10:
    movl $msg,     %ecx    # 缓冲区指针  
	movl $1,    %ebx    # 文件描述符  
	movl $4,    %eax    # 系统调用号  
	int  $0x80        # 系统调用  


	movl $0,    %ebx  
	movl $1,    %eax    # 系统调用号,_exit
	int  $0x80        # 系统调用

#指令
##repne scasb
逐个字节的读取edi指向内存的数据,并且和al(eax)的值进行比较,
ZF = (al == *(BYTE *)edi);并且ecx的值减少1,当ecx=0或者ZF=1时候,则循环退出。
###实例-计算字符串的长度

.code32  
.data  
msg:  
        .ascii"Hello word!\n"  
        len=.-msg  
	  
	  
	.text  
	_start:  
	movl $len,%eax      #eax=12
    movl $msg,     %edi    # 数组指针给edi 
	xor %al,%al    #eax=0
	xor %ecx,%ecx  #ecx=0
	NOT %ecx       #ecx=0xffffffff
	repne scasb
	not %ecx     #ecx=0xd
	dec %ecx     #ecx=0xc

等价于下面c函数

al=0
ecx=0
ecx=0xffffffff
while (ecx != 0) {
    ZF = (0 == *(BYTE *)edi);
    edi++;
    ecx--;
    if (ZF) break;
}
ecx = ~ecx;
ecx--;

#打印调试信息

.section .bbs
.section .data
buf:
   .ascii "0x01abcdef\n"
.section .text
.globl _start
_start:
	print:
	movl $1,%ebx
	movl $4,%eax
	movl $buf,%ecx
	movl $10,%edx
	int $0x80
    movl $0,%ebx
	movl $1,%eax
	int $0x80

#参考
REPNZ SCAS Assembly Instruction

汇编 REPE/REPZ 指令,CMPSB指令

int调用

猜你喜欢

转载自blog.csdn.net/chengbeng1745/article/details/81385829
GNU