Ted's Blog

Happy coding

linux汇编常见问题

Ted posted @ 2008年9月02日 23:34 in 未分类 with tags ASM , 4820 阅读

1.gcc嵌入汇编
(1). 在gcc嵌入汇编中输入输出使用相同的寄存器?

static void * __memcpy(void * to, const void * from, size_t n)
{
 int d0,d1,d2;
 __asm__ __volatile__(
  "rep;movsl\n\t"
  "testb $2,%b4\n\t"
  "je 1f\n\t"
  "movsw\n"
  "1:\ttestb $1,%b4\n\t"
  "je 2f\n\t"
  "movsb\n"
  "2:"
  :"=&c" (d0), "=&D" (d1), "=&S" (d2)
  :"0" (n/4), "q" (n), "1" ((long) to), "2" ((long) from)
  :"memory");
 return (to);
}

操作数0,1,2和3,5,6使用相同的寄存器的理解:
a. 3,5,6在输入过程中将值n/4,to和from的地址读入ecx,edi,esi寄存器中;
b. 0,1,2在输出过程中将ecx,edi,esi寄存器中的值存入d0,d1,d2内存变量中。
c. 注意在上面的语句中也有"&"限定符,但输入和输出仍使用相同的寄存器,如操作数0和3都使用寄存器ecx,这是因为"0"限定的结果,如果

把"0" (n/4)换成"c" (n/4),则因为"=&c"的限定而使得编译时报错。

(2). 关于gcc嵌入式汇编中"&"限定符的作用?
"&"限定符用于输出操作数,使其唯一的使用某寄存器

int bar,foo;
__asm__ __volatile__(
 "call func \n\t"
 "mov ebx,%1"
 :"=a" (foo)
 :"r" (bar));

在gcc编译时默认会让bar也使用eax寄存器,如果把"=a"改为"=&a",那么foo将唯一使用eax,而让bar使用其它的寄存器。

(3). _start和main的关系?
main是gcc看到的程序入口点,而ld和as所看到的程序入口点其实是_start。libc库中的_start会调用main函数。
a. 编译带_start的汇编程序时的步骤:as -o a.o a.s,ld -o a a.o; gcc -g -c a.s,ld -o a a.o。(使用gcc编译可以增加调试选项-g,这

时不能直接用gcc -o a a.s编译的原因是gcc会默认在程序中查找main函数,并且会将libc中的_start加入进来。如果直接用gcc -o a a.s编译

,则会报两个错误"重复的_start"和"没找到main")
b. 编译带main的汇编程序或C程序时的步骤:gcc -o a a.s;gcc -o a a.c。

(4).section和.previous
将这两个.section和.previous中间的代码汇编到各自定义的段中,然后跳回去,将这之后的的代码汇编到上一个section中(一般是.text段),

也就是自定义段之前的段。.section和.previous必须配套使用。

2. AT&T汇编
(1).data,.section等都是伪操作,不能直接翻译成机器码,只有相应的assembler才能识别
(2).section将程序分成几个片断,如.data,.text,.bss
(3).globl 函数名表示该函数被export,并可以被其它文件中的函数调用
(4).bss可以用来申请一块空间,但不需要对其进行初始化
(5)使用内核定义函数如open,read等可以通过int $80中断来完成
(6)MOVL $FOO,%EAX是把FOO在内存中的地址放到EAX中,而MOVL FOO,%EAX是把FOO这个变量的内容放入EAX
(7).include "文件名",将其它文件包含进来
(8)如何在汇编中表示结构?如c语言中的如下结构:
struct para
{
 char Firstname[40];
 char Lastname[40];
 char Address[240];
 long Age;//4 bytes
}
在汇编中可以表示成:
.section data
record1:
.ascii "Fredrick\0"
.rept 31 #Padding to 40 bytes
.byte 0
.endr
.ascii "Bartlett\0"
.rept 31 #Padding to 40 bytes
.byte 0
.endr
.ascii "4242 S Prairie\nTulsa, OK 55555\0"
.rept 209 #Padding to 240 bytes
.byte 0
.endr
.long 45
其中.rept n和.endr表示重复两者之间的序列n次,可用于填充数据
(9)当汇编程序由多个文件构成时,可以采用以下方式编译与连接:
as write-records.s -o write-records.o (gcc -g -c write-records.s)
as write-record.s -o write-record.o (gcc -g -c write-record.s)
ld write-record.o write-records.o -o write-records
(10)如何在汇编语言中使用动态库中的函数?
#helloworld-lib.s
.section .data
helloworld:
.ascii "hello world\n\0"
.section .text
.globl _start
_start:
pushl $helloworld
call printf
pushl $0
call exit

