6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 8:52 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Mon Dec 20, 2010 4:37 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
This is old news, but there's a little programming quiz in this 2006 blog posting: what does the following do (by way of reading the value in BYTE and producing a result in A)?
Code:
 LDA BYTE
 LSR
 LSR
 LSR
 ADC BYTE
 ROR
 LSR
 LSR
 ADC BYTE
 ROR
 LSR
 LSR
 RTS

The code is taken from an early issue of Bob Sander-Cederlof's newsletter Apple Assembly Line - which is handy to search using a technique like this:
http://www.google.com/search?q=site:web ... gs+monitor
http://www.google.com/search?q=site:web ... quare+root

There's also a downloadable archive here courtesy of Scott Alfter, as noted nearby in the Source Code Repository.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Dec 20, 2010 5:21 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1748
Location: Sacramento, CA
It appears to be a divide by 7 routine. Very clever!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 22, 2010 1:59 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
Somewhere I've got a routine that generates object code for that type of division routine. You pass it N, and you get a divide-by-N routine. It was inspired by that AAL article/routine.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 22, 2010 4:41 am 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 411
Location: Minnesota
Code:
 LDA BYTE  ; byte
 LSR       ; byte/2
 LSR       ; byte/4
 LSR       ; byte/8
 ADC BYTE  ; byte + byte/8 = 9/8 byte
 ROR       ; (9/8 byte)/2
 LSR       ; (9/8 byte)/4
 LSR       ; (9/8 byte)/8 = 9/64 byte
 ADC BYTE  ; byte + 9/64 byte = 73/64 byte
 ROR       ; (73/64 byte)/2
 LSR       ; (73/64 byte)/4
 LSR       ; (73/64 byte)/8 = 73/512 byte
 RTS
 


- 73 * 7 = 511
- 512/73 = 7.01369863, where everything after the decimal point repeats indefinitely

Not that I would have recognized this without it already having been pointed out (really, uncommented code that does obscure things...). Then I had to see how it worked.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Dec 23, 2010 8:50 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
Thanks for the annotation - it helped me realise why (exactly) two RORs were in there: the additions produce a 9 bit result, because we multiplied by a little more than one.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Dec 24, 2010 6:20 am 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
I never would have figured this out just by looking at the code. Wow, that is slick. :)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: