Hatena::Groupgeneration1986

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

 | 

2008-05-07題名を変えたよ

FizzBuzz書いたよ\(^o^)/

| 08:09

gasでFizzBuzz書いたよ\(^o^)/

手書きだよ\(^o^)/


.file "fb.s"
.globl main
	.section .text

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

.text 1
	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, */
/* the last statement can be commented out */
	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

.text 2
Fizz:
	pushl	%edx
	subl	$20, %esp
	movl	$_Fizz, (%esp)
	call	printf
	addl	$20, %esp
	popl	%eax
	cmpl	%eax, %eax
	je	after_print
Buzz:
	pushl	%edx
	subl	$20, %esp
	movl	$_Buzz, (%esp)
	call	printf
	addl	$20, %esp
	popl	%eax
	cmpl	%eax, %eax
	je	after_print
FizzBuzz:
	pushl	%edx
	subl	$20, %esp
	movl	$_FizzBuzz, (%esp)
	call	printf
	addl	$20, %esp
	popl	%eax
	cmpl	%eax, %eax
	je	after_print

mod:
/*
 This subroutine calculates
	 (mod %eax %ebx)
 returns the mod on %eax.
 */
mod_loop:
	cmpl	%eax, %ebx
	jg	mod_end
	subl	%ebx, %eax
	cmpl	%eax, %eax
	je	mod_loop
	ret


.section .rodata
_Fizz:	
.ascii "Fizz \0"
	.align 4
_Buzz:	
.ascii "Buzz \0"
	.align 4
_FizzBuzz:	
.ascii "FizzBuzz \0"
	.align 4
_Number:
.ascii "%d \0"
	.align 4

真面目な記事は自称すーじー。の方に書くよ\(^o^)/

文字列の配置に間違いがあったので修正したよ\(^o^)/

.data じゃなくて .rodata に置くべきだったよ\(^o^)/

syou6162syou61622008/05/07 10:11なんだこれw\(^o^)/

hogeloghogelog2008/05/07 16:39わかりやすくていいなあ。

でも先生!
pushl %edx
subl $20, %esp
movl $_FizzBuzz, (%esp)
call printf
addl $20, %esp
popl %eax
cmpl %eax, %eax
似たような行あって、コード圧縮したくなりませんか!

suu-gsuu-g2008/05/07 21:20圧縮したいよ\(^o^)/
でも関数とかジャンプとかじゃなくて、
マクロのディレクティブを使ってみようと思うよ\(^o^)/

aycaayca2012/05/24 10:32Big help, big help. And superlative news of cosure.

ckjmqjtiwckjmqjtiw2012/05/24 11:46ZiIksm <a href="http://jsltmyvtuijx.com/">jsltmyvtuijx</a>

ldcunkglmzldcunkglmz2012/05/25 15:12WaBFBn , [url=http://axbctupdqkot.com/]axbctupdqkot[/url], [link=http://brymuhhpvumz.com/]brymuhhpvumz[/link], http://vwkaiprprpar.com/

cjwftlygcvkcjwftlygcvk2012/05/25 17:17rE6qaq , [url=http://yhncrjmakxoz.com/]yhncrjmakxoz[/url], [link=http://hesnaapuxszf.com/]hesnaapuxszf[/link], http://gbduxnjszwcr.com/

ugwvsmkbugwvsmkb2012/05/25 19:18q7CQSP , [url=http://gtkxwurxgywx.com/]gtkxwurxgywx[/url], [link=http://oggpkyplneoj.com/]oggpkyplneoj[/link], http://vjhzjenhafrn.com/

aggnzbqxsznaggnzbqxszn2012/05/26 16:12ev9Of0 <a href="http://sxzarvwatamj.com/">sxzarvwatamj</a>

 |