读书人

ATamp;T汇编处置字符串

发布时间: 2012-12-31 11:57:52 作者: rapoo

AT&T汇编处理字符串

注:以下内容为学习笔记,多数是从书本、资料中得来,只为加深印象,及日后参考。然而本人表达能力较差,写的不好。因非翻译、非转载,只好选原创,但多数乃摘抄,实为惭愧。但若能帮助一二访客,幸甚!

一.传送字符串

把字符串从一个内存位置复制到另一个内存位置。


1.MOVS3种格式:
MOVSB:传送单一字节
MOVSW:传送一个字(2字节)
MOVSL:传送一个双字(4字节)
MOVS使用隐含的源(ESI)、目的(EDI)操作数。
两种加载ESI、EDI值的方式:
1)间接寻址:
movl $output, %edi
2)lea指令加载一个对象的有效地址
leal output, %esi

每次执行MOVS指令时,数据传送后,ESI和EDI寄存器会自动改变,为另一次传送做准备。
ESI、EDI可能递增也可能递减,这取决于EFLAGS中的DF标志。如果DF被清零则递增,DF被设置,则递减。
CLD将DF清零
STD设置DF标志
示例:
# Finding the len of a string useing the SCAS instruction.section .dataoutput:.asciz"The len of the str is : %d.\n"str:.asciz"I am learning AT&T assembly language."len:.int. - str - 1# 减一表示去掉最后的'0'.section .text.global _start_start:noppushllenpushl$outputcallprintfaddl$8,%esplealstr,%edimovl$0xffff,%ecx# 支持的最大长度0xfffff = 65535movb$0,%alcldrepnescasb# 每次迭代ECX递减jnenotfoundsubw$0xffff,%cx# %cx-0xffff为进行了多少次迭代的负数neg%cx# 求补,即负数变整数dec%cx# 减去‘0’,即字符串长度不包含最后的‘0’movl$1,%eaxmovl%ecx,%ebxint$0x80notfound:movl$1,%eaxmovl$0,%ebxint$0x80
运行:
liury@liury-laptop:~/program/asm/working_with_string/str_len$ make
as -o str_len.o str_len.s
ld -dynamic-linker /lib/ld-linux.so.2 -lc -o str_len str_len.o
liury@liury-laptop:~/program/asm/working_with_string/str_len$ ./str_len
The len of the str is : 37.
liury@liury-laptop:~/program/asm/working_with_string/str_len$ echo $?
37

用两种方法求得字符串的长度,两者相等。


1楼han_yankun2009昨天 09:07
汇编,微机原理。
Re: guzhou_diaoke昨天 19:00
回复han_yankun2009n~

读书人网 >编程

热点推荐