It is currently Sun Apr 05, 2020 11:09 am

 All times are UTC

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: ModuloPosted: Fri Nov 21, 2014 5:35 am

Joined: Thu Nov 06, 2014 6:31 pm
Posts: 6
Hello,

I've looked in a lot of places and I couldn't find much about modulo!

Like I have this for example:

Code:
int X = 0;
int Y = 1;
while(X <= 10 ){
if(X % 2 == 0)
Y = Y * X;
else
Y++;
X++;
}
cout << "Y is: "<< Y << endl;

And I'm pretty much stuck on the X % 2 == 0 part..

This is what I have so far..

Code:
ORG    \$1000
START:                  ; first instruction of program

* Put program code here

MOVE.W  #0,D1       ; X-value = 0
MOVE.W  #1,D2       ; Y-value = 1

WHILE   CMPI.B  #10,D1
BLE     MOD

*----- Attempt on doing the modulus -----*
MOD     CLR.L   D2          ; Clear out all 32 bits of D2
MOVE.W  D0,D2       ; Copy the contents of D0 into D2's last 16 bits (word length)
DIVU    D1,D2       ; Divide D2 by D1... D2 should now have the remainder in the upper 16 bits? (i think)
SWAP    D2          ; Swap the upper and lower words of D2... And now I can refer to D2.W as the remainder of D0 divided by D1
*----------------------------------------*

* Put variables and constants here
CR      EQU     \$0D
LF      EQU     \$0A

OUTPUT   DC.B    'Y is: ',0
NEWLINE  DC.B    '',CR,LF,0

END    START        ; last line of source

Top

 Post subject: Re: ModuloPosted: Fri Nov 21, 2014 3:05 pm

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1118
Something like this
Code:
*-----------------------------------------------------------
* Title      :
* Written by :
* Date       :
* Description:
*-----------------------------------------------------------
ORG    \$1000
START:                  ; first instruction of program

* Put program code here
MOVE.W  #0,D1       ; X-value = 1
MOVE.W  #1,D2       ; Y-value = 1

WHILE D1 <LE> #10 DO

*----- Attempt on doing the modulus -----*
MOD     CLR.L   D3          ; Clear out all 32 bits of D3
MOVE.W  D1,D3       ; Copy the contents of D1 into D3's last 16 bits (word length)
DIVU    #2,D3       ; Divide D3 by 2... D3 should now have the remainder in the upper 16 bits? (i think)
SWAP    D3          ; Swap the upper and lower words of D3... And now I can refer to D3.W as the remainder of D0 divided by D1
*----------------------------------------*
IF.W D3 <EQ> #0 THEN    ; if(X % 2 == 0)
MULS D1,D2          ; Y = Y * X
ELSE
ENDI
ENDW
SIMHALT                 ; halt the simulator

* Put variables and constants here
CR      EQU     \$0D
LF      EQU     \$0A

OUTPUT   DC.B    'Y is: ',0
NEWLINE  DC.B    '',CR,LF,0

END    START        ; last line of source

_________________
Prof. Kelly

Top

 Post subject: Re: ModuloPosted: Sat Nov 29, 2014 11:19 am

Joined: Sat Oct 18, 2014 3:47 pm
Posts: 4
I just thought I'd mention that you can get a remainder-by-2 much quicker, since that's just the least-significant bit. If you just want to branch on that you can:
Code:
btst #0, d1
beq modIs0

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 4 guests

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

Search for:
 Jump to:  Select a forum ------------------ EASy68K Forum    FAQ    Latest Features    EASy68K Questions    68000 Programming Questions    Wish List    Wishes Granted    Undocumented Features    Documented Features    General Discussion    EASy68K Projects