Docsity

Assembly Language Programming - Lecture Notes | ECE 4436, Study notes of Microprocessors

Partial preview of the text

Download Assembly Language Programming - Lecture Notes | ECE 4436 and more Study notes Microprocessors in PDF only on Docsity!

ECE 4436ECE 4436

Chap 8: Assembly Language ProgrammingChap. 8: Assembly Language ProgrammingDept. of Electrical & Computer Engineering

University of Houston

llFall 2008

ECE 4436ECE 4436

8 1 Assembly Language Programming Style8.1 Assembly Language Programming Style

ECE 4436ECE 4436

Pretty => Readable & Understandablee y^

eadab e & U de s a dab e

-^ Adopt a consistent indentation style: Indent and align thep

y^

g

opcode, operand, and comment fields.• Use frequent comments. Include design statements.Comments should explain the thought process, not theComments should explain the thought process, not theopcode!• Place equates in one place in the program at thebeginningbeginning.• Use white space – blank lines – to separate minorsections of code.• Use comment lines of asterisk characters (*) to outlinemajor sections of code. (Don’t use * boxes!)• User upper and lower case writing style in comments.User upper and lower case writing style in comments.

ECE 4436ECE 4436

Pretty => Readable & Understandable

(cont’d)

e y^

eadab e & U de s a dab e

(co^ d)

-^ Labels of equates (EQU, SET) should be in all UPPER

q^ (^

Q^ ,^

CASE.• Labels of data (variable or constant) and of jump/brancht^ t^

h^ ld b^

i^ l^

Mi^ d C

targets should be in lower case or Mixed Case.• Jump/branch target labels should be on a separate line.• Opcodes should be in lower case• Opcodes should be in lower case.• Assembler directives should be in all UPPER CASE.

ECE 4436ECE 4436

Table 8 1 AssemblyTable 8-1 AssemblyLanguage programelements

ECE 4436ECE 4436

Main Program File: main.asm ;^ MC68HCS12 Assembler Example – Main Program ;^ This program is to demonstrate a ;^ readable programming style

It i iti li

th^

A/D^

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:

M6812EX1_REL.ASM

; Author:

F^ M^

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 ;********************************************

ECE 4436ECE 4436

;********************************************; 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 ;********************************************

ECE 4436ECE 4436

********************************************;********************************************; 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

: SECTION

Delay1:

DC.W^

DELAY

String:

DC.B^

"This is a string of constants."

Assembler directiveupper case

;********************************************; Variable data area in RAM MyData : SECTIONLast Val: DS

B^1

Last_Val: DS.B

Data labels are lower/mixedcase and on same line

ECE 4436ECE 4436

;******************************** 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...

ECE 4436ECE 4436

;********************************************; 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

DJM

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

ECE 4436ECE 4436

********************************************;********************************************; 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:

CCR

; 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

ECE 4436ECE 4436

8.2 Structured Assembly Language

ProgrammingProgramming

ECE 4436ECE 4436

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;***********************************************

ECE 4436ECE 4436

Decision Structure: IF-THEN-ELSE