6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed May 08, 2024 8:40 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Programming MODULO
PostPosted: Mon Sep 01, 2003 2:41 pm 
Offline

Joined: Sat Aug 30, 2003 5:09 pm
Posts: 1
hello,

i have a big Problem. I would like to program modulo.
which I can do in Michael Kowalski of 6502
macroassembler & simulator tests.
for sample i will test.

B modulo 3 = 2


*= $200

io_puth = io_area + 3
count1 = $41
count2 = $42

LDA #$0B
STA io_puth
STA count1

LDA #$03
STA io_puth
STA count2

; ????????? count1 modulo count2 = 2
; OR
; B modulo 3 =2


as I can make that :cry:

thanks ahead
lee


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Sep 02, 2003 6:47 am 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8432
Location: Southern California
How about:

LDA count1
SEC

label:
SBC count2
BCS label

ADC count1

IOW, you just subtract count2 from the first number (which started out as the contents of count1) until the subtraction results in a borrow. Then you add count1 back in to get back to the last number you had before the borrow occurred. You don't need CLC before the ADC because it's already clear by the time we get there. Unless I'm forgetting something, this should work for any pair of unsiged 8-bit numbers, even if the subtraction result causing the carry looks positive. (For example, for $4E mod $F3, $4E minus $F3 yields $5B.) This still works because we're testing the C flag and not the N flag. It is not necessary to test V.

Garth


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Sep 02, 2003 11:25 am 
Offline

Joined: Tue Sep 03, 2002 12:58 pm
Posts: 298
When the numbers get bigger, there are more complex but much faster methods. This one works for all sorts of problems, including converting binary to decimal.

To copy one number to another, we can repeatedly halve the input while doubling the output, adding one whenver we halved an odd number. To convert from one representation to another, just do the appropriate "halve" and "double" operations for those representations.

In this case we want the output modulo count2, so the doubling has to be done modulo count2. That's easy - double, and if the result is greater than count2, subtract count2.

If you wanted to convert to decimal, you'd do the doubling in decimal mode. To convert from decimal, you need a decimal-mode halve. You can even convert to or from a mixed-radix representation like BCD hours:minutes:seconds.

LDA #0
loop:
LSR count1
ROL
CMP count2
BCS skip
SEC
SBC count2
skip:
LDX count1
BNE loop

Untested, and I might have got the branch condition wrong (I always do that)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 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: