Ted's Blog

Happy coding

AT&T汇编语法

Ted posted @ 2008年9月01日 19:17 in asm with tags ASM , 6646 阅读

0.3.1 Overview 

 

开 发一个OS,尽管绝大部分代码只需要用C/C++等高级语言就可以了,但至少和硬件相关部分的代码需要使用汇编语言,另外,由于启动部分的代码有大小限 制,使用精练的汇编可以缩小目标代码的Size。另外,对于某些需要被经常调用的代码,使用汇编来写可以提高性能。所以我们必须了解汇编语言,即使你有可 能并不喜欢它。

 

如果你是计算机专业的话,在大学里你应该学习过Intel格式的8086/80386汇编,这里就不再讨论。如果我们选择的OS开发工具是GCC以及GAS的话,就必须了解AT&T汇编语言语法,因为GCC/GAS只支持这种汇编语法。

 

本书不会去讨论8086/80386的汇编编程,这类的书籍很多,你可以参考它们。这里只会讨论AT&T的汇编语法,以及GCC的内嵌汇编语法。


0.3.2 Syntax 

1.寄存器引用

引用寄存器要在寄存器号前加百分号%,如“movl %eax, %ebx”。

80386有如下寄存器:

  • 8个32-bit寄存器 %eax,%ebx,%ecx,%edx,%edi,%esi,%ebp,%esp;
  • 8个16-bit寄存器,它们事实上是上面8个32-bit寄存器的低16位:%ax,%bx,%cx,%dx,%di,%si,%bp,%sp;
  • 8个8-bit寄存器:%ah,%al,%bh,%bl,%ch,%cl,%dh,%dl。它们事实上是寄存器%ax,%bx,%cx,%dx的高8位和低8位;
  • 6个段寄存器:%cs(code),%ds(data),%ss(stack), %es,%fs,%gs;
  • 3个控制寄存器:%cr0,%cr2,%cr3;
  • 6个debug寄存器:%db0,%db1,%db2,%db3,%db6,%db7;
  • 2个测试寄存器:%tr6,%tr7;
  • 8个浮点寄存器栈:%st(0),%st(1),%st(2),%st(3),%st(4),%st(5),%st(6),%st(7)。

2. 操作数顺序

操作数排列是从源(左)到目的(右),如“movl %eax(源), %ebx(目的)”

3. 立即数

使用立即数,要在数前面加符号$, 如“movl $0x04, %ebx”

或者:

para = 0x04

movl $para, %ebx

指令执行的结果是将立即数04h装入寄存器ebx。

4. 符号常数

符号常数直接引用 如

value: .long 0x12a3f2de

movl value , %ebx

指令执行的结果是将常数0x12a3f2de装入寄存器ebx。

引用符号地址在符号前加符号$, 如“movl $value, % ebx”则是将符号value的地址装入寄存器ebx。

5. 操作数的长度

操作数的长度用加在指令后的符号表示b(byte, 8-bit), w(word, 16-bits), l(long, 32-bits),如“movb %al, %bl”,“movw %ax, %bx”,“movl %eax, %ebx ”。

如果没有指定操作数长度的话,编译器将按照目标操作数的长度来设置。比如指令 “mov %ax, %bx”,由于目标操作数bx的长度为word,那么编译器将把此指令等同于“movw %ax, %bx”。同样道理,指令“mov $4, %ebx”等同于指令“movl $4, %ebx”,“push %al”等同于“pushb %al”。对于没有指定操作数长度,但编译器又无法猜测的指令,编译器将会报错,比如指令“push $4”。

6. 符号扩展和零扩展指令

绝大多数面向80386的AT&T汇编指令与Intel格式的汇编指令都是相同的,符号扩展指令和零扩展指令则是仅有的不同格式指令。

符号扩展指令和零扩展指令需要指定源操作数长度和目的操作数长度,即使在某些指令中这些操作数是隐含的。

在AT&T语法中,符号扩展和零扩展指令的格式为,基本部分 "movs"和"movz"(对应Intel语法的movsx和movzx),后面跟上源操作数长度和目的操作数长度。movsbl意味着movs (from)byte (to)long;movbw意味着movs (from)byte (to)word;movswl意味着movs (from)word (to)long。对于movz指令也一样。比如指令“movsbl %al, %edx”意味着将al寄存器的内容进行符号扩展后放置到edx寄存器中。

其它的Intel格式的符号扩展指令还有:

  • cbw -- sign-extend byte in %al to word in %ax;
  • cwde -- sign-extend word in %ax to long in %eax;
  • cwd -- sign-extend word in %ax to long in %dx:%ax;
  • cdq -- sign-extend dword in %eax to quad in %edx:%eax;

对应的AT&T语法的指令为cbtw,cwtl,cwtd,cltd。

7. 调用和跳转指令

