PDA

View Full Version : مقدمه ایی بر اسمبلی 64 بیتی



Best Programmer
یک شنبه 17 آبان 1383, 01:23 صبح
Introduction

This document is meant to summarise differences between x86-64 and i386 assembly assuming that you already know well the i386 gas syntax. I will try to keep this document up to date until official documentation is available.
Register set extensions

X86-64 defines eight new integer registers named r8-r15. These registers are encoded using special REX prefix and so using them in non-64-bit instruction implies instruction length growth by 1 byte. They are named as follows:

rXb for 8 bit register (containing the lowest byte of the 64-bit value)
rXw for 16 bits
rXd for 32 bits
rX for 64 bits
Where X stands for integer in the range of 8 to 16.

Original integer registers keeps their irregular names and the 64-bit versions of the 32-bit registers eax, edx, exc, ebx, esi, esi, edi, esp and ebp are now called rax, rdx, rcx, rbx, rsi, rdi, rsp and respectivetly rbp.

The new registers can be used in the same places as the old ones, except for implicit register usage. Some instructions implicitly use specific fixed registers, e.g. as shift counters, source and destination for string operations, etc.
Extended 8-bit instructions

Instructions with REX prefix change behaviour of 8-bit register parts so that all registers can be accessed as 8-bit registers. The upper halves (ah, dh, ch, bh) are replaced by lower halves of next 4 registers (sil, dil, spl, bpl). Then the rules described above are applied.

Unfortunatly some instructions require a REX prefix, so you can't use upper halves together with addresses requiring REX prefix:

addb %ah, (%r10)# Invalid instruction.
64bit instructions
By default most operations remain 32-bit and the 64-bit counterparts are invoked by the fourth bit in the REX prefix. This means that each 32-bit instruction has it's natural 64-bit extension and that extended registers are for free in 64-bit instructions.

To write 64bit instructions, use 'q' as a suffix (q for 'quad-word'):

movl $1, %eax# 32-bit instruction
movq $1, %rax# 64-bit instruction

Exceptions from this rule are instructions manipulating the stack (push, pop, call, ret, enter and leave) which are implicitly 64-bit and their 32-bit counterparts are not available anymore, yet their 16-bit counterparts are. So:

pushl %eax# Illegal instruction
pushq %rax# 1 byte instruction encoded as pushl %eax in 32 bits
pushq %r10# 2 byte instruction encoded as pushl preceeded by REX.
Implicit zero extend

Results of 32-bit operations are implicitly zero extended to 64-bit values. This differs from 16 and 8 bit operations, that don't affect the upper part of registers. This can be used for code size optimisations in some cases, such as:

movl $1, %eax # one byte shorter movq $1, %rax
xorq %rax, %rax# three byte equivalent of mov $0,%rax
andl $5, %eax# equivalent for andq $5, %eax
Immediates

Immediate values inside instructions remain 32 bits and their value is sign extended to 64 bits before calculation. This means that:

addq $1, %rax # Valid instruction
addq $0x7fffffff, %rax # As this
addq $0xffffffffffffffff, %rax # as this one
addq $0xffffffff, %rax # Invalid instruction
addl $0xffffffff, %eax # Valid instruction

Only exception from this rule are the moves of constant to registers that have 64bit form. This means:

movl 1, %eax# 5 byte instruction
movq 1, %rax# 7 byte instruction
movq 0xffffffffffffffff, %rax # 7 byte instruction
movq 0x1122334455667788, %rax # 10 byte instruction
movq 0xffffffff, %rax# 10 byte instruction
movl 0xffffffff, %eax# 5 byte instruction equivalent to above

You may write symbolic expressions as operands to both 64-bit and 32-bit operations. For 32-bit operations they result in zero extending relocations, while in 64-bit operations they result in sign extending ones.

movl $symb, %eax# 5 byte instruction
movq $symb, %rax# 7 byte instruction

So in case you know that the symbol is in the first 32 bits, you should use 32bit instructions whenever possible.

To load a symbol as 64-bit value, you need to use movabs instruction, that is a synonym for mov only changes the default behaviour:

movandq %symb, %rax# 11 byte instruction
Displacements

Similarly as immediates, the displacements are also sign extended and pretty much the same rules apply to them. X86-64 defines a special form of move instruction having 64-bit displacement and similarly, as for immediates, it is implicitly used when the value is known to not fit at compilation time and you need to use movabs to force a 64-bit relocation:

