éæ²¢è¡ãã®çµé»ãéããã®ã§ã
ã¢ã»ã³ãã©ã«ãããããã¦ã¿ãã
ã¨ãããã
ãããªã³ã¼ããæ¾ã£ã¦ãããããã§é£¯ä¸æ¯ã¯é£ãã
# sample000.s .text .globl _main _main: movl $0, %eax ret
åããã¦ã¿ã
$ gcc -g sample000.s -o sample000 && gdb ./sample000 (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample000 Reading symbols for shared libraries ++. done Program exited normally. (gdb)
ãªããåããã¿ãã
ãã¬ã¼ã¯ãã¦ã¿ã
(gdb) break main Breakpoint 1 at 0x1ffa: file sample000.s, line 5. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample000 Breakpoint 1, _main () at sample000.s:5 5 movl $0, %eax (gdb)
ãããæ¢ã¾ã£ãã
register ã®å¤ãè¦ã¦ã¿ããï¼ã¡ãªã¿ã« regist ã¨ããè±èªã¯ãªããããï¼
(gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ffa 0x1ffa <_main> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 Current language: auto; currently asm (gdb)
ãã¹wwwãæå³ä¸www
step ã㦠register ã®å¤ãè¦ã¦ã¿ã
(gdb) step 6 ret (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1fff 0x1fff <_main+5> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
eip ã®å¤ã 0x1fa ãã 0x1ff ã«ãªã£ãã ãããä»å®è¡ä¸ã®å ´æã表ãä½ãã¨ãããã¨ãã
_main ã _main+5 ã«ãªã£ã¦ãããã¸ã¼ããããããã¨ãã
movl $0 %eax 㯠5 ãã¤ãã®å¦çã¨ãããã¨ããããã
step ãã¦ã¿ã
(gdb) step 0x00001fce in start ()
ãªããã start ã£ã¦ããé¢æ°ã«æ»ã£ã¦ããã
ç¥ã®é åã«å
¥ã£ã¦ããã¿ãããªæ°åã
ã¨ããããã next ããã¾ãã£ã¦çµäºããããªãããããããªãã¨ãè¡ããã¦ãããã ãªãã
ã¡ãã£ã¨ãã¤ã¬ãã£ã¦ãã
æ»ã£ã¦ããã
ã¹ã¿ãã¯ãã¤ã³ã¿ãè¦ã¦ã¿ããã
_main ãã _main ãå¼ã³åºãã¦ã©ããªã£ã¦ãããè¦ã¦ã¿ã
# sample001.s .text .globl _main _main: call _main
ãããªæãï¼
ç¡éã«ã¼ãã ããããã®ã¾ã¾å®è¡ããªãããã«æ³¨æããªããå®è¡
$ gcc -g sample001.s -o sample001 && gdb ./sample001 (gdb) break main Breakpoint 1 at 0x1ffa: file sample001.s, line 4. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample001 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample001.s:4 4 call _main (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ffa 0x1ffa <_main> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 Current language: auto; currently asm
ããã§ãããä¸å main ãå¼ã³åºã
(gdb) step Breakpoint 1, _main () at sample001.s:4 4 call _main (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8e8 0xbffff8e8 ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ffa 0x1ffa <_main> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55
ããã§å¤ãå¤ãã£ãã®ã esp ã ããã¹ã¿ãã¯ã®ãã¤ã³ã¿ãã
ã¹ã¿ãã¯ã®ãã¤ã³ã¿ã£ã¦ã¹ã¿ãã¯ã«ãã¼ã¿ãç©ãã¨æ¸ããã§ããã
ret ã¯ãã åã« jump ã㦠4 ãã¤ãã®ä½ããã¹ã¿ãã¯ã«ç©ãã ãã®å½ä»¤ãªãã ã
ãã£ã¨ããããããã®ãã¨æã£ã¦ããï¼ãã£ã¦ããããããªããã©ï¼
ã¹ã¿ãã¯ã«ä½ãå
¥ã£ã¦ããããè¦ã¦ã¿ãã
ããã§ãã¬ã¸ã¹ã¿ã®å¤ãã©ããã£ã¦ print ãã使ããããããwww
ç´æ¥ã¢ãã¬ã¹ãæå®ãã¦ã¿ã
(gdb) print *(void**) 0xbffff8e8 $1 = (void *) 0x1fff
ããã sample000.s ã®ã¨ãã«ç¢ºèªãã eip ã®å¤ã _main+5 ã®ä½ç½®ãå
¥ã£ã¦ããã ã
ã£ã¦ãã¨ã¯ call main ã 5 ãã¤ãå½ä»¤ã§ã call main ã®æ¬¡ã®è¡ãæãã¦ããã ã
ãããã ret ã¯ããã«æ»ãããã®ãã®ãã
次ã¯ãå¥ã®é¢æ°ãå¼ã³åºãããã°ã©ã ãæ¸ãã¦ã¿ãã
.text .globl _sub _sub ret .globl _main _main: call _sub movl $0, %eap ret
ãããªæã
$ gcc -g sample002.s -o sample002 && gdb ./sample002 (gdb) break main Breakpoint 1 at 0x1ff3: file sample002.s, line 7. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample002 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample002.s:7 7 call _sub (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff3 0x1ff3 <_main> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 Current language: auto; currently asm (gdb) step _sub () at sample002.s:4 4 ret (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8e8 0xbffff8e8 ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff2 0x1ff2 <_sub> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ãã _main ã«ããã¨ã㨠_sub ã«ããã¨ãã® eip ã®å·®ã 1 ã¨ãããã¨ã¯ ret 㯠1 ãã¤ãã®å½ä»¤ãªãã ã¼ã
ã§ã step ã㦠ret ã®æåãè¦ã
(gdb) step _main () at sample002.s:8 8 movl $0, %eax (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff8 0x1ff8 <_main+5> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ãããããããwãã¹w åã®ã¹ã¿ãã¯ã®å¤ã調ã¹ãã®å¿ããï¼ï¼
ãã£ããæ»ã£ã¦èª¿ã¹ã
(gdb) kill Kill the program being debugged? (y or n) y (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample002 Breakpoint 1, _main () at sample002.s:7 7 call _sub (gdb) step _sub () at sample002.s:4 4 ret (gdb) print *(void**) 0xbffff8e8 $1 = (void *) 0x1ff8
ãããã 0x1ff8 ã¤ã¾ãã ret 㯠movl $0, %eax ã«çããã
ã§ã %eip 㨠%esp ã®å¤ããå¤ãã£ã¦ããªãã¨ãããã¨ããã ret ã¯ã¹ã¿ãã¯ã®ä¸çªä¸ã®å¤ãããããã¦ã¸ã£ã³ãããã ãã ã¨åããã
ãããã ret ã®é¨åããããã¨ã¸ã£ã³ãã«ãã¦ãåãæåããããããããã¦ã¿ãã
ã¸ã£ã³ãã¨ããããã©ãæ¸ãã°ããã調ã¹ã¦ããï¼
調ã¹ä¸
http://developer.apple.com/documentation/DeveloperTools/Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
ã¯ãã¯ãããã£ã¨èª¿ã¹ã
ãããªæãï¼
.text .globl _sub _sub: popl %ebx jmp (%ebx) .globl _main _main: call _sub movl $0, %eax ret
å®è¡ãã¦ã¿ã
$ gcc -g sample003.s -o sample003 && gdb ./sample003 (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample003 Reading symbols for shared libraries ++. done Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x000000b8 0x000000b8 in ?? () (gdb)
ãã¯wwã£w è½ã¡ããww
$ gcc -g sample003.s -o sample003 && gdb ./sample003 (gdb) break main Breakpoint 1 at 0x1ff5: file sample003.s, line 8. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample003 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample003.s:8 8 call _sub (gdb) step Current language: auto; currently asm _sub () at sample003.s:4 4 popl %ebx (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8e8 0xbffff8e8 ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff2 0x1ff2 <_sub> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb) step _sub () at sample003.s:5 5 jmp (%ebx) (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0x1ffa 8186 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff3 0x1ff3 <_sub+1> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb) step Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x000000b8 0x000000b8 in ?? () (gdb)
0xb8 ã«é£ã¼ãã¨ãã¦ããã jmp ã®ã¨ããã®æ¬å¼§ããããªããã ãªã
æ¬å¼§ã¯ã¡ã¢ãªãåç
§ããããã®ãã®ã
.text .globl _sub _sub: popl %ebx jmp %ebx .globl _main _main: call _sub movl $0, %eax ret
ç´ãã¦å®è¡ï¼
$ gcc -g sample003.s -o sample003 && gdb ./sample003 (gdb) break main Breakpoint 1 at 0x1ff5: file sample003.s, line 8. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample003 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample003.s:8 8 call _sub (gdb) step Current language: auto; currently asm _sub () at sample003.s:4 4 popl %ebx (gdb) step _sub () at sample003.s:5 5 jmp %ebx (gdb) step _main () at sample003.s:9 9 movl $0, %eax (gdb) step 10 ret (gdb) step 0x00001fc6 in start () (gdb)
ããï¼åãããï¼ ret ã®åã㯠ok
ããã§ã¹ã¿ãã¯ã¨ call ã ret ãç解ã§ããããããã°ã©ã ã£ã½ããã®ãä½ã£ã¦ã¿ã
ãããªã¨ãã¯
ã¿ããªå¤§å¥½ããã£ããããï¼
ã¾ã足ãç®ãåãããªãããã
ãããè¦ãã¨æ®éã«å¼ãæ¸ããã¿ããã ãããã¼ addl ã¨ããããªããã ã¼ã
http://developer.apple.com/documentation/DeveloperTools/Reference/Assembler/ASMSyntax/chapter_3_section_3.html#//apple_ref/doc/uid/TP30000821-TPXREF114
1 + 1 ãã㦠%eax ã«å
¥ãã _sub é¢æ°ãä½ã£ã¦ã¿ãã
1 + 1 㯠$ 1 + 1 ã§ã§ããã
ãã®å¾ãå¤æ°ã«å
¥ãããæ°åã«ãªã£ããã©ããã¯ã¢ã»ã³ãã©ã®ä¸çã ãããå¤æ°ãªã©ãªãã®ã ã£ãã
.text .globl _sub _sub: movl $1 + 1, %eax ret .globl _main _main: call _sub movl $0, %eax ret
å®è¡
$ gcc -g sample004.s -o sample004 && gdb ./sample004 (gdb) break main Breakpoint 1 at 0x1ff4: file sample004.s, line 8. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample004 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample004.s:8 8 call _sub (gdb) step Current language: auto; currently asm _sub () at sample004.s:4 4 movl $1 + 1, %eax (gdb) step 5 ret (gdb) step _main () at sample004.s:9 9 movl $0, %eax (gdb) info register eax 0x2 2 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff9 0x1ff9 <_main+5> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ãã %eax ã« 2 ãå
¥ã£ã¦ãã 1 + 1 㯠2 ã ï¼
ã§ãããã® movl $ 1 + 1, %eax ã£ã¦ãå®éã¯ãããããããã®å½ä»¤ãå«ã¾ãã¦ããã ãããªãã
ã¨ããããã§ä½ãã¤ããããè¦ã¦ã¿ã
$ gcc -g sample004.s -o sample004 && gdb ./sample004 (gdb) break main Breakpoint 1 at 0x1ff4: file sample004.s, line 8. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample004 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample004.s:8 8 call _sub (gdb) step Current language: auto; currently asm _sub () at sample004.s:4 4 movl $1 + 1, %eax (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8e8 0xbffff8e8 ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1fee 0x1fee <_sub> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb) step 5 ret (gdb) info register eax 0x2 2 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8e8 0xbffff8e8 ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ff3 0x1ff3 <_sub+5> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
%eip ã® _sub ã _sub+5 ã«ãªã£ã¦ããã£ã¦ãã¨ã¯ 5 ãã¤ãã§ããã®ï¼
ã£ã¦ãã¨ã¯ãããå®éã¯å
ã«è¨ç®ãã㦠movl $2, %eax ã¨ãã¦å®è¡ããã¦ããããï¼ï¼
足ãç®ããã®ã¯ãã³ã³ãã¤ã©ã§ãããããã¨ãããªãã
ããããã
ããä»æ§ãèªãã§ã¿ãã¨å®æ°ããå¼ã«ã§ããªãã¿ããã ãªã
ã¨ããããã§ããã£ãã足ãç®ãããããã°ã©ã ãæ¸ãã¦ã¿ã
.text .globl _sub _sub: popl %eax popl %ebx add %ebx, %eax ret .globl _main _main: pushl $1 pushl $1 call _sub movl $0, %eax ret
ãããå®è¡ãã¦ã¿ã
$ gcc -g sample005.s -o sample005 && gdb ./sample005 (gdb) break main Breakpoint 1 at 0x1fef: file sample005.s, line 10. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample005 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample005.s:10 10 pushl $1 (gdb) step Current language: auto; currently asm _main () at sample005.s:11 11 pushl $1 (gdb) step _main () at sample005.s:12 12 call _sub (gdb) step _sub () at sample005.s:4 4 popl %eax (gdb) step _sub () at sample005.s:5 5 popl %ebx (gdb) step _sub () at sample005.s:6 6 movl %ebx, %eax (gdb) info register eax 0x1ff8 8184 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0x1 1 esp 0xbffff8e8 0xbffff8e8 ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1fec 0x1fec <_sub+2> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ãããï¼
1 ã push ã㦠1 ã push ã㦠call ãããã ããããã®ã³ã¼ãã 㨠0x1ff8 (æ¬æ¥ã®æ»ãçªå°) + 1 ã«ãªã£ã¦ãã¾ãã®ãï¼ï¼
ãããæ»ãçªå°ã 0x1 ã¨ããå ´æã«ãªã£ã¦ãã¾ã
ã¨ãããã¨ã¯ãå
ã«æ»ãçªå°ã pop ãã¦ãããå¼æ°ã pop ãã¦è¨ç®ãã¦ãã¾ãæ»ãçªå°ã push ã㦠ret ããã°ããã®ããªï¼
ããã©ããããã
ã¨ããããããããªæãã«ãªãã®ããªã
.text .globl _sub _sub: popl %edx # æ»ãçªå°ãéé¿ popl %eax popl %ebx add %ebx, %eax pushl %edx # æ»ãçªå°ãè¨å® ret .globl _main _main: pushl $1 pushl $1 call _sub movl $0, %eax ret
å®è¡ãã¦ã¿ã
$ gcc -g sample005.s -o sample005 && gdb ./sample005 (gdb) break main Breakpoint 1 at 0x1ff1: file sample005.s, line 12. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample005 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample005.s:12 12 pushl $1 (gdb) step Current language: auto; currently asm _main () at sample005.s:13 13 pushl $1 (gdb) step _main () at sample005.s:14 14 call _sub (gdb) step _sub () at sample005.s:4 4 popl %edx # æ»ãçªå°ãéé¿ (gdb) step _sub () at sample005.s:5 5 popl %eax (gdb) step _sub () at sample005.s:6 6 popl %ebx (gdb) step _sub () at sample005.s:7 7 add %ebx, %eax (gdb) step 8 pushl %edx # æ»ãçªå°ãè¨å® (gdb) step _sub () at sample005.s:9 9 ret (gdb) step _main () at sample005.s:15 15 movl $0, %eax (gdb) info register eax 0x2 2 ecx 0xbffff914 -1073743596 edx 0x1ffa 8186 ebx 0x1 1 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ffa 0x1ffa <_main+9> eflags 0x202 514 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ããããã %eax ã« 2 ãå
¥ã£ã¦ããï¼ãã£ã 1 + 1 ãã§ãããï¼ã¯ã©ã©ãç«ã£ããï¼
ãµã¨æã£ãããã® _sub ã£ã¦ C è¨èªãããå¼ã¹ãã®ããªï¼
C è¨èªã§ã¯å¼æ°ã¯ã©ããã£ã¦æ¸¡ããã ããã
ã¨ããããã§ã main ã C è¨èªã®ã»ãã§å®è£
ãã
# sample006.s .text .globl _sub _sub: popl %edx # æ»ãçªå°ãéé¿ popl %eax popl %ebx add %ebx, %eax pushl %edx # æ»ãçªå°ãè¨å® ret
/* sample006.c */ int sub(int, int); int main() { sub(1, 2); return 0; }
å®è¡ãã¦ã¿ã
$ gcc -g sample006.s sample006.c -o sample006 && gdb ./sample006 (gdb) break main Breakpoint 1 at 0x1fe3 (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample006 Reading symbols for shared libraries ++. done Breakpoint 1, 0x00001fe3 in main () (gdb) step Single stepping until exit from function main, which has no line number information. _sub () at sample006.s:4 4 popl %edx # æ»ãçªå°ãéé¿ (gdb) step Current language: auto; currently asm _sub () at sample006.s:5 5 popl %eax (gdb) step _sub () at sample006.s:6 6 popl %ebx (gdb) step _sub () at sample006.s:7 7 add %ebx, %eax (gdb) step 8 pushl %edx # æ»ãçªå°ãè¨å® (gdb) step _sub () at sample006.s:9 9 ret (gdb) info register eax 0x3 3 ecx 0xbffff914 -1073743596 edx 0x1ff7 8183 ebx 0x2 2 esp 0xbffff8d4 0xbffff8d4 ebp 0xbffff8e8 0xbffff8e8 esi 0x0 0 edi 0x0 0 eip 0x1fdc 0x1fdc <_sub+6> eflags 0x206 518 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb) print *(void**) 0xbffff8d4 $1 = (void *) 0x1ff7 (gdb) step 0x00001ff7 in main () (gdb)
ãããã¡ãã㨠1 + 2 ã®çµæã %eax ã«ã®çµæãå
¥ã£ãï¼
ã§ãã¡ãã㨠ret ãåãã¦ãã£ã½ã
å¼æ°ã¯ (1, 2) ã 㨠pushl $2 ã pushl $1 ã®é 㧠push ãããã¿ããã å¿ãããã ããè¦ãã¦ãããã
C è¨èªããã åã«å¼æ°ã push ã㦠call ãã¦ãã ãã ã¨åãã£ãï¼
足ãç®ãã§ããããã¨ã¯ãæ¡ä»¶åå²ã ãªãã
ã¡ãã£ã¨ãæ¡ä»¶ä»ãã¸ã£ã³ãå½ä»¤ã«ã¤ãã¦èª¿ã¹ã¦ããï¼
ã¨ããããããã£ããããæ°ãæ±ããé¢æ°ãæ¸ãã¦ã¿ãï¼
.text .globl _sub _sub: popl %edx popl %ecx cmp $2, %ecx ja sub_a movl $1, %eax pushl %edx jmp sub_return sub_a: pushl %edx dec %ecx pushl %ecx dec %ecx pushl %ecx call _sub popl %ecx pushl %eax pushl %ecx call _sub popl %ecx add %ecx, %eax sub_return: ret .globl _main _main: pushl $6 call _sub ret
ã¯ãã¯ãã
ã§ããå®è¡ãã¦ã¿ããéãå¤ãããã£ã¦ããï¼ï¼
ã ãã ããããããå
¨ç¶ãããã°ã§ããã³ã¼ãããããï¼
ã¹ãã²ããã£ã ï¼ï¼
ã¡ãã£ã¨ä¸æ¦ãã£ããããæ°ã¯ããããããï¼ï¼
ä»ã®ç¥èã ã¨ã©ãæ¸ãã¦ã訳ã®åãããªãã³ã¼ãã«ãªã£ã¦ãã¾ãï¼ï¼ï¼
ã¢ã»ã³ãã©ã®ã¹ãã²ããã£ã«ãªããªãæ¸ãæ¹ã調ã¹ã
ã¡ãã£ã¨ããµã³ãã«ã³ã¼ããæ¢ãã¦ããï¼
Google Code Search ++
http://google.com/codesearch?hl=en&lr=&q=file%3A%5C.s%24+%22.globl+_main%22&btnG=Search
å¤ã«ååãä»ããæ¹æ³ãç¥ã£ãã
ããã¯ä½¿ãã
http://developer.apple.com/documentation/DeveloperTools/Reference/Assembler/ASMLayout/chapter_4_section_6.html#//apple_ref/doc/uid/TP30000822-TPXREF111
ãã¨ããã leal ã£ã¦ããå½ä»¤ã使ããã¦ããªããã©ããªå½ä»¤ãªãã ããã調ã¹ã¦ã¿ãã
leal ã«ã¤ã㦠Load Effective Address ã¨èª¬æãã¦ããããã£ã±ãåãããã
ã¡ãã£ã¨ã°ã°ã£ã¦ã¿ãã
http://alohakun.blog7.fc2.com/blog-entry-422.html
alohakun ããã®ãã¼ã¸ã«è©³ããæ¸ãã¦ãã£ãã
ã¨ããããã¾ããã®ãã¼ã¸ãä¸èªããã¹ããä»ããèªã¿ã¾ãã
ããããåãã£ããã¨
# ã¡ã¢ãªã®å¤ã®ä»£å ¥ movl (%ebp), %eax # ã¡ã¢ãªã®å¤ã®ä»£å ¥ï¼ã¤ã³ããã¯ã¹ä»ãï¼ movl 4(%ebp), %eax # ãã¶ãã以ä¸ã¨ %eax ã¯åããªãï¼ add $4, %ebp movl (%ebp), %eax # ã¡ã¢ãªã®ã¢ãã¬ã¹ã®ä»£å ¥ leal 4(%ebp), %eax # ãã¶ãã以ä¸ã¨ %eax ã¯åãã«ãªãï¼ add $4, %ebp movl %ebp, %eax
ã¤ã¾ãã leal ã¯è¶³ãç®ã®ä»£å ¥ã£ã¦ãã¨ã
leal (%edx,%eax), %ecx
ã¨ããã° %edx + %eax ã®å¤ã %ecx ã«å
¥ããããã
alohakun ããåå¼·ã«ãªãã¾ããï¼ãããã¨ããããã¾ããï¼
ã¨ãã㧠(%abp, $4) 㨠4(%ebp) ã¯ã ãããåãæå³ãªãã§ãããããã
ããããããã«ä»æ§ããã£ãï¼
http://developer.apple.com/documentation/DeveloperTools/Reference/Assembler/i386Instructions/chapter_7_section_3.html#//apple_ref/doc/uid/TP30000825-TPXREF109
ãããããªãã»ã©
ããèªã㨠leal ã¯è¶³ãç®ã¨ããããã¯ãã¢ãã¬ã¹ãæ±ããæ¼ç®ãããçµæã ãã使ãã®ãã®ãã
ã§ãã¢ãã¬ã¹æ±ããçµæããã¢ãã¬ã¹ã®ä¸ã®å¤ã使ãã®ã movl ã£ã¦ãã¨ãã
ã²ãã£ã¨ãã¦ã load ã£ã¦ã¢ãã¬ã¹ã®ã³ãã¼ã§ãã ã¼ããå¤ã®ã³ãã¼ã£ã¦æå³ãªã®ããªãéãããªãã¾ããããããã
ãªãã»ã©ãªãã»ã©ã
ã¢ãã¬ã¹ãæ±ããããã®ãªãã©ã³ãã¯ä»¥ä¸ã®ããã«å®ç¾©ããã¦ããã
displacement(base_register,index_register,scale)
以ä¸ã®ã³ã¼ããæ¸ãã¦ã¿ã¦ ecx ãä½ã«ãªãããè¦ã¦ã¿ãã
.text .globl _main _main: movl $16, %eax movl $32, %ebx leal 8(%eax, %ebx, 4), %ecx ret
$ gcc -g sample008.s -o sample008 && gdb ./sample008 (gdb) break main Breakpoint 1 at 0x1fee: file sample008.s, line 4. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample008 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample008.s:4 4 movl $16, %eax (gdb) step Current language: auto; currently asm 5 movl $32, %ebx (gdb) step 6 leal 8(%eax, %ebx, 4), %ecx (gdb) step 7 ret (gdb) info register eax 0x10 16 ecx 0x98 152 edx 0x0 0 ebx 0x20 32 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ffc 0x1ffc <_main+14> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
152 ãã
ã¨ãããã¨ã¯ã 8 + %eax + (%ebx * 4) ã§ããã®ã㪠8 + 16 + 128 = 152 ã ãããã
ä¸å¿ãå¥ã®å¤ã§ç¢ºèªãã¨ããã
ä½ã 4 åãããã®ãåãããããããã«ã 1 㨠10 㨠100 ã§ãã£ã¦ã¿ãã
.text .globl _main _main: movl $1, %eax movl $10, %ebx leal 100(%eax, %ebx, 4), %ecx ret
$ gcc -g sample009.s -o sample009 && gdb ./sample009 (gdb) break main Breakpoint 1 at 0x1fee: file sample009.s, line 4. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample009 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample009.s:4 4 movl $1, %eax (gdb) step Current language: auto; currently asm 5 movl $10, %ebx (gdb) step 6 leal 100(%eax, %ebx, 4), %ecx (gdb) step 7 ret (gdb) info register eax 0x1 1 ecx 0x8d 141 edx 0x0 0 ebx 0xa 10 esp 0xbffff8ec 0xbffff8ec ebp 0xbffff90c 0xbffff90c esi 0x0 0 edi 0x0 0 eip 0x1ffc 0x1ffc <_main+14> eflags 0x246 582 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ãããã£ã±ãããã
leal a(b, c, d), e 㯠e = a + b + (c * d) ã£ã¦ãã¨ã§ FAã
ãã¨ããããããªã³ã¼ããè¦ã¦ã㨠ret ã®åã« leave ã£ã¦ãã£ã¦ãããªãã ãããã
ããã«é¢ãã¦ã以ä¸ã®ã¨ã³ããªã®ã³ã¡ã³ãæ¬ã«ããã¾ãã
http://alohakun.blog7.fc2.com/blog-entry-422.html
ãã¨ãã¤ã³ãã«ã®ã¨ããã«ãä½ãããããããã¾ããã
Resource & Design Center for Development with Intel
leave 㯠enter ã¨ã»ãããªãã§ãããã¡ãã£ã¨ã¤ã³ãã«ã®ããã¥ã¢ã«ãèªãã§ã¿ã¾ãã
ãªãããããåããããã©é¢æ°ã®å
é 㧠enter ããã¦ãé¢æ°ã®æå¾ã§ leave ãããã°ãã¹ã¿ãã¯ãã¬ã¼ã ã®ããã® push ã¨ã pop ã¨ãæ¸ããªãã¦ããã£ã¦ãã¨ããªï¼
ã§ãã enter ã¯ã»ã¨ãã©ä½¿ãããªãããããããã¦ã Google Code Search ã§ã使ç¨ä¾ããã¾ããªãã
ã¨ããããã leave ã使ã£ã¦ãä¾ãè¦ã¤ããã®ã§ã³ãããã¦ã gdb ã§æåãã¿ãã
.text .globl _main _main: pushl %ebp movl %esp, %ebp subl $8, %esp movl $0, %eax leave ret
ãã¼ã¼ã¼ããªãã»ã©ãªãã»ã©ï¼
%ebp ã«ã¹ã¿ãã¯ãã¤ã³ã¿ãä¿åãã¦ããã¦ãã¹ã¿ãã¯ãã¬ã¼ã åã¹ã¿ãã¯ãã¤ã³ã¿ãããã㦠leave ãããæ»ãã¦ãã®ãï¼
ã¨ãããããããã§ä»®ç ãã¨ãï¼ãã£ããããã®éã¯é ããªï¼
ãããã
ã¨ããã§ããã£ãã®ã³ã¼ãã£ã¦ subl $8, %espãã¦ããã©ããªã㧠8 ãªãã ããã
ãªã¿ã¼ã³ã¢ãã¬ã¹ããä¿åããªãããªã subl $4, %esp ã ãã§ããããªãã
0(%esp) 㨠4(%esp) ã«ã¯ä½ãå
¥ã£ã¦ãããã ããã
ã¡ãã£ã¨ sub é¢æ°ãä½ã£ã¦ãã£ãããããã¦ã¿ãï¼
.text .globl _sub _sub: pushl %ebp movl %esp, %ebp subl $8, %esp movl $0, %eax leave ret .globl _main _main: pushl %ebp movl %esp, %ebp subl $8, %esp call _sub movl $0, %eax leave ret
$ gcc -g sample013.s -o sample013 && gdb ./sample013 (gdb) break main Breakpoint 1 at 0x1ff1: file sample013.s, line 12. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample013 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample013.s:15 15 call _sub (gdb) step Current language: auto; currently asm _sub () at sample013.s:4 4 pushl %ebp (gdb) step _sub () at sample013.s:5 5 movl %esp, %ebp (gdb) step 6 subl $8, %esp (gdb) step 7 movl $0, %eax (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8d0 0xbffff8d0 ebp 0xbffff8d8 0xbffff8d8 esi 0x0 0 edi 0x0 0 eip 0x1fe4 0x1fe4 <_sub+6> eflags 0x282 642 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb) print *(void**) 0xbffff8d0 $1 = (void *) 0x0 (gdb) print *(void**) 0xbffff8d4 $2 = (void *) 0x0 (gdb) print *(void**) 0xbffff8d8 $3 = (void *) 0xbffff8e8 (gdb) step 8 leave (gdb) step _sub () at sample013.s:9 9 ret (gdb) info register eax 0x0 0 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0xbffff978 -1073743496 esp 0xbffff8dc 0xbffff8dc ebp 0xbffff8e8 0xbffff8e8 esi 0x0 0 edi 0x0 0 eip 0x1fea 0x1fea <_sub+12> eflags 0x282 642 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb) print *(void**) 0xbffff8dc $4 = (void *) 0x1ff6 (gdb)
ãããããããªããã leave ã®æåãæ³åã¨éããããªãã
ãããã£ãããªã¿ã¼ã³ã¢ãã¬ã¹ã¯ call ã«ãã£ã¦ push ããã¦ãããã ãã èªå㧠subl ã§ã¹ã¿ãã¯ãã¤ã³ã¿ããããå¿
è¦ã¯ãªãã®ãã
ãã£ãã調ã¹ã¦ã¿ã㨠16 byte 㧠align ããããã«ã¹ã¿ãã¯ãã¤ã³ã¿ã subl ãã¦ãã£ã¦æ¸ãã¦ãã£ãã
Uli's Programming Blog
align ã£ã¦ãªãã®ããã«å¿
è¦ãªãã ããã
ããããã°ãæããã¯ãã¼ã¯ããæ°ãããããã£ã¦ã¦è¯ãã£ããã¯ã¦ãªããã¯ãã¼ã¯ãï¼PRï¼ï¼
データ型のアラインメントとは何か,なぜ必要なのか?
ããä¸åèªãã ããªãã»ã©ãªãã»ã©ã
ãã¢ã»ã³ãã©ã ããéããã¨ããããã§ã¯ãªãã¦ãã¢ã©ã¤ã³ã¡ã³ãã®ããã«èªåã§æ³¨æãã¦æèçã«æ¸ããªãã¨éãã³ã¼ããæ¸ããªãå ´åããããã§ããã
次ã«ãã¹ã¿ãã¯ä¸ã®ã¡ã¢ãªãå¤æ°ã¨ãã¦ä½¿ãæ¹æ³ãèãã¦ã¿ãã
ã¬ã¸ã¹ã¿ã¯ããã¤ãããã¯ã¹ã³ã¼ãã®å¤æ°ã¿ãããªãã®ã§ãå¼ã³åºããå
ã®é¢æ°ã«ãã£ã¦ç ´å£ããã¦ãã¾ãå¯è½æ§ãããã
ãªã®ã§ãã¨ã«ããå¤æ°çãªãã®ã¯ãã¹ã¦ã¹ã¿ãã¯ãã辿ããªããã°ãªããªããã ãããï¼ããã§ããã¬ã¸ã¹ã¿ã§ãã£ã¦ pusha ã¨ãã§ãããã®ããï¼
ãããã¸ãããªã
http://developer.apple.com/documentation/DeveloperTools/Reference/Assembler/ASMLayout/chapter_4_section_6.html#//apple_ref/doc/uid/TP30000822-TPXREF111
ãããããã©ãããã® identifier ããã¡ã¤ã«ã¹ã³ã¼ãã£ã½ãæãã ã
ãããããããã¾ã§ããã®ååãã¿ãããªã®ã§ããªãããªã
ååã®éè¤é¿ãããã£ãããã¡ã¤ã«ãåããã£ã¦ãã¨ããªï¼ï¼
å¤æ°ã«ååãä»ããã 24 ã£ã¦ãªã£ã¦ãã®ã¯ã¹ã¿ãã¯ã®ã¢ã©ã¤ã³ã¡ã³ããæãããã
å¤æ°ã使ã£ã¦è¶³ãç®é¢æ°ãæ¸ãç´ãã¦ã¿ãã
.text .set _sub_var_a, 8 + 0 .set _sub_var_b, 8 + 4 .globl _sub _sub: pushl %ebp movl %esp, %ebp subl $8, %esp movl _sub_var_a(%ebp), %eax movl _sub_var_b(%ebp), %ebx addl %ebx, %eax leave ret .globl _main _main: pushl %ebp movl %esp, %ebp subl $24, %esp movl $3, (%esp) movl $4, 4(%esp) call _sub movl $0, %eax leave ret
å®è¡ãã¦ã¿ã
$ gcc -g sample013.s -o sample013 && gdb ./sample013 (gdb) break main Breakpoint 1 at 0x1fe4: file sample013.s, line 18. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample013 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample013.s:21 21 movl $3, (%esp) (gdb) step Current language: auto; currently asm 22 movl $4, 4(%esp) (gdb) step 23 call _sub (gdb) step _sub () at sample013.s:7 7 pushl %ebp (gdb) step _sub () at sample013.s:8 8 movl %esp, %ebp (gdb) step 9 subl $8, %esp (gdb) step 10 movl _sub_var_a(%ebp), %eax (gdb) step 11 movl _sub_var_b(%ebp), %ebx (gdb) step 12 addl %ebx, %eax (gdb) step 13 leave (gdb) step _sub () at sample013.s:14 14 ret (gdb) step _main () at sample013.s:24 24 movl $0, %eax (gdb) info register eax 0x7 7 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0x4 4 esp 0xbffff8d0 0xbffff8d0 ebp 0xbffff8e8 0xbffff8e8 esi 0x0 0 edi 0x0 0 eip 0x1ff8 0x1ff8 <_main+26> eflags 0x202 514 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ããã %eax ã 7 ã«ãªã£ã
ããä¸åº¦ããã£ããããæ°ãæ±ããããã°ã©ã ã«ãã£ã¬ã³ã¸ãã
.text .set _sub_var_n, 8 + 0 .globl _sub _sub: pushl %ebp movl %esp, %ebp subl $24, %esp movl _sub_var_n(%ebp), %ebx cmp $2, %ebx ja L_sub_above movl $1, %eax jmp L_sub_return L_sub_above: dec %ebx movl %ebx, (%esp) movl %ebx, 4(%esp) call _sub movl %eax, 8(%esp) movl 4(%esp), %ebx dec %ebx movl %ebx, (%esp) call _sub addl 8(%esp), %eax L_sub_return: leave ret .globl _main _main: pushl %ebp movl %esp, %ebp subl $24, %esp movl $10, (%esp) call _sub movl $0, %eax leave ret
ã§ãããå®è¡ãã¦ã¿ãã
$ gcc -g sample014.s -o sample014 && gdb ./sample014 (gdb) break main Breakpoint 1 at 0x1fed: file sample014.s, line 36. (gdb) run Starting program: /Users/amachang/projects/lang/assembler/sample014 Reading symbols for shared libraries ++. done Breakpoint 1, _main () at sample014.s:39 39 movl $10, (%esp) (gdb) next Current language: auto; currently asm 40 call _sub (gdb) next 41 movl $0, %eax (gdb) info register eax 0x37 55 ecx 0xbffff914 -1073743596 edx 0x0 0 ebx 0x2 2 esp 0xbffff8d0 0xbffff8d0 ebp 0xbffff8e8 0xbffff8e8 esi 0x0 0 edi 0x0 0 eip 0x1ff9 0x1ff9 <_main+18> eflags 0x202 514 cs 0x17 23 ss 0x1f 31 ds 0x1f 31 es 0x1f 31 fs 0x0 0 gs 0x37 55 (gdb)
ãã£ãã¼ã¼ã¼ã¼ã¼ï¼ã§ããï¼ï¼
10 ãå
¥ããããã¡ãã㨠%eax ã« 55 ãããã£ã¦ãããï¼