段内调用和跳转指令为"call","ret"和"jmp",段间调用和跳转指令为"lcall","lret"和"ljmp"。

段间调用和跳转指令的格式为“lcall/ljmp $SECTION, $OFFSET”,而段间返回指令则为“lret $STACK-ADJUST”。

8. 前缀

操作码前缀被用在下列的情况:

  • 字符串重复操作指令(rep,repne);
  • 指定被操作的段(cs,ds,ss,es,fs,gs);
  • 进行总线加锁(lock);
  • 指定地址和操作的大小(data16,addr16);

在AT&T汇编语法中,操作码前缀通常被单独放在一行,后面不跟任何操作数。例如,对于重复scas指令,其写法为:

             repne
             scas

上述操作码前缀的意义和用法如下:

  • 指定被操作的段前缀为cs,ds,ss,es,fs,和gs。在AT&T语法中,只需要按照section:memory-operand的格式就指定了相应的段前缀。比如:lcall %cs:realmode_swtch
  • 操作数/地址大小前缀是“data16”和"addr16",它们被用来在32-bit操作数/地址代码中指定16-bit的操作数/地址。
  • 总 线加锁前缀“lock”,它是为了在多处理器环境中,保证在当前指令执行期间禁止一切中断。这个前缀仅仅对ADD, ADC, AND, BTC, BTR, BTS, CMPXCHG,DEC, INC, NEG, NOT, OR, SBB, SUB, XOR, XADD,XCHG指令有效,如果将Lock前缀用在其它指令之前,将会引起异常。
  • 字符串重复操作前缀"rep","repe","repne"用来让字符串操作重复“%ecx”次。

9. 内存引用

Intel语法的间接内存引用的格式为:

section:[base+index*scale+displacement]

而在AT&T语法中对应的形式为:

section:displacement(base,index,scale)

其中,base和index是任意的32-bit base和index寄存器。scale可以取值1,2,4,8。如果不指定scale值,则默认值为1。section可以指定任意的段寄存器作为段前 缀,默认的段寄存器在不同的情况下不一样。如果你在指令中指定了默认的段前缀,则编译器在目标代码中不会产生此段前缀代码。

下面是一些例子:

-4(%ebp):base=%ebp,displacement=-4,section没有指定,由于base=%ebp,所以默认的section=%ss,index,scale没有指定,则index为0。

foo(,%eax,4):index=%eax,scale=4,displacement=foo。其它域没有指定。这里默认的section=%ds。

foo(,1):这个表达式引用的是指针foo指向的地址所存放的值。注意这个表达式中没有base和index,并且只有一个逗号,这是一种异常语法,但却合法。

%gs:foo:这个表达式引用的是放置于%gs段里变量foo的值。

如果call和jump操作在操作数前指定前缀“*”,则表示是一个绝对地址调用/跳转,也就是说jmp/call指令指定的是一个绝对地址。如果没有指定"*",则操作数是一个相对地址。

任何指令如果其操作数是一个内存操作,则指令必须指定它的操作尺寸(byte,word,long),也就是说必须带有指令后缀(b,w,l)。

Avatar_small
DPC Watchdog Vionalt 说:
2020年1月13日 17:53

Well, this is definitely an interesting post out there and I would like to know more about it. Let us know if we can include a little bit information about this on our website.

Avatar_small
UMAIR 说:
2021年3月17日 15:21

