我写了一个程序,使用异或交换两个变量。
var1 = var1 ^ var2;
var2 = var2 ^ var1;
var1 = var1 ^ var2;
我编译它以获得程序集输出以及其他
$ gcc Q1.c -save-temps -o Q1
I Get this Output in Assembly form…
movl 24(%esp), %edx
movl 28(%esp), %eax
xorl %edx, %eax
movl %eax, 24(%esp)
movl 28(%esp), %edx
movl 24(%esp), %eax
xorl %edx, %eax
movl %eax, 28(%esp)
movl 24(%esp), %edx
movl 28(%esp), %eax
xorl %edx, %eax
我不熟悉x86汇编,但我在ARM汇编工作过。24和28在这里是什么意思?
movl 28(%esp), %edx
movl 24(%esp), %eax
%esp
为堆栈指针。24(%esp)
读取地址%esp
+ 24的值。
我很容易看出你的困惑。有人纠正我,但我想那是AT&T语法,至于他们如何或在哪里放"%"符号,使用括号等等,编译器编写者可以随心所欲。(如果你不喜欢他们所做的,写你自己的编译器;而且是免费的,等等)
我已经用Intel语法为你重写了这个。我记不清具体叫什么了,但不管怎样,在他们的语法中,目标在指令中首先出现,其他部分紧随其后。寄存器名周围的括号表示"您将在此寄存器指向的地址上找到的东西"使用许多寄存器,您可以添加自己的偏移量,芯片将生成添加该偏移量的地址。
警告,我认为这是对的,但我现在真的应该回家睡觉了。
不管怎样,看看这是否有帮助
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Arun's Swap Via Xor Function ;
; ;
; Arun is studying C and ASM ;
; ;
; On Entry: var1 is at the 24th place in the stack ;
; ;
; var2 is at the 28th place in the stack ;
; ;
; Both of these are 32 bit numbers which ;
; is why they are 4 bytes apart ;
; ;
; On Exit: var1 is in Eax ;
; ;
; var2 is in Edx ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Aruns_Swap_Via_Xor_Function:
MovL Edx, [Esp + 24] ;var1 goes into Edx
MovL Eax, [Esp + 28] ;var2 goes into Eax
XorL Eax, Edx ;Xor them and put the result in Eax
MovL [Esp + 24], Eax ;Store the result in var1 on the stack
MovL Edx, [Esp + 28] ;Original var2 goes in Edx this time
MovL Eax, [Esp + 24] ;The bit fiddled var1 is now in Eax
;Be aware, this is not exactly optimized
; but it will work, and this compiler apparently
; doesn't want to take chances.
; The brass tacks are that this instruction
; as it appears here, is a defacto Nop
XorL Eax, Edx ;Now Xor both of those values and put the result in Eax
MovL [Esp + 28], Eax ;That modified value goes into var2
;(Be alert, this is really the original var1)
MovL Edx, [Esp + 24] ;The original var2 is now in Edx
MovL Eax, [Esp + 28] ;The modified var2 is now in Eax
XorL Eax, Edx ;Xor those two and put the result in Eax
;Now Eax and Edx hold each other's original contents
;
;(and life goes on)
就这么多了。以防你在某些汇编语言课程中遇到这个问题,这个问题多年来一直吸引着人们(包括我)。这可能是你的教授在寻找的东西。顺便说一下,你可以在互联网上找到这个,实际上是维基百科。
前面的11条指令可以减少到3条指令
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; A slightly optimized swap function ;
; ;
; Arun is studying C and ASM ;
; ;
; On Entry: var1 is in Eax ;
; ;
; var2 is in Ebx ;
; ;
; Both of these are 32 bit numbers, and ;
; so we will use 32 bit instructions. ;
; ;
; On Exit: var1 is in Ebx ;
; ;
; var2 is in Eax ;
; ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Slightly_Optimized_Swap_Routine:
XorL Eax, Ebx ;Xor them and put the result in Ebx
;At this point Eax is var1 and Ebx is weird number
XorL Ebx, Eax ;Xor that result with the origial var1
;At this point, Eax is var2 and Ebx is still weird number
XorL Eax, Ebx ;Xor them and put the result in Ebx
;At this point, Eax is var2 and Ebx is Var1
;
;(and life goes on)
最后,硬件设计师和微处理器来拯救…
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; A slightly better optimized swap function ;
; ;
; Brought to you by Clint on Stack Overflow ;
; ;
; On Entry: var1 is in Eax ;
; ;
; var2 is in Ebx ;
; ;
; Both of these are 32 bit numbers, and ;
; so we will use 32 bit instructions. ;
; ;
; On Exit: var1 is in Ebx ;
; ;
; var2 is in Eax ;
; ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Slightly_Better_Swap_Routine:
XchgL Eax, Ebx ;Microprocessor magic, exchange them in one instruction.
; Honest, hardware guys can do stuff like that. The previous
; eleven instructions have been reduced to one. Ta-Da
;
;(and life goes on)
我推荐阅读Programming Ground Up (x86汇编,GAS语法)
http://gnu.mirrors.pair.com/savannah/savannah//pgubook/ProgrammingGroundUp-1-0-booksize.pdf你也可以试试这个
gcc -S -masm=intel test.c
查看英特尔语法
这些都是偏移量,你必须加上esp的值才能得到真正的地址