Skip to content

Instantly share code, notes, and snippets.

@medvecky
Created July 27, 2023 09:49
Show Gist options
  • Save medvecky/6160dc2b92cc4f632008bc9748b756df to your computer and use it in GitHub Desktop.
Save medvecky/6160dc2b92cc4f632008bc9748b756df to your computer and use it in GitHub Desktop.
Square root factorization calculator for Commodore 64 on 6510 assembly
.const print_str = $ab1e;
.const clearscreen = $e544;
.const set_cursor = $e50c;
.const print_char = $ffd2
.const getin = $ffe4
.const fp_string_to_fac = $b7b5;
.const fp_store_fac_to_ram = $bbd4
.const fp_load_ram_to_fac = $bba2
.const fp_fac_print = $aabc;
.const fp_sqr = $bf71;
.const fp_int = $bccc;
.const fp_add = $b867;
.const fp_cmp = $bc5b;
.const fp_mult = $ba28;
.const fp_div = $bb0f;
.const fp_subst =$b850;
.const fp_to_str = $bddd;
.const text_color = $0286;
.const background = $d021;
.const border = $d020;
.var new_line = $8d;
.var q_sym = $51;
.var right_p_sym = $29;
.var string_length = $06;
.var space_sym = $20;
BasicUpstart2( $1000 )
*= $1000
main:
jsr prepare_screen
jsr main_usage
wait_for_continue:
jsr getin
beq wait_for_continue
cmp #q_sym
beq go_to_exit
jmp get_args
go_to_exit:
jmp restore_and_exit
get_args:
jsr get_argument
jsr init_variables
//calculate and print sqr(argument)
lda #<argument
ldy #>argument
jsr fp_load_ram_to_fac
jsr fp_sqr
ldx #<ul
ldy #>ul
jsr fp_store_fac_to_ram
jsr fp_fac_print
//ul = int(sqr(argument)) + 1
lda #<ul
ldy #>ul
jsr fp_load_ram_to_fac
jsr fp_int
lda #<one
ldy #>one
jsr fp_add
ldx #<ul
ldy #>ul
jsr fp_store_fac_to_ram
loop: // for mf=1 to ul
//x = mf^2
lda #<mf
ldy #>mf
jsr fp_load_ram_to_fac
lda #<mf
ldy #>mf
jsr fp_mult
ldx #<x
ldy #>x
jsr fp_store_fac_to_ram
//y = int(argument) / int(x)
lda #<argument
ldy #>argument
jsr fp_load_ram_to_fac
jsr fp_int
ldx #<y
ldy #>y
jsr fp_store_fac_to_ram
lda #<x
ldy #>x
jsr fp_load_ram_to_fac
jsr fp_int
lda #<y
ldy #>y
jsr fp_div
ldx #<y
ldy #>y
jsr fp_store_fac_to_ram
//if argument - int(y) * x <> 0 goto next iteration
lda #<y
ldy #>y
jsr fp_load_ram_to_fac
jsr fp_int
lda #<x
ldy #>x
jsr fp_mult
lda #<argument
ldy #>argument
jsr fp_subst
lda #<zero
ldy #>zero
jsr fp_cmp
cmp #$0
bne not_factor
lda #<x
ldy #>x
jsr fp_load_ram_to_fac
ldx #<maxf
ldy #>maxf
jsr fp_store_fac_to_ram
not_factor:
lda #<mf
ldy #>mf
jsr fp_load_ram_to_fac
lda #<one
ldy #>one
jsr fp_add
ldx #<mf
ldy #>mf
jsr fp_store_fac_to_ram
lda #<ul
ldy #>ul
jsr fp_cmp
cmp #$1
bne go_to_loop
jmp end_loop
go_to_loop:
jmp loop
end_loop:
//sr = int(sqr(maxf))
lda #<maxf
ldy #>maxf
jsr fp_load_ram_to_fac
jsr fp_sqr
jsr fp_int
ldx #<sr
ldy #>sr
jsr fp_store_fac_to_ram
//of = int(argument)/int(maxf)
lda #<argument
ldy #>argument
jsr fp_load_ram_to_fac
jsr fp_int
ldx #<of
ldy #>of
jsr fp_store_fac_to_ram
lda #<maxf
ldy #>maxf
jsr fp_load_ram_to_fac
jsr fp_int
lda #<of
ldy #>of
jsr fp_div
jsr fp_int
ldx #<of
ldy #>of
jsr fp_store_fac_to_ram
//result output
lda #<sr
ldy #>sr
jsr fp_load_ram_to_fac
lda #<one
ldy #>one
jsr fp_cmp
cmp #$1
bne next_part
lda #<sr
ldy #>sr
jsr fp_load_ram_to_fac
jsr fp_to_str
jsr print_str
next_part:
lda #<of
ldy #>of
jsr fp_load_ram_to_fac
lda #<one
ldy #>one
jsr fp_cmp
cmp #$1
bne end_of_output
lda #<result_string_1
ldy #>result_string_1
jsr print_str
lda #<of
ldy #>of
jsr fp_load_ram_to_fac
jsr fp_to_str
jsr print_str
lda #right_p_sym
jsr print_char
end_of_output:
lda #new_line
jsr print_char
wait_to_exit:
jsr usage_at_exit
wait_for_input:
jsr getin
beq wait_for_input
cmp #q_sym
bne continue
jmp restore_and_exit
continue:
jsr clearscreen
jmp get_args
restore_and_exit:
jsr restore_screen
rts
// END OF MAIN
prepare_screen:
// set gren text color
lda #5
sta text_color
jsr clearscreen
// set border and background black
lda #$00
sta border
sta background
rts
restore_screen:
// restore border and background colors
lda #$0f6
sta background
lda #$fe
sta border
// restore text color
lda #$e
sta text_color
jsr clearscreen
// restore text mode
lda #$015
sta $d018
rts
main_usage:
ldx #$00
ldy #$0a
jsr set_cursor
lda #<usage_1
ldy #>usage_1
jsr print_str
lda #new_line
jsr print_char
jsr print_char
lda #<usage_2
ldy #>usage_2
jsr print_str
lda #new_line
jsr print_char
lda #<usage_3
ldy #>usage_3
jsr print_str
lda #new_line
jsr print_char
rts
usage_at_exit:
lda #<usage_2
ldy #>usage_2
jsr print_str
lda #new_line
jsr print_char
lda #<usage_3
ldy #>usage_3
jsr print_str
lda #new_line
jsr print_char
rts
get_argument:
jsr input_arg_prompt
jsr input_string_proc
jsr string_to_fp
ldx #<argument
ldy #>argument
jsr fp_store_fac_to_ram
lda #space_sym
jsr print_char
lda #new_line
jsr print_char
jsr cursor_blink_off
rts
input_arg_prompt:
lda #<input_argument
ldy #>input_argument
jsr print_str
jsr cursor_blink_on
rts
cursor_blink_on:
lda #$00
sta $cc
rts
cursor_blink_off:
lda $00cf
beq cursor_blink_off
lda #$01
sta $cc
rts
input_string_proc:
ldy #$00
ldx #$00
lda #$00
sta counter
input_get:
jsr getin
beq input_get
cmp #$0d
beq input_string_end
cmp #$14
bne increase_counter
jsr print_char
ldx counter
dex
stx counter
lda #$00
sta input_string,x
jmp input_get
increase_counter:
ldx counter
sta input_string,x
jsr print_char
inx
stx counter
cpx #string_length
bne input_get
input_string_end:
rts
string_to_fp:
lda #<input_string
sta $22
lda #>input_string
sta $23
lda #string_length
jsr fp_string_to_fac
jsr clear_input_string
rts
clear_input_string:
ldx #$0
lda #$0
clear_loop:
sta input_string, x
inx
cpx #string_length
bne clear_loop
rts
init_variables:
lda #<one
ldy #>one
jsr fp_load_ram_to_fac
ldx #<maxf
ldy #>maxf
jsr fp_store_fac_to_ram
ldx #<of
ldy #>of
jsr fp_store_fac_to_ram
ldx #<sr
ldy #>sr
jsr fp_store_fac_to_ram
ldx #<mf
ldy #>mf
jsr fp_store_fac_to_ram
rts
input_string:
.byte $00, $00, $00, $00, $00, $00, $00
counter:
.byte $0
usage_1:
.text "SQUARE ROOT CALCULATOR"
.byte $00
usage_2:
.text "PRESS ANY KEY TO CONTINUE"
.byte $00
usage_3:
.text "OR Q TO EXIT"
.byte $00
input_argument:
.text "ARGUMENT?"
.byte $00
one:
.byte $81, $00, $00, $00, $00
zero:
.byte $00, $00, $00, $00, $00
argument:
.byte $00, $00, $00, $00, $00
ul:
.byte $00, $00, $00, $00, $00
maxf:
.byte $00, $00, $00, $00, $00
mf:
.byte $00, $00, $00, $00, $00
of:
.byte $00, $00, $00, $00, $00
sr:
.byte $00, $00, $00, $00, $00
x:
.byte $00, $00, $00, $00, $00
y:
.byte $00, $00, $00, $00, $00
result_string_1:
.text "SQR("
.byte $00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment