Hatena::Groupgeneration1986

最後に改行がないとコンパイルできない [suu-gの日記]

 | 

2008-05-10天使はおじさんだったよ\(^o^)/

マクロ使ってみたよ

| 19:15

アセンブラディレクティブのマクロを使ってみたよ\(^o^)/

これすごいよ\(^o^)/

.macro printhoge pname
	.text 1
\pname:
	pushl	%edx
	subl	$20, %esp
	movl	$_\pname, (%esp)
	call	printf
	addl	$20, %esp
	popl	%eax
	cmpl	%eax, %eax
	je	after_print

	.section .rodata
_\pname:
	.ascii "\pname \0"
.endm

printhoge Fizz
printhoge Buzz
printhoge FizzBuzz

.macro macroname [args]

っていう宣言で、.endmまでがマクロ宣言\(^o^)/

マクロ内で\引数とすればその場所が引数に置き換えられるよ\(^o^)/

文字列の中でもシンボル名の途中からでも置き換えちゃうよ\(^o^)/

冗談で試したら本当に出来てしまった\(^o^)/強力すぎる\(^o^)/


全体ではこんな感じだよ\(^o^)/

.globl main

.macro printhoge pname
	.text 1
\pname:
	pushl	%edx
	subl	$20, %esp
	movl	$_\pname, (%esp)
	call	printf
	addl	$20, %esp
	popl	%eax
	cmpl	%eax, %eax
	je	after_print

	.section .rodata
_\pname:
	.ascii "\pname \0"
.endm

.text 0
main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ecx

	movl	$1, %eax
	movl	%eax, %ecx
loop:

	movl	%eax, %edx
	movl	$15, %ebx
	call	mod
	cmpl	$0, %eax
	je	FizzBuzz

/*	movl	%edx, %eax */
/* since x mod 5 == (x mod 15) mod 5  */
	movl	$5, %ebx
	call	mod
	cmpl	$0, %eax
	je	Buzz

	movl	%edx, %eax
	movl	$3, %ebx
	call	mod
	cmpl	$0, %eax
	je	Fizz

	movl	%edx, %eax
	pushl	%eax
	subl	$20, %esp
	movl	%eax, 4(%esp)
	movl	$_Number, (%esp)
	call	printf
	addl	$20, %esp
	popl	%eax
	
after_print:
	addl	$1, %eax
	cmpl	$100, %eax
	jle	loop

loop_end:
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret

printhoge Fizz
printhoge Buzz
printhoge FizzBuzz

.text 2
mod:
/*
 This subroutine calculates
	 (mod %eax %ebx)
 returns the mod on %eax.
 mod()
 */
/*	movl	%ecx, %eax*/
mod_loop:
	cmpl	%eax, %ebx
	jg	mod_end
	subl	%ebx, %eax
	cmpl	%eax, %eax
	je	mod_loop
mod_end:
	ret
	
.section .rodata
_Number:
.ascii "%d \0"

アセンブリコードレベルでの記述は簡単になっているけど、オブジェクトファイルでは勿論展開されてるよ\(^o^)/

fb2.o:     file format elf32-i386

Disassembly of section .text:

00000000 <main>:
   0:	8d 4c 24 04          	lea    0x4(%esp),%ecx
   4:	83 e4 f0             	and    $0xfffffff0,%esp
   7:	ff 71 fc             	pushl  0xfffffffc(%ecx)
   a:	55                   	push   %ebp
   b:	89 e5                	mov    %esp,%ebp
   d:	51                   	push   %ecx
   e:	b8 01 00 00 00       	mov    $0x1,%eax
  13:	89 c1                	mov    %eax,%ecx

00000015 <loop>:
  15:	89 c2                	mov    %eax,%edx
  17:	bb 0f 00 00 00       	mov    $0xf,%ebx
  1c:	e8 95 00 00 00       	call   b6 <mod>
  21:	83 f8 00             	cmp    $0x0,%eax
  24:	74 78                	je     9e <FizzBuzz>
  26:	bb 05 00 00 00       	mov    $0x5,%ebx
  2b:	e8 86 00 00 00       	call   b6 <mod>
  30:	83 f8 00             	cmp    $0x0,%eax
  33:	74 51                	je     86 <Buzz>
  35:	89 d0                	mov    %edx,%eax
  37:	bb 03 00 00 00       	mov    $0x3,%ebx
  3c:	e8 75 00 00 00       	call   b6 <mod>
  41:	83 f8 00             	cmp    $0x0,%eax
  44:	74 28                	je     6e <Fizz>
  46:	89 d0                	mov    %edx,%eax
  48:	50                   	push   %eax
  49:	83 ec 14             	sub    $0x14,%esp
  4c:	89 44 24 04          	mov    %eax,0x4(%esp)
  50:	c7 04 24 00 00 00 00 	movl   $0x0,(%esp)
  57:	e8 fc ff ff ff       	call   58 <loop+0x43>
  5c:	83 c4 14             	add    $0x14,%esp
  5f:	58                   	pop    %eax

00000060 <after_print>:
  60:	83 c0 01             	add    $0x1,%eax
  63:	83 f8 64             	cmp    $0x64,%eax
  66:	7e ad                	jle    15 <loop>

00000068 <loop_end>:
  68:	59                   	pop    %ecx
  69:	5d                   	pop    %ebp
  6a:	8d 61 fc             	lea    0xfffffffc(%ecx),%esp
  6d:	c3                   	ret    

0000006e <Fizz>:
  6e:	52                   	push   %edx
  6f:	83 ec 14             	sub    $0x14,%esp
  72:	c7 04 24 00 00 00 00 	movl   $0x0,(%esp)
  79:	e8 fc ff ff ff       	call   7a <Fizz+0xc>
  7e:	83 c4 14             	add    $0x14,%esp
  81:	58                   	pop    %eax
  82:	39 c0                	cmp    %eax,%eax
  84:	74 da                	je     60 <after_print>

00000086 <Buzz>:
  86:	52                   	push   %edx
  87:	83 ec 14             	sub    $0x14,%esp
  8a:	c7 04 24 06 00 00 00 	movl   $0x6,(%esp)
  91:	e8 fc ff ff ff       	call   92 <Buzz+0xc>
  96:	83 c4 14             	add    $0x14,%esp
  99:	58                   	pop    %eax
  9a:	39 c0                	cmp    %eax,%eax
  9c:	74 c2                	je     60 <after_print>

0000009e <FizzBuzz>:
  9e:	52                   	push   %edx
  9f:	83 ec 14             	sub    $0x14,%esp
  a2:	c7 04 24 0c 00 00 00 	movl   $0xc,(%esp)
  a9:	e8 fc ff ff ff       	call   aa <FizzBuzz+0xc>
  ae:	83 c4 14             	add    $0x14,%esp
  b1:	58                   	pop    %eax
  b2:	39 c0                	cmp    %eax,%eax
  b4:	74 aa                	je     60 <after_print>

000000b6 <mod>:
  b6:	39 c3                	cmp    %eax,%ebx
  b8:	7f 06                	jg     c0 <mod_end>
  ba:	29 d8                	sub    %ebx,%eax
  bc:	39 c0                	cmp    %eax,%eax
  be:	74 f6                	je     b6 <mod>

000000c0 <mod_end>:
  c0:	c3                   	ret    

mod_endとかそういうラベルがまだ残ってるのが気持ち悪いね\(^o^)/

確かLから始まるラベルはリンカに行く前に自動的に消えるとかそういう決まりがあったはず\(^o^)/

VerdyVerdy2012/05/24 09:28A simple and intleigelnt point, well made. Thanks!

lmbrxhvlmbrxhv2012/05/24 11:21TrAu7n <a href="http://wwuykdgrlbza.com/">wwuykdgrlbza</a>

nzhntgfnzhntgf2012/05/25 14:45tlfIoO , [url=http://eiryyejlhvts.com/]eiryyejlhvts[/url], [link=http://ewsszgmprrmw.com/]ewsszgmprrmw[/link], http://hpiqhlhhjgdx.com/

ydvzohydvzoh2012/05/25 16:50BqXqyX , [url=http://culblaylukbu.com/]culblaylukbu[/url], [link=http://kegndllkpirk.com/]kegndllkpirk[/link], http://aqofdkpzecki.com/

tiudkljgvvtiudkljgvv2012/05/25 19:01mVVMsl , [url=http://wbxlcbeekjgj.com/]wbxlcbeekjgj[/url], [link=http://xrzpqapisgap.com/]xrzpqapisgap[/link], http://ypwvlrgkinxs.com/

fojqxqlshfojqxqlsh2012/05/26 17:09yVT9Ax <a href="http://fslqbdilpznu.com/">fslqbdilpznu</a>

wppqsjwppqsj2012/05/27 20:484poIp1 , [url=http://dzjgthbdyiyi.com/]dzjgthbdyiyi[/url], [link=http://smezewkwiywr.com/]smezewkwiywr[/link], http://zmlgacmdvlzt.com/

efkowudoquefkowudoqu2017/01/04 23:57http://dutasterideavodartonline.org/ - dutasterideavodartonline.org.ankor <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

usaxeduehusaxedueh2017/01/05 05:18http://dutasterideavodartonline.org/ - dutasterideavodartonline.org.ankor <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

atgoruputaatgoruputa2017/01/05 05:34http://dutasterideavodartonline.org/ - dutasterideavodartonline.org.ankor <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

idagijidibiidagijidibi2017/01/05 23:05http://dutasterideavodartonline.org/ - dutasterideavodartonline.org.ankor <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

cutuwiqcutuwiq2017/01/05 23:21http://dutasterideavodartonline.org/ - dutasterideavodartonline.org.ankor <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

jabiqebutjabiqebut2017/01/06 21:09http://dutasterideavodartonline.org/ - dutasterideavodartonline.org.ankor <a href="http://online-purchaselevitra.net/">online-purchaselevitra.net.ankor</a> http://20mg-cheapest-pricelevitra.net/

exevohaexevoha2017/03/24 04:52http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

apiyejiogapiyejiog2017/03/24 05:08http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

wigejanojaywigejanojay2017/03/24 05:11http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

ociueybesubiociueybesubi2017/03/24 05:22http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

eferivumauzuleferivumauzul2017/03/24 05:27http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

acanoniracanonir2017/03/24 05:41http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

ipaolizeshuipaolizeshu2017/03/24 14:19http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

ukidufepequpaukidufepequpa2017/03/24 14:39http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

afvigziqeafvigziqe2017/03/25 09:42http://usa-onlineprednisone.net/ - usa-onlineprednisone.net.ankor <a href="http://salbutamol-ventolin-buy.net/">salbutamol-ventolin-buy.net.ankor</a> http://online-viagracanada.net/

 |