You are on page 1of 18

Example 6.

3
Write a 68000 assembly program at address $002000 to clear 100 consecutive bytes to zero from
LOW to HIGH addresses starting at location $003000.

Solution

LOOP
FINISH

ORG
$2000
MOVEA.L #$3000,A0
MOVE.W #99,D0
CLR.B
(A0)+
DBF.W
D0,LOOP
JMP

FINISH

; STARTING ADDRESS
; LOAD A0 WITH $3000
; MOVE 99 INTO D0
; CLEAR[3000H]+
; DECREMENT AND
; BRANCH
; HALT

Code Used:
ORG $3000
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
DC.B $01,$02,$03,$04,$05
DC.B $06,$07,$08,$09,$10
ORG $2000
MOVEA.L #$3000,A0
MOVE.W #99,D0
LOOP CLR.B (A0)+
DBF.W D0,LOOP
FINISH JMP
FINISH

;Load A0 with $3000


;Move 99 into D0
;Clear [3000] and point to next address
;Decrement and branch
;Halt

Example 6.4
Write a 68000 assembly language program at address $4000 to move a block of 16-bit data of
length 10010 from the source block starting at location 002000 16 to the destination block starting at
location 00300016 from low to high addresses.

Solution
The assembly language program is shown below:

START
STAY

ORG
$4000
MOVEA.W #$2000,A4 ;LOAD A4 WITH SOURCE ADDR
MOVEA.W #$3000,A5 ;LOAD A5 WITH DEST ADDR
MOVE.W #99,D0
;LOAD D0 WITH COUNT -1=99
MOVE.W (A4)+,(A5)+;MOVE SOURCE DATA TO DEST
DBF.W
D0,START ;BRANCH IF D0 1
JMP
STAY
;HALT

The above program is modified to move five 16-bit data as follows:

START
STAY

ORG
DC.W
ORG
MOVEA.W
MOVEA.W
MOVE.W
MOVE.W
DBF.W
JMP

$2000
$0100,$0200,$0300,$0400,$0500
$4000
#$2000,A4
#$3000,A5
#4,D0
(A4)+,(A5)+
D0,START
STAY

EXAMPLE 6.8
Write a 68000 assembly language program at address $3000 to add two 64-bit numbers as
follows:
[D0.L] [D1.L]
+ [D2.L] [D3.L]
----------------------[D2.L] [D3.L]
Solution
ORG
$3000
ADD.L
D1,D3 ; Add low 32 bits, store result in D3.L
ADDX.L
D0,D2 ; Add with carry high 32 bits, store result
END JMP
END
; Halt

EXAMPLE 6.9
Write a 68000 assembly language program at address $2000 to add four 32-bit numbers stored in
consecutive locations from low to high addresses starting at address $3000. Store the 32-bit

result onto the user stack. Assume that no carry is generated due to addition of two consecutive
32-bit numbers and A7 is already initialized.
Solution
ORG
$3000
DC.L
1,2,3,4
ORG
$2000
MOVEQ.L
#3,D0
MOVEA.L
#$3000,A0
CLR.L
D1
START ADD.L
(A0)+,D1
; Add
DBF.W
D0,START
MOVE.L
D1,-(A7)
FINISH
JMP
FINISH

; Move 3 into D0
;Initialize A0
; Clear sum to 0
; perform loop
; push result

Numerical Example
Assume data as 1,2,3,4. The final result , $A (10 decimal) will be stored in D1.L.
Simulation Results
The following screen shot shows data , $00000001, $00000002, $00000003 and
$00000004 in memory locations starting at address $3000. The data are circled, each 4
bytes long:

As the program is initializing values, the registers are being changed accordingly:

After the first number ($00000001) is added to the sum in D1.L during the first pass,
register D1.L will contain the 32-bit SUM, $00000001 as follows:

The following screen shots show how all the numbers are added, and the 32-bit result
($0000000A) is pushed onto the user stack:

A7 was $00010000 and storing the result of the sum, D1, using predecrement addressing,
A7 is FFFC and the data is pushed in to the stack. A7 is decremented by 4 because a long
word is stored. D0.W = $FFFF indicating it is -1 to jump out of the loop.
EXAMPLE 6.10

Write a 68000 assembly language program at address $2000 to add ten 32-bit numbers stored in
consecutive locations starting at address $3000. Initialize A6 to $00200504 and use low 24 bits
of A6 as the stack pointer to push the 32-bit result. Use only ADDX instruction for adding two
32-bit numbers each time through the loop. Assume that no carry is generated due to addition of
two consecutive 32-bit numbers; this will provide the 32-bit result. This example illustrates use
of the 68000 ADDX instruction.
Solution
The assembly language program is shown below:

