- 论坛徽章:
- 0
|
本帖最后由 hanshu830 于 2012-09-11 11:01 编辑
我声明2个全局变量, 想计算2个变量之间偏移,
但是gcc翻译成的汇编, 把两个变量地址相减的结果右移了两位才输出。
以下是C代码:- int start = 1;
- int end = 2;
- int a()
- {
- int off;
- off = &end - &start;
- }
复制代码 以下是 x86_gcc 翻译的汇编结果- Disassembly of section .text:
- 0000000000000000 <a>:
- 0: 55 push %rbp
- 1: 48 89 e5 mov %rsp,%rbp
- 4: ba 00 00 00 00 mov $0x0,%edx
- 5: R_X86_64_32 end
- 9: b8 00 00 00 00 mov $0x0,%eax
- a: R_X86_64_32 start
- e: 48 89 d1 mov %rdx,%rcx
- 11: 48 29 c1 sub %rax,%rcx
- 14: 48 89 c8 mov %rcx,%rax
- 17: 48 c1 f8 02 sar $0x2,%rax
- 1b: 89 45 fc mov %eax,-0x4(%rbp)
- 1e: c9 leaveq
- 1f: c3 retq
- Disassembly of section .data:
- 0000000000000000 <start>:
- 0: 01 00 add %eax,(%rax)
- ...
- 0000000000000004 <end>:
- 4: 02 00 add (%rax),%al
- ...
复制代码 以下是 arm-eabi-gcc4.4.3的结果- Disassembly of section .text:
- 00000000 <a>:
- 0: e52db004 push {fp} ; (str fp, [sp, #-4]!)
- 4: e28db000 add fp, sp, #0 ; 0x0
- 8: e24dd00c sub sp, sp, #12 ; 0xc
- c: e59f2018 ldr r2, [pc, #24] ; 2c <a+0x2c> //取 end变量的地址
- 10: e59f3018 ldr r3, [pc, #24] ; 30 <a+0x30> //取 start变量的地址
- 14: e0633002 rsb r3, r3, r2 //相减
- 18: e1a03143 asr r3, r3, #2 //算术右移 2位
- 1c: e50b3008 str r3, [fp, #-8] //付值给 off
- 20: e28bd000 add sp, fp, #0 ; 0x0
- 24: e8bd0800 pop {fp}
- 28: e12fff1e bx lr
- ...
- 2c: R_ARM_ABS32 end
- 30: R_ARM_ABS32 start
- Disassembly of section .data:
- 00000000 <start>:
- 0: 00000001 .word 0x00000001
- 00000004 <end>:
- 4: 00000002 .word 0x00000002
复制代码 |
|