mads 1.9.0 build 21 (16 Aug 09)
Source: C:\nes\atalan\codebase\atalan\examples/bit_sieve.asm
     1 = 0600			_stdbuf equ 1536
     2 = 0080			_arr equ 128
     3 = 0653			_putchr_proc_adr equ 1619
     4 = 0034			_TEMPW2 equ 52
     5 = 0600			_std_print_out___5 equ _stdbuf
     6 = 0653			_std_print_out___6 equ _putchr_proc_adr
     7 = 0014			rtclock1 equ 20
     8 = 0013			rtclock2 equ 19
     9 = 0082			count equ 130
    10 = 0084			_s1__i equ 132
    11 = 0086			_s2__i equ 134
    12 = 0087			_s2___21 equ 135
    13 = 0088			_s2___22 equ 136
    14 = 0089			_s3__k equ 137
    15 = 008B			_s3___25 equ 139
    16 = 008D			_s3___27 equ 141
    17 = 008F			_s3___28 equ 143
    18 = 0090			_s4__k equ 144
    19 = 0092			_s4___30 equ 146
    20 = 0094			t equ 148
    21 = 0096			_35 equ 150
    22 = 0097			_36 equ 151
    23 = 0098			_37 equ 152
    24 				   org $2e0
    25 FFFF> 02E0-02E1> 00 20	   dta a($2000)
    26 02E2			   org $2000
    27 				;### bit_sieve.atl(24) rtclock1 = 0
    28 2000-2292> A0 00		   ldy #0
    29 2002 84 14		   sty rtclock1
    30 				;### bit_sieve.atl(25) rtclock2 = 0
    31 2004 84 13		   sty rtclock2
    32 				;### bit_sieve.atl(27) for i:0..bmax flags(i)=$aa
    33 2006 84 85		   sty _s1__i-0+1
    34 2008			_lbl5:
    35 2008 A9 93		   lda #<flags
    36 200A 85 80		   sta _arr-0
    37 200C A9 22		   lda #>flags
    38 200E 18			   clc
    39 200F 65 85		   adc _s1__i+1
    40 2011 85 81		   sta _arr-0+1
    41 2013 A9 AA		   lda #170
    42 2015 91 80		   sta (_arr),y
    43 2017 C8			   iny
    44 2018 D0 02		   jne _lbl12
    45 201A E6 85		   inc _s1__i+1
    46 201C			_lbl12:
    47 201C C0 00		   cpy #0
    48 201E D0 E8		   jne _lbl5
    49 2020 A5 85		   lda _s1__i-0+1
    50 2022 C9 20		   cmp #32
    51 2024 D0 E2		   jne _lbl5
    52 				;### bit_sieve.atl(29) for i:3..sqrt max_prime step 2 where (flags(i/8) bitand mask(i mod 8) <> 0)
    53 2026 A9 03		   lda #3
    54 2028 85 86		   sta _s2__i
    55 202A			_lbl9:
    56 202A A5 86		   lda _s2__i
    57 202C 4A			   lsr
    58 202D 4A			   lsr
    59 202E 4A			   lsr
    60 202F 85 87		   sta _s2___21
    61 2031 A5 86		   lda _s2__i
    62 2033 29 07		   and #7
    63 2035 85 88		   sta _s2___22
    64 2037 A5 87		   lda _s2___21
    65 				   ;
    66 2039 AA			   tax
    67 203A BD 93 22		   lda flags-0,x
    68 203D 85 96		   sta _35
    69 203F A6 88		   ldx _s2___22
    70 2041 BD 45 21		   lda mask-0,x
    71 2044 25 96		   and _35
    72 2046 C9 00		   cmp #0
    73 2048 F0 5B		   jeq _lbl6
    74 				;### bit_sieve.atl(30) 	for k:i*i..max_prime step 2*i
    75 204A A5 86		   lda _s2__i
    76 204C A6 86		   ldx _s2__i
    77 204E 20 79 22		   jsr _sys_mul8
    78 2051 A5 86		   lda _s2__i
    79 2053 0A			   asl
    80 2054 85 8B		   sta _s3___25-0
    81 2056 A9 00		   lda #0
    82 2058 2A			   rol
    83 2059 85 8C		   sta _s3___25-0+1
    84 205B A5 34		   lda _TEMPW2-0
    85 205D 85 89		   sta _s3__k-0
    86 205F A5 35		   lda _TEMPW2-0+1
    87 2061 85 8A		   sta _s3__k-0+1
    88 2063			_lbl8:
    89 				;### bit_sieve.atl(31) 		flags(k/8) = flags(k/8) bitand maskx(k mod 8)
    90 2063 A5 89		   lda _s3__k-0
    91 2065 85 8D		   sta _s3___27-0
    92 2067 A5 8A		   lda _s3__k-0+1
    93 2069 4A			   lsr
    94 206A 66 8D		   ror _s3___27-0+0
    95 206C 4A			   lsr
    96 206D 66 8D		   ror _s3___27-0+0
    97 206F 4A			   lsr
    98 2070 66 8D		   ror _s3___27-0+0
    99 2072 85 8E		   sta _s3___27-0+1
   100 2074 A5 89		   lda _s3__k-0
   101 2076 29 07		   and #7
   102 2078 85 8F		   sta _s3___28
   103 207A A9 93		   lda #<flags
   104 207C 85 80		   sta _arr-0
   105 207E A9 22		   lda #>flags
   106 2080 18			   clc
   107 2081 65 8E		   adc _s3___27+1
   108 2083 85 81		   sta _arr-0+1
   109 2085 A4 8D		   ldy _s3___27-0+0
   110 2087 B1 80		   lda (_arr),y
   111 2089 85 97		   sta _36
   112 208B A5 8F		   lda _s3___28
   113 				   ;
   114 208D AA			   tax
   115 208E A5 97		   lda _36
   116 2090 3D 4D 21		   and maskx,x
   117 2093 18			   clc
   118 2094 91 80		   sta (_arr),y
   119 2096 A5 89		   lda _s3__k-0
   120 2098 18			   clc
   121 2099 65 8B		   adc _s3___25+0
   122 209B 85 89		   sta _s3__k-0
   123 209D A5 8A		   lda _s3__k-0+1
   124 209F 65 8C		   adc _s3___25+1
   125 20A1 85 8A		   sta _s3__k-0+1
   126 20A3 90 BE		   jcc _lbl8
   127 20A5			_lbl6:
   128 20A5 A5 86		   lda _s2__i
   129 20A7 18			   clc
   130 20A8 69 02		   adc #2
   131 20AA 85 86		   sta _s2__i
   132 20AC C9 01		   cmp #1
   133 20AE F0 03 4C 2A 20	   jne _lbl9
   134 				;### bit_sieve.atl(33) count = 1
   135 20B3 A9 01		   lda #1
   136 20B5 85 82		   sta count-0
   137 20B7 A9 00		   lda #0
   138 20B9 85 83		   sta count-0+1
   139 				;### bit_sieve.atl(34) for k:3..max_prime step 2 where (flags(k/8) bitand mask(k mod 8) <> 0) 
   140 20BB A9 03		   lda #3
   141 20BD 85 90		   sta _s4__k-0
   142 20BF A9 00		   lda #0
   143 20C1 85 91		   sta _s4__k-0+1
   144 20C3			_lbl11:
   145 20C3 A5 90		   lda _s4__k-0
   146 20C5 85 92		   sta _s4___30-0
   147 20C7 A5 91		   lda _s4__k-0+1
   148 20C9 4A			   lsr
   149 20CA 66 92		   ror _s4___30-0+0
   150 20CC 4A			   lsr
   151 20CD 66 92		   ror _s4___30-0+0
   152 20CF 4A			   lsr
   153 20D0 66 92		   ror _s4___30-0+0
   154 20D2 85 93		   sta _s4___30-0+1
   155 20D4 A5 90		   lda _s4__k-0
   156 20D6 29 07		   and #7
   157 20D8 AA			   tax
   158 20D9 A9 93		   lda #<flags
   159 20DB 85 80		   sta _arr-0
   160 20DD A9 22		   lda #>flags
   161 20DF 18			   clc
   162 20E0 65 93		   adc _s4___30+1
   163 20E2 85 81		   sta _arr-0+1
   164 20E4 A4 92		   ldy _s4___30-0+0
   165 20E6 B1 80		   lda (_arr),y
   166 20E8 85 98		   sta _37
   167 20EA BD 45 21		   lda mask-0,x
   168 20ED 25 98		   and _37
   169 20EF C9 00		   cmp #0
   170 20F1 F0 06		   jeq _lbl10
   171 				;### bit_sieve.atl(35) 	inc count
   172 20F3 E6 82		   inc count+0
   173 20F5 D0 02		   jne _lbl10
   174 20F7 E6 83		   inc count+1
   175 20F9			_lbl10:
   176 20F9 A5 90		   lda _s4__k-0
   177 20FB 18			   clc
   178 20FC 69 02		   adc #2
   179 20FE 85 90		   sta _s4__k-0
   180 2100 90 02		   jcc _lbl14
   181 2102 E6 91		   inc _s4__k+1
   182 2104			_lbl14:
   183 2104 A5 90		   lda _s4__k-0
   184 2106 C9 01		   cmp #1
   185 2108 D0 B9		   jne _lbl11
   186 210A A5 91		   lda _s4__k-0+1
   187 210C C9 00		   cmp #0
   188 210E D0 B3		   jne _lbl11
   189 				;### bit_sieve.atl(37) t = rtclock2 * 256 + rtclock1
   190 2110 A9 00		   lda #0
   191 2112 18			   clc
   192 2113 65 14		   adc rtclock1
   193 2115 85 94		   sta t-0
   194 2117 A5 13		   lda rtclock2
   195 2119 69 00		   adc #0
   196 211B 85 95		   sta t-0+1
   197 				;### bit_sieve.atl(39) "[count] prime numbers in [t] ticks"
   198 211D 20 61 21		   jsr _std_print_out
   199 2120 82 82 00		   dta b(130),a(count)
   200 2123 12 20 70 72 69 6D +    dta b(18),c' prime numbers in '
   201 2136 82 94 00		   dta b(130),a(t)
   202 2139 06 20 74 69 63 6B +    dta b(6),c' ticks'
   203 2140 80			   dta b(128)
   204 2141 00			   dta b(0)
   205 2142			_lbl15:
   206 2142 4C 42 21		   jmp _lbl15
   207 2145			mask:
   208 				;### bit_sieve.atl(19) 	mask:array(0..7) = 1,2,4,8,16,32,64,128
   209 2145 01			   dta b(1)
   210 2146 02			   dta b(2)
   211 2147 04			   dta b(4)
   212 2148 08			   dta b(8)
   213 2149 10			   dta b(16)
   214 214A 20			   dta b(32)
   215 214B 40			   dta b(64)
   216 214C 80			   dta b(128)
   217 214D			maskx:
   218 				;### bit_sieve.atl(20) 	maskx:array(0..7) = %1111'1110,%1111'1101,%1111'1011,%1111'0111,%1110'1111,%1101'1111,%1011'1111,%0111'1111
   219 214D FE			   dta b(254)
   220 214E FD			   dta b(253)
   221 214F FB			   dta b(251)
   222 2150 F7			   dta b(247)
   223 2151 EF			   dta b(239)
   224 2152 DF			   dta b(223)
   225 2153 BF			   dta b(191)
   226 2154 7F			   dta b(127)
   227 2155 60			   rts
   228 2156			   icl 'C:\nes\atalan\codebase\atalan\platform\atari\atari.asm'
