Skip to content

Instantly share code, notes, and snippets.

@agesome
Created March 21, 2012 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agesome/2145568 to your computer and use it in GitHub Desktop.
Save agesome/2145568 to your computer and use it in GitHub Desktop.
.text
.global main
main:
push %rbp
mov %rsp, %rbp
call setup
movb $0xf0, color
mov $STEP_DIVISOR, %r8
cvtsi2ss %r8, %xmm6
rcpss %xmm6, %xmm7 # xmm7 = 1 / STEP_DIVISOR
mov $-ZERO_POINT/STEP_DIVISOR, %r8
cvtsi2ss %r8, %xmm0 # x
a_loop:
mov $-ZERO_POINT/STEP_DIVISOR, %r8
cvtsi2ss %r8, %xmm1 # y
b_loop:
call calculate
cmp $0, %rax
jne nopaint
mov $STEP_DIVISOR, %r8
cvtsi2ss %r8, %xmm2
movss %xmm0, %xmm3
movss %xmm1, %xmm4
mulss %xmm2, %xmm3
mulss %xmm2, %xmm4
cvttss2si %xmm3, %r8
cvttss2si %xmm4, %r9
call paint_pixel
nopaint:
mov $ZERO_POINT/STEP_DIVISOR, %r8
cvtsi2ss %r8, %xmm6
movss %xmm1, %xmm3
cmpless %xmm6, %xmm3
addss %xmm7, %xmm1
cvtss2si %xmm3, %rax
cmp $0, %rax
jne b_loop
addss %xmm7, %xmm0
movss %xmm0, %xmm3
cmpless %xmm6, %xmm3
cvtss2si %xmm3, %rax
cmp $0, %rax
jne a_loop
movb $0xff, color
call paint_axis
call flip
call getchar
call examine
pop %rbp
ret
calculate:
movss %xmm0, %xmm2 #x
movss %xmm1, %xmm3 #y
mov $2, %r8
cvtsi2ss %r8, %xmm5
mulss %xmm5, %xmm2 #2x
mov $1, %r8
cvtsi2ss %r8, %xmm5
addss %xmm5, %xmm2 #2x + 1
movq $0x7fffffff, bitmask
movss bitmask, %xmm4
andps %xmm4, %xmm2 #|2x + 1|
sqrtss %xmm2, %xmm4 #sqrt(|2x + 1|)
mov $-1, %r8
cvtsi2ss %r8, %xmm5
mulss %xmm5, %xmm4 # -xmm4
cmpless %xmm4, %xmm3
cvtss2si %xmm3, %rax
ret
flip:
push %rbp
mov %rsp, %rbp
mov surface, %rdi
call SDL_Flip
pop %rbp
ret
paint_pixel:
push %rax
push %rbx
push %rcx
push %r8
push %r9
mov pixels, %rax
add $ZERO_POINT, %r8
add %r8, %rax
mov $SIDE, %rbx
sub %r9, %rbx
sub $ZERO_POINT, %rbx
imul $SIDE, %rbx
mov nopaint_flag, %cl
cmp $1, %cl
je npaint
mov color, %cl
movb %cl, (%rax, %rbx)
npaint:
#nopaint flag set, return value
movb (%rax, %rbx), %r15b
pop %r9
pop %r8
pop %rcx
pop %rbx
pop %rax
ret
setup:
push %rbp
mov %rsp, %rbp
mov $text_sdl_init, %rdi
call printf
mov $SDL_INIT_VIDEO, %rdi
call SDL_Init
mov $SIDE, %rdi
mov $SIDE, %rsi
mov $BPP, %rdx
mov $0, %rcx
call SDL_SetVideoMode
mov 32(%rax), %rbx
movq %rbx, pixels
movq %rax, surface
pop %rbp
ret
paint_axis:
push %rbp
mov %rsp, %rbp
mov $0, %rax
mov $-ZERO_POINT, %rbx
yloop:
mov %rax, %r8
mov %rbx, %r9
call paint_pixel
inc %rbx
cmp $ZERO_POINT, %rbx
jle yloop
mov $0, %rbx
mov $-ZERO_POINT, %rax
xloop:
mov %rax, %r8
mov %rbx, %r9
call paint_pixel
inc %rax
cmp $ZERO_POINT, %rax
jl xloop
mov $1, %rax
mov $-ZERO_POINT + STEP_DIVISOR, %rbx
ytick:
mov %rax, %r8
mov %rbx, %r9
call paint_pixel
inc %r8
call paint_pixel
inc %r8
call paint_pixel
add $STEP_DIVISOR, %rbx
cmp $ZERO_POINT, %rbx
jle ytick
mov $1, %rbx
mov $-ZERO_POINT + STEP_DIVISOR, %rax
xtick:
mov %rax, %r8
mov %rbx, %r9
call paint_pixel
inc %r9
call paint_pixel
inc %r9
call paint_pixel
add $STEP_DIVISOR, %rax
cmp $ZERO_POINT, %rax
jle xtick
pop %rbp
ret
examine:
push %rbp
mov %rsp, %rbp
movb $1, nopaint_flag
mov $-ZERO_POINT, %rax
reflexive:
mov %rax, %r8
mov %rax, %r9
call paint_pixel
cmp $0, %r15
jne 1f
movb $0, prop_reflexive
jmp 2f
1:
movb $0, prop_areflex
2:
inc %rax
cmp $ZERO_POINT, %rax
jl reflexive
xor %r8, %r8
mov prop_reflexive, %r8
cmp $0, %r8
je nreflexive
mov $text_true, %rdi
call printf
jmp isreflexive
nreflexive:
mov $text_false, %rdi
call printf
isreflexive:
mov $text_reflexive, %rdi
call printf
mov prop_areflex, %r8
cmp $0, %r8
je nareflex
mov $text_true, %rdi
call printf
jmp isareflex
nareflex:
mov $text_false, %rdi
call printf
isareflex:
mov $text_areflex, %rdi
call printf
symmetric:
mov $-ZERO_POINT, %rax
1:
mov $-ZERO_POINT, %rbx
2:
mov %rax, %r8
mov %rbx, %r9
call paint_pixel
cmp $0, %r15
je 3f
mov %rax, %r9
mov %rbx, %r8
call paint_pixel
cmp $0, %r15
je nsymmetric
3:
inc %rbx
cmp $ZERO_POINT, %rbx
jl 2b
inc %rax
cmp $ZERO_POINT, %rbx
jl 1b
mov $text_true, %rdi
call printf
movb $1, prop_symmetric
jmp issymmetric
nsymmetric:
mov $text_false, %rdi
call printf
issymmetric:
mov $text_symmetric, %rdi
call printf
transitive:
mov $-ZERO_POINT, %rax
1:
mov $-ZERO_POINT, %rbx
2:
mov %rax, %r8
mov %rbx, %r9
call paint_pixel
cmp $0, %r15
je 3f
mov $-ZERO_POINT, %rcx
4:
mov %rbx, %r8
mov %rcx, %r9
call paint_pixel
cmp $0, %r15
je 5f
mov %rax, %r8
mov %rcx, %r9
call paint_pixel
cmp $0, %r15
je ntrans
5:
inc %rcx
cmp $ZERO_POINT, %rcx
jl 4b
3:
inc %rbx
cmp $ZERO_POINT, %rbx
jl 2b
inc %rax
cmp $ZERO_POINT, %rbx
jl 1b
mov $text_true, %rdi
call printf
movb $1, prop_trans
jmp istrans
ntrans:
mov $text_false, %rdi
call printf
istrans:
mov $text_trans, %rdi
call printf
pop %rbp
ret
.data
SDL_INIT_VIDEO = 0x00000020
ZERO_POINT = SIDE / 2
SIDE = 600
STEP_DIVISOR = 50
BPP = 8
surface: .quad 0
pixels: .quad 0
color: .byte 0xff
bitmask: .quad 0
nopaint_flag: .byte 0
prop_reflexive: .byte 1
prop_symmetric: .byte 0
prop_trans: .byte 0
prop_areflex: .byte 1
.quad 0
text_true: .asciz "[Y] "
text_false: .asciz "[N] "
text_reflexive: .asciz "Reflexive\n"
text_areflex: .asciz "Antireflexive\n"
text_symmetric: .asciz "Symmetric\n"
text_trans: .asciz "Transitive\n"
text_sdl_init: .asciz "Initializing SDL\n"
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment