It is currently Wed Jan 22, 2020 6:27 am

 All times are UTC

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Finding Even/Odd numbers depending on inputPosted: Tue Dec 06, 2016 8:28 pm

Joined: Tue Dec 06, 2016 7:55 pm
Posts: 1
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

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

Top

 Post subject: Re: Finding Even/Odd numbers depending on inputPosted: Wed Dec 07, 2016 12:14 pm

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1115
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

_________________
Prof. Kelly

Top

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

 All times are UTC

#### Who is online

Users browsing this forum: Google [Bot] and 6 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