movl 0x1, %eax# load with 32bit sign extended relocation
movl 0xffffffff, %eax# load with 64bit relocation
movl symb, %eax# load with 32bit sign extended relocation
movabsl symb, %eax# load with 64bit sign extended relocation
Loads and stores with 64-bit displacement are available only for the eax instruction.
RIP relative addressing

X86-64 defines a new instruction pointer relative addressing mode to simplify writing of position independent code. The original displacement-only addressing of are overwritten by this one and displacement only is now encoded by one of the redundant SIB form. This means that RIP relative addressing is actually cheaper than displacement only.

To encode this addressing, just write rip as yet another register:

movl $0x1, 0x10(%rip)
will store the value 0x1 10 bytes after the end of the instruction.

Symbolic relocation will be implicitly RIP relative, so

movl $0x1, symb(%rip)
Will write 0x1 to the address of symbol "symb".

FIXME: This looks particularly confusing in the Intel syntax [symb+rip] suggest different location than [symb]. Suggestions for better syntax with symbols?

You are recommended to use RIP relative addressing whenever possible to reduce code size.

The RIP relative branch instructions are still encoded equally to 32bit mode. This means that they are implicitly RIP relative and "*" is used to switch to absolute form.
R13 addressing limitations
The R13 is upper-half equivalent of RBP, that is used in MODRM encoding to escape out into SIB. The R13 also does the encoding (to prevent REX prefix from changing instruction length), so pretty much same limitations to RBP addressing apply to the R13. This means that

(%rbp,index,scale)
is not encodable and:

0(%rbp,index,scale)
must be used.

This text was written by Jan Hubicka

Developer Programmer
یک شنبه 17 آبان 1383, 12:16 عصر
مرسی علی جان!

aakh1361
پنج شنبه 21 آبان 1383, 02:58 صبح
دستت درد نکنه
لاقل فارسی می نوشتی

MSK
جمعه 06 آذر 1383, 10:58 صبح
دوست عزیز شما راجع به CPU جدید میکروسافت یعنی Xeon چیزی میدونی؟

aakh1361
یک شنبه 08 آذر 1383, 02:33 صبح
دوست عزیز شما راجع به CPU جدید میکروسافت یعنی Xeon چیزی میدونی؟دوست من این سی پی یو ساخت اینتل هست :mrgreen:

MiRHaDi
یک شنبه 08 آذر 1383, 03:55 صبح
سلام
مایکروسافت از کی تاحالا MicroHard هم شده که CPU بزنه ؟
البته اینتل هم از آش خورای مایکروسافته ها ولی خوب :)
بای

MSK
یک شنبه 08 آذر 1383, 16:28 عصر
شرمنده. :mrgreen:
مثل اینکه حواس من جای دیگه ای بوده. :sorry:

Best Programmer
دوشنبه 23 آذر 1383, 03:27 صبح
دوست عزیز شما راجع به CPU جدید میکروسافت یعنی Xeon چیزی میدونی؟

:sorry: برای اینکه این همه مدت نیومدم.
خوب سوال شما:
XEON یک cpu هست که اینتل برای کارای سرور کوچک طراحی کرده بود. به مانند cpu های athlon MP / Athlon opteron
اگر اطلاعات بیشتری راجع به اسمبلی نسل 64 بیتی Intel میخوای میتونم در اختیارت بزارم.

aakh1361
جمعه 27 آذر 1383, 18:25 عصر
اگر در مورد Amd64 باشه خیلی بهتر چون در مورد تعداد رجیسترهاش و مشخصات فنی اش تا حالا مطلب فارسی پیدا نکردم :thnx:

MSK
جمعه 27 آذر 1383, 22:08 عصر
جناب Best Programmer شرمنده ولی جوابایی که توی تاپیکهای قبلی گرفتم منو از اسمبلی نا امید کرده.
از اینجابود که این موضوع زیاد برام جالب نبود و موضوعی که به نظرم نامربوط نمیومد رو مطرح کردم.
مجددا از آف تاپیک معزرت می خوام!

matgogoia
یک شنبه 19 تیر 1390, 02:02 صبح
سلام
من باید در مورد اسمبلی 64 بیتی تا آخر هفته آینده برای درس اسمبلی ارائه داشته باشم.
ممنون میشم اگر منبع یا مقاله ای در این مورد در اختیارم قرار بدین.

