Solve Some 68000 Problems

Solution

macros.X68

OPT     MEX

CODE    EQU     0

TEXT    EQU     1

SECTION     TEXT

ORG         \$2000

SECTION     CODE

ORG         \$1000

* print the text string

* use ENDL as second argument to add return and linefeed

PRINT   MACRO

SECTION     TEXT

MSG\@    DC.B    ‘\1’

IFARG    2

IFC      ‘\2′,’ENDL’

DC.B    \$D,\$A

ENDC

ENDC

DC.B    0

SECTION     CODE

MOVEM.L     D0/A1,-(SP)

LEA         MSG\@,A1

MOVE.B      #14,D0

TRAP        #15

MOVEM.L     (SP)+,D0/A1

ENDM

* print the number

PRINTN    MACRO

SECTION     CODE

MOVEM.L     D0/D1,-(SP)

IFNC ‘\1′,’D1’          if some register other than D1

MOVE.L    \1,D1       put number to display in D1

ENDC

MOVE.B  #3,D0

TRAP    #15             display number in D1

MOVEM.L (SP)+,D0/D1

ENDM

* Enter a number

SECTION           CODE

MOVE.L      D0, -(SP)

MOVEQ #4, D0

TRAP  #15                     read value into d1

MOVE.L      (SP)+, D0

ENDM

* Usage POWER N, P

* Raises N to the P power

* Resulting number is in D1

POWER    MACRO

MOVEM.L     D0/D2,-(A7) save

IFNC ‘\2′,’D2’

MOVE.W    \2,D2

ENDC

IFNC ‘\1′,’D1’          if first param is not D1

MOVE.W    \1,D1

ENDC

IFNC ‘\1′,’D0’

MOVE.W    \1,D0

ENDC

WHILE.W D2 <HI> #1 DO.S

MULU  D0,D1           compute power

SUB.W #1,D2

ENDW

MOVEM.L (A7)+,D0/D2

ENDM

**********************

*    Program Start

**********************

*START

*    PRINT   <Macro Demonstration Program>,ENDL

*

*    POWER   #4,#3

*    PRINT   <4^3 = >

*    PRINTN  D1

*

*    SIMHALT                 Halt the program

*    END     START

*~Font name~Courier New~

*~Font size~10~

*~Tab type~1~

*~Tab size~4~

q1-3.X68

ORG   \$1000

INCLUDE     “macros.x68”

START:                  ; first instruction of program

* Put program code here

*     MOVE.W #3, D3     ; the number of values to read

MOVEQ #0, D7      ; keep track of the total

MOVE.W D3, D6     ; we need to know how many were entered for average

SUBQ #1, D3 ; dbra means until 0 so need to -1 to account for that

LOOP:

PRINT <Enter number:>

DBRA  D3, LOOP

DIVU  D6, D7      ; calculate the average

PRINT <The average is:>

PRINTN      D7    ; display the average

SIMHALT                 ; halt simulator

* Put variables and constants here

END   START       ; last line of source

*~Font name~Courier New~

*~Font size~12~

*~Tab type~0~

*~Tab size~4~

q2-2.X68

ORG   \$1000

START:                  ; first instruction of program

* Put program code here

LEA   DATA, A0    ; Read the data from here

MOVE.W      #19, D0     ; 20 numbers to be read

MOVEQ.L #0, D3    ; Positive count

MOVEQ.L #0, D1    ; Negative count

MOVEQ.L #0, D2    ; Zero count

LOOP: TST.W (A0)+ ; Check value at (A0)

BEQ   INCZERO

BMI   INCNEG

JMP   ENDLOOP

JMP   ENDLOOP

ENDLOOP:

DBRA  D0, LOOP

MOVE.W      D1, NEGATIVE

MOVE.W      D2, (A6)

SIMHALT                 ; halt simulator

* Put variables and constants here

ORG   \$2000

NEGATIVE:   DC.W  0

ORG   \$8000

DATA: DC.W  10, 20, 30, 40, 50

DC.W  -10, -20, -30, -40, 50

DC.W  0, 0, 1, 2, 3

DC.W  4, 5, 6, 7, 8

END   START       ; last line of source

*~Font name~Courier New~

*~Font size~12~

*~Tab type~0~

*~Tab size~4~

q2-3.X68

INCLUDE “macros.X68”

* Put program code here

ORG   \$1000

START:                  ; first instruction of program

PRINT <Enter number of values to check:>

MOVE.L      #DATA, A0

MOVEQ #0, D0

MOVEQ #0, D2      ; temp to read

MOVE.W      (A0), D0    ; smallest value found

SUBQ.W      #1, D1

LOOP: MOVE.W      (A0)+, D2   ; read value

CMP.L D2, D0

BGE   NEXT

MOVE.W      D2, D0      ; update smallest

NEXT:

DBRA  D1, LOOP

PRINT <The smallest value found:>

PRINTN      D0

SIMHALT                 ; halt simulator

* Put variables and constants here

ORG   \$8000

DATA: DC.W  31415,9265,3589,7932,3846,2643,3832,7950,2884,1971,6939,937,510

DC.W  5820,9749,4459,2307,8164,0628,6208,9986,2803,4825,3421,170,679

DC.W  8214,8086,5132,8230,6647,0938,4460,9550,5822,3172,5359,408,128

DC.W  4811,1745,0284,1027,0193,8521,1055,5964,4622,9489,5493,038,196

DC.W  4428,8109,7566,5933,4461,2847,5648,2337,8678,3165,2712,019,091

DC.W  4564,8566,9234,6034,8610,4543,2664,8213,3936,0726,0249,141,273

DC.W  7245,8700,6606,3155,8817,4881,5209,2096,2829,2540,9171,536,436

DC.W  7892,5903,6001,1330,5305,4882,0466,5213,8414,6951,9415,116,094

DC.W  3305,7270,3657,5959,1953,0921,8611,7381,9326,1179,3105,118,548

DC.W  0744,6237,9962,7495,6735,1885,7527,2489,1227,9381,8301,194,912

END   START       ; last line of source

*~Font name~Courier New~

*~Font size~12~

*~Tab type~0~

*~Tab size~4~