午前

コンパイラの課題の仕上げ。


うおーーーー。ちゃんと動いたーーーー。感動やーーー。

[i041184@orchid-calc1 tiny_c_advanced]$ ./tiny-cc-x86 < queen.tc > queen.s
[i041184@orchid-calc1 tiny_c_advanced]$ less queen.s
.comm grid,256,32
.comm resultCount,4,4
.comm dummy,4,4
.text
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
subl $24,%esp
movl %ebx,-4(%ebp)
movl $0,%eax
movl %eax,resultCount
movl $0,%eax
movl %eax,dummy
call initGrid
call count
movl $0,%eax
jmp .L0
.L0: movl -4(%ebp), %ebx
leave
ret
.section .rodata
.LC3:
.string "%d results found."
.text
.align 4
.globl count
.type count,@function
count:
pushl %ebp
movl %esp,%ebp
subl $28,%esp
movl %ebx,-4(%ebp)
movl $0,%eax
movl %eax,-24(%ebp)
.L1:
movl -24(%ebp),%eax
movl $8,%ebx
cmpl %ebx,%eax
jl .L5
movl $0,%eax
jmp .L6
.L5: movl $1,%eax
.L6: cmpl $0,%eax
je .L2
movl -24(%ebp),%eax
pushl %eax
movl $0,%eax
pushl %eax
call tryNext
movl -24(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
movl %eax,-24(%ebp)
jmp .L1
.L2:
movl resultCount,%eax
pushl %eax
pushl $.LC3
call println
addl $8,%esp
.L4: movl -4(%ebp), %ebx
leave
ret
.text
.align 4
.globl initGrid
.type initGrid,@function
initGrid:
pushl %ebp
movl %esp,%ebp
subl $32,%esp
movl %ebx,-4(%ebp)
movl $0,%eax
movl %eax,-24(%ebp)
.L7:
movl -24(%ebp),%eax
movl $8,%ebx
cmpl %ebx,%eax
jl .L12
movl $0,%eax
jmp .L13
.L12: movl $1,%eax
.L13: cmpl $0,%eax
je .L8
movl $0,%eax
movl %eax,-28(%ebp)
.L9:
movl -28(%ebp),%eax
movl $8,%ebx
cmpl %ebx,%eax
jl .L14
movl $0,%eax
jmp .L15
.L14: movl $1,%eax
.L15: cmpl $0,%eax
je .L10
movl $0,%eax
pushl %eax
movl -28(%ebp),%eax
pushl %eax
movl -24(%ebp),%eax
pushl %eax
call setValue
movl -28(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
movl %eax,-28(%ebp)
jmp .L9
.L10:
movl -24(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
movl %eax,-24(%ebp)
jmp .L7
.L8:
.L11: movl -4(%ebp), %ebx
leave
ret
.text
.align 4
.globl tryNext
.type tryNext,@function
tryNext:
pushl %ebp
movl %esp,%ebp
subl $28,%esp
movl %ebx,-4(%ebp)
movl $1,%eax
pushl %eax
movl 12(%ebp),%eax
pushl %eax
movl 8(%ebp),%eax
pushl %eax
call setValue
movl 12(%ebp),%eax
pushl %eax
movl 8(%ebp),%eax
pushl %eax
call checkLegal
add $8,%esp
cmpl $0,%eax
je .L16
movl 8(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
movl $8,%ebx
cmpl %ebx,%eax
je .L23
movl $0,%eax
jmp .L24
.L23: movl $1,%eax
.L24: cmpl $0,%eax
je .L17
movl resultCount,%eax
movl $1,%ebx
addl %ebx,%eax
movl %eax,resultCount
movl $0,%eax
pushl %eax
movl 12(%ebp),%eax
pushl %eax
movl 8(%ebp),%eax
pushl %eax
call setValue
movl $0,%eax
jmp .L22
.L17:
movl $0,%eax
movl %eax,-24(%ebp)
.L18:
movl -24(%ebp),%eax
movl $8,%ebx
cmpl %ebx,%eax
jl .L25
movl $0,%eax
jmp .L26
.L25: movl $1,%eax
.L26: cmpl $0,%eax
je .L19
movl -24(%ebp),%eax
movl 12(%ebp),%ebx
cmpl %ebx,%eax
je .L27
movl $0,%eax
jmp .L28
.L27: movl $1,%eax
.L28: cmpl $0,%eax
je .L20
movl $0,%eax
movl %eax,dummy
jmp .L21
.L20:
movl -24(%ebp),%eax
pushl %eax
movl 8(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
pushl %eax
call tryNext
.L21:
movl -24(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
movl %eax,-24(%ebp)
jmp .L18
.L19:
.L16:
movl $0,%eax
pushl %eax
movl 12(%ebp),%eax
pushl %eax
movl 8(%ebp),%eax
pushl %eax
call setValue
movl $0,%eax
jmp .L22
.L22: movl -4(%ebp), %ebx
leave
ret
.text
.align 4
.globl getValue
.type getValue,@function
getValue:
pushl %ebp
movl %esp,%ebp
subl $24,%esp
movl %ebx,-4(%ebp)
movl $8,%eax
movl 8(%ebp),%ebx
imull %ebx,%eax
movl 12(%ebp),%ebx
addl %ebx,%eax
movl grid(,%eax,4),%ebx
movl %ebx,%eax
jmp .L29
.L29: movl -4(%ebp), %ebx
leave
ret
.text
.align 4
.globl setValue
.type setValue,@function
setValue:
pushl %ebp
movl %esp,%ebp
subl $24,%esp
movl %ebx,-4(%ebp)
movl $8,%eax
movl 8(%ebp),%ebx
imull %ebx,%eax
movl 12(%ebp),%ebx
addl %ebx,%eax
movl 16(%ebp),%ebx
movl %ebx,grid(,%eax,4)
movl $0,%eax
jmp .L30
.L30: movl -4(%ebp), %ebx
leave
ret
.text
.align 4
.globl checkLegal
.type checkLegal,@function
checkLegal:
pushl %ebp
movl %esp,%ebp
subl $32,%esp
movl %ebx,-4(%ebp)
movl $0,%eax
movl %eax,-24(%ebp)
.L31:
movl -24(%ebp),%eax
movl 8(%ebp),%ebx
cmpl %ebx,%eax
jl .L39
movl $0,%eax
jmp .L40
.L39: movl $1,%eax
.L40: cmpl $0,%eax
je .L32
movl 8(%ebp),%eax
movl -24(%ebp),%ebx
subl %ebx,%eax
movl %eax,-28(%ebp)
movl 12(%ebp),%eax
movl -28(%ebp),%ebx
addl %ebx,%eax
movl $8,%ebx
cmpl %ebx,%eax
jl .L41
movl $0,%eax
jmp .L42
.L41: movl $1,%eax
.L42: cmpl $0,%eax
je .L33
movl 12(%ebp),%eax
movl -28(%ebp),%ebx
addl %ebx,%eax
pushl %eax
movl -24(%ebp),%eax
pushl %eax
call getValue
add $8,%esp
movl $1,%ebx
cmpl %ebx,%eax
je .L43
movl $0,%eax
jmp .L44
.L43: movl $1,%eax
.L44: cmpl $0,%eax
je .L34
movl $0,%eax
jmp .L38
.L34:
.L33:
movl 12(%ebp),%eax
movl -28(%ebp),%ebx
subl %ebx,%eax
movl $0,%ebx
movl $1,%ecx
subl %ecx,%ebx
cmpl %ebx,%eax
jg .L45
movl $0,%eax
jmp .L46
.L45: movl $1,%eax
.L46: cmpl $0,%eax
je .L35
movl 12(%ebp),%eax
movl -28(%ebp),%ebx
subl %ebx,%eax
pushl %eax
movl -24(%ebp),%eax
pushl %eax
call getValue
add $8,%esp
movl $1,%ebx
cmpl %ebx,%eax
je .L47
movl $0,%eax
jmp .L48
.L47: movl $1,%eax
.L48: cmpl $0,%eax
je .L36
movl $0,%eax
jmp .L38
.L36:
.L35:
movl 12(%ebp),%eax
pushl %eax
movl -24(%ebp),%eax
pushl %eax
call getValue
add $8,%esp
movl $1,%ebx
cmpl %ebx,%eax
je .L49
movl $0,%eax
jmp .L50
.L49: movl $1,%eax
.L50: cmpl $0,%eax
je .L37
movl $0,%eax
jmp .L38
.L37:
movl -24(%ebp),%eax
movl $1,%ebx
addl %ebx,%eax
movl %eax,-24(%ebp)
jmp .L31
.L32:
movl $1,%eax
jmp .L38
.L38: movl -4(%ebp), %ebx
leave
ret

[i041184@orchid-calc1 tiny_c_advanced]$ cc -o queen.out queen.s println.c
[i041184@orchid-calc1 tiny_c_advanced]$ ./queen.out
92 results found.


ちなみに、.commっていうセクションが今回の課題で吐くようにした部分。