xman_1365_x
یک شنبه 19 تیر 1390, 22:33 عصر
مرجع پست اول اینجاست (http://www.x86-64.org/documentation/assembly.html)
که ایبوک در ارتباط با abi هم داره که مفیده!
مقدمه ای در ارتباط با IA-64 (http://en.wikipedia.org/wiki/X86-64)
x86 Instruction Set Architecture Comprehensive 32-64-bit Coverage (http://www.mindshare.com/files/ebooks/x86%20Instruction%20Set%20Architecture%20Comprehen sive%2032-64-bit%20Coverage.pdf)
اینم هست که اگر عضو نیستین از گوگل پیدا کنید شاید بعدا خودم آپلودش کنم
http://book.pdfchm.net/32-64-bit-80x86-assembly-language-architecture/9781598220025/
و
http://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf (http://www.cs.cmu.edu/%7Efp/courses/15213-s07/misc/asm64-handout.pdf)

ایبوک های مفید زیادی هم از سایت اینتل (http://www.intel.com/products/processor/manuals/)هست که میتونید بگیرید
اینم تولز اینتل (http://download.intel.com/technology/itj/q41999/pdf/assemble.pdf)
فکر کنم دیگه همینا کافیه
موفق باشی

matgogoia
دوشنبه 20 تیر 1390, 11:58 صبح
مرجع پست اول اینجاست (http://www.x86-64.org/documentation/assembly.html)
که ایبوک در ارتباط با abi هم داره که مفیده!
مقدمه ای در ارتباط با IA-64 (http://en.wikipedia.org/wiki/X86-64)
x86 Instruction Set Architecture Comprehensive 32-64-bit Coverage (http://www.mindshare.com/files/ebooks/x86%20Instruction%20Set%20Architecture%20Comprehen sive%2032-64-bit%20Coverage.pdf)
اینم هست که اگر عضو نیستین از گوگل پیدا کنید شاید بعدا خودم آپلودش کنم
http://book.pdfchm.net/32-64-bit-80x86-assembly-language-architecture/9781598220025/
و
http://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf (http://www.cs.cmu.edu/%7Efp/courses/15213-s07/misc/asm64-handout.pdf)

ایبوک های مفید زیادی هم از سایت اینتل (http://www.intel.com/products/processor/manuals/)هست که میتونید بگیرید
اینم تولز اینتل (http://download.intel.com/technology/itj/q41999/pdf/assemble.pdf)
فکر کنم دیگه همینا کافیه
موفق باشی
آقا ممنون - دستتون درد نکنه
در این مورد فارسی چیری ندارید؟ چون الان تو امتحانام و باید تا چند روز دیگه تحویل بدم احتمالاً نمیتونم ترجمه کنم اینا رو برای ارائه.

xman_1365_x
دوشنبه 20 تیر 1390, 19:06 عصر
من که ندیدم اینا هم مختصر و کلی هست همین مطالبم خسته شدم همشون از یکجا کپی کردن:اشتباه:
http://fa.wikipedia.org/wiki/%D8%A7%D8%B3%D9%85%D8%A8%D9%84%DB%8C
http://parnianweb.com/articles/57.html
http://parnianweb.com/articles/183.html

اما این (http://www.lomont.org/Math/Papers/2009/Introduction%20to%20x64%20Assembly.pdf)سادست و کم حجم(13 صفحه) به انگلیسی و مثالی هم داره که کار شما رو راه میندازه

موفق باشی

matgogoia
شنبه 25 تیر 1390, 17:26 عصر
من که ندیدم اینا هم مختصر و کلی هست همین مطالبم خسته شدم همشون از یکجا کپی کردن:اشتباه:
http://fa.wikipedia.org/wiki/%D8%A7%D8%B3%D9%85%D8%A8%D9%84%DB%8C
http://parnianweb.com/articles/57.html
http://parnianweb.com/articles/183.html

اما این (http://www.lomont.org/Math/Papers/2009/Introduction%20to%20x64%20Assembly.pdf)سادست و کم حجم(13 صفحه) به انگلیسی و مثالی هم داره که کار شما رو راه میندازه

موفق باشی
خیلی ممنون از لطفتون
باز هم اگر با مطلبی برخورد داشتین ممنون میشم کمک کنید.