as helloworld-lib.s -o helloworld-lib.o
ld -dynamic-linker /lib/ld-linux.so.2 -o helloworld-lib helloworld-lib.o -lc
产生动态库:ld -shared write-record.o read-record.o -o librecord.so
(11)使用汇编文件生成动态库
as write-record.s -o write-record.o
as read-record.s -o read-record.o
ld -shared write-record.o read-record.o -o librecord.so
as write-records.s -o write-records.o
ld -L . -dynamic-linker /lib/ld-linux.so.2 -o write-records -lrecord write-records.o
记得运行write-records时还需要将动态库路径加到/etc/ld.so.conf中,并运行ldconfig
(12)编译汇编文件时如何产生调试符号
as --gstabs a.s -0 a.o 或者gcc -g -c a.s

附录:
(1)函数调用时栈的情况
#Parameter #N <--- N*4+4(%ebp)
#...
#Parameter 2 <--- 12(%ebp)
#Parameter 1 <--- 8(%ebp)
#Return Address <--- 4(%ebp)
#Old %ebp <--- (%ebp)
#Local Variable 1 <--- -4(%ebp)
#Local Variable 2 <--- -8(%ebp) and (%esp)

(2)例子
 .include "external_func.s"

 .section .data
data_array:     #定义long型数组
 .long 3,67,34,0        
data_strings:    #定义字符串
 .ascii "Hello there\0"
data_long: #定义long型变量
 .long 5

 .section .bss
 .lcomm my_buffer, 500   #申请一块500字节的内存

 .section .text
 .equ LINUX_SYSCALL, 0x80 #定义符号LINUX_SYSCALL的值为0x80
 .globl _start
_start:
 pushl %edx
 movl data_long,%edx     #将data_long变量的值放入edx寄存器
 movl $data_long,%edx    #将data_long的地址放入edx寄存器
 popl %edx

 pushl $3      #push second argument
 pushl $2      #push first argument
 call power    #call the function
 addl $8, %esp #move the stack pointer back
 pushl %eax    #save the first answer before,calling the next function

 movl $1, %eax #exit (%ebx is returned)
 int $LINUX_SYSCALL    

 .type power, @function #定义函数power
power:
 pushl %ebp         #save old base pointer
 movl %esp, %ebp    #make stack pointer the base pointer
 subl $4, %esp      #get room for our local storage
 movl 8(%ebp), %eax #put first argument in %eax
 movl 12(%ebp), %ebx #put second argument in %ebx
 imull %ebx,%eax
 movl %ebp, %esp    #restore the stack pointer
 popl %ebp          #restore the base pointer
 ret

Avatar_small
Isabella McKillop 说:
2020年7月31日 04:13

The reason behind the concept that the education is his compulsory tool and the devise of the life is that my assignment help review now know with the circumstance that the educations will intensification the quality of their drudgery. And the circumstances of the education will offer with the huge number of the welfares.

Avatar_small
How to play Sony Liv 说:
2020年10月09日 15:24

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! How to play Sony Liv KBC Play Along

Avatar_small
Navodaya Result 说:
2021年1月26日 23:08

