Download Assembly Language Programming - Lecture Notes | ECE 4436 and more Study notes Microprocessors in PDF only on Docsity! ECE 4436 Chap 8: Assembly Language Programming. Dept. of Electrical & Computer Engineering University of Houston llFa 2008
/ ECE 4436 —\
8.1 Assembly Language Programming Style
NS S
ECE 4436 Pretty => Readable & Understandable (cont’d) • Labels of equates (EQU, SET) should be in all UPPER CASE. • Labels of data (variable or constant) and of jump/branch t t h ld b i l Mi d Carge s s ou e n ower case or xe ase. • Jump/branch target labels should be on a separate line. • Opcodes should be in lower case . • Assembler directives should be in all UPPER CASE. ECE 4436 T bl 8 2a e - Upper/lower case ECE 4436 Table 8 1 Assembly - Language program elements ECE 4436;******************************************** ; Main process loop starts here: loop: ; Get value from A/D jsr get_AD pshb ; Save it Jump/branch target label lower case & on separate line ; Display on LEDs jsr put_LED ; Delay about 0.5 seconds ldx #DELAY Full line comment to start a block of code jsr delay_X_ms ; Now display the last value ldab Last_Val Same line comment to explain a line of code. They should start in same column if ibljsr put_LED pulb ; Get the value back stab Last_Val ; Save it for next time ; Delay about 0 5 seconds poss e. . ldx Delay1 jsr delay_X_ms ; Delays # ms in X ; Do forever bra loop ;******************************************** ECE 4436 ********************************************; ; Subroutines and functions ; (This relocatable assembler program does ; not have any subroutines in the main ; module. If you do, however, this is the ; place to put them.) ;******************************************** ; Constant data area in ROM MyConst: SECTION Delay1: DC.W DELAY String: DC.B "This is a string of constants." Assembler directive upper case ;******************************************** ; Variable data area in RAM MyData: SECTION Last Val: DS B 1_ . Data labels are lower/mixed case and on same line ECE 4436 ; Subroutines to be used for example program Subroutines File: subs.asm ;;******************************************** ; External symbol definitions ; None ;******************************************** ; Internal symbol definitions XDEF get_AD, init_AD XDEF enable LED put LED _ , _ ;******************************************** ; Constants for I/O Ports INCLUDE "slk_leds.inc" ; Include for LEDs INCLUDE "slk_ad.inc" ; Include for A/D ;******************************************** ; init_AD - Initialize A/D converter ; Input: nothing S b ti b i ith ; Output: nothing ; Reg mod: none, CCR ; Common data: none ******************************** u rou nes eg n w a header that describes the purpose and the calling sequence ; init_AD: ECE 4436 ********************************************; ; enable_LED ; Set up I/O to allow output to the LED bank ; Input: nothing ; Output: CCR ; Reg mod: none ; Common data: none ;******************************** enable_LED: ; Set the latch enable bit on Port CAN ; to output bset LATCHDDR,LEDON ; make it output rts ECE 4436 ********************************************; ; put_LED ; Output data to the LED ; Input: B register has value to output ; Must be complemented for 1=LED on ; Output: Nothing ; Reg modified: CCR ; 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 LEDON, ; Set the bus to input bclr BUSDDR,ALLBITS rts
7
\
8.2 Structured Assembly Language
Programming
ECE 4436 \
S
ECE 4436
/ Decision Structure: IF-THEN-ELSE
Example 8-4 Decision Element Assembly Language Program
Pseudocode Design
; Get the temperature
; IF Temperature > Allowed Maximum
; THEN Turn the water valve off
; ELSE Turn the water valve on
; END IF temperature > Allowed Maximum
Structured Assembly Code
Metrowerks HC12-Assembler
(c) COPYRIGHT METROWERKS 1987-2003
Rel. Loc Obj. code Source line
1 ; 68HCS12 Structured assembly code
2 ; IF-THEN-ELSE example.
3 ; Equates define constants needed by the code
0000 0091
0000 0080 MAX_TEMP:
0000 0000 VALVE_OFF:
0000 0001 VALVE_ON:
0000 0258 VALVE_PORT:
000000
000002
000004
000006
000008
00000B
00000D
OO000F
9691
8180
2307
9600
7A02
2005
58
9601
7A02 58
AD_PORT: EQU $91 ; A/D Data Port
EQU 128 ; Maximum temperature
EQU 0 ; Bits for valve off
EQU 1 ; Bits for valve on
EQU $258 ; Port P for the valve
;
i
7
; Get the temperature
idaa
AD_PORT
; IF Temperature > Allowed Maximum
cmpa
bls
: THEN Turn the
ldaa
staa
bra
; ELSE Turn the
ELSE_PART:
idaa
staa
END_IF:
#MAX_TEMP
ELSE_PART
water valve off
VALVE_OFF
VALVE_PORT
END_IF
water valve on
VALVE_ON
VALVE_PORT
; END IF Temperature > Allowed Maximum
Example 8-5
For each of the logic statements, give the appropriate HCS12 code to set the condition code
register and to branch to the ELSE part of an IF-THEN-ELSE. Assume P and Q are
8-bit,|signed|numbers in memory locations P and Q.
A. IFP>=Q.
B. IFQ>P.
Cc. IFP=Q.
Solution:
A. ; IF P s= 0
ldaa P
cmpa QO
blt ELSE_PART
B. ; IFQ>P
ldaa QO
cmpa Pp
ble ELSE_PART
ldaa PB
cmpa Q
bne ELSE_PART
aA HD HO FPF W NY BF
10
11
12
13
14
15
16
17
18
19
000000
000002
000004
000006
000008
0000 0091
0000 0080
0000 0001
0000 0000
0000 0258
9691
9180
2314
9601
7A02 58
; 68HC12 Structured assembly code
; WHILE - DO Example
; Equates needed
AD_PORT: EQU $91 ; A/D Data port
MAX_ALLOWED:EQU 128 ; Maximum Temp
LIGHT_ON: EQU 1
LIGHT_OFF: EQU 0
LIGHT_PORT: EQU $258 ; Port P
Get the temperature from the A
idaa AD_PORT
; WHILE the temperature > maximum allowed
WHILE_START;
cmpa MAX_ALLOWED
bls END_WHILE
; DO
; Flash light 0.5 second on, 0.5 second off
ldaa LIGHT_ON
staa LIGHT_PORT ; Turn the light
21
22
23
24
25
26
27
28
29
30
31
32
33
O0000B 16xx xx
OOO000R 9600
000010 7A02 58
000013 16xx xx
000016 9691
7
000018 20E8
ECE 4436
jsr delay 30.5 second delay
ldaa LIGHT_OFF
staa LIGHT_PORT ; Turn the light off
jsr delay
End flashing the light
Ge e te Fature from the A/D
3 AD_
END_DO
bra WHILE START
END_WHILE:
; END_WHILE the temperature > maximum allowed
i
Dummy subroutine
OO001A 3D delay: rts
NS
SS
ECE 4436
fo Loop Structure —- DO-WHILE
Example 8-8 DO-WHILE Assembly Language Code
Pseudocode Design
; DO
i Get data from the switches
: Output the value to the LEDs
; ENDO
; WHILE Any switch is set
Structured Assembly Code
Metrowerks HC12-Assembler
(c) COPYRIGHT METROWERKS 1987-2003
Rel. Loc Obj. code Source line
1 ; 68HC12 Structured assembly code
2 ; DO-WHILE example
3 ; Equates needed for this example
4 0000 0028 SW_PORT: EQU $28 ; Switches are on Port J
—
ECE 4436
Subroutine/Function Calls
Call
Subroutine
Program Flow
Next
Instruction
Information
Flow
——
ey,
Subroutine
Return
Figure 8-1 Information transfer
between modules.
ECE 4436 —
TABLE 8-4 Subroutine Header Comments
*
*
*
*
Subroutine Name:
Author:
Date:
Function:
Input Registers:
Output Registers:
Registers modified:
Global data modified:
Functions called:
KERR KKKKRK KKK KKH RARER RRR a RR RR aR aa eee eR A a ae ee eee a Aw oe
SORT
F. M. Cady
July 19, 1993
Calculate the square root of a 16
bit integer number.
D = 16 bit integer number
B = 8 bit integer square root
Carry flag = 1 if input number is negative
Carry flag = 0 if input number is positive
B, condition code register
none
none
Te keke ee de ee ke eke ek ke eke kek kk ke de kkk eee de ee ee ae ae ke dee ke de dee ek ke de de eek de dee
NO
SY
ECE 4436 Passing Information to/from Subroutines TO the subroutine: • Calling module places data into a common area before the call; subroutine finds the data there and retrieves it. FROM the subroutine: S b i l d i b f h• u rout ne p aces ata nto a common area e ore t e return; calling module finds the data there and retrieves it. The common areas are: • In the Registers I Gl b l D t A ( ’t thi )• n a o a a a rea we won use s • In a Local Data Area (we’ll use this later with I/O drivers) • On the Stack ECE 4436 Passing Information in a Global Data Area These modules are in separate files .
ECE 4436
Passing Information in a Local Data Area
—
Module_1 lodule_2
Data
Element_1
Source File 1
——
ns
Source File 2
Data
Module_3 Eee Module_4
i
Figure 8-3 Information in local data areas.
ECE 4436 Passing Information on the Stack data to be passed to subroutine ECE 4436 Output Argument Restore Registers Return Remove Output Argument ECE 4436 Local Variables in a Subroutine • Temporary variables (in RAM) that the subroutine needs to create should be created on the stack. 1. On entry to the subroutine, allocate space for them th t kon e s ac . 2. Use symbolic offsets (if there is more than one) and indexed addressing to access the variables. 3. On exit from the subroutine, deallocate the space. • Use of the stack for these variables greatly reduces RAM requirements in small embedded systems. • This is the same procedure used by C programs for local variables defined in functions . ECE 4436 Local Variable on the Stack ; Subroutine to illustrate a single local variable ; on the stack. sub1: pshd ;save register clr 1,-sp ;allocate counter and ; init to zero d th t ff allocate ; ... o o er s u inc 0,sp ;increment counter ; ...more other stuff access ins ;deallocate local variable puld ;restore register rts deallocate