Source: C:\nes\atalan\codebase\atalan\platform\atari\atari.asm
     1 = 009B			_EOL_CHAR equ 155
     2
     3 2156			_adr_putchr .proc
     4 				;Write byte to address specified by _arr and increment the address by 1.
     5 					
     6 					;Convert ASCII to ATARI character screen code
     7 					;
     8 					;The rules are following:
     9 					;0..31   +64
    10 					;32..95  -32
    11 					;96..127 0  (codes are same)
    12 					 	
    13 					tax
    14 					rol 
    15 					rol 
    16 					rol 
    17 					rol
    18 					and #3
    19 					tay
    20 					txa
    21 					eor tbl,y
    22 					
    23 					;write the char to specified address	
    24 					ldy #0
    25 					sta (_arr),y
    26
    27 					inc _arr
    28 					bne skip1
    29 					inc _arr+1
    30 2156			skip1
    31 					rts
    32
    33 2156			tbl	dta b(%01000000)		;%0 00 00000		00..31	+64
    34 						dta b(%00100000)		;%0 01 00000		32..63	-32
    35 						dta b(%01100000)		;%0 10 00000    64..95  -32
    36 						dta b(%00000000)		;%0 11 00000    96..127 0
    37 					
    38 				.endp
    39
    40 2156			_out_putchr .proc
    41 				;Write character from A to output.
    42 					
    43 2156 AA					tax
    44 2157 AD 47 03				lda $347	;ICPUTB+1
    45 215A 48					pha
    46 215B AD 46 03				lda $346	;ICPUTB
    47 215E 48					pha
    48 215F 8A					txa
    49 2160 60					rts
    50
    51 				.endp
   229 2161			   icl 'C:\nes\atalan\codebase\atalan\processor\m6502\m6502.asm'