START_ADR
COUNT

AGAIN
MOVE.L
FINISH

ORG
$3000
DC.L
2,3,7,5,1,9,6,4,6,1
EQU
$3000
ORG
$2000
EQU
9
MOVEA.L #START_ADR,A0
MOVE.B #COUNT,D0
MOVEA.L #$00200504,A6
CLR.L
D1
ADDI.B
#0,D6
MOVE.L (A0)+,D3
ADDX.L D3,D1
DBF.W D0,AGAIN
D1,-(A6)
JMP
FINISH

; LOAD STARTING ADDRESS IN A0


; USE D0 AS A COUNTER
; USE A6 AS THE SP
; CLEAR D1
; CLEAR X BIT
; MOVE A 32 BIT NUMBER IN D3
;ADD NUMBERS USING ADDX
;REPEAT UNTIL D0 = -1
;PUSH 32-BIT RESULT ONTO STACK

Example 6.11
Write a 68000 assembly program at address $2000 to multiply an 8-bit signed number in
the low byte of D1 by a 16-bit signed number in the high word of D5. Store the result in
D3. Assume the number is already stored in D1.B.
Solution

FINISH

ORG
EXT.W
SWAP.W
MULS.W
MOVE.L
JMP

$2000
D1
D5
D1,D5
D5,D3
FINISH

; SIGN EXTENDS LOW BYTE OF D1


; SWAP LOW WORD WITH HIGH WORD OF D5
; MULTIPLY D1 WITH D5, STORE RESULT
; COPY RESULT IN D3

EXAMPLE 6.12

Write a 68000 assembly language program at address $2000 to compute i1 X Y , where X s and
i i

Yi ,s are signed 16-bit numbers and N = 100. Store the 32-bit result in D1. Assume that the
starting addresses of Xi and Yi are 300016 and 400016 respectively.
Solution
The assembly language program is provided below:
P
Q

EQU
EQU
ORG
MOVE.W
LEA.L
LEA.L

$3000
$4000
$2000
#99,D0
P,A0
Q,A1

;MOVE 99 INTO DO
;LOAD ADDRESS P INTO A0
;LOAD ADDRESS Q INTO A1

LOOP

FINISH

CLR.L
MOVE.W
MULS.W
ADD.L D2,D1
DBF.W
JMP

D1
(A0)+,D2
(A1)+,D2

;INITIALIZE D1 TO ZERO
;MOVE [X] TO D2
;D2 <--[X]*[Y]
;D1 <-- SUM XiYi
D0,LOOP
;DECREMENT AND BRANCH
FINISH
;HALT

EXAMPLE 6.13
Write a 68000 assembly language program to convert temperature from Fahrenheit to Celsius
using the following equation: C = [(F - 32)/9] 5 . Assume that the low byte of D0 contains the
temperature in Fahrenheit. The temperature can be positive or negative. Store result in D0.
Solution

FINISH

EXT.W D0
SUBI.W #32,D0
MULS.W #5,D0
DIVS.W #9,D0
JMP FINISH

;SIGN EXTEND (F) LOW BYTE OF D0


;PERFORM F-32
;PERFORM 5* (F-32)/9 AND STORE
;REMAINDER IN HIGH WORD OF D0
;AND QUOTIENT IN LOW WORD OF D0

EXAMPLE 6.14
Write a 68000 assembly program at address $1000 which is equivalent to the following C
language segment:
sum = 0;
for (i = 0; i <= 9; i = i + 1)
sum = sum + x[i] * y[i];
Assume that the arrays, x[i] and y[i] contain unsigned 16-bit numbers already stored in memory
starting at addresses $3000 and $4000 respectively. Store the 32-bit result at address $5000.
Solution
x
y
sum

LOOP

FINISH

ORG
EQU
EQU
EQU
MOVE.W
LEA.L
LEA.L
LEA.L
CLR.L
MOVE.W
MULU.W
ADD.L D2,D5
DBF.W
MOVE.L
JMP

EXAMPLE 6.15

$1000
$3000
$4000
$5000
#9,D0
;USE D0 AS A LOOP COUNTER
x,A0
;INITIALIZE A0 WITH x
y,A1
;INITIALIZE A1 WITH y
sum,A2 ;INITIALIZE A2 WITH SUM
D5
;CLEAR SUM TO 0
(A0)+,D2 ;MOVE X[i] INTO D2
(A1)+,D2 ;COMPUTE X[i] *y[i]
;UPDATE SUM
D0,LOOP ;REPEAT UNTIL D0=-1
D5,(A2) ;STORE SUM IN MEMORY
FINISH

