Skip to content

Instantly share code, notes, and snippets.

@creamidea
Last active January 1, 2016 12:09
Show Gist options
  • Save creamidea/8142576 to your computer and use it in GitHub Desktop.
Save creamidea/8142576 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void print(int a, int b) {
printf("a=%d, b=%d\n", a, b);
}
int v1(int a, int b) {
int c;
c = a;
a = b;
b = c;
print(a, b);
}
int v2(int a, int b) {
a = a + b - (b = a);
print(a, b);
}
int v3(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
print(a, b);
}
int v4(int a, int b) {
a = a - b;
b = b + a;
a = b - a;
print(a, b);
}
int v5(int a, int b) {
a = a ^ b;
b = b ^ a;
a = a ^ b;
print(a, b);
}
int v6(int a, int b) {
b = b ^ a;
a = a ^ b;
b = b ^ a;
print(a, b);
}
int main(int argc, char *argv[])
{
int a = 10, b = 20;
printf("Origin: a=%d, b=%d\n", a, b);
v1(a, b);
v2(a, b);
v3(a, b);
v4(a, b);
v5(a, b);
v6(a, b);
return 0;
}
;; #include <stdio.h>
;; int main(int argc, char *argv[])
;; {
;; int p = 10, q = 20;
;; p = p + q - (q = p);
;; return 0;
;; }
.file "hello.c"
.def ___main; .scl 2; .type 32; .endef
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB6:
;; .cfi_startproc is used at the beginning of each function that should have an entry in .eh_frame. It initializes some internal data structures and emits architecture dependent initial CFI instructions. Don't forget to close the function by .cfi_endproc.
;; from: http://web.mit.edu/rhel-doc/3/rhel-as-en-3/index.html
.cfi_startproc ;加锁
pushl %ebp ;将ebp压栈,pushl = push long
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp ;esp值赋给ebp,设置main函数的栈基址
.cfi_def_cfa_register 5
andl $-16, %esp ;内存对齐?
subl $16, %esp ;esp向下移动16字节,存储局部变量
call ___main
;; 赋值 p q
movl $10, 12(%esp) ;12(esp) = 10
movl $20, 8(%esp) ;8(esp) = 20
movl 8(%esp), %eax ;eax = 20
movl 12(%esp), %edx ;edx = 10
addl %eax, %edx ;edx = 10 + 20
movl 12(%esp), %eax ;eax = 10
movl %eax, 8(%esp) ;8(%esp) = 10
movl %edx, %eax ;eax = 30
subl 8(%esp), %eax ;eax = 30 - 10 (%eax - 8(%esp))
movl %eax, 12(%esp) ;12(%esp) = 20
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc ;解锁
LFE6:
@creamidea
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment