mirror of
https://gitlab.com/camelot/kickc.git
synced 2024-12-28 16:31:36 +00:00
Moved remainder to global var
This commit is contained in:
parent
8705527316
commit
1fd0cd6874
@ -1,11 +1,13 @@
|
||||
// Binary division implementation
|
||||
|
||||
// Remainder after signed 8 bit division
|
||||
byte rem8u =0;
|
||||
|
||||
// Performs division on two 8 bit unsigned bytes
|
||||
// Returns dividend/divisor.
|
||||
// If remainder is non-null it will be set to the remainder.
|
||||
// The remainder will be set into the global variable rem8u
|
||||
// Implemented using simple binary division
|
||||
byte div8u(byte dividend, byte divisor, byte* remainder) {
|
||||
byte div8u(byte dividend, byte divisor) {
|
||||
byte rem = 0;
|
||||
byte quotient = 0;
|
||||
for( byte i : 0..7) {
|
||||
@ -20,7 +22,7 @@ byte div8u(byte dividend, byte divisor, byte* remainder) {
|
||||
rem = rem - divisor;
|
||||
}
|
||||
}
|
||||
*remainder = rem;
|
||||
rem8u = rem;
|
||||
return quotient;
|
||||
}
|
||||
|
||||
@ -29,8 +31,10 @@ signed byte rem8s = 0;
|
||||
|
||||
// Perform division on two signed 8-bit numbers
|
||||
// Returns dividend/divisor.
|
||||
// The remainder will be set into the global variable rem16u
|
||||
// The remainder will be set into the global variable rem8s.
|
||||
// Implemented using simple binary division
|
||||
// Follows the C99 standard by truncating toward zero on negative results.
|
||||
// See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf section 6.5.5
|
||||
signed byte div8s(signed byte dividend, signed byte divisor) {
|
||||
byte neg = 0;
|
||||
byte dividendb = 0;
|
||||
@ -47,8 +51,7 @@ signed byte div8s(signed byte dividend, signed byte divisor) {
|
||||
} else {
|
||||
divisorb = (byte)divisor;
|
||||
}
|
||||
byte rem8u = 0;
|
||||
byte resultb = div8u(dividendb, divisorb, &rem8u);
|
||||
byte resultb = div8u(dividendb, divisorb);
|
||||
if(neg==0) {
|
||||
rem8s = (signed byte)rem8u;
|
||||
return (signed byte)resultb;
|
||||
|
@ -34,14 +34,14 @@ void test_8u() {
|
||||
for( byte i: 0..5 ) {
|
||||
byte dividend = dividends[i];
|
||||
byte divisor = divisors[i];
|
||||
byte res = div8u(dividend, divisor, &rem);
|
||||
byte res = div8u(dividend, divisor);
|
||||
print_byte(dividend);
|
||||
print_str(" / @");
|
||||
print_byte(divisor);
|
||||
print_str(" = @");
|
||||
print_byte(res);
|
||||
print_str(" @");
|
||||
print_byte(rem);
|
||||
print_byte(rem8u);
|
||||
print_ln();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
.label SCREEN = $400
|
||||
.label char_cursor = 8
|
||||
.label line_cursor = 3
|
||||
.label rem8s = $e
|
||||
.label rem16u = $a
|
||||
jsr main
|
||||
main: {
|
||||
@ -220,12 +221,11 @@ div16u: {
|
||||
rts
|
||||
}
|
||||
test_8u: {
|
||||
.label rem = $14
|
||||
.label dividend = 7
|
||||
.label divisor = $e
|
||||
.label res = $f
|
||||
.label i = 2
|
||||
lda #0
|
||||
sta rem
|
||||
sta i
|
||||
b1:
|
||||
ldy i
|
||||
@ -235,13 +235,9 @@ test_8u: {
|
||||
sta divisor
|
||||
lda dividend
|
||||
sta div8u.dividend
|
||||
lda #<rem
|
||||
sta div8u.remainder
|
||||
lda #>rem
|
||||
sta div8u.remainder+1
|
||||
jsr div8u
|
||||
lda div8u.return
|
||||
tax
|
||||
sta res
|
||||
lda line_cursor
|
||||
sta char_cursor
|
||||
lda line_cursor+1
|
||||
@ -260,15 +256,15 @@ test_8u: {
|
||||
lda #>str1
|
||||
sta print_str.str+1
|
||||
jsr print_str
|
||||
stx print_byte.b
|
||||
lda res
|
||||
sta print_byte.b
|
||||
jsr print_byte
|
||||
lda #<str2
|
||||
sta print_str.str
|
||||
lda #>str2
|
||||
sta print_str.str+1
|
||||
jsr print_str
|
||||
lda rem
|
||||
sta print_byte.b
|
||||
stx print_byte.b
|
||||
jsr print_byte
|
||||
jsr print_ln
|
||||
inc i
|
||||
@ -287,7 +283,6 @@ div8u: {
|
||||
.label quotient = $10
|
||||
.label return = $10
|
||||
.label divisor = $e
|
||||
.label remainder = 5
|
||||
ldx #0
|
||||
txa
|
||||
sta quotient
|
||||
@ -316,14 +311,13 @@ div8u: {
|
||||
cpx #8
|
||||
bne b1
|
||||
tya
|
||||
ldy #0
|
||||
sta (remainder),y
|
||||
tax
|
||||
rts
|
||||
}
|
||||
test_8s: {
|
||||
.label dividend = 7
|
||||
.label divisor = $15
|
||||
.label res = $e
|
||||
.label divisor = $14
|
||||
.label res = $f
|
||||
.label i = 2
|
||||
lda #<SCREEN
|
||||
sta line_cursor
|
||||
@ -334,6 +328,7 @@ test_8s: {
|
||||
lda #>SCREEN
|
||||
sta char_cursor+1
|
||||
lda #0
|
||||
sta rem8s
|
||||
tax
|
||||
sta i
|
||||
b1:
|
||||
@ -368,7 +363,8 @@ test_8s: {
|
||||
lda #>str2
|
||||
sta print_str.str+1
|
||||
jsr print_str
|
||||
stx print_sbyte.b
|
||||
lda rem8s
|
||||
sta print_sbyte.b
|
||||
jsr print_sbyte
|
||||
jsr print_ln
|
||||
inc i
|
||||
@ -406,7 +402,6 @@ print_sbyte: {
|
||||
}
|
||||
div8s: {
|
||||
.label neg = $11
|
||||
.label rem8u = $16
|
||||
cmp #0
|
||||
bpl b16
|
||||
eor #$ff
|
||||
@ -427,29 +422,23 @@ div8s: {
|
||||
eor #1
|
||||
sta neg
|
||||
b4:
|
||||
lda #0
|
||||
sta rem8u
|
||||
sty div8u.dividend
|
||||
stx div8u.divisor
|
||||
lda #<rem8u
|
||||
sta div8u.remainder
|
||||
lda #>rem8u
|
||||
sta div8u.remainder+1
|
||||
jsr div8u
|
||||
lda div8u.return
|
||||
tay
|
||||
lda neg
|
||||
bne b5
|
||||
tya
|
||||
ldx rem8u
|
||||
stx rem8s
|
||||
breturn:
|
||||
rts
|
||||
b5:
|
||||
lda rem8u
|
||||
txa
|
||||
eor #$ff
|
||||
clc
|
||||
adc #1
|
||||
tax
|
||||
sta rem8s
|
||||
tya
|
||||
eor #$ff
|
||||
clc
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -11,9 +11,9 @@
|
||||
(byte*) char_cursor#104 char_cursor zp ZP_WORD:8 23.0
|
||||
(byte*) char_cursor#105 char_cursor zp ZP_WORD:8 17.333333333333332
|
||||
(byte*) char_cursor#109 char_cursor zp ZP_WORD:8 2.4444444444444446
|
||||
(byte*~) char_cursor#121 char_cursor zp ZP_WORD:8 22.0
|
||||
(byte*~) char_cursor#128 char_cursor zp ZP_WORD:8 22.0
|
||||
(byte*~) char_cursor#137 char_cursor zp ZP_WORD:8 22.0
|
||||
(byte*~) char_cursor#122 char_cursor zp ZP_WORD:8 22.0
|
||||
(byte*~) char_cursor#129 char_cursor zp ZP_WORD:8 22.0
|
||||
(byte*~) char_cursor#138 char_cursor zp ZP_WORD:8 22.0
|
||||
(byte*) char_cursor#14 char_cursor zp ZP_WORD:8 2.5227272727272725
|
||||
(byte*) char_cursor#64 char_cursor zp ZP_WORD:8 5.0
|
||||
(word()) div16u((word) div16u::dividend , (word) div16u::divisor)
|
||||
@ -64,21 +64,19 @@
|
||||
(signed byte) div8s::dividend
|
||||
(signed byte) div8s::dividend#0 reg byte a 5.0
|
||||
(byte) div8s::dividendb
|
||||
(byte) div8s::dividendb#3 reg byte y 0.75
|
||||
(byte) div8s::dividendb#3 reg byte y 0.8571428571428571
|
||||
(byte~) div8s::dividendb#7 reg byte y 4.0
|
||||
(byte~) div8s::dividendb#8 reg byte y 4.0
|
||||
(signed byte) div8s::divisor
|
||||
(signed byte) div8s::divisor#0 reg byte x 2.142857142857143
|
||||
(byte) div8s::divisorb
|
||||
(byte) div8s::divisorb#3 reg byte x 2.0
|
||||
(byte) div8s::divisorb#3 reg byte x 3.0
|
||||
(byte~) div8s::divisorb#4 reg byte x 4.0
|
||||
(byte~) div8s::divisorb#5 reg byte x 4.0
|
||||
(byte) div8s::neg
|
||||
(byte) div8s::neg#2 neg zp ZP_BYTE:17 2.0
|
||||
(byte) div8s::neg#3 neg zp ZP_BYTE:17 1.0
|
||||
(byte) div8s::neg#4 neg zp ZP_BYTE:17 0.8571428571428571
|
||||
(byte) div8s::rem8u
|
||||
(byte) div8s::rem8u#0 rem8u zp ZP_BYTE:22 0.024390243902439025
|
||||
(byte) div8s::neg#4 neg zp ZP_BYTE:17 1.0
|
||||
(byte) div8s::resultb
|
||||
(byte) div8s::resultb#0 reg byte y 0.6666666666666666
|
||||
(signed byte) div8s::return
|
||||
@ -86,7 +84,7 @@
|
||||
(signed byte) div8s::return#2 reg byte a 5.0
|
||||
(signed byte) div8s::return#3 reg byte a 22.0
|
||||
(signed byte~) div8s::return#6 reg byte a 2.0
|
||||
(byte()) div8u((byte) div8u::dividend , (byte) div8u::divisor , (byte*) div8u::remainder)
|
||||
(byte()) div8u((byte) div8u::dividend , (byte) div8u::divisor)
|
||||
(byte~) div8u::$1 reg byte a 202.0
|
||||
(label) div8u::@1
|
||||
(label) div8u::@2
|
||||
@ -119,8 +117,6 @@
|
||||
(byte) div8u::rem#4 reg byte y 202.0
|
||||
(byte) div8u::rem#5 reg byte y 101.0
|
||||
(byte) div8u::rem#8 reg byte y 101.66666666666667
|
||||
(byte*) div8u::remainder
|
||||
(byte*) div8u::remainder#8 remainder zp ZP_WORD:5 0.13333333333333333
|
||||
(byte) div8u::return
|
||||
(byte) div8u::return#0 return zp ZP_BYTE:16 45.142857142857146
|
||||
(byte) div8u::return#2 reg byte a 4.0
|
||||
@ -199,10 +195,13 @@
|
||||
(word) rem16u
|
||||
(word) rem16u#16 rem16u zp ZP_WORD:10 110.0
|
||||
(signed byte) rem8s
|
||||
(signed byte) rem8s#1 reg byte x 2.0
|
||||
(signed byte) rem8s#18 reg byte x 110.0
|
||||
(signed byte) rem8s#3 reg byte x 1.0833333333333333
|
||||
(signed byte~) rem8s#32 reg byte x 4.0
|
||||
(signed byte) rem8s#1 rem8s zp ZP_BYTE:14 2.0
|
||||
(signed byte) rem8s#18 rem8s zp ZP_BYTE:14 110.0
|
||||
(signed byte) rem8s#3 rem8s zp ZP_BYTE:14 1.0833333333333333
|
||||
(signed byte~) rem8s#32 rem8s zp ZP_BYTE:14 4.0
|
||||
(byte) rem8u
|
||||
(byte) rem8u#1 reg byte x 0.48
|
||||
(byte) rem8u#33 reg byte x 110.0
|
||||
(void()) test_16u()
|
||||
(label) test_16u::@1
|
||||
(label) test_16u::@10
|
||||
@ -249,14 +248,14 @@
|
||||
(signed byte[]) test_8s::dividends
|
||||
(const signed byte[]) test_8s::dividends#0 dividends = { (byte/signed byte/word/signed word/dword/signed dword) 127, -(byte/signed byte/word/signed word/dword/signed dword) 127, -(byte/signed byte/word/signed word/dword/signed dword) 127, (byte/signed byte/word/signed word/dword/signed dword) 127, (byte/signed byte/word/signed word/dword/signed dword) 127, (byte/signed byte/word/signed word/dword/signed dword) 127 }
|
||||
(signed byte) test_8s::divisor
|
||||
(signed byte) test_8s::divisor#0 divisor zp ZP_BYTE:21 3.3000000000000003
|
||||
(signed byte) test_8s::divisor#0 divisor zp ZP_BYTE:20 3.3000000000000003
|
||||
(signed byte[]) test_8s::divisors
|
||||
(const signed byte[]) test_8s::divisors#0 divisors = { (byte/signed byte/word/signed word/dword/signed dword) 5, (byte/signed byte/word/signed word/dword/signed dword) 7, -(byte/signed byte/word/signed word/dword/signed dword) 11, -(byte/signed byte/word/signed word/dword/signed dword) 13, (byte/signed byte/word/signed word/dword/signed dword) 17, (byte/signed byte/word/signed word/dword/signed dword) 19 }
|
||||
(byte) test_8s::i
|
||||
(byte) test_8s::i#1 i zp ZP_BYTE:2 11.0
|
||||
(byte) test_8s::i#10 i zp ZP_BYTE:2 1.8333333333333333
|
||||
(signed byte) test_8s::res
|
||||
(signed byte) test_8s::res#0 res zp ZP_BYTE:14 2.4444444444444446
|
||||
(signed byte) test_8s::res#0 res zp ZP_BYTE:15 2.4444444444444446
|
||||
(const string) test_8s::str str = (string) " / @"
|
||||
(const string) test_8s::str1 str1 = (string) " = @"
|
||||
(const string) test_8s::str2 str2 = (string) " @"
|
||||
@ -283,29 +282,27 @@
|
||||
(byte) test_8u::i
|
||||
(byte) test_8u::i#1 i zp ZP_BYTE:2 16.5
|
||||
(byte) test_8u::i#10 i zp ZP_BYTE:2 1.76
|
||||
(byte) test_8u::rem
|
||||
(byte) test_8u::rem#0 rem zp ZP_BYTE:20 0.12745098039215685
|
||||
(byte) test_8u::res
|
||||
(byte) test_8u::res#0 reg byte x 2.2
|
||||
(byte) test_8u::res#0 res zp ZP_BYTE:15 2.2
|
||||
(const string) test_8u::str str = (string) " / @"
|
||||
(const string) test_8u::str1 str1 = (string) " = @"
|
||||
(const string) test_8u::str2 str2 = (string) " @"
|
||||
|
||||
zp ZP_BYTE:2 [ test_16u::i#10 test_16u::i#1 test_8u::i#10 test_8u::i#1 test_8s::i#10 test_8s::i#1 ]
|
||||
zp ZP_WORD:3 [ line_cursor#17 line_cursor#33 line_cursor#1 line_cursor#37 print_cls::sc#2 print_cls::sc#1 ]
|
||||
zp ZP_WORD:5 [ print_word::w#4 print_word::w#0 print_word::w#1 print_word::w#2 print_word::w#3 test_16u::dividend#0 print_str::str#10 print_str::str#12 print_str::str#0 div8u::remainder#8 ]
|
||||
zp ZP_WORD:5 [ print_word::w#4 print_word::w#0 print_word::w#1 print_word::w#2 print_word::w#3 test_16u::dividend#0 print_str::str#10 print_str::str#12 print_str::str#0 ]
|
||||
zp ZP_BYTE:7 [ print_byte::b#7 print_byte::b#9 print_byte::b#1 print_byte::b#2 print_byte::b#3 print_byte::b#4 print_byte::b#5 print_byte::b#6 print_sbyte::b#6 print_sbyte::b#5 print_sbyte::b#1 print_sbyte::b#2 print_sbyte::b#3 print_sbyte::b#4 print_sbyte::b#0 test_8u::dividend#0 test_8s::dividend#0 ]
|
||||
reg byte a [ print_char::ch#3 print_char::ch#1 print_char::ch#2 ]
|
||||
zp ZP_WORD:8 [ char_cursor#64 char_cursor#105 char_cursor#102 char_cursor#104 char_cursor#121 char_cursor#100 char_cursor#14 char_cursor#128 char_cursor#103 char_cursor#1 char_cursor#109 char_cursor#137 div16u::dividend#2 div16u::dividend#1 div16u::dividend#0 ]
|
||||
zp ZP_WORD:8 [ char_cursor#64 char_cursor#105 char_cursor#102 char_cursor#104 char_cursor#122 char_cursor#100 char_cursor#14 char_cursor#129 char_cursor#103 char_cursor#1 char_cursor#109 char_cursor#138 div16u::dividend#2 div16u::dividend#1 div16u::dividend#0 ]
|
||||
zp ZP_WORD:10 [ div16u::rem#4 rem16u#16 div16u::rem#8 div16u::rem#5 div16u::rem#1 div16u::rem#2 div16u::rem#3 ]
|
||||
zp ZP_WORD:12 [ div16u::quotient#3 div16u::return#0 div16u::quotient#1 div16u::quotient#2 div16u::return#2 test_16u::res#0 ]
|
||||
reg byte x [ div16u::i#2 div16u::i#1 ]
|
||||
zp ZP_BYTE:14 [ div8u::divisor#6 div8u::divisor#0 div8u::divisor#1 test_8u::divisor#0 test_8s::res#0 ]
|
||||
zp ZP_BYTE:14 [ div8u::divisor#6 div8u::divisor#0 div8u::divisor#1 test_8u::divisor#0 rem8s#18 rem8s#3 rem8s#32 rem8s#1 ]
|
||||
reg byte y [ div8u::rem#4 div8u::rem#8 div8u::rem#5 div8u::rem#1 div8u::rem#2 div8u::rem#3 ]
|
||||
zp ZP_BYTE:15 [ div8u::dividend#3 div8u::dividend#5 div8u::dividend#1 div8u::dividend#2 div8u::dividend#0 ]
|
||||
zp ZP_BYTE:15 [ div8u::dividend#3 div8u::dividend#5 div8u::dividend#1 div8u::dividend#2 div8u::dividend#0 test_8u::res#0 test_8s::res#0 ]
|
||||
zp ZP_BYTE:16 [ div8u::quotient#3 div8u::return#0 div8u::quotient#1 div8u::quotient#2 ]
|
||||
reg byte x [ div8u::i#2 div8u::i#1 ]
|
||||
reg byte x [ rem8s#18 rem8s#3 rem8s#32 rem8s#1 ]
|
||||
reg byte x [ rem8u#33 rem8u#1 ]
|
||||
reg byte y [ div8s::dividendb#3 div8s::dividendb#7 div8s::dividendb#8 ]
|
||||
reg byte x [ div8s::divisorb#3 div8s::divisorb#4 div8s::divisorb#5 ]
|
||||
zp ZP_BYTE:17 [ div8s::neg#4 div8s::neg#3 div8s::neg#2 ]
|
||||
@ -315,16 +312,13 @@ reg byte a [ print_byte::$0 ]
|
||||
reg byte a [ print_byte::$2 ]
|
||||
reg byte a [ div16u::$1 ]
|
||||
reg byte a [ div16u::$2 ]
|
||||
zp ZP_BYTE:20 [ test_8u::rem#0 ]
|
||||
reg byte a [ div8u::return#3 ]
|
||||
reg byte x [ test_8u::res#0 ]
|
||||
reg byte a [ div8u::$1 ]
|
||||
zp ZP_BYTE:21 [ test_8s::divisor#0 ]
|
||||
zp ZP_BYTE:20 [ test_8s::divisor#0 ]
|
||||
reg byte a [ div8s::dividend#0 ]
|
||||
reg byte x [ div8s::divisor#0 ]
|
||||
reg byte a [ div8s::return#3 ]
|
||||
reg byte a [ div8s::$2 ]
|
||||
reg byte x [ div8s::$7 ]
|
||||
zp ZP_BYTE:22 [ div8s::rem8u#0 ]
|
||||
reg byte a [ div8u::return#2 ]
|
||||
reg byte y [ div8s::resultb#0 ]
|
||||
|
Loading…
Reference in New Issue
Block a user