Vous êtes sur la page 1sur 2

Fibonacci in 68000 Assembler

ORG.L $. . . ; data follows


FIBMAX EQU n ; maximum i
N DC.W FIBMAX ; for counter
RES DS.L FIBMAX ; result array
ORG.L $. . . ; code follows
ENTRY MOVE.W N, D0 ; initialize counter
MOVEQ.L #0, D1 ; initialize old
MOVEQ.L #1, D2 ; initialize actual
MOVEA.L #RES, A0 ; initialize pointer
MOVE.L D1, (A0)+ ; store value #0
MOVE.L D2, (A0)+ ; store value #1
SUBQ.W #2, D0 ; decrement D0 for {0,1}
LOOP MOVE.L D1, D3 ; add old and actual
ADD.L D2, D3 ; . . . into newvalue
MOVE.L D2, D1 ; save actual to old
MOVE.L D3, D2 ; save newvalue
MOVE.L D2, (A0)+ ; store value #i
SUBQ.W #1, D0 ; decrement D0 for {i}
BGT LOOP ; repeat
END
139
68000 Assembler Directives
1. . . . tell the assembler:
To dene constants: EQU
start new memory segment
ORIGIN
initialize a variable DC.x
reserve static memory DS.x
2. How to chose values for ORIGIN?
Keep static memory at the borders
consider special cases (NULL pointer)
allow stack and heap (malloc) to
grow towards each other
140
R/O
R/W
Example for Definition of Memory Segments:
Constants
Stack Area
Variables
Program Code
Memory Mapping
$000000
$ffffff
141
Decrement and Branch
1. The common loop instruction sequence
SUBQ.W #1, Dn
CMPI.W #1, Dn ||
BNE d(PC)
can be abbreviated with
DBRA Dn, d(PC)
2. DBRA can be combined with any abort
condition
DBcc Dn, d(PC)
equals
Bcc 4(PC)
DBRA Dn, d(PC)
142
Example for DBcc Usage
KBSTAT KBBUF
System Bus
Status Buffer
; buffer with 80 characters
MOVEA.L #LINE, A3 ; set up pointer
MOVE.W #79, D0 ; max. length
; begin of reading loop
RWAIT BTST.B #0, KBSTAT ; check bit
BEQ RWAIT ; waiting loop
MOVE.B KBBUF, D1 ; fetch character
MOVE.B D1, (A3)+ ; store character
CMP.B #\n, D1 ; end of line?
DBEQ D0, RWAIT ; check condition and
. . . ; decrement counter!
143
Testing Instructions
1. Setting the Z and N condition codes from
a value:
TST.[BWL] E
2. Setting Z from a single bit:
BTST.[BL] Dn, E
BTST.[BL] #v, E
where the source species the bit number
to test in the destination
3. Set a value from condition codes:
Scc.B E if cc then
set all bits in E 1
else
set all bits in E 0
144
Rotate Operations
1. Basic Rotate:
ROL, ROR Dx, Dy Dy by Dx bits
ROL, ROR #v, Dy Dy by v bits
ROL, ROR E Memory E by one bit
(all with BWL sizes, memory W only)
2. Rotate with extend (X) bit:
ROXL, ROXR . . .
7 6 5 4 3 2 1 0 X
ROXL.B
6 5 4 3 2 1 0 X 7
ROXL.B
5 4 3 2 1 0 X 7 6
145
Example for Rotate with Extend
Splitting up quotient and remainder:
MOVE.L A, D0 ; load dividend
MOVE.L B, D1 ; load divisor
DIVU D1, D0 ; divide
; quotient in lower half of D0
; remainder in upper half of D0
; rotate remainder into D2
MOVEQ #15, D7 ; bit counter
LREM ROXL.L #1, D0 ; MSB X
ROXL.W #1, D2 ; X LSB of D2
DBRA D7, LREM
; rotate quotient into D3
MOVEQ #15, D7 ; bit counter
LQUOT ROXL.L #1, D0 ; MSB X
ROXL.W #1, D3 ; X LSB of D3
DBRA D7, LQUOT
MOVE.W D2, REM
MOVE.W D3, QUOT ; done
146
Shift Operations
1. Logical Shift:
LSL, LSR Dx, Dy Dy by Dx bits
LSL, LSR #v, Dy Dy by v bits
LSL, LSR E Memory E by one bit
bit pattern is shifted, new bit 0
2. Arithmetic Shift:
ASL, ASR Dx, Dy Dy by Dx bits
ASL, ASR #v, Dy Dy by v bits
ASL, ASR E Memory E by one bit
retains sign bit and sets V condition
Value after ASR.W after LSR.W
12 6 6
35 17 17
52 26 32742
147
Example for Arithmetic Shift
Splitting up quotient and remainder:
MOVE.L A, D0 ; load dividend
MOVE.L B, D1 ; load divisor
DIVS D1, D0 ; divide signed
; quotient in lower half of D0
; remainder in upper half of D0
; lets assume we need 32 bit results
MOVE.W D0, D1 ; copy quotient
EXT.L D1 ; extend quotient
ASR.L #8, D0 ; shift 2 bytes
ASR.L #8, D0
MOVE.L D0, REM
MOVE.L D1, QUOT ; done
148
Signed Extension
EXT.W Dn Dn
8
Dn
16
EXT.L Dn Dn
16
Dn
32
Sign bit
EXT.L:
EXT.W:
149

Vous aimerez peut-être aussi