EASy68K
http://easy68k.com/EASy68Kforum/

Finding Even/Odd numbers depending on input
http://easy68k.com/EASy68Kforum/viewtopic.php?f=6&t=1563
Page 1 of 1

Author:  norma2886 [ Tue Dec 06, 2016 8:28 pm ]
Post subject:  Finding Even/Odd numbers depending on input

Hi Everyone,

I have an assignment that I'm really struggling with. The goal is to take a negative or positive number from the user and then either display all the even numbers in the array or all the odd numbers in the array.

Initialize an array [1, 7, 13, 0, 11, 24, 8]. Then,
1) Load a signed number from the keyboard. (20 pts.)
2) If the number is negative, print out odd elements in the array. Otherwise, print out even elements. Do not forget the commas. (30 pts.)
Desired output:
1, 7, 13, 11
Or:
0, 24, 8

My code is below, I am unsure A) how to check whether the input is negative or positive to then branch it to display even/odd numbers B) how to find the even/odd numbers in array (I think for odd numbers I had to divide by 2 and swap but I don't know where to put it and if I am even right)

Any help would be appreciated!

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

    LEA ARRAY, A0
   
    LEA MESSAGE, A1
    MOVE #14, D0
    TRAP #15
   
    MOVE.B #4, D0   ;USER INPUT
    TRAP #15
   
    CMP.L #0,D1    ;LOOKING FOR NEG
    BLT ODDNUM
   
   
    ;BRA END

ODDNUM
    CMP.L #ARRAYSIZE, D3    ;NOT END OF ARRAY LOOP
    ;BHS ENDL
   
    CMP.L #4, D3
    BLE CHECK
   
    LEA COMMA, A1
    MOVE.B #14, D0
    TRAP #15
 
CHECK
    CLR.L D1
    MOVE (A0,D3),D1
   
    MOVE.B #15, D0
    MOVE.B #10, D2
    TRAP #15
   
    ADD.L #4,D3
    BRA ODDNUM


END 
 
ENDL
 

    SIMHALT             ; halt simulator

CR   EQU   $0D
LF   EQU   $0A

ARRAY DC.L 1,7,13,0,11,24,8
ARRAYSIZE EQU 14
SPACE   DC.B ' ',0
MESSAGE DC.B 'ENTER A SIGNED NUMBER (NEG FOR ODD, POS FOR EVEN): ', 0
COMMA DC.B  ',',0

    END    START        ; last line of source





Author:  profkelly [ Wed Dec 07, 2016 12:14 pm ]
Post subject:  Re: Finding Even/Odd numbers depending on input

The DIVU instruction returns the modulus in the upper word of the result.
number mod 2 is 0 if number is even and 1 if number is odd.

Code:
* Test for even or odd in D0
    divu    #2,D0
    swap    D0
    tst.w   D0
    beq     even
* the number in D0 was odd   
* put code here to handle odd values
    bra     next
* the number in D0 was even
* put code here to handle even values
even

next

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/