Tamilnadu Aavin Factory Asst Previous Model Paper 2022 TN Aavin Factory Asst Previous Questions Model Papers 2020 TN Junior Executive, Technician Model Question Papers Download all Syllabus wise in (Tamilnadu Aavin Factory Asst Previous Papers (Tamilnadu Co-Operative Department Old Question Papers

Avatar_small
科技券 说:
2021年3月19日 18:26

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!

Avatar_small
UMAIR 说:
2021年3月19日 20:06

people are creative and want to try something with photographs as one did make a collage. With the help of altoconvertpdftoexcel she was able to make a collage. Not only this people can convert that collage into pdf with ease.

Avatar_small
利得稅 说:
2021年3月19日 23:03

I surely acquiring more difficulties from each surprisingly more little bit of it

Avatar_small
shred guitar riff 说:
2021年3月22日 21:10

I surely acquiring more difficulties from each surprisingly more little bit of it

Avatar_small
streetwear deutschla 说:
2021年3月23日 19:18

very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post!

Avatar_small
UMAIR 说:
2021年3月24日 15:47

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information Nas100 signals

Avatar_small
Envío de flores a do 说:
2021年3月25日 18:37

Going to graduate school was a positive decision for me. I enjoyed the coursework, the presentations, the fellow students, and the professors. And since my company reimbursed 100% of the tuition, the only cost that I had to pay on my own was for books and supplies. Otherwise, I received a free master’s degree. All that I had to invest was my time.

Avatar_small
thermal oxidizer 说:
2021年3月27日 17:48

I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!

Avatar_small
ETHENS 说:
2021年3月28日 14:15

I simply couldn’t go away your website before suggesting that I actually enjoyed the standard information an individual provide on your visitors? Is gonna be back frequently in order to inspect new posts. one piece filler

Avatar_small
seomoz 说:
2021年3月28日 21:19

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. judi bola

Avatar_small
horse paintings 说:
2021年3月28日 22:51

I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!

Avatar_small
damascus steel chef 说:
2021年3月29日 18:57

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.

Avatar_small
Tube Mastery 说:
2021年3月31日 18:47

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

Avatar_small
danske casino 说:
2021年8月22日 02:00

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks.

Avatar_small
virus 说:
2022年1月04日 14:52

Admiring the time and effort you put into your blog and detailed information you offer!.. casino news website

Avatar_small
virus 说:
2022年1月04日 19:45

You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. I agree with your way of thinking. Thank you for sharing. alcohol treatment centers in Florida

Avatar_small
HGDFFH 说:
2022年1月11日 21:04

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

Avatar_small
COOK 说:
2022年1月12日 16:23

I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. small business consulting services

Avatar_small
COOK 说:
2022年1月12日 21:32

I went over this website and I believe you have a lot of wonderful information, saved to my bookmarks pubfilm

Avatar_small
virus 说:
2022年2月08日 19:14

Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return. rumahwip

Avatar_small
. 카지노사이트 说:
2022年2月18日 02:12

I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help. CBD cream

Avatar_small
virus 说:
2022年2月21日 18:33

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. https://www.google.ga/url?q=https://skytechers.com/

Avatar_small
virus 说:
2022年3月05日 17:25

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. Film Subgenre Lists

Avatar_small
. 카지노사이트 说:
2022年3月15日 01:48

We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. Kitchen Cabinet Installation Brentwood TN Brentwood Kitchen Remodeling

Avatar_small
virus 说:
2022年3月16日 22:03

this is really nice to read..informative post is very good to read..thanks a lot! คาสิโนออนไลน์

Avatar_small
. 카지노사이트 说:
2022年3月20日 00:36

I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... ดูหนัง hd

Avatar_small
. 카지노사이트 说:
2022年3月23日 01:44

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. NB Relationship Counselling

Avatar_small
virus 说:
2022年3月23日 20:49

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. jasa sablon kaos acara gathering kantor

Avatar_small
. 카지노사이트 说:
2022年3月24日 03:14

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! pg slot

Avatar_small
. 카지노사이트 说:
2022年3月25日 02:04

This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post. feed.kroger.comm

Avatar_small
virus 说:
2022年3月29日 19:22

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. Surrogacy treatment

Avatar_small
Nomi 说:
2022年4月09日 20:14

Your blog has the same post as another author but i like your better.
<a href="https://cheap-usedcars.com">Cars for Sale in Nashville</a>

Avatar_small
. 카지노사이트 说:
2022年4月10日 01:57

Discrete your breath likewise delivers rose tinctures, tender skin gels, along side vape carts. https://hempbombsplus.com/delta-9-gummies/

Avatar_small
Back Pain Clinics 说:
2022年4月20日 18:25

I like this blog so much, saved to my bookmarks .

Avatar_small
Nashville TN Stamped 说:
2022年4月20日 21:14

I simply couldn’t go away your website before suggesting that I actually enjoyed the standard information an individual provide on your visitors? Is gonna be back frequently in order to inspect new posts.

Avatar_small
virus 说:
2022年4月21日 16:13

I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it.. 먹튀검증커뮤니티

Avatar_small
COOK 说:
2022年5月01日 20:01

Great content material and great layout. Your website deserves all of the positive feedback it’s been getting. เว็บสล็อต

Avatar_small
. 카지노사이트 说:
2022年5月03日 02:08

We think that everyone deserves a chance at solid & secure authentication tools which includes privacy by default. That's why we created AuthGS™ We're proud to be one of the easiest authentication providers to integrate in your application, and we have a lot of security features waiting for you! dashboard

Avatar_small
COOK 说:
2022年5月11日 00:06

Your article has piqued a lot of positive interest. I can see why since you have done such a good job of making it interesting. Packers and Movers Kolkata to Patna

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

It is my first visit to your blog, and I am very impressed with the articles that you serve. Give adequate knowledge for me. Thank you for sharing useful material. I will be back for the more great post. truck mattress

Avatar_small
virus 说:
2022年5月25日 22:06

i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. best place to buy cvv

Avatar_small
. 카지노사이트 说:
2022年7月03日 02:12

I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. voyance par telephone gratuite

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

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:32

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:11

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:26

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:57

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年8月22日 21:36

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. UFASLOT


登录 *


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