EASy68K  
It is currently Sat Apr 29, 2017 5:36 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Modulo
PostPosted: Fri Nov 21, 2014 5:35 am 
Offline

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
 Profile  
 
 Post subject: Re: Modulo
PostPosted: Fri Nov 21, 2014 3:05 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1036
Location: Monroe County Community College, Monroe Michigan, U.S.A.
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
            ADD #1,D2           ; Y++
        ENDI
        ADD #1,D1               ; X++
    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
 Profile  
 
 Post subject: Re: Modulo
PostPosted: Sat Nov 29, 2014 11:19 am 
Offline

Joined: Sat Oct 18, 2014 3:47 pm
Posts: 3
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
 Profile  
 
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 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group