mads 1.9.0 build 21 (16 Aug 09)
Source: C:\nes\atalan\codebase\atalan\examples/esieve.asm
     1 = 0600			_stdbuf equ 1536
     2 = 0080			_arr equ 128
     3 = 0653			_putchr_proc_adr equ 1619
     4 = 0600			_std_print_out___5 equ _stdbuf
     5 = 0653			_std_print_out___6 equ _putchr_proc_adr
     6 = 002F			key__Q equ 47
     7 = 00FF			key__none equ 255
     8 = 02FC			CH equ 764
     9 = 0014			RTCLOCK equ 20
    10 = 0082			k equ 130
    11 = 0084			j equ 132
    12 = 0086			time equ 134
    13 = 0087			_s2__i equ 135
    14 				   org $2e0
    15 FFFF> 02E0-02E1> 00 20	   dta a($2000)
    16 02E2			   org $2000
    17 				;### esieve.atl(16) "Computing primes..."
    18 2000-224D> 20 36 21	   jsr _std_print_out
    19 2003 13 43 6F 6D 70 75 +    dta b(19),c'Computing primes...'
    20 2017 80			   dta b(128)
    21 2018 00			   dta b(0)
    22 				;### esieve.atl(18) for k 
    23 2019 A0 00		   ldy #0
    24 201B 84 83		   sty k-0+1
    25 201D			_lbl5:
    26 				;### esieve.atl(19) 	sieve(k) = 1
    27 201D A9 4E		   lda #<sieve
    28 201F 85 80		   sta _arr-0
    29 2021 A9 22		   lda #>sieve
    30 2023 18			   clc
    31 2024 65 83		   adc k+1
    32 2026 85 81		   sta _arr-0+1
    33 2028 A9 01		   lda #1
    34 202A 91 80		   sta (_arr),y
    35 202C C8			   iny
    36 202D D0 02		   jne _lbl17
    37 202F E6 83		   inc k+1
    38 2031			_lbl17:
    39 2031 C0 00		   cpy #0
    40 2033 D0 E8		   jne _lbl5
    41 2035 A5 83		   lda k-0+1
    42 2037 C9 20		   cmp #32
    43 2039 D0 E2		   jne _lbl5
    44 				;### esieve.atl(21) RTCLOCK = 0
    45 203B A9 00		   lda #0
    46 203D 85 14		   sta RTCLOCK
    47 				;### esieve.atl(23) for i:2..SQRT_COUNT where sieve(i) = 1
    48 203F A9 02		   lda #2
    49 2041 85 87		   sta _s2__i
    50 2043			_lbl10:
    51 2043 A5 87		   lda _s2__i
    52 				   ;
    53 2045 AA			   tax
    54 2046 BD 4E 22		   lda sieve-0,x
    55 2049 C9 00		   cmp #0
    56 204B F0 39		   jeq _lbl8
    57 				;### esieve.atl(24) 		j = i * 2
    58 204D A5 87		   lda _s2__i
    59 204F 0A			   asl
    60 2050 85 84		   sta j-0
    61 2052 A9 00		   lda #0
    62 2054 2A			   rol
    63 2055 85 85		   sta j-0+1
    64 				;### esieve.atl(28) 			j = j + i
    65 2057 4C 76 20		   jmp _lbl7
    66 205A			_lbl9:
    67 				;### esieve.atl(26) 			sieve(j) = 0
    68 205A A9 4E		   lda #<sieve
    69 205C 85 80		   sta _arr-0
    70 205E A9 22		   lda #>sieve
    71 2060 18			   clc
    72 2061 65 85		   adc j+1
    73 2063 85 81		   sta _arr-0+1
    74 2065 A4 84		   ldy j-0+0
    75 2067 A9 00		   lda #0
    76 2069 91 80		   sta (_arr),y
    77 				;### esieve.atl(27) 			j = j + i
    78 206B A5 84		   lda j-0
    79 206D 18			   clc
    80 206E 65 87		   adc _s2__i
    81 2070 85 84		   sta j-0
    82 2072 90 02		   jcc _lbl7
    83 2074 E6 85		   inc j+1
    84 2076			_lbl7:
    85 				;### esieve.atl(25) 		while j<=8191
    86 2076 A5 85		   lda j-0+1
    87 2078 C9 1F		   cmp #31
    88 207A F0 02		   jeq _lbl19
    89 207C B0 08		   jcs _lbl8
    90 207E			_lbl19:
    91 207E A5 84		   lda j-0
    92 2080 C9 FF		   cmp #255
    93 2082 F0 D6		   jeq _lbl9
    94 2084 90 D4		   jcc _lbl9
    95 2086			_lbl8:
    96 2086 E6 87		   inc _s2__i
    97 2088 A5 87		   lda _s2__i
    98 208A C9 5C		   cmp #92
    99 208C D0 B5		   jne _lbl10
   100 				;### esieve.atl(29) time = RTCLOCK
   101 208E A5 14		   lda RTCLOCK
   102 2090 85 86		   sta time
   103 				;### esieve.atl(31) "Time used: [time] ticks"
   104 2092 20 36 21		   jsr _std_print_out
   105 2095 0B 54 69 6D 65 20 +    dta b(11),c'Time used: '
   106 20A1 81 86 00		   dta b(129),a(time)
   107 20A4 06 20 74 69 63 6B +    dta b(6),c' ticks'
   108 20AB 80			   dta b(128)
   109 20AC 00			   dta b(0)
   110 				;### esieve.atl(32) "Press Q to quit, any other key for list"
   111 20AD 20 36 21		   jsr _std_print_out
   112 20B0 27 50 72 65 73 73 +    dta b(39),c'Press Q to quit, any other key for list'
   113 20D8 80			   dta b(128)
   114 20D9 00			   dta b(0)
   115 				;### esieve.atl(34) CH = none
   116 20DA A9 FF		   lda #key__none
   117 20DC 8D FC 02		   sta CH
   118 				;### esieve.atl(37) for k where sieve(k) = 1 until CH = Q
   119 20DF			_lbl13:
   120 				;### esieve.atl(35) until CH <> none
   121 20DF AD FC 02		   lda CH
   122 20E2 C9 FF		   cmp #key__none
   123 20E4 F0 F9		   jeq _lbl13
   124 20E6			_lbl12:
   125 20E6 A9 00		   lda #0
   126 20E8 85 82		   sta k-0
   127 20EA 85 83		   sta k-0+1
   128 				;### esieve.atl(40) CH = none
   129 20EC 4C 0A 21		   jmp _lbl14
   130 20EF			_lbl16:
   131 20EF A9 4E		   lda #<sieve
   132 20F1 85 80		   sta _arr-0
   133 20F3 A9 22		   lda #>sieve
   134 20F5 18			   clc
   135 20F6 65 83		   adc k+1
   136 20F8 85 81		   sta _arr-0+1
   137 20FA A4 82		   ldy k-0+0
   138 20FC B1 80		   lda (_arr),y
   139 20FE C9 00		   cmp #0
   140 2100 F0 08		   jeq _lbl14
   141 				;### esieve.atl(38) 	 "[k]"
   142 2102 20 36 21		   jsr _std_print_out
   143 2105 82 82 00		   dta b(130),a(k)
   144 2108 80			   dta b(128)
   145 2109 00			   dta b(0)
   146 210A			_lbl14:
   147 210A AD FC 02		   lda CH
   148 210D C9 2F		   cmp #key__Q
   149 210F F0 12		   jeq _lbl15
   150 2111 E6 82		   inc k+0
   151 2113 D0 02		   jne _lbl20
   152 2115 E6 83		   inc k+1
   153 2117			_lbl20:
   154 2117 A5 82		   lda k-0
   155 2119 C9 00		   cmp #0
   156 211B D0 D2		   jne _lbl16
   157 211D A5 83		   lda k-0+1
   158 211F C9 20		   cmp #32
   159 2121 D0 CC		   jne _lbl16
   160 2123			_lbl15:
   161 2123 A9 FF		   lda #key__none
   162 2125 8D FC 02		   sta CH
   163 2128			_lbl21:
   164 2128 4C 28 21		   jmp _lbl21
   165 212B			   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 212B			_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 212B			skip1
    31 					rts
    32
    33 212B			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 212B			_out_putchr .proc
    41 				;Write character from A to output.
    42 					
    43 212B AA					tax
    44 212C AD 47 03				lda $347	;ICPUTB+1
    45 212F 48					pha
    46 2130 AD 46 03				lda $346	;ICPUTB
    47 2133 48					pha
    48 2134 8A					txa
    49 2135 60					rts
    50
    51 				.endp
   166 2136			   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 2136			_std_print_adr .proc
    76
    77 						lda #<_adr_putchr
    78 						ldx #>_adr_putchr
    79 						clc
    80 						bcc system__print
    81 						.endp
    82 						
    83 2136			_std_print_out .proc
    84 2136 A9 2B				lda #<_out_putchr
    85 2138 A2 21				ldx #>_out_putchr
    86 213A 18					clc											;TODO: No Jump, if we are directly before the _std_print
    87 213B 90 00				bcc system__print
    88 						.endp
    89
    90 				 .IF .NOT .DEF _EOL_CHAR
    91 						_EOL_CHAR equ 0
    92 				 .ENDIF
    93
    94 213D			system__print .proc
    95 213D 8D 53 06				sta _putchr_proc_adr 
    96 2140 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 2143 68					pla
   105 2144 85 32				sta ?argptr
   106 2146 68					pla
   107 2147 85 33				sta ?argptr+1
   108 2149 20 A4 21				jsr _read_byte	;just skip next byte (that is part of return address)
   109
   110 						;Read command from input				
   111 214C			command
   112 214C 20 A4 21				jsr _read_byte
   113 214F 85 15				sta ?size
   114 2151 A8					tay				; to set the flags
   115 2152 F0 4C				beq done		;command 0 means end of sequence
   116 2154 10 3E				bpl str			;1..127 constant string
   117
   118 						;Write n-byte integer variable
   119
   120 2156 29 7F				and #$7f		; zero top bit (it's always 1)
   121 2158 85 15				sta ?size
   122 215A 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 215C 20 A4 21				jsr _read_byte
   127 215F 85 34				sta ?varptr		
   128 2161 20 A4 21				jsr _read_byte
   129 2164 85 35				sta ?varptr+1
   130
   131 						.IF _SYS_PRINT_SIGNED = 1 
   132 						;signed number has bit 5 set
   133 2166 A9 00				lda #0
   134 2168 85 16				sta ?sign
   135 						
   136 216A A5 15				lda ?size
   137 216C 29 20		    and #32		;%00100000
   138 216E F0 13		    beq unsigned
   139
   140 2170 A5 15				lda ?size	;size uses only 4 bits
   141 2172 29 1F				and #31		;%00011111
   142 2174 85 15				sta ?size
   143 				    
   144 2176 A8			    tay				;test, whether the integer is negative (i.e. top byte is negative)
   145 2177 88			    dey
   146 2178 B1 34		    lda (?varptr),y
   147 217A 10 07		    bpl unsigned
   148 						 
   149 217C C6 16		    dec ?sign				;sign = $ff  (-1)
   150 						
   151 217E A9 2D				lda #'-'
   152 2180 20 AF 21				jsr _std_putchr
   153 						
   154 2183			unsigned
   155 						.ENDIF
   156 						    
   157 2183 20 FD 21				jsr _std_bin_to_bcd
   158 2186 20 B3 21				jsr _std_print_hex
   159 2189 18					clc
   160 218A 90 C0				bcc command		;jmp command
   161 218C			eol
   162 218C A9 9B				lda #_EOL_CHAR	
   163 218E 20 AF 21				jsr _std_putchr
   164 2191 18					clc
   165 2192 90 B8				bcc command		;jmp command
   166 						;Write constant string (size is 1..127, already stored)
   167 									
   168 2194			str
   169 2194 20 A4 21				jsr _read_byte
   170 2197 20 AF 21				jsr _std_putchr
   171 219A C6 15				dec ?size
   172 219C D0 F6				bne str
   173 219E F0 AC				beq	command
   174 										
   175 21A0			done		
   176 21A0 6C 32 00				jmp (?argptr)	
   177 21A3 60					rts
   178 						
   179 21A4			_read_byte
   180 21A4 A0 00				ldy #0
   181 21A6 B1 32				lda (?argptr),y
   182 21A8 E6 32				inc ?argptr
   183 21AA D0 02				bne skip
   184 21AC E6 33				inc ?argptr+1
   185 21AE			skip
   186 21AE 60					rts
   187 				.endp
   188
   189 21AF			_std_putchr .proc
   190 21AF 6C 53 06				jmp (_putchr_proc_adr)
   191 21B2 60					rts
   192 				.endp
   193
   194
   195 21B3			.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 21B3 A5 15				lda ?size
   206 21B5 85 39				sta ?aux
   207 21B7 A9 00				lda #0
   208 21B9 85 38				sta ?aux2		; number of non-zero digits on output
   209 21BB F0 12				beq _loop
   210 21BD			_outbyte
   211 21BD A4 39				ldy ?aux		
   212 21BF B1 34				lda (?varptr),y
   213 21C1 48					pha
   214 21C2 4A					lsr
   215 21C3 4A					lsr
   216 21C4 4A					lsr
   217 21C5 4A					lsr
   218 21C6 20 DD 21				jsr _write_digit
   219 					
   220 21C9 68					pla
   221 21CA 29 0F				and #$0f
   222 21CC 20 DD 21				jsr _write_digit
   223 21CF			_loop	
   224 21CF C6 39				dec ?aux
   225 21D1 10 EA				bpl _outbyte
   226 						
   227 						;If no character has been written, write at least one 0		
   228 21D3 A5 38				lda ?aux2
   229 21D5 D0 05				bne _no_empty
   230 21D7 A9 30				lda #48
   231 21D9 20 AF 21				jsr _std_putchr
   232 21DC			_no_empty
   233 				 
   234 21DC 60					rts
   235 					
   236 21DD			_write_digit
   237 				;In: a 4 bit digit
   238
   239 21DD AA					tax
   240 21DE D0 04				bne _non_zero
   241 21E0 A5 38				lda ?aux2
   242 21E2 F0 08				beq _done		;this is zero and there has been no char before - no output
   243 21E4			_non_zero
   244 21E4 BD ED 21				lda hex,x
   245 21E7 20 AF 21				jsr _std_putchr
   246 21EA E6 38				inc ?aux2
   247 21EC			_done
   248 21EC 60					rts
   249
   250 21ED 30 31 32 33 34 35 + hex     dta c"0123456789ABCDEF"
   251
   252 				.endp
   253
   254 21FD			.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 21FD A4 15				ldy ?size
   272 21FF 84 39				sty ?aux		; used to count later
   273 2201 C8					iny				;add space to result
   274 2202 84 15				sty ?size
   275 						
   276 						;Zero the destination buffer
   277 2204 A9 00				lda #0
   278 2206 99 FF 05		zero	sta _stdbuf-1,y
   279 2209 88					dey
   280 220A D0 FA				bne zero
   281 						
   282 						;**** We convert the number byte a time
   283 220C F8					sed
   284 						
   285 						;?aux = varptr(?aux)
   286 220D			bytes
   287 220D C6 39				dec ?aux
   288 220F A4 39				ldy ?aux
   289 2211 B1 34				lda (?varptr),y	
   290 						.IF _SYS_PRINT_SIGNED = 1 
   291 2213 45 16				eor ?sign
   292 						.ENDIF	
   293 2215 85 38				sta ?aux2
   294 2217 38					sec				;set top bit to 1
   295 2218 B0 12				bcs loop		
   296
   297 221A			shift_byte			
   298 221A A2 00				ldx #0
   299 221C A4 15				ldy ?size
   300 221E			bcd_mul2
   301 221E BD 00 06				lda _stdbuf,x
   302 2221 7D 00 06				adc	_stdbuf,x			;buf2(x) = buf2(x) * 2 + carry
   303 2224 9D 00 06				sta _stdbuf,x
   304 2227 E8					inx
   305 2228 88					dey								;TODO: cpx ?size
   306 2229 D0 F3				bne bcd_mul2
   307 							
   308 222B 18					clc
   309 222C 26 38		loop	rol ?aux2		;divide by two, if result is 0, end
   310 222E D0 EA				bne shift_byte		
   311 						
   312 2230 A5 39				lda ?aux
   313 2232 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 2234 A5 16				lda ?sign
   319 2236 0A					asl
   320 2237 A2 00				ldx #0
   321 2239			carry1
   322 2239 BD 00 06				lda _stdbuf,x
   323 223C 69 00				adc #0
   324 223E 9D 00 06				sta _stdbuf,x
   325 2241 E8					inx
   326 2242 B0 F5				bcs carry1
   327 						.ENDIF
   328 									
   329 2244 D8					cld		
   330 						
   331 2245 A9 00				lda #<_stdbuf
   332 2247 85 34				sta ?varptr	
   333 2249 A9 06				lda #>_stdbuf
   334 224B 85 35				sta ?varptr+1	
   335 224D 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 224E			_sys_mul8  .proc
   354
   355 = 0032			MUL1 = _TW1
   356 = 0033			MUL2 = _TW1+1
   357 = 0034			RES  = _TW2		;_TW2+1
   358
   359 						sta MUL1			;input comes in A and X
   360 						stx MUL2
   361 						
   362 						lda #0			;RES = 0
   363 						sta RES
   364 						
   365 						ldx #8  
   366 224E			loop
   367 						lsr MUL1		;MUL1 = MUL1 / 2
   368 						bcc noadd
   369 						
   370 						clc					;RES = RES + (MUL2 * $ff)
   371 						adc MUL2	  
   372 224E			noadd 
   373 						ror @				;RES = RES / 2		(Carry is 0, when there was no add)
   374 						ror RES
   375
   376 						dex
   377 						bne loop
   378 						
   379 						sta RES+1
   380 						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 224E			_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 224E			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 224E			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 224E			_sys_div8  .proc
   450 				  ldx #08  ; For an 8-bit divisor
   451 					
   452 224E			_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 224E			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 224E			addit 
   473 				  adc divisor
   474 224E			next  
   475 				  dex
   476 				  bne dloop
   477 				  bcs fini
   478 				  adc divisor
   479 				  clc
   480 224E			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 224E			_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 224E			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 224E			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 224E			Next
   568 					DEX			; decrement bit pair count
   569 					BNE	Loop		; loop if not all done
   570
   571 					lda Root
   572 					
   573 					RTS
   574 				.endp
   167
   168 224E			sieve:
   169 = 224E			   .ds 8192