Source: C:\nes\atalan\codebase\atalan\processor\m6502\m6502.asm
     1 				;********************************************************
     2 				;
     3 				;  ATALAN 6502 processor definition support routines
     4 				;
     5 				;  Parts of code have been taken from Effectus library.
     6 				;
     7 				;********************************************************
     8
     9
    10 				;Noname
    11 				;651-655         028B-028F
    12 				;
    13 				;More spare bytes that you shouldn't use because future versions of the OS might use them. 
    14 				;Locations 651 and 652 are already used by version "B" as part of the interrupt handler routines.
    15
    16 				;OS registers we use as temporary:
    17 				;
    18 				;BUFRLO,BUFRHI   50,51 $32,$33
    19 				;BFENLO,BFENHI   52,53 $34,$35
    20
    21 				;ADRESS  100,101   $64,$65  A temporary storage location used by the display handler for so many things that it made my mind spin and I forgot what they were.
    22 				;MLTTMP  102,103   $66,$67  More temporary storage, with aliases OPNTMP and TOADR.
    23 				;SAVADR  104,105   $68,$69  Also know as FRMADR. Also used for temporary storage. 
    24 				;                           Also not significant enough to explain (look at the OS listing if, for some reason, you really care).
    25
    26 				;Registers used when drawing line
    27 				;ROWAC   112,113   $70,$71
    28 				;COLAC   114,115   $72,$73
    29 				;ENDPT   116,117   $74,$75
    30 				;COUNTR  126,127   $7E,$7F  COUNTR tells how many points have to be plotted before the line is finished.
    31
    32 				;
    33 = 0001			_SYS_PRINT_SIGNED = 1
    34
    35 				;_putchr_proc_adr = $653  ;+$654 
    36 = 0032			?argptr =   $32		;$33
    37 = 0034			?varptr	=	$34
    38 = 0015			?size	=	$15		;15		//$16 BUFADR
    39 						.IF _SYS_PRINT_SIGNED = 1 
    40 = 0016			?sign = $16
    41 						.ENDIF
    42 = 0038			?aux2	=	$38
    43 = 0039			?aux	=	$39
    44
    45 				;Following assigns are defined by mc6502.atl
    46
    47 = 0032			_TW1 = $32	;$33  mc6502.atl defines these labels as _TEMPW1, _TEMPW2.
    48 = 0034			_TW2 = $34  ;$35
    49 = 0015			_TW3 = $15	;$16
    50 = 0070			_TL1 = $70	;four byte register ($70-$73) defined under name _TEMPL1 in mc6502.atl		
    51
    52 				;_stdbuf    = $600		;TODO: use LINBUF ($247-$26E)  (100,101 can point to it)
    53
    54 				;Argument list:
    55 				;	0	end of list
    56 				;	1..127  Constant string of specified lenght
    57 				; 128     EOL
    58
    59 				;   7 6 5 4 3 2 1 0 
    60 				;  |0| | | | | | | |		String of length 0..127
    61 				;  |1|0|0| | | | | |    Pointer to unsigned integer of length 1..31 bytes
    62 				;  |1|0|1| | | | | |    Pointer to signed integer of length 1..31 bytes
    63
    64 				;         ---- ptr to unsigned integer ---- (7-bit set)
    65 				; 129     one byte integer reference
    66 				;	130     two byte integer reference
    67 				;	131     three byte integer eference
    68 				;	132     four byte integer reference
    69 				;         ---- ptr to signed -----  (5-bit set)
    70 				; 161     one byte
    71 				; 162     two byte
    72 				; 163     three byte
    73 				; 164     four byte
    74
    75 2161			_std_print_adr .proc
    76
    77 						lda #<_adr_putchr
    78 						ldx #>_adr_putchr
    79 						clc
    80 						bcc system__print
    81 						.endp
    82 						
    83 2161			_std_print_out .proc
    84 2161 A9 56				lda #<_out_putchr
    85 2163 A2 21				ldx #>_out_putchr
    86 2165 18					clc											;TODO: No Jump, if we are directly before the _std_print
    87 2166 90 00				bcc system__print
    88 						.endp
    89
    90 				 .IF .NOT .DEF _EOL_CHAR
    91 						_EOL_CHAR equ 0
    92 				 .ENDIF
    93
    94 2168			system__print .proc
    95 2168 8D 53 06				sta _putchr_proc_adr 
    96 216B 8E 54 06				stx _putchr_proc_adr+1
    97 						
    98 						;... continue to _std_print
    99 				;.endp
   100 						
   101 				;_std_print	.proc
   102
   103 						;Get address of argument from stack
   104 216E 68					pla
   105 216F 85 32				sta ?argptr
   106 2171 68					pla
   107 2172 85 33				sta ?argptr+1
   108 2174 20 CF 21				jsr _read_byte	;just skip next byte (that is part of return address)
   109
   110 						;Read command from input				
   111 2177			command
   112 2177 20 CF 21				jsr _read_byte
   113 217A 85 15				sta ?size
   114 217C A8					tay				; to set the flags
   115 217D F0 4C				beq done		;command 0 means end of sequence
   116 217F 10 3E				bpl str			;1..127 constant string
   117
   118 						;Write n-byte integer variable
   119
   120 2181 29 7F				and #$7f		; zero top bit (it's always 1)
   121 2183 85 15				sta ?size
   122 2185 F0 30				beq eol			;size 0 means end of line (0 byte integer would be nonsense)
   123
   124 				    ;Read address of the variable
   125
   126 2187 20 CF 21				jsr _read_byte
   127 218A 85 34				sta ?varptr		
   128 218C 20 CF 21				jsr _read_byte
   129 218F 85 35				sta ?varptr+1
   130
   131 						.IF _SYS_PRINT_SIGNED = 1 
   132 						;signed number has bit 5 set
   133 2191 A9 00				lda #0
   134 2193 85 16				sta ?sign
   135 						
   136 2195 A5 15				lda ?size
   137 2197 29 20		    and #32		;%00100000
   138 2199 F0 13		    beq unsigned
   139
   140 219B A5 15				lda ?size	;size uses only 4 bits
   141 219D 29 1F				and #31		;%00011111
   142 219F 85 15				sta ?size
   143 				    
   144 21A1 A8			    tay				;test, whether the integer is negative (i.e. top byte is negative)
   145 21A2 88			    dey
   146 21A3 B1 34		    lda (?varptr),y
   147 21A5 10 07		    bpl unsigned
   148 						 
   149 21A7 C6 16		    dec ?sign				;sign = $ff  (-1)
   150 						
   151 21A9 A9 2D				lda #'-'
   152 21AB 20 DA 21				jsr _std_putchr
   153 						
   154 21AE			unsigned
   155 						.ENDIF
   156 						    
   157 21AE 20 28 22				jsr _std_bin_to_bcd
   158 21B1 20 DE 21				jsr _std_print_hex
   159 21B4 18					clc
   160 21B5 90 C0				bcc command		;jmp command
   161 21B7			eol
   162 21B7 A9 9B				lda #_EOL_CHAR	
   163 21B9 20 DA 21				jsr _std_putchr
   164 21BC 18					clc
   165 21BD 90 B8				bcc command		;jmp command
   166 						;Write constant string (size is 1..127, already stored)
   167 									
   168 21BF			str
   169 21BF 20 CF 21				jsr _read_byte
   170 21C2 20 DA 21				jsr _std_putchr
   171 21C5 C6 15				dec ?size
   172 21C7 D0 F6				bne str
   173 21C9 F0 AC				beq	command
   174 										
   175 21CB			done		
   176 21CB 6C 32 00				jmp (?argptr)	
   177 21CE 60					rts
   178 						
   179 21CF			_read_byte
   180 21CF A0 00				ldy #0
   181 21D1 B1 32				lda (?argptr),y
   182 21D3 E6 32				inc ?argptr
   183 21D5 D0 02				bne skip
   184 21D7 E6 33				inc ?argptr+1
   185 21D9			skip
   186 21D9 60					rts
   187 				.endp
   188
   189 21DA			_std_putchr .proc
   190 21DA 6C 53 06				jmp (_putchr_proc_adr)
   191 21DD 60					rts
   192 				.endp
   193
   194
   195 21DE			.proc _std_print_hex
   196 				;Print hexadecimal number of arbitrary length (?size). 
   197 				;Leading zeroes are not printed.
   198 				;In:
   199 				;	?varptr		Pointer to memory containing the number
   200 				;	?size		  Number of bytes to print
   201 				;Uses:
   202 				;	?aux
   203 				;	?aux2
   204 					
   205 21DE A5 15				lda ?size
   206 21E0 85 39				sta ?aux
   207 21E2 A9 00				lda #0
   208 21E4 85 38				sta ?aux2		; number of non-zero digits on output
   209 21E6 F0 12				beq _loop
   210 21E8			_outbyte
   211 21E8 A4 39				ldy ?aux		
   212 21EA B1 34				lda (?varptr),y
   213 21EC 48					pha
   214 21ED 4A					lsr
   215 21EE 4A					lsr
   216 21EF 4A					lsr
   217 21F0 4A					lsr
   218 21F1 20 08 22				jsr _write_digit
   219 					
   220 21F4 68					pla
   221 21F5 29 0F				and #$0f
   222 21F7 20 08 22				jsr _write_digit
   223 21FA			_loop	
   224 21FA C6 39				dec ?aux
   225 21FC 10 EA				bpl _outbyte
   226 						
   227 						;If no character has been written, write at least one 0		
   228 21FE A5 38				lda ?aux2
   229 2200 D0 05				bne _no_empty
   230 2202 A9 30				lda #48
   231 2204 20 DA 21				jsr _std_putchr
   232 2207			_no_empty
   233 				 
   234 2207 60					rts
   235 					
   236 2208			_write_digit
   237 				;In: a 4 bit digit
   238
   239 2208 AA					tax
   240 2209 D0 04				bne _non_zero
   241 220B A5 38				lda ?aux2
   242 220D F0 08				beq _done		;this is zero and there has been no char before - no output
   243 220F			_non_zero
   244 220F BD 18 22				lda hex,x
   245 2212 20 DA 21				jsr _std_putchr
   246 2215 E6 38				inc ?aux2
   247 2217			_done
   248 2217 60					rts
   249
   250 2218 30 31 32 33 34 35 + hex     dta c"0123456789ABCDEF"
   251
   252 				.endp
   253
   254 2228			.proc _std_bin_to_bcd
   255 				;Convert binary number to BCD. 
   256 				;Arbitrary size (up to 127 bytes) are supported.
   257 				;In:
   258 				;	?varptr	pointer to binary number
   259 				; a   0 means unsigned number
   260 				;     $ff means signed number
   261 				;	?size	number of bytes
   262 				;Out:
   263 				;	?size	on output, returns size of resulting bcd number
   264 				;   ?varptr	on output, containg pointer to converted BCD
   265 				;Uses:
   266 				;	_stdbuf
   267 				;	?aux
   268 				;	?aux2
   269
   270 						;Compute size of resulting number 
   271 2228 A4 15				ldy ?size
   272 222A 84 39				sty ?aux		; used to count later
   273 222C C8					iny				;add space to result
   274 222D 84 15				sty ?size
   275 						
   276 						;Zero the destination buffer
   277 222F A9 00				lda #0
   278 2231 99 FF 05		zero	sta _stdbuf-1,y
   279 2234 88					dey
   280 2235 D0 FA				bne zero
   281 						
   282 						;**** We convert the number byte a time
   283 2237 F8					sed
   284 						
   285 						;?aux = varptr(?aux)
   286 2238			bytes
   287 2238 C6 39				dec ?aux
   288 223A A4 39				ldy ?aux
   289 223C B1 34				lda (?varptr),y	
   290 						.IF _SYS_PRINT_SIGNED = 1 
   291 223E 45 16				eor ?sign
   292 						.ENDIF	
   293 2240 85 38				sta ?aux2
   294 2242 38					sec				;set top bit to 1
   295 2243 B0 12				bcs loop		
   296
   297 2245			shift_byte			
   298 2245 A2 00				ldx #0
   299 2247 A4 15				ldy ?size
   300 2249			bcd_mul2
   301 2249 BD 00 06				lda _stdbuf,x
   302 224C 7D 00 06				adc	_stdbuf,x			;buf2(x) = buf2(x) * 2 + carry
   303 224F 9D 00 06				sta _stdbuf,x
   304 2252 E8					inx
   305 2253 88					dey								;TODO: cpx ?size
   306 2254 D0 F3				bne bcd_mul2
   307 							
   308 2256 18					clc
   309 2257 26 38		loop	rol ?aux2		;divide by two, if result is 0, end
   310 2259 D0 EA				bne shift_byte		
   311 						
   312 225B A5 39				lda ?aux
   313 225D D0 D9				bne bytes
   314 					
   315 						.IF _SYS_PRINT_SIGNED = 1 
   316 						;If this is negative number, add 1
   317 						;In case sign is $ff, asl will set the C to 1, otherwise to 0	
   318 225F A5 16				lda ?sign
   319 2261 0A					asl
   320 2262 A2 00				ldx #0
   321 2264			carry1
   322 2264 BD 00 06				lda _stdbuf,x
   323 2267 69 00				adc #0
   324 2269 9D 00 06				sta _stdbuf,x
   325 226C E8					inx
   326 226D B0 F5				bcs carry1
   327 						.ENDIF
   328 									
   329 226F D8					cld		
   330 						
   331 2270 A9 00				lda #<_stdbuf
   332 2272 85 34				sta ?varptr	
   333 2274 A9 06				lda #>_stdbuf
   334 2276 85 35				sta ?varptr+1	
   335 2278 60					rts
   336
   337 				.endp
   338 				 
   339 				/*
   340 				  Mul8 - 8-bit multiplication routine
   341 				  
   342 				  Original source:
   343 				  Book Atari Roots (Chapter Ten - Assembly Language Math)
   344 				  Hyperlink: http://www.atariarchives.org/roots/chapter_10.php
   345 				  
   346 				  Parameters:
   347 					  a First multiplicant
   348 					  x Second multiplicant
   349 				  Result: 
   350 						TEMPW2,TEMPW2+1	high byte of the result
   351 				*/
   351
   352
   353 2279			_sys_mul8  .proc
   354
   355 = 0032			MUL1 = _TW1
   356 = 0033			MUL2 = _TW1+1
   357 = 0034			RES  = _TW2		;_TW2+1
   358
   359 2279 85 32				sta MUL1			;input comes in A and X
   360 227B 86 33				stx MUL2
   361 						
   362 227D A9 00				lda #0			;RES = 0
   363 227F 85 34				sta RES
   364 						
   365 2281 A2 08				ldx #8  
   366 2283			loop
   367 2283 46 32				lsr MUL1		;MUL1 = MUL1 / 2
   368 2285 90 03				bcc noadd
   369 						
   370 2287 18					clc					;RES = RES + (MUL2 * $ff)
   371 2288 65 33				adc MUL2	  
   372 228A			noadd 
   373 228A 6A					ror @				;RES = RES / 2		(Carry is 0, when there was no add)
   374 228B 66 34				ror RES
   375
   376 228D CA					dex
   377 228E D0 F3				bne loop
   378 						
   379 2290 85 35				sta RES+1
   380 2292 60					rts
   381
   382 						.endp
   383
   384 				/*
   385 				  Mul16 - 16-bit multiplication routine
   386 				    
   387 				  Parameters:
   388 					  _TW1 First multiplicant (we use only two bytes now)
   389 					  _TW2 Second multiplicant
   390 				  Result: 
   391 						_TL1 Result
   392 				*/
   392
   393
   394 2293			_sys_mul16  .proc
   395
   396 = 0032			MUL1 = _TW1
   397 = 0034			MUL2 = _TW2
   398 = 0070			RES  = _TL1
   399 						
   400 						lda #0      ;RES = 0;
   401 						sta RES
   402 						sta RES+1
   403 						sta RES+2
   404 						sta RES+3
   405 						
   406 						ldx #16  
   407 2293			loop
   408 						lsr MUL1+1		;MUL1 = MUL1 / 2
   409 						ror MUL1
   410 						bcc noadd
   411 						
   412 						lda RES+2			;RES = RES + (MUL2 * $ffff)  (only upper half) 
   413 						clc		
   414 						adc MUL2
   415 						sta RES+2
   416 						lda RES+3
   417 						adc MUL2+1
   418 						sta RES+3
   419 2293			noadd
   420 						ror RES+3				;RES = RES / 2		(Carry is 0, when there was no add)
   421 						ror RES+2
   422 						ror RES+1
   423 						ror RES+0
   424 						 
   425 						dex
   426 						bne loop
   427 						
   428 						rts
   429
   430 						.endp
   431 				 
   432 				/*
   433 				  Div8 - 8-bit division routine
   434 				 
   435 				  Original source:
   436 				  Book Atari Roots (Chapter Ten - Assembly Language Math)
   437 				  Hyperlink: http://www.atariarchives.org/roots/chapter_10.php
   438 				 
   439 				  Parameters:
   440 				  _TEMPW1		16-bit dividend
   441 				  a					 p1_math:  8-bit divisor
   442 				  x          number of bits of an divisir (8 max)
   443 				 
   444 				  Result:
   445 				  x STORE1: 8-bit quotient
   446 				  a STORE2: 8-bit remainder
   447 				*/
   447
   448
   449 2293			_sys_div8  .proc
   450 				  ldx #08  ; For an 8-bit divisor
   451 					
   452 2293			_sys_div
   453
   454 = 0034			divisor  = _TW2
   455 = 0035			quotient = _TW2+1
   456
   457 				  sta divisor
   458 					lda #0     				; for non-8 divis
   459 					sta quotient
   460 				  lda _TW1+1
   461 				  sec 
   462 				  sbc divisor
   463 2293			dloop 
   464 				  php  					; The loop that divides 
   465 				  rol quotient
   466 				  asl _TW1
   467 				  rol @
   468 				  plp
   469 				  bcc addit
   470 				  sbc divisor
   471 				  jmp next
   472 2293			addit 
   473 				  adc divisor
   474 2293			next  
   475 				  dex
   476 				  bne dloop
   477 				  bcs fini
   478 				  adc divisor
   479 				  clc
   480 2293			fini
   481 					tax            ; x = remainder
   482 				  lda quotient   ; a = quotient
   483 				  rol
   484 				  rts
   485 				 
   486 				  .endp
   487
   488 				/*
   489
   490 				  Square Root
   491
   492 				  Calculates the 8 bit root and 9 bit remainder of a 16 bit unsigned integer in
   493 				  Numberl/Numberh. The result is always in the range 0 to 255 and is held in
   494 				  Root, the remainder is in the range 0 to 511 and is held in Reml/Remh
   495
   496 				  partial results are held in templ/temph
   497
   498 				  Destroys A, X registers.
   499
   500 					Arguments:
   501 					
   502 					_TEMPW1  16-bit number to compute root of
   503 					
   504 					<a        Square root
   505 					<_TEMPW2  Remainder of square
   506 					
   507 				*/
   507
   508
   509 2293			_sys_sqrt16 .proc
   510
   511 = 0032			Numberl		= _TW1		; number to find square root of low byte
   512 = 0033			Numberh		= _TW1+1	; number to find square root of high byte
   513 = 0034			Reml		  = _TW2		; remainder low byte
   514 = 0035			Remh		  = _TW2+1	; remainder high byte
   515 = 0070			templ		  = _TL1		; temp partial low byte
   516 = 0071			temph		  = _TL1+1	; temp partial high byte
   517 = 0072			Root		  = _TL1+2	; square root
   518
   519 					LDA	#$00		; clear A
   520 					STA	Reml		; clear remainder low byte
   521 					STA	Remh		; clear remainder high byte
   522 					STA	Root		; clear Root
   523 					LDX	#$08		; 8 pairs of bits to do
   524 2293			Loop
   525 					ASL	Root		; Root = Root * 2
   526
   527 					ASL	Numberl		; shift highest bit of number ..
   528 					ROL	Numberh		;
   529 					ROL	Reml		; .. into remainder
   530 					ROL	Remh		;
   531
   532 					ASL	Numberl		; shift highest bit of number ..
   533 					ROL	Numberh		;
   534 					ROL	Reml		; .. into remainder
   535 					ROL	Remh		;
   536
   537 					LDA	Root		; copy Root ..
   538 					STA	templ		; .. to templ
   539 					LDA	#$00		; clear byte
   540 					STA	temph		; clear temp high byte
   541
   542 					SEC			; +1
   543 					ROL	templ		; temp = temp * 2 + 1
   544 					ROL	temph		;
   545
   546 					LDA	Remh		; get remainder high byte
   547 					CMP	temph		; comapre with partial high byte
   548 					BCC	Next		; skip sub if remainder high byte smaller
   549
   550 					BNE	Subtr		; do sub if <> (must be remainder>partial !)
   551
   552 					LDA	Reml		; get remainder low byte
   553 					CMP	templ		; comapre with partial low byte
   554 					BCC	Next		; skip sub if remainder low byte smaller
   555
   556 								; else remainder>=partial so subtract then
   557 								; and add 1 to root. carry is always set here
   558 2293			Subtr
   559 					LDA	Reml		; get remainder low byte
   560 					SBC	templ		; subtract partial low byte
   561 					STA	Reml		; save remainder low byte
   562 					LDA	Remh		; get remainder high byte
   563 					SBC	temph		; subtract partial high byte
   564 					STA	Remh		; save remainder high byte
   565
   566 					INC	Root		; increment Root
   567 2293			Next
   568 					DEX			; decrement bit pair count
   569 					BNE	Loop		; loop if not all done
   570
   571 					lda Root
   572 					
   573 					RTS
   574 				.endp
   230
   231 2293			flags:
   232 = 2293			   .ds 8192