Write a 68000 assembly language program at address $002000 to add all the elements in a
table containing eight 16-bit numbers stored in memory in consecutive memory locations
starting at an address $005000. Store the 16-bit result in D1.W.
Solution

ORG
DC.W
DC.W
ORG
LEA.L
MOVE.L
MOVE.L
CLR.W
MOVE.W
BACK ADD
LSL.L
ADDQ.L
MOVE.L
DBF.W
END

JMP

$005000
1,2,3,4
5,6,7,8
$002000
$00005000,A0; A0 = Starting address of the table
#0,D0
; Move element number 0 into D0.L
D0,D3
; Copy element number 0 into D3.L
D1
; Clear 16-bit sum in D1 to 0
#7,D2
; Initialize D2.W with loop count
(A0,D0.L),D1 ; Add elements with sum in D1.W
#1,D0
; unsigned multiplication of element# by 2 forW
#1,D3
; Increment element number in D3.L by 1
D3,D0
; Copy element number in D0.L
D2,BACK
; Decrement D2 and branch to BACK if
; D2 is not equal to -1
END
; Halt

Example 6.16
Write a 68000 assembly language program at address $1000 to find the trace (Sum of the
diagonal elements) of a 3x3 matrix containing 16-bit words stored 16-bit result in D0.W
Assume that the matrix is stored in row-major ordering starting at an offset $4000 as
follows:
$4000 a[0,0]
$4002 a[0,1]
.
.
.
$400E a[2,1]
$4010 a[2,2]
The program is shown below:

BACK

ORG
$4000
DC.W
$1,$2,$3,$4
DC.W
$5,$6,$7,$8,$9
ORG
$1000
MOVE.L #0,D1
;Load col 0 into D1
MOVE.L D1,D4
;Copy D1 into D4
MOVE.L #0,D2
;Load row 0 into D2
MOVE.L D2,D6
;Copy D2 into D6
MOVE.W #2,D7
;initialize loop count
CLR.W
D0
;Sum=0
LEA.L
$4000,A0
;Load A0 starting address in A0
MULU.W #6,D6
;preform 6*i
ADDA.L D6,A0
;A0+6*i

FINISH

ADD.W
ADDQ.L
MOVE.L
LSL.L
ADDQ.L
MOVE.L
LEA.L
DBF.W
JMP

(A0,D1.L),D0;Sum diagonal elements in D0


#1,D4
;Increment Col by 1
D4,D1
;Copy D4 in D1
#1,D1
;preform 2*j
#1,D2
;Increment row by 1
D2,D6
;Copy D2 in D6
$4000,A0
;Reinitialize A0
D7,BACK
FINISH

EXAMPLE 6.17
Write a 68000 assembly language program at address $3000 that will multiply a 32-bit
unsigned number in D0.L by 4 to provide a 32-bit product , and then, perform the following
operations on the contents of D0.L:
set bits 0 and 3 to one without changing other bits in D0.L.
clear bit 5 to zero without changing other bits in D0.L.
ones-complement bit 7 without changing other bits in D0.L.
Use only logic, and shift
instructions. Do not use any
instructions. Assume data is already in D0.L.

multiplication or any other

Solution
ORG
LSL.L

$3000
#2,D0
ORI.L
ANDI.L

D0.L to one
in D0.L to zero
complement bit 7 in D0 FINISH JMP

; Unsigned multiply D0 by 4
#$00000009,D0
; set bits 0 and 3 in
#$FFFFFFDF,D0
; clear bit 5
EORI.L
#$00000080,D0
; ones
FINISH
; Stop

EXAMPLE 6.18
Write a 68000 assembly language program that will multiply a 16-bit unsigned number in D0
by 4 to provide a 32-bit product , and then perform the following operations on the contents of
D0:
set bits 0 and 3 to one without changing other bits in D0.
clear bit 5 to zero without changing other bits in D0.
Ones-complement bit 7 without changing other bits in D0.
Use only shift and bit manipulation instructions. Do not use any multiplication or any other
instruction. Assume data is already stored in D0.

Solution
LSL.L

#2,D0
BSET.L

; Unsigned multiply D0 by 4
#0,D0
; set bit 0 in D0.L