The Result of Navodaya Vidyalaya will be declared in online mode. The candidate can check results by login to the official webs announced online, It has been announced in login and candidates can check it by entering registration number and date of birth. Merit list for the exam shall be available soon. Exam for JNVST class 6 was held on [URL=https://www.navodayaresult2020-5th.in/]Navodaya Result Result 2021 Chhattisgarh[/URL] January 11 and for class 9 it was conducted on February 08, 2020. The new date for phase 2 is yet to be announced and for class 11 admission the merit list will be released directly on the basis of qualifying marks score. Check more details on JNVST 2020 result from this page.

Avatar_small
Navodaya Result 说:
2021年1月26日 23:09

The Result of Navodaya Vidyalaya will be declared in online mode. The candidate can check results by login to the official webs announced online, it has been announced in login and candidates can check it by entering registration number and date of birth. Merit list for the exam shall be available soon. Exam for JNVST class 6 was held on https://www.navodayaresult2020-5th.in/ Navodaya Result Result 2021 Chhattisgarh January 11 and for class 9 it was conducted on February 08, 2020. The new date for phase 2 is yet to be announced and for class 11 admission the merit list will be released directly on the basis of qualifying marks score. Check more details on JNVST 2020 result from this page.

Avatar_small
UMAIR 说:
2021年2月06日 14:56

We come to the my web portal so happy we wish the all students we can get NBSE 12th Model paper 2021 The Nagaland Board of School Education (NBSE) is Conducted to Higher Secondary School Leaving Certificate (HSSLC)Nagalanda 12th Question Paper 2021

Avatar_small
UMAIR 说:
2021年2月10日 14:14

Very much impressed with the services and the quality of this site https://businesspartnermagazine.com/enhancing-productivity-with-signnow-electronic-solutions/enhancing-productivity-with-signnow-electronic-solutions. I don’t know how I ended up being here, but I am super happy that I did. Highly recommeded to you guys.

Avatar_small
Party Rentals Cincin 说:
2021年10月14日 05:16

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

Avatar_small
sildalis tablet pric 说:
2021年10月29日 17:57

In this article understand the most important thing, the item will give you a keyword rich link a great useful website page:

Avatar_small
Phoenix birthday par 说:
2021年10月31日 05:07

Very good topic, similar texts are I do not know if they are as good as your work out.

Avatar_small
party rentals Phoeni 说:
2021年11月06日 05:50

Very good topic, similar texts are I do not know if they are as good as your work out.

Avatar_small
wedding tents for re 说:
2021年11月09日 21:07

It is rather very good, nevertheless glance at the data with this handle.

Avatar_small
water slide rentals 说:
2021年11月11日 21:35

Find the best essays on is my friend's profile page.

Avatar_small
The Bounce House Com 说:
2021年11月12日 03:13

I should say only that its awesome! The blog is informational and always produce amazing things.

Avatar_small
Inflatable Christmas 说:
2021年11月13日 04:25

I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog.

Avatar_small
Cincinnati Concrete 说:
2021年11月14日 00:18

These you will then see the most important thing, the application provides you a website a powerful important internet page:

Avatar_small
blacktop Cincinnati 说:
2021年11月15日 04:24

Very good topic, similar texts are I do not know if they are as good as your work out.

Avatar_small
COOK 说:
2022年1月02日 19:25

Register an author account and start writing articles on your products and services. Now that you have a list of keywords, you can start writing immediately. Daftar Slot

Avatar_small
COOK 说:
2022年1月10日 18:16

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. 0x0 0x0

Avatar_small
COOK 说:
2022年1月12日 00:26

I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. sgt-151

Avatar_small
virus 说:
2022年1月12日 16:26

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. small business consulting

Avatar_small
virus 说:
2022年1月18日 19:49

Thanks for providing recent updates regarding the concern, I look forward to read more. voice data cabling

Avatar_small
COOK 说:
2022年1月21日 03:50

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. chia anime

Avatar_small
virus 说:
2022年1月24日 17:29

You’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!. Website

Avatar_small
virus 说:
2022年2月08日 15:46

I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. ciberseguridad online

Avatar_small
. 카지노사이트 说:
2022年2月17日 01:45

Very useful info. Hope to see more posts soon!. situs slot 2022

Avatar_small
virus 说:
2022年2月24日 19:07

Everything has its value. Thanks for sharing this informative information with us. GOOD works! my essay writer

Avatar_small
virus 说:
2022年3月07日 20:16

Great job here on. I read a lot of blog posts, but I never heard a topic like this. I Love this topic you made about the blogger's bucket list. Very resourceful. animal curiosities

Avatar_small
virus 说:
2022年3月10日 18:17

I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... 안전놀이터

Avatar_small
virus 说:
2022年3月12日 20:48

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... cleaning hero

Avatar_small
virus 说:
2022年3月23日 17:12

I got what you mean , thanks for posting .Woh I am happy to find this website through google. truck mattress

Avatar_small
umair 说:
2022年4月07日 17:21

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. <a href="https://puppiesshops.com/teacup-puppies.html">Teacup puppy for sale near me</a>

Avatar_small
Ingénieur 说:
2022年4月21日 16:23

Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks.

Avatar_small
꽁머니 2만 说:
2022年4月23日 00:30

You delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared.

Avatar_small
COOK 说:
2022年4月27日 16:56

Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work. Home Design Institute

Avatar_small
COOK 说:
2022年5月01日 17:27

I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. slot

Avatar_small
COOK 说:
2022年5月09日 03:55

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. truck mattress

Avatar_small
COOK 说:
2022年5月10日 20:53

Cool stuff you have got and you keep update all of us. tangkasnet

Avatar_small
COOK 说:
2022年5月11日 17:17

I gotta favorite this website it seems very helpful . slot88

Avatar_small
COOK 说:
2022年5月11日 21:38

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts. Cancel my registration

Avatar_small
COOK 说:
2022年5月12日 04:24

Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info. 토토사이트

Avatar_small
COOK 说:
2022年5月12日 16:27

A very excellent blog post. I am thankful for your blog post. I have found a lot of approaches after visiting your post. slot88

Avatar_small
COOK 说:
2022年5月12日 19:58

Merely a smiling visitant here to share the love (:, btw outstanding style. judi slot online

Avatar_small
COOK 说:
2022年5月15日 17:57 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:57 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:57 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:58 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:58 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:58 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:58 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 17:59 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 18:02 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 18:02 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 18:02 Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. link alternatif sbobet
Avatar_small
COOK 说:
2022年5月15日 22:38

Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. http://www.ruled8tode.com

Avatar_small
7yjyhtythwth 说:
2022年5月25日 19:21

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... สล็อต

Avatar_small
HASEEB 说:
2022年6月27日 20:13

I was looking at some of your posts on this website and I conceive this web site is really instructive! Keep putting up.. skyward fbisd

Avatar_small
dark web/deep web/d 说:
2022年8月04日 18:48

The Dark Web does not have a distinguishable interface like the Normal Web. This means that when you try to view any web site on the Dark Web you will not be able to tell at a glance what it is.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月04日 19:31

Once these criminals get your information, they may use it for illegal activities. Hackers can open up a new account using your name or create entirely new email accounts that contain sensitive information, making it extremely difficult to stop their activities.  deep web

Avatar_small
dark web/deep web/d 说:
2022年8月04日 19:48

It is important to understand the risks of exposing your identity and personal information to the public. While there are legitimate reasons to use these types of sites, you should exercise caution when doing so.  dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:10

Recently, I have been discussing on twitter about how the dark web is a home of many illegal activities, including money laundering, credit card fraud, and identity theft. Many people ask me why the dark web has such a bad reputation. Well, it's time we take a closer look.  dark web sites

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:25

In my experience, the dark web is full of dangerous individuals. The only way to avoid these links is to simply use a VPN service that will keep you safe from the many connections that exist on the dark web.   dark web

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:41

You will need to create a marketing plan for your affiliate product, and you will also find helpful information on finding the best advertising sources for your product. Some marketers even find useful information in books on how to market through the use of article marketing, forum marketing, or email marketing.  work from home jobs

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:56

While some are benefiting from a few select techniques, others are benefiting from a comprehensive overhaul. If you've been around for awhile, there's no doubt you've heard the same stories.   affiliate marketing success

Avatar_small
boxing 说:
2022年9月08日 18:41

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. แทงบอล 888

Avatar_small
หวยฮานอย 说:
2023年1月08日 21:19

I wish more writers of this sort of substance would take the time you did to explore and compose so well. I am exceptionally awed with your vision and knowledge. หวยฮานอย

Avatar_small
Maryland Fake driver 说:
2023年1月20日 00:40

Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job! Maryland Fake driver's license

Avatar_small
Buy Magic Mushrooms 说:
2023年1月30日 06:02

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work Albino Penis Envy Mushrooms

Avatar_small
SEO 说:
2023年6月12日 18:11

i love reading this article so beautiful!!great job! 메리트카지노

Avatar_small
sophia 说:
2023年6月20日 16:42

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. <a href="https://making2022.com/">천안출장마사지</a>

Avatar_small
sophia 说:
2023年6月20日 16:42

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. 천안출장마사지

Avatar_small
Denver web and SEO 说:
2023年7月20日 05:04

I was looking for SEO services and came across this site. Another great site I recommend is Bus Web Design for SEO experts.

Avatar_small
Windshield Replaceme 说:
2023年7月20日 05:06

In regard to this article, we also have the perfect resource for Autoglass services.

Avatar_small
Sell My Car Denver 说:
2023年7月20日 05:10

Thank you for sharing this great post! I came across this site while searching for how to sell my car for cash.

Avatar_small
Automotive Staffing 说:
2023年7月20日 05:16

With thousands of staffing agencies out there, it’s hard to find a good one. Tier2Tek staffing has always come through for our company, time and time again.

Avatar_small
sophia 说:
2023年8月30日 01:15

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. เว็บแทงบอลดีที่สุดUFABET

Avatar_small
jsimitseo 说:
2024年1月31日 21:06

I get a kick out of the chance to suggest only fine in addition to proficient data and realities, thus see it:  concierge doctor naples


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter