



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































Chap 8: Assembly Language ProgrammingChap. 8: Assembly Language ProgrammingDept. of Electrical & Computer Engineering
8 1 Assembly Language Programming Style8.1 Assembly Language Programming Style
Pretty => Readable & Understandablee y^
eadab e & U de s a dab e
-^ Adopt a consistent indentation style: Indent and align thep
Pretty => Readable & Understandable
e y^
eadab e & U de s a dab e
-^ Labels of equates (EQU, SET) should be in all UPPER
Table 8 1 AssemblyTable 8-1 AssemblyLanguage programelements
Main Program File: main.asm ;^ MC68HCS12 Assembler Example – Main Program ;^ This program is to demonstrate a ;^ readable programming style
It i iti li
th^
t Ex. 8-1 Thel t d
; It initializes the A/D converter; and a bank of LEDs. It then reads the; value on the A/D, displays it, and delays; about 0.5 seconds. It then displays the
completedprogram
; last value it converted for about 0.5; seconds and repeats.; Source File:
; Author:
Cady ; Author:
F. M. Cady ; Created: 7/26/2004; Modifications:
None ;********************************************; External symbol definitions
XREF^ get_AD, init_ADXREF^ enable_LED, put_LEDXREF^ delay X msXREF^ delay_X_ms XREF^ __SEG_END_SSTACK ;********************************************
;********************************************; Main process loop starts here: lloop : ; Get value from A/D
jsr^ get_AD pshb^
Jump/branch target label lowercase & on separate line; Save it p ; Display on LEDs
jsr^ put_LED ; Delay about 0.5 seconds
ldx^ #DELAY
Full line comment to start ablock of code ldx^ #DELAY jsr^ delay_X_ms ; Now display the last value
ldab^ Last_Val
Same line comment toexplain a line of code. Theyshould start in same columnif^
ibl
jsr^ put_LED pulb^
; Get the value back stab^ Last_Val
; Save it for next time
; Delay about 0 5 seconds
if^ possible.
;^ Delay about 0.5 seconds
ldx^ Delay1 jsr^ delay_X_ms
; Delays # ms in X
; Do forever
bra^ loop ;********************************************
********************************************;********************************************; Subroutines and functions; (This relocatable assembler program does; not have any subroutines in the main
y ;^ module. If you do, however, this is the;^ place to put them.);********************************************; Constant data area in ROM; Constant data area in ROM MyConst
Delay1:
String:
"This is a string of constants."
Assembler directiveupper case
;********************************************; Variable data area in RAM MyData : SECTIONLast Val: DS
Last_Val: DS.B
Data labels are lower/mixedcase and on same line
;******************************** i it AD
Subroutine labels arelower/mixed case and ont
li
init_AD
; Generate a "short" delay > 100 microsec
ldaa^ #DELAY
; 200 loops for; 800 clock cycles
separate line. y
delay:^
nopdbne^ a,delay ; Now set up the A/D; Normal flag clearing
run in WAIT mode
; Normal flag clearing, run in WAIT mode,; no external trigger, no interrupts
bclr^ ATDCTL2,AFFC|AWAI|ETRIGE|ASCIE ; Set 1 conversion per sequence - S8C:S4C:S2C:S1C=0001; Set non-FIFO mode - FIFO = 0; Continue in freeze mode - FRZ1:FRZ0 = 00
bclr^ ATDCTL3,S8C|S4C|S2C|S1C|FIFO|FRZ1|FRZ0bset^ ATDCTL3 S1Cbset^ ATDCTL3,S1C ; Select 8-bit resolution - SRES8 = 1; Select 16 A/D conversion clock periods;^ SMP1:SMP0 = 00...
;********************************************; get_AD - Get 8-bit data value from
A/D converter
I^ t^
thi ; Input: nothing; Output: B register with value; Reg mod: B, CCR; Common data: none;********************************; A/D Mode: Right justified: DJM = 1;^
Unsigned: DSGN = 0 ;^
Single conversion: SCAN = 0 ;^
Single conversion: SCAN = 0 ;^
Single channel: MULT = 0 ;^
Channel 0: CC:CB:CA = 000 ADMODE: EQU
get_AD : ; Start the conversion by writing the scan; select information to ATDCTL
ldab^ #ADMODEldab^ #ADMODEstab^ ATDCTL ; Now wait until the conversion is completespin:^
brclr ATDSTAT0,SCF,spin ;^ Get A/D channel 0 value
ldab^ ADR0H+1 ; Channel 0 is hererts
********************************************;********************************************; put_LED; Output data to the LED; Input:^
B register has value to outputp g^
p
Must be complemented for 1=LED on ; Output:
Nothing ; Reg modified:
; Common Data:
None ; Common Data:
None ;******************************** put_LED
: stab^ BUS
; output data to the bus bset^ BUSDDR,ALLBITS
; All bits output
; Strobe the latch
bset^ LATCH,LEDON
; Assert LE
bclr^ LATCH LEDONbclr^ LATCH,LEDON ; Set the bus to input
bclr^ BUSDDR,ALLBITSrts
8.2 Structured Assembly Language
ProgrammingProgramming
SequenceSeque ce Example 8-3 Assembly Language for Sequence Block ;***********************************************; DO_A; Comments describing the function of this block....^
(Assembly language to do the function) ; ENDO_A;***********************************************
Decision Structure: IF-THEN-ELSE