to one
BSET.L
#3,D0
; set bit 3 in
D0.L to one
BCLR.L
#5,D0
; clear bit 5
in D0.L to zero
BCHG.L
#7,D0
; ones
complement bit 7 in D0
FINISH
JMP
FINISH
; Halt
EXAMPLE 6.19
Write a 68000 assembly language program at address $2000 that will perform : 5 X + 6 Y +
[Y/8] [ D1.L] where X is an unsigned 8-bit number stored in the lowest byte of D0 and Y is a
16-bit signed number stored in the upper 16 bits of D1. Neglect the remainder of Y/8.
Solution

FINISH

ORG
ANDI.W
MULU.W
SWAP.W D1
MOVE.W
MULS.W
ADD.L
EXT.L
ASR.L
ADD.L
JMP

$2000
#$00FF,D0 ;CONVERT X TO UNSIGNED 16-BIT
#5,D0
;COMPUTE UNSIGNED 5*X IN D0.L
;MOVE Y TO LOW 16 BITS IN D1
D1,D2
;SAVE Y TO LOW 16 BITS OF D2
#6,D1
;COMPUTE SIGNED 6*Y IN D1.L
D0,D1
;ADD 5*X WITH 6*Y
D2
;SIGN EXTEND
#3,D2
;PERFORM Y/8;DISCARD REMAINDER
D2,D1
;PERFORM 5*X+6*Y +Y/8
FINISH

Example 6. 20
Write a 68000 assembly language program at address $2000 to add two words, each containing
two ASCII digits. The first word is stored in two consecutive locations (from LOW to HIGH)
with the low byte pointed to by A0 at address 3000 16, and the second word is stored in two
consecutive locations (from LOW to HIGH) with low byte pointed to by A1 at 7000 16. Store the
packed BCD result in D5.

Solution
The assembly language program is provided below:

START

ORG
$2000
MOVEQ.L
#1,D2
MOVEA.W #$3000,A0
MOVEA.W #$7000,A1
ANDI.B
#$0F,(A0)+
ANDI.B
#$0F,(A1)+
DBF.W
D2,START
MOVE.B
-(A0),D6
MOVE.B
-(A0),D7
LSL.B
#4,D6
OR.B
D7,D6
MOVE.B
-(A1),D5
MOVE.B
-(A1),D4
LSL.B
#4,D5
OR.B
D4,D5
ADDI.B
#0,D0

;#1 INITIALIZE D2
;#2 INITIALIZE A0
;#3 INITIALIZE A1
;#4 CONVERT IST # TO UNPAC.BCD
;#5 CONVERT 2ND # TO UNPAC.BCD
;#6 DECREMENT AND BRANCH IF D2 -1
;#7 GET HIGH UNPAC.BYTE OF IST#
;#8 GET LOW UNPAC. BYTE OF IST#
;#9 SHIFT IST# HIGH BYTE 4 TIMES
;#10 D6=PACKED BCD BYTE OF IST#
;#11 GET HIGH UNPAC. BYTE OF 2ND#
;#12 GET LOW UNPAC. BYTE OF 2ND#
;#13 SHIFT 2ND # HIGH BYTE 4 TIMES
;#14 D5 HAS PACKED BCD BYTE OF 2ND#
;#15 CLEAR X-BIT

FINISH

ABCD.B
JMP

D6,D5
FINISH

;#16 D5.B =PACKED BCD RESULT

Numerical Example

Assume that the two ASCII digits to be added are $3430 and $3934 in addresses $3000
and $7000. The program will convert $3430 and $3934 into packed BCD bytes as $40
and $90. The result of packed BCD addition is 40+94=134. The packed BCD result 134
is stored as 34 in D5.B and 1 in the carry flag.
Virtual Simulation Results
The correct operation of the program is then verified using the Simulator as follows.
Enter manually ASCII values of 40 ($3430) and 94 ($3934) in big endian format at
addresses $3000 and $7000, respectively as provided in the following two screen shots:

Initialize the loop count in D2.W and set A0 and A1 to point to $3000 and $7000
respectively as shown in the following display:

Convert the two ASCII digits to unpacked BCD. Loop out when D2.W becomes -110
(FFFF16). The unpacked BCD at address $7000 is shown in the following display:

1st number. Obtain the high unpacked byte to D6.B and the low in D7.B:

Then shift D6.B left 4 bits:

OR D7.B and D6.B store the result in D6.B:

Same execution is done for the 2nd number but the data register used is D5.B and D4.B.
Result is stored in D5:

Clearing the carry and X-bit and adding the two packed BCD values in D6.B and D5.B
and storing the result in D5.B. 4010+9410=13410 is the result we expect. The packed BCD
result 134 is stored as 34 in D5.B and 1 in the carry flag as follows:

The result is displayed in hex. Note that representation of packed 34 and hex 34 in binary
are the same.
Example 6.21
Write a 68000 assembly language program at address $2000 to subtract two 32-bit packed BCD
numbers. The BCD number 1 is stored at the locations starting from $003000 through $003003,
with the least significant byte at $003003 and the most significant byte at $003000. Similarly, the
BCD number 2 is stored at the locations starting from $004000 through $004003, with the least
significant byte at $004003 and the most significant byte at $004000. The BCD number 2 is to be
subtracted from BCD number 1. Store the packed BCD result at addresses $005000 (Lowest byte
of the result) through $005003 (Highest byte of the result). In the program, first initialize loop
counter D7 to 4, source pointer A0 to $003000, source pointer A1 to $004000, destination pointer
A3 to $005000, and then write the program to accomplish the above using these initialized
values.
Solution
ORG
DC.L
ORG
DC.L
ORG
MOVE.W

$003000
$99221133
$004000
$33552211
$2000
#4,D7

;NUMBER OF BYTES TO BE SUBTRACTED

LOOP

FINISH

MOVEA.W
MOVEA.W
ADDA.W
ADDA.W
MOVEA.W
SUBQ.W
ADDI.B
MOVE.B
MOVE.B
SBCD.B
MOVE.B
DBF
JMP

#$3000,A0
#$4000,A1
D7,A0
D7,A1
#$5000,A3
#1,D7
#0,D7
-(A0),D0
-(A1),D1
D1,D0
D0,(A3)+
D7,LOOP
FINISH

;STARTING ADDRESS FOR FIRST NUMBER


;STARTING ADDRESS FOR SECOND NUMBER
;MOVE ADDRESS POINTERS TO THE END
;OF EACH 32 BIT PACKED BCD NUMBER
;LOAD POINTER FOR DESTINATION ADDR
;SUBTRACT D7 by 1 for DBF
;CLEAR X-BIT
;GET A BYTE FROM FIRST NUMBER
;GET A BYTE FROM SECOND NUMBER
;BCD SUBTRACTION, RESULT IN D0
;STORE RESULT IN DESTINATION ADDR
;CONTINUE UNTIL COUNTER HAS EXPIRED

Numerical Example
Number1
Number2
Result

99221133
- 33552211
65668922

Simulation Results
The simulator is used to verify the correct operation of the program. This will be
accomplished via single-stepping.
A0, A1, and A3 are initialized. D7 is decremented by 1 before going in to the loop so the
loop only runs 4 times:

In the loop, Number2 (packed BCD, addresses $4000 through $4003) is subtracted from
Number1 (packed BCD,addresses $3000 through $3003) byte by byte using SBCD.B:

The packed BCD result is stored in the memory locations $5000 through $5003. Note
that the data that are at $5000 is stored in the big endian format:

Example 6.22
N

Write a 68000 subroutine to compute Y = ( X i ) / N .


2

i 1

Assume the Xis are 16-bit signed integers and N = 100. The numbers are stored in
consecutive locations. Assume A0 points to the Xis and A7 is already initialized in the
main program. Store the 32-bit result in D1, where the 16-bit remainder is stored in high
word of D1 and the 16-bit quotient is stored in the low word of D1.Assume user mode.
Also, write the main program at address $2000, which will initialize A0 to $005000, call
the subroutine, and stop.
Solution
The assembly language program is provided below:
Main Program

FINISH
Subroutine
SQR
BACK

ORG
MOVEA.W
JSR
JMP

$2000
#$5000,A0
SQR
FINISH

MOVEM.L
CLR.L
MOVE.W
MOVE.W
MULS.W
ADD.L D3,D1
DBF.W
DIVU.W
MOVEM.L
RTS

D2/D3/A0,-(A7)
;SAVE REGISTERS
D1
;CLEAR SUM
#99,D2
;INITIALIZE LOOP COUNT
(A0)+,D3
;MOVE Xi's INTO D3
D3,D3
;COMPUTE Xi**2 USING MULS
;SINCE Xi**2 IS ALWAYS +VE
D2,BACK
;COMPUTE
#100,D1
;SUM OF Xi**2/N USING DIVU
(A7)+,D2/D3/A0
;RESTORE REGISTERS

;Initialize A0 to $005000
;Call the subroutine