??xml version="1.0" encoding="utf-8" standalone="yes"?>
让我们首先看一?Linux 中用的基本汇编E序语法。GCCQ用?Linux ? GNU C ~译器)使用 AT&T 汇编语法。下面列Zq种语法的一些基本规则。(该列表肯定不完整Q只包括了与内联汇编相关的那些规则。)
寄存器命?/strong>
寄存器名U有 % 前缀。即Q如果必M?eaxQ它应该用作 %eax?
源操作数和目的操作数的顺?/strong>
在所有指令中Q先是源操作敎ͼ然后才是目的操作数。这与将源操作数攑֜目的操作C后的
Intel 语法不同?
mov %eax, %ebx, transfers the contents of eax to ebx. |
操作数大?/strong>
Ҏ操作数是字节 (byte)、字 (word) q是长型
(long)Q指令的后缀可以?b、w ?l。这q不是强制性的QGCC
会尝试通过d操作数来提供相应的后~。但手工指定后缀可以改善代码的可L,q可以消除编译器猜测不正的可能性?
movb %al, %bl -- Byte move |
立即操作?/strong>
通过使用 $ 指定直接操作数?
movl $0xffff, %eax -- will move the value of 0xffff into eax register. |
间接内存引用
M对内存的间接引用都是通过使用 ( ) 来完成的?
movb (%esi), %al -- will transfer the byte in the memory |
GCC 为内联汇~提供特D结构,它具有以下格式:
GCG ?"asm" l构
asm ( assembler template |
本例中,汇编E序模板由汇~指令组成。输入操作数是充当指令输入操作数使用? C 表达式。输出操作数是将对其执行汇编指o输出?C 表达式?/p>
内联汇编的重要性体现在它能够灵zL作,而且可以使其输出通过 C 变量昄出来。因为它hq种能力Q所?"asm" 可以用作汇编指o和包含它?C E序之间的接口?/p>
一个非常基本但很重要的区别在于 单内联汇~?/em>只包括指令,? 扩展内联汇编包括操作数。要说明q一点,考虑以下CZQ?
内联汇编的基本要?/strong>
{ |
在上例中Q我们用汇~指令 "b" 的值等? "a"。请注意以下几点Q?/p>
movl %1, %%eax
?"a" 的值移?%eax
中,
movl %%eax, %0
?%eax 的内容移?
"b" 中?
现在让我们更详细的了解每一的含义?/p>
汇编E序模板是一l插入到 C E序中的汇编指oQ可以是单个指oQ也可以是一l指令)。每条指令都应该由双引号括vQ或者整l指令应该由双引h赗每条指令还应该用一个定界符l尾。有效的定界Wؓ新行 (\n) 和分?(;)?'\n' 后可以跟一?tab(\t) 作ؓ格式化符P增加 GCC 在汇~文件中生成的指令的可读性?指o通过?%0?1 {来引用 C 表达式(指定为操作数Q?/p>
如果希望保~译器不会在 "asm" 内部优化指oQ可以在 "asm" 后用关键字 "volatile"。如果程序必M ANSI C 兼容Q则应该使用 __asm__ ?__volatile__Q而不?asm ?volatile?/p>
C 表达式用?"asm" 内的汇编指o操作数。在汇编指o通过?C E序?C 表达式进行操作来执行有意义的作业的情况下Q操作数是内联汇~的主要Ҏ?/p>
每个操作数都由操作数U束字符串指定,后面跟用括弧括v?C 表达式,例如Q?constraint" (C expression)。操作数U束的主要功能是定操作数的d方式?/p>
可以在输入和输出部分中同时用多个操作数。每个操作数由逗号分隔开?/p>
在汇~程序模板内部,操作数由数字引用。如果d? n 个操作数Q包括输入和输出Q,那么W一个输出操作数的编号ؓ 0Q逐项递增Q最后那个输入操作数的编号ؓ n -1。L作数的数目限制在 10Q如果机器描qCM指o模式中的最大操作数数目大于 10Q则使用后者作为限制?
如果 "asm" 中的指o指的是硬件寄存器Q可以告?GCC 我们自׃用和修改它们。这PGCC ׃会假讑֮装入到这些寄存器中的值是有效倹{通常不需要将输入和输出寄存器列ؓ clobberedQ因?GCC 知道 "asm" 使用它们Q因为它们被明确指定为约束)。不q,如果指o使用M其它的寄存器Q无论是明确的还是隐含的Q寄存器不在输入U束列表中出玎ͼ也不在输出约束列表中出现Q,寄存器都必须被指定ؓ修饰列表。修饰寄存器列在W三个冒号之后,其名U被指定为字W串?/p>
至于关键字,如果指o以某些不可预知且不明的方式修改了内存,则可能将 "memory" 关键字添加到修饰寄存器列表中。这样就告诉 GCC 不要在不同指令之间将内存值高速缓存在寄存器中?/p>
前面提到q,"asm" 中的每个操作数都应该由操作数U束字符串描qͼ后面跟用括弧括v?C 表达式。操作数U束主要是确定指令中操作数的d方式。约束也可以指定Q?/p>
U束q要求两个操作数匚w?/p>
在可用的操作数约束中Q只有一部分是常用的;下面列出了这些约束以及简要描q。有x作数U束的完整列表,请参? GCC ?GAS 手册?/p>
寄存器操作数U束 (r)
使用q种U束指定操作数时Q它们存储在通用寄存器中。请看下例:
asm ("movl %%cr3, %0\n" :"=r"(cr3val)); |
q里Q变?cr3val 保存在寄存器中,%cr3 的值复制到寄存器上Qcr3val 的g该寄存器更新到内存中。指?"r" U束ӞGCC 可以变?cr3val 保存在Q何可用的 GPR 中。要指定寄存器,必须通过使用特定的寄存器U束直接指定寄存器名?/p>
a %eax |
内存操作数约?(m)
当操作数位于内存中时QQ何对它们执行的操作都在内存位置中直接发生,q与寄存器约束正好相反,后者先值存储在要修改的寄存器中Q然后将它写回内存位|中。但寄存器约束通常只在对于指o来说它们是绝对必需的,或者它们可以大大提高进E速度时用。当需要在
"asm" 内部更新 C
变量Q而您又确实不希望使用寄存器来保存其值时Q用内存约束最为有效。例如,idtr
的值存储在内存位置 loc 中:
("sidt %0\n" : :"m"(loc)); |
匚wQ数字)U束
在某些情况下Q一个变量既要充当输入操作数Q也要充当输出操作数。可以通过使用匚wU束?
"asm" 中指定这U情c?
asm ("incl %0" :"=a"(var):"0"(var)); |
在匹配约束的CZ中,寄存?%eax 既用作输入变量,也用作输出变量。将 var 输入d? %eaxQ增加后更新的 %eax 再次存储?var 中。这里的 "0" 指定W?0 个输出变量相同的U束。即Q它指定 var 的输出实例只应该存储?%eax 中。该U束可以用于以下情况Q?/p>
使用匚wU束最重要的意义在于它们可以导致有效地使用可用寄存器?/p>
以下CZ通过各种不同的操作数U束说明了用法。有如此多的U束以至于无法将它们一一列出Q这里只列出了最l常使用的那些约束类型?/p>
"asm" 和寄存器U束 "r" 让我们先看一下用寄存器U束 r ?"asm"。我们的CZ昄?GCC 如何分配寄存器,以及它如何更新输出变量的倹{?
int main(void) |
在该例中Qx 的值复制ؓ "asm" 中的 y。x ?y 都通过存储在寄存器中传递给 "asm"。ؓ该例生成的汇~代码如下:
main: |
当?"r" U束ӞGCC 在这里可以自由分配Q何寄存器。在我们的示例中Q它选择 %edx 来存? x。在d?%edx ?x 的值后Q它?y 也分配了相同的寄存器?/p>
因ؓ y 是在输出操作数部分中指定的,所?%edx 中更新的值存储在 -8(%ebp)Q堆栈上 y 的位|中。如?y 是在输入部分中指定的Q那么即使它?y 的时寄存器存储?(%edx) 中被更新Q堆栈上 y 的g不会更新?/p>
因ؓ %eax 是在修饰列表中指定的QGCC 不在M其它地方使用它来存储数据?/p>
输入 x 和输?y 都分配在同一?%edx 寄存器中Q假设输入在输出产生之前被消耗。请注意Q如果您有许多指令,׃是这U情况了。要保输入和输出分配到不同的寄存器中,可以指定 & U束修饰W。下面是d了约束修饰符的示例?/p>
int main(void) |
以下是ؓ该示例生成的汇编代码Q从中可以明昑֜看出 x ?y 存储? "asm" 中不同的寄存器中?/p>
main: |
现在让我们看一下如何将个别寄存器作为操作数的约束指定。在下面的示例中Qcpuid 指o采用 %eax 寄存器中的输入,然后在四个寄存器中给出:%eax?ebx?ecx?edx。对 cpuid 的输入(变量 "op"Q传递到 "asm" ?eax 寄存器中Q因?cpuid 希望它这样做。在输出中?a、b、c ?d U束Q分别收集四个寄存器中的倹{?/p>
asm ("cpuid" |
在下面可以看Cؓ它生成的汇编代码Q假?_eax、_ebx {?.. 变量都存储在堆栈上)Q?/p>
movl -20(%ebp),%eax /* store 'op' in %eax -- input */ |
strcpy 函数可以通过以下方式使用 "S" ?"D" U束来实玎ͼ
asm ("cld\n |
通过使用 "S" U束源指针 src 攑օ %esi 中,使用 "D" U束目的指?dst 攑օ %edi 中。因?rep 前缀需?count |所以将它放?%ecx 中?/p>
在下面可以看到另一个约束,它用两个寄存器 %eax ?%edx 两? 32 位的值合q在一P然后生成一?4 位的|
#define rdtscll(val) \ |
在下面将看到pȝ调用的代码,它有四个参数Q?/p>
#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ |
在上例中Q通过使用 b、c、d ?S U束系l调用的四个自变量放? %ebx?ecx?edx ?%esi 中。请注意Q在输出中用了 "=a" U束Q这P位于 %eax 中的pȝ调用的返回值就被放入变?__res 中。通过匹配约?"0" 用作输入部分中第一个操作数U束Qsyscall ? __NR_##name 被放?%eax 中,q用作对pȝ调用的输入。这Pq里? %eax 既可以用作输入寄存器Q又可以用作输出寄存器。没有其它寄存器用于q个目的。另h意,输入Qsyscall P在生输出(syscall 的返回|之前被消耗(使用Q?/p>
误虑下面的原子递减操作Q?/p>
__asm__ __volatile__( |
为它生成的汇~类gQ?/p>
#APP |
您可能考虑在这里ؓ counter 使用寄存器约束。如果这样做Qcounter 的值必d复制到寄存器Q递减Q然后对其内存更新。但q样您会无法理解锁定和原子性的全部意图Q这些明显CZ使用内存U束的必要性?/p>
误虑内存拯的基本实现?/p>
asm ("movl $count, %%ecx; |
?lodsl 修改 %eax Ӟlodsl ?stosl 指o隐含C用它?ecx 寄存器明装?count。但 GCC 在我们通知它以前是不知道这些的Q我们是通过?%eax ?%ecx 包括在修饰寄存器集中来通知 GCC 的。在完成q一步之前,GCC 假设 %eax ?%ecx 是自qQ它可能军_它们用作存储其它的数据。请注意Q?esi ?%edi ?"asm" 使用Q它们不在修饰列表中。这是因为已l声?"asm" 在输入操作数列表中使用它们。这里最低限度是Q如果在 "asm" 内部使用寄存器(无论是明还是隐含地Q,既不出现在输入操作数列表中,也不出现在输出操作数列表中,必须它列ؓ修饰寄存器?/p>
ȝ来说Q内联汇~非常巨大,它提供的许多Ҏ我们甚臛_q里Ҏ没有涉及到。但如果掌握了本文描q的基本材料Q您应该可以开始对自己的内联汇~进行编码了?/p>
Original ipmsg protocol specification is written in Japanese.
This document was translated by Mr.Kanazawa.
This document is not verified yet.
----------------------------------------------------------------------
IP Messenger communication protocol (Draft-9) 1996/02/21
Modified 2003/01/14
H.Shirouzu
shirouzu@h.email.ne.jp
----------------------------------------------------------------------
About IP Messenger
This is a Send/Receive message service using the TCP/UDP Port.
Characteristics
IP Messenger can be installed in any OS if TCP/IP is used on your machine.
Dynamic member recognition can be done within your network or specified network.
You can exchange messages between all IPMsg members.
Function description
Use TCP/UDP port(default:2425). See the following descriptions
(Message Send/Receive: UDP, File Send/Receive: TCP)
1. Command
1) Command functions (Low 8 bits from command number 32 bits)
IPMSG_NOOPERATION No Operation
IPMSG_BR_ENTRY Entry to service (Start-up with a Broadcast command)
IPMSG_BR_EXIT Exit from service (End with a Broadcast command)
IPMSG_ANSENTRY Notify a new entry
IPMSG_BR_ABSENCE Change absence mode
IPMSG_BR_ISGETLIST Search valid sending host members
IPMSG_OKGETLIST Host list sending notice
IPMSG_GETLIST Host list sending request
IPMSG_ANSLIST Host list sending
IPMSG_SENDMSG Message transmission
IPMSG_RECVMSG Message receiving check
IPMSG_READMSG Message open notice
IPMSG_DELMSG Message discarded notice
IPMSG_ANSREADMSG Message open confirmation notice(added from version-8 )
IPMSG_GETFILEDATA File Transfer request by TCP
IPMSG_RELEASEFILES Discard attachment file
IPMSG_GETDIRFILES Attachment hierarchical file request
IPMSG_GETINFO Get IPMSG version info.
IPMSG_SENDINFO Send IPMSG version info.
IPMSG_GETABSENCEINFO Get absence sentence
IPMSG_SENDABSENCEINFO Send absence sentence
IPMSG_GETPUBKEY RSA Public Key Acquisition
IPMSG_ANSPUBKEY RSA Public Key Response
2) Option flag (High 24 bits from command number 32 bits)
IPMSG_ABSENCEOPT Absence mode(Member recognition command)
IPMSG_SERVEROPT Server(Reserved)
IPMSG_DIALUPOPT Send individual member recognition command
IPMSG_SENDCHECKOPT Transmission check
IPMSG_SECRETOPT Sealed message
IPMSG_READCHECKOPT Sealed message check(added from ver8 )
IPMSG_PASSWORDOPT Lock
IPMSG_BROADCASTOPT Broadcast message
IPMSG_MULTICASTOPT Multi-cast(Multiple casts selection)
IPMSG_NEWMUTIOPT New version multi-cast(reserved)
IPMSG_AUTORETOPT Automatic response(Ping-pong protection)
IPMSG_NOLOGOPT No log files
IPMSG_NOADDLISTOPT Notice to the members outside of BR_ENTRY
IPMSG_FILEATTACHOPT File attachment
IPMSG_ENCRYPTOPT Code
IPMSG_NOPOPUPOPT (No longer valid)
IPMSG_RETRYOPT Re-send flag(Use when acquiring HOSTLIST)
3) Extended code flag (hex format combination)
IPMSG_RSA_512
IPMSG_RSA_1024
IPMSG_RSA_2048
IPMSG_RC2_40
IPMSG_RC2_128
IPMSG_RC2_256
IPMSG_BLOWFISH_128
IPMSG_BLOWFISH_256
IPMSG_SIGN_MD5
4) Extended files for attachment (fileattr low 8 bits)
IPMSG_FILE_REGULAR
IPMSG_FILE_DIR
IPMSG_FILE_RETPARENT
IPMSG_FILE_SYMLINK
IPMSG_FILE_CDEV
IPMSG_FILE_BDEV
IPMSG_FILE_FIFO
IPMSG_FILE_RESFORK
5) Attachment file extended attribute(fileattr high 24 bits)
IPMSG_FILE_RONLYOPT
IPMSG_FILE_HIDDENOPT
IPMSG_FILE_EXHIDDENOPT
IPMSG_FILE_ARCHIVEOPT
IPMSG_FILE_SYSTEMOPT
6) Extended file attribute for attachment file
IPMSG_FILE_UID
IPMSG_FILE_USERNAME
IPMSG_FILE_GID
IPMSG_FILE_GROUPNAME
IPMSG_FILE_PERM
IPMSG_FILE_MAJORNO
IPMSG_FILE_MINORNO
IPMSG_FILE_CTIME
IPMSG_FILE_MTIME
IPMSG_FILE_ATIME
IPMSG_FILE_CREATETIME
IPMSG_FILE_CREATOR
IPMSG_FILE_FILETYPE
IPMSG_FILE_FINDERINFO
IPMSG_FILE_ACL
IPMSG_FILE_ALIASFNAME
IPMSG_FILE_UNICODEFNAME
2.Command format(Use all character strings)
1) Command(Format version-1)
Ver(1) : PacketNo : SenderName : SenderHost : CommandNo : AdditionalSection
2) An example for Message Send/Receive by using the current command format
"1:100:shirouzu:jupiter:32:Hello"
3.Command process overview
1) Member recognition
An IPMSG_BR_ENTRY command notifies a new entry to the current
members at start-up.
All members add the new member to their list after getting a notification message.
An IPMSG_ANSENTRY command sends a message back to the new member.
The new member gets the current member data by a
IPMSG_ANSENTRY command. All members can communicate as long as an
IP packet exists.
An IPMSG_BR_ABSENCE command broadcasts absence mode cancel or
nickname change to all members. However, an IPMSG_ANSENTRY command
does not send a message back, which is different from an IPMSG_BR_ENTRY
command.
IPMSG_BR_ENTRY, IPMSG_ANSENTRY, and IPMSG_BR_ABSENCE commands
use an IPMSG_ABSENCEOPT flag for absence mode. Input a nickname to
additional command.
Add an IPMSG_DIALUPOPT flag for dial-up users who can't be reached by
a broadcast command. A member recognition command needs to be
sent individually to the members with this optional flag.
(Extended group)IPMSG_BR_ENTRY and IPMSG_BR_ABSENCE commands
sends a group name by adding the new group name after the current
command format character strings (Input '\0' between the current
command and extended name).
2) Send/Receive Message
Send Message uses an IPMSG_SENDMSG command that can input a message
in the extended area.
Receive Message sends back an IPMSG_RECVMSG command only
if an IPMSG_SENDCHECKOPT flag is ON. Input the original packet number
to the extended area.
Broadcast Message Send uses an IPMSG_BOADCASTOPT command
and an IPMSG_SENDMSG flag should be ON.
Auto-Send packet(absence notice) needs to be added to IPMSG_AUTORETOPT
for ping-pong protection. If either one or another packet is ON, then
confirmation/auto-send packet is not sent back.
Send Message Sealing needs to be an IPMSG_SECRETOPT packet ON.
In this case, Receive Message sends an IPMSG_READMSG command.
Input the original packet number to the extended area.
(Additional IPMSG_NOADDLISTOPT)
When receiving an IPMSG_SENDMSG packet from a host that is
not on your Send/Receive list, IPMsg will either confirm a host by
sending an IPMSG_BR_ENTRY command or add a host name to
the Send/Receive list.
However, single-shot Message Send/Receive action needs to be avoided.
Add an IPMSG_NOADDLISTOPT flag to an IPMSG_SENDMSG command.
(Additional IPMSG_READCHECKOPT from version-8 )
When an IPMSG_READMSG command contains an IPMSG_READCHECKOPT flag,
IPMsg process is the same as IPMSG_SENDMSG with an
IPMSG_SENDCHECKOPT flag.
However, Send Message uses an IPMSG_ANSREADMSG command,
not IPMSG_RECVMSG.
3) Message Send/Receive 亅encrypted extension (Added in the version-9 )
Use the combination of Public-key(RSA) and common key(RC2/Blowfish).
(Encrypted extension area is used in hex format.)
(Public key acquisition)Send an IPMSG_GETPUBKEY command to Receive
Message. Receive Message gets an IPMSG_ANSPUBKEY that
means receiving RSA public key from Send Message.
IPMSG_GETPUBKEY/IPMSG_ANSPUBKEY both require the value which is
encryption capability (Exp. IPMSG_RSA_1024) flag uses "OR" at first
part of extension
In addition, In IPMSG_ANSPUBKEY, public key written as EE-NNNNNN
E=Exponent丄N=method)devide by ':'. and Input the Fdelimiter '-'
between E and N.
This sequence can be skipped after the 2nd Send/Receive process by
memorizing public key and encrypted data.
(Encrypted message)After a sender creates a common key that is
supported both sender and receiver, a common key can encrypt a message.
In addition, a receiver's public key encrypts the common key.
(Encrypted message transmission) IPMSG_ENCRYPTOPT is used in
IPMSG_SENDMSG. At the first part of extension, input the value which
is 'or' resoult from Convination of public key and common key type .
Then use common key which encrypt with public key devide by ':'.
Then input message which is eccrypted by public key devide by ':'.
If both supports IPMSG_SIGN_XXX, then add ':' and signeture.
Also, In the method of encode padding, PKCS#1ECB key is used for RSA,
PKCS#5 CBC common key is used for RC2/blowfish.
Also, The Packet related to Entry manifestation the capability of
ecryption support using IPMSG_ENCRYPTOPT
4) Extension with file attachment(Available from version-9 )
An IPMSG_SENDMSG command with an IPMSG_FILEATTACHOPT flag for
File transfer (download permission)notification sends a message
with attachment.
Input '\0' after the message and attachment file data.
fileID:filename:size:mtime:fileattr[:extend-attr=val1
[,val2...][:extend-attr2=...]]:\a:fileID...
(size, mtime, and fileattr describe hex format.
If a filename contains ':', please replace with "::".)
When Receive Message downloads an attachment file, an IPMSG_GETFILEDATA
command requests a data transmission packet to the TCP port that is the same number
as the UDP sending port number. Input packetID:fileID: offset to the extended area.
(Use all hex format.)
File Transfer side receives the request. After recognizing that it's a correct request,
then send the specified data (no format)
When the data receiving side downloads a hierarchical attachment file,
use an IPMSG_GETDIRFILES command and input a packetID:fileID
to the extended area and send a data transmission request packet.
(all hex format)
Data sending side sends the following hierarchical data format.
header-size:filename:file-size:fileattr[:extend-attr=val1
[,val2...][:extend-attr2=...]]:contents-data
Next headersize: Next filename...
(All hex format except for filename and contetns-data)
header-size is from the beginning of header-size to the delimiter ':'
that is before contents-data. extend-attr can be omitted and used multiple
extended attributes. Use '=' for data input.
When fileattr is IPMSG_FILE_DIR, IPMsg recognizes that it is automatically
in the directory, the next file data is after the directory.
When fileattr is IPMSG_FILE_RETPARENT, IMPsg recognizes that it returns
to the parent directory. In this case, File name is always "." and the attribute
value is the current directory data.
Sending process starts from the attachment directly and returns the
IPMSG_FILE_RETPARENT command to the attachment directory.
Add an IPMSG_FILEATTACHOPT flag for an Entry packet to support the
attachment file.
5) Other commands
When acquiring different versions, send an IPMSG_GETINFO command.
Receiving side sends the version information character string to
extended area.
Send an IPMSG_GETABSENCEINFO command for acquiring an absence message.
Receiving side sends an IPMSG_SENDABSENCEINFO back if the status is absence mode.
If the status is not absence mode, a character string "Not absence mode" will be sent back.
6) Confirmation/Retry
If a confirmation packet for IPMSG_SENDMSG or IPMSG_RECVMSG is not delivered
within a specified time, then it will be sent again.
A number of retry actions or interval period is depended on the current condition.
4. Other
1) Linefeed
Linefeed characters in Send Message is standardized with UNIX type ('0x0a').
Please change if needed.
2) Delimiter ':'
':' is used as a delimiter. You can't use this delimiter for user name
and host name.
If the use/host names contain a ':', please replace with another sign,
for an example ';'.
Although using this delimiter isn