//Use KickAssembler to assembly this source


.var wy       = $fb

		 .pc = $0801 "Basic Upstart"
		 :BasicUpstart($0810)

         .pc = $0810 "Main program"

         sei
         lda #$37
         sta $01
         jsr $ff5b
//---------------------------------------
mloop:
         lda #$01
         sta $0286
         jsr $e544


         lda #$00
         sta $c6
         tay
         sta $fb
         lda #$04
         sta $fc

         lda liczba1
         jsr prh //printhex

         jsr razy
         lda liczba2
         jsr prh
         jsr rownasie
         lda #$00
         ldx liczba1
         jsr bcdconv
         jsr razy
         lda #$00
         ldx liczba2
         jsr bcdconv
         jsr rownasie
        

         lda liczba1
         ldx liczba2
         jsr unsmult
         jsr bcdconv
         

         lda #$50
         sta $fb
         ldy #$00

         lda liczba1
         jsr signprh
         jsr razy
         lda liczba2
         jsr signprh
         jsr rownasie

         lda liczba1
         jsr signbcdlo
         jsr razy
         lda liczba2
         jsr signbcdlo
         jsr rownasie

         lda liczba1
         ldx liczba2
         jsr signmult
         jsr signbcd

         lda #<txt
         ldy #>txt
         jsr $ab1e
         
                                   

gk00:
         cli
         jsr $ffe4
         beq gk00
         cmp #$85    //f1
         bne gk01
         inc liczba1
gk01:     cmp #$89    //f2
         bne gk02
         dec liczba1
gk02:     cmp #$86    //f3
         bne gk03
         lda liczba1
         clc
         adc #10
         sta liczba1
         lda #$00
gk03:     cmp #$8a    //f4
         bne gk04
         lda liczba1
         sec
         sbc #10
         sta liczba1
         lda #$00
gk04:     cmp #$87    //f5
         bne gk05
         lda liczba2
         clc
         adc #10
         sta liczba2
         lda #$00
gk05:     cmp #$8b    //f6
         bne gk06
         lda liczba2
         sec
         sbc #10
         sta liczba2
         lda #$00
gk06:     cmp #$88    //f7
         bne gk07
         inc liczba2
gk07:     cmp #$8c    //f8
         bne gk08
         dec liczba2
gk08:     cmp #$5f    //st. w lewo
         bne gk09
         rts
gk09:     jmp mloop
//---------------------------------------
//-------
liczba1:  .byte $00
liczba2:  .byte $00

//--------

mnozna:   .byte 0
mnoznik:  .byte 0
sign:     .byte 0
//--------
signmult:

         sta mnozna
         stx mnoznik

         eor mnoznik  //check sign of
         sta sign     //result
         bpl sign2
         lda mnozna
         beq sign2    //result will be
                      //0 not < 0
         lda mnoznik
         beq sign2

         lda #'-'
         bne sign3
sign2:    lda #$20
sign3:    sta sign
         lda mnozna
         bpl sign4
         eor #$ff
         clc
         adc #$01
         sta mnozna
sign4:
         lda mnoznik
         bpl sign5
         eor #$ff
         clc
         adc #$01
         sta mnoznik

sign5:
         lda mnozna
         ldx mnoznik
//--------
unsmult:
         sta mnozna
         stx mnoznik

         lda #$00
         ldx #$08

mnoze:
         ror mnoznik
         bcc skok
         clc
         adc mnozna
skok:     ror
         dex
         bne mnoze
         ror mnoznik
         ldx mnoznik
         sta mnozna

         rts

         //èibyte->á ìobyte->ø
//-------
signprh:
         bpl prh
         pha
         lda #'-'
         jsr storechar
         pla
         eor #$ff
         clc
         adc #$01
         jmp prh2

prh:
         pha
         jsr sendspace
         pla
prh2:
         pha
         lda #'$'
         jsr storechar
         pla
         pha
         lsr
         lsr
         lsr
         lsr
         jsr conv2
         pla
         jsr conv1

sendspace:
         lda #$20
storechar:
         sta (wy),y
         iny
         rts
//---
conv1:    and #$0f
conv2:    ora #$30
         cmp #$3a
         bcc *+4
         adc #$06
         and #$3f
         jmp storechar
razy:     lda #'*'
         bne storechar
rownasie:
         lda #'='
         bne storechar
//--------
signbcdlo:
         bpl bcdsg1
         ldx #'-'
         stx sign
         eor #$ff
         clc
         adc #$01
         tax
         lda #$00
         beq signbcd
//-
bcdsg1:   tax
         lda #$20
         sta sign
         lda #$00
signbcd:
         sta hibyte
         stx lobyte
         lda sign
         sta znak
         jmp contbcd
//-
bcdconv:
         sta hibyte
         stx lobyte

         lda #$20
         sta znak
contbcd:
         tya    //save ù
         pha

         lda #$30
         ldx #$04
         sta raz,x
         dex
         bne *-4
         ldx #$04
bcd2:
         lda lobyte
         sec
         sbc tablo,x
         sta lobyte
         lda hibyte
         sbc tabhi,x
         bcs bcd3
         lda lobyte
         clc
         adc tablo,x
         sta lobyte
         dex
         bne bcd2
         beq bcd4
bcd3:     sta hibyte
         inc raz,x
         jmp bcd2
bcd4:

         lda lobyte
         clc
         adc #$30
         sta raz

         ldy #$00
         ldx #$04
bcd6:
         lda raz,x
         cmp #$30
         bne bcd5
         dex
         bne bcd6

bcd5:     lda raz,x
         sta wynik,y
         iny
         dex
         bpl bcd5
         lda #$20
         sta wynik,y
         iny
         lda #$00
         sta wynik,y

         pla
         tay        //restore ù

         ldx #$00
bcd8:
         lda dana,x
         beq bcd7
         jsr storechar
         inx
         bne bcd8
bcd7:
         rts
//--------
hibyte:   .byte 0
lobyte:   .byte 0

//--------
raz:      .byte 0
dzies:    .byte 0
sto:      .byte 0
tysiac:   .byte 0
dtysiac:  .byte 0

dana:     .byte 32
znak:     .byte 32
wynik:    .text "0000000"

//--------
//---   tablica hex 1,10,100,1000,10000
//--------
tablo:
         .byte 1
         .byte 10
         .byte 100
         .byte $e8
         .byte $10

tabhi:
         .byte 0
         .byte 0
         .byte 0
         .byte 3
         .byte $27

//--------
txt:
         .byte 13,13,13,13,13
         .text "F1 INC NUM1"
         .byte 13,13
         .text "F2 DEC NUM1"
         .byte 13,13
         .text "F3 +10 NUM1"
         .byte 13,13
         .text "F4 -10 NUM1"
         .byte 13,13
         .text "F5 +10 NUM2"
         .byte 13,13
         .text "F6 -10 NUM2"
         .byte 13,13
         .text "F7 INC NUM2"
         .byte 13,13
         .text "F8 DEC NUM2"
         .byte 13,13,13
         .text "UNSIGNED AND SIGNED 8 "
         .text "BIT MULTIPLY"
         .byte 0
//--------

