Post on 03-Jul-2020
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
ME 6405
Introduction to Mechatronics
Fall 2005
Instructor: Professor Charles Ume
LECTURE 9
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
1. Write an assembly language program to clear the usable internal RAM in the M68HC11E9.
Solution: Clear Locations $0000 - $002C.
ORG $1040LDX #$0000
LOOP CLR $00,XINXCPX #$002DBNE LOOPSWIEND
(NOTE: 1. You could have also written this program to clear $0100 to $01FF2. You should hand-assemble this program.)
Homework 1 Solution
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Homework 1 Solution
2. Write a program to add even/odd numbers located in addresses $0000 through $00FE.Solution: (for adding odd numbers)
ORG $1040LDY #$0000 1040 18 CE 0000LDX #$0000 1044 CE 0000
IND LDAA #$FF 1047 86 FFLDAB $00,X 1049 E6 00
0000 00110000 0001 BITB #$01 104B 85 010000 0001
BEQ EVEN 104D 27 02ABY 104F 18 3A
EVEN INX 1051 08STX $2000 1052 FF 2000
1ST time:#$2001: 0000 0001 EORA $2001 1055 B8 2001acc. A: 1111 1111
1111 1110 BNE IND 1058 26 EDSWI 105A 3FEND
ORDECA 4AIf you usethis, then IND shouldbe moveddown to thenext line
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405Write an assembly language program to find the largest signed number in a list of numbersstored in address $0020 thru $0029. Repeat for unsigned number.
FIRST EQU $0020LAST EQU $002AANSWR EQU $0010
ORG $1040 LDX #FIRSTLDAA FIRSTSTAA ANSWRINX Goes with DECB. If DECB is removed, then LDAB #$0A LDAB must be removed
NUB LDAA $00,X CMPA ANSWR (A - ANSWR)
Branch if accumulator A BLE NEXT; (signed)* *(Branch if ≤ zero.is less than ANSWR Branches if 2's complement
OR number in register A was less or equal to the 2's complement number represented by ANSWR)
BLS NEXT; (unsigned)** **(Branch if lower or same, i.e, Branch if register A was lower or same as ANSWR)
STAA ANSWRNEXT INX
CPX #LAST OR DECB BNE NUBSWIEND
Homework 2 Solution
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Homework 2 Solution Continued
Signed Example:
#$3F (positive)#$F4 (negative)
Convert Numbers to Decimal:
#$F4 = %11110100
1's Comp #$0B = %000010112's Comp #$0C = %00001100
#$F4 = -1210
#$3F = %00111111 #$3F = 6310
#$3F is larger than #$F4 since 6310 is larger than -1210
Un-Signed Example:
#$3F (positive)#$F4 (positive)
Convert Numbers to Decimal:
#$F4 = %11110100 #$F4 = 24410
#$3F = %00111111 #$3F = 6310
#$F4 is larger than #$3F since 24410 is larger than 6310
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Homework
Homework 3• Write a sample program to output a string of characters to the
screen.
• Write a subroutine to save the first 5 odd (8-bit) numbers pointed to by the x-register (passed in) onto the stack.
Homework 4• Write a program to output a square wave thru port D pin 2. The
output can be observed on the scope, and the period T of the wave should be measured. More than one period wave should be generated. The machine cycle time of the M68HC11E9 should be estimated. Draw the square wave.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 ASCII Character Codes
ASCII – American Standard Code for Information InterchangeASCII assigns a hexadecimal ‘code’ to individual characters
Examples:Character ASCII‘A $41‘E $45‘e $65 (Note: Different codes for lower
and upper case)‘1 $31BS $08 (Note: BS is Backspace. )
A microcontroller must send these codes to a display terminal inorder for the terminal to display them
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 ASCII Character Codes (continued)
Hex to ASCII Conversion table from Programming Reference Guide Page 58
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 ASCII Character Codes
An array of characters is called a stringExample:
character array String ASCII Representation‘H ‘e ‘l ‘l ‘o “Hello” $48 $65 $6C $6C $6F
(Note:Character ASCII‘H $48‘e $65‘l $6C‘o $6F
)
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Assembly Directive Types
Assembly Control• ORG, END
Symbol Definition• EQU
Data Definition/Storage Allocation• FCC, FCB, FDB, RMB, ZMB, BSZ, FILL
Listing Control• PAGE, OPT
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Assembly Control
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
ORG and END
ORG : Store translated machine language instructions in sequencestarting at given address for any mnemonic instructions that follow
END: Stop translating mnemonics instructions until another ORG is encountered
(Note: These were already discussed in Lecture 7)
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Symbol Definition
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 EQUEQU lets you refer to a number or an address as a variable name.
Example:VALA EQU $10 *LABEL VALA USED TO REFER TO $10
ORG $1040LDAA #VALA *LOAD HEX NUMBER $10 IN ACCUMULATOR A LDAB VALA *LOAD CONTENT OF MEMORY LOCATION $10
*IN ACC. BSWIEND
Same As:
VALA EQU $10 *LABEL VALA USED TO REFER TO $10ORG $1040LDAA #$10 *LOAD HEX NUMBER $10 IN ACCUMULATOR A LDAB $10 *LOAD CONTENT OF MEMORY
*LOCATION $10 IN ACC. BSWIEND
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Data Definition/Storage Allocation
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FCCFCC – Form Constant Character string• FCC stores ASCII characters into consecutive bytes of memory. • Any printable ASCII characters can be contained in the string. • String is specified between two identical delimiters, which can be
any printable ASCII character. • First non-blank character after the string is used as a delimiter.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FCC Cont’d
$00
$01
$02
$03
$04
Address Prebyte Opcode Operand
31
32
33
34
35
Example:
ORG $00FCC “12345”
Result
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FCB
FCB – Form Constant Byte• FCB has one or more operands. • Value of each operand is truncated to eight bits, and is
stored in single byte of object program. • Operand may be a numeric constant, character
constant, a symbol or an expression. • Multiple operands are separated by commas, and are
stored in successive memorybytes.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FCB Cont’d
Example: VALA EQU $10ORG $00FCB $34,’A, $28AC, $0A ,VALA
$00
$01
$02
$03
Address Prebyte Opcode Operand
34
41
AC
0A
Result
$04 10
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FDB
FDB – Form Constant Double Byte• FDB stores a double (two byte) word. • May have one or more operands separated by
commas.• Operand may be a numeric constant, a character
constant, a symbol, or an expression.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FDB Cont’d
Example:
ORG $00FDB $1234,’&,’G
Note: ASCII value for & is $26ASCII value for G is $47
$00
$01
$02
$03
$04
Address Prebyte Opcode Operand
12
34
00
26
00
47$05
Result
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
RMBRMB – Reserve Memory Byte• RMB saves a place in memory for a
number. Example:
ORG $00XVAR RMB 2 *TWO MEMORY
*LOCATIONS $00*and $01 ARE*RESERVED FOR XVAR
ORG $1040LDD #$FFAASTD XVARSWIEND
$00
$01
Address Prebyte Opcode Operand
FF
AA
Result
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Question a student asked:
What happens if you change the previous example to "XVAR RMB 3" instead of "XVAR RMB 2" ? What happens to the 3rd reserved byte when a 2 byte number is stored in XVAR?
Modified Program:
ORG $00XVAR RMB 3
ORG $1040LDD #$FFAASTD XVARSWIEND
Answer: Remains unchanged
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 ZMB, BSZZMB – Zero Memory Byte and BSZ – Block Storage of Zero• These directives fill a given number of memory locations with zero. • Causes assembler to allocate a block of memory bytes, and each
memory byte is assigned a value of zero. • Both directives do the same thing.• Number of bytes allocated is given in the operand field.
Example:ORG $00ZMB #$02BSZ #$02
$00$01
Address Prebyte Opcode Operand
00
00$02$03
00
00
Result
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
FILL
Fill given number of memory locations with anynumber. (Note: Fill uses one byte. If two bytes are specified, then it will truncate it and use LS Byte.)
Example:
ORG $00FILL #$FF, #$02 $00
$01
Address Prebyte Opcode Operand
FF
FF
Result
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Question a student asked:What happens when the previous example is changed to "FILL #$9ABC, #$02" instead of "FILL #$FF,#$02"? What happens if you fill memory with a 2 byte number?
Answer: FILL will just use the LS Byte
Modified Example:
ORG $00FILL #$9ABC,#$02END
(Note: There is no "Go 1040 on the screen since these are just assembly directives and not a program)
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Listing Control
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
PAGE
PAGE • The PAGE directive causes a page break in
the list file. • If no source listing is being produced, the
PAGE directive will have no effect. • The directive is not printed on the source
listing.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
OPTOPT• Allows for various options in assembly of a program, including
generating a listing and counting instruction cycles.
Options:nol-no output listing (default)l-do an output listing noc-no cycle number count (default)c-turn on cycle count using zero initial valuecontc-turn cycle count on, begin with last valuecre-create a cross reference table (default anyway) RMBs-create a symbol table (default anyway) EQU
Example:OPT l – Print source listing from this point
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Buffalo Monitor Utility Subroutines
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Buffalo Monitor Utility Subroutines
Buffalo Monitor Utility Subroutines• These subroutines are available for performing I/O
tasks. • A jump table has been set up in ROM
directly beneath the interrupt vectors. • To use these subroutines, execute a jump to
subroutine (JSR) command to the appropriate entry in the jump table.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Subroutine ListingUPCASE If character in accumulator A is lower case alpha, convert to
upper case.WCHEK Test character in accumulator A and return with Z bit set if
character is whitespace (space, comma, tab).
DCHEK Test character in accumulator A and return with Z bit set if
character is delimiter (carriage return or whitespace).
INIT Initialize I/O device.
INPUT Read I/O device.OUTPUT Write I/O device.
OUTLHLF Convert left nibble of accumulator A contents to
ASCII and output to terminal port.
OUTRHLF Convert right nibble of accumulator A contents to
ASCII and output to terminal port.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Subroutine Listing Cont’d
OUTA Output accumulator A ASCII character.OUT1BYT Convert binary byte at address in index register X to two ASCII
characters and output. Return address in index register X pointing tonext byte.
OUT1BSP Convert binary byte at address in index register X to two ASCIIcharacters and output followed by a space. Returns address in indexregister.
OUT2BSP Convert two consecutive binary bytes starting at address in index X tofour ASCII characters and output followed by a space. Returns addressin index register X pointing to next byte.
OUTCCRLF Output ASCII carriage return followed by a line feed.OUTSTRG Output string of ASCII bytes pointed to by address in index register X
until character is an end of transmission ($04).OUTSTRGO Same as OUTSTRG except leading carriage return and line feed is
skipped.INCHAR Waits for you to type an ASCII character from the keyboard, and stores
the corresponding ASCII number in accumulator A, and then outputs the ASCII character to the screen.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
• $FFAO UPCASE Convert character to uppercase• $FFA3 WCHEK Test character for whitespace• $FFA6 DCHEK Check character for delimiter• $FFA9 INIT Initialize I/O device• $FFAC INPUT Read I/O device• $FFAF OUTPUT Write I/O device• $FFB2 OUTLHLF Convert left nibble to ASCII and output• $FFB5 OUTRHLF Convert right nibble to ASCII and output• $FFB8 OUTA Output ASCII character• $FFBB OUTlBYT Convert binary byte to 2 ASCII characters and output• $FFBE OUT1BSP Convert binary byte to 2 ASCII characters and output
followed by space• $FFCl OUT2BSP Convert 2 consecutive binary bytes to 4 ASCII
characters and output followed by space.• $FFC4 OUTCRLF Output ASCII carriage return followed by line feed• $FFC7 OUTSTRG Output ASCII string until end of transmission ($04)• $FFCA OUTSTRGO Same as OUTSTRG except leading carriage return and
line feed is skipped• $FFCD INCHAR Input ASCII character and echo back
To use an I/O subroutine, Jump Sub Routine (JSR) to the specified addresslisted below.
ADDRESS SUBROUTINE FUNCTION
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Utility Subroutines Examples
Example: OUTA ORG $1040LDAA #$41 *Load acc. A with ASCII code for the
*character A.JSR $FFB8 *JSR to the subroutine OUTA SWIEND
Resultà A is written to the screen.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Example: OUTSTRG (Out String)STR1 EQU $2100OUTSTRG EQU $FFC7
ORG STR1FCC “ABCDEFG”FCB #$04
ORG $1040LDX #STR1JSR OUTSTRG *Go to OUTSTRG routine which
*outputs ASCII characters contained in each*address starting from $2100 until*an EOT character is found.
SWIEND
ResultàABCDEFG written to the screen on a new line.
(Note: ASCII end of transmission (EOT) character must be used with theOUTSTRG subroutine to let it know when to stop reading from memory. EOT ASCII = #$04)
Utility Subroutines Examples
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405Difference Between the Utility Subroutines OUTSTRG and
OUTSTRG0
Assume you want to print the following to the screen:MEMORY LOCATION $_____CONTAINS _____ IN DECIMAL.
Use subroutine OUTSTRG to print “MEMORY LOCATION $” to screen on a new line (It always starts printing on a new line, because it outputs carriage return with line feed).Use subroutine OUTSTRG0 to print “CONTAINS” to screen on same line as “MEMORY LOCATION $” (Because it does not output carriage return with line feed)Use subroutine OUTSTRG0 to print “IN DECIMAL.” to screen on the same line
(Note: Refer to Lab website for example program to printdecimal numbers to the screen.)
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Example: OUTLHLF (Out Left Half) and OUTRHLF (Out Right Half)
ORG $1040LDAA #$AF *Puts hex number $AF in acc. AJSR $FFB2 *Goes to subroutine OUTLHLF which converts left
*nibble, #$A, to ASCII number, $41, and then outputs*its ASCII character, ‘A, to the screen.
JSR $FFB5 *Goes to subroutine OUTRHLF which converts right*nibble, #$F, to ASCII number, $46, and then outputs*its ASCII character, ‘F, to the screen.
SWIEND
Result à AF is written to the screen.
Utility Subroutines Examples
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Example: OUT1BYT (Out 1 Byte) and OUT1BSP (Out 1 Byte with Space)Assume that memory contains the following:2100 FA FBNow execute the following code:
ORG $1040LDX #$2100JSR $FFBB *Goes to subroutine OUT1BYT which converts the
*content of the address pointed to by the X register to*two ASCII equivalents and outputs their characters to *the screen, and increments X register by #$1.
JSR $FFBE *Goes to subroutine OUT1BSP which converts the*content of the address pointed to by the X register to*two ASCII equivalents and outputs their characters to the *screen followed by a space.
SWIEND
Resultà FAFB_ sent to screen (Note: last character is a space)
Utility Subroutines Examples
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Example: OUT2BSP (Out 2 Bytes with Space)
Assume that memory contains the following:2100 FA FB
Now execute the following code.ORG $1040LDX #$2100JSR $FFC1 *JSR to subroutine OUT2BSP which converts two
*consecutive binary bytes to their 4 ASCII numbers*and outputs their ASCII characters to the screen followed by a*space.
SWIENDResultàFAFB_ sent to screen (last character is a space).
Utility Subroutines Examples
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Example: OUTCRLF (Output Carriage Return with Line Feed)
ORG $1040JSR $FFC4 *Go to subroutine OUTCRLF
*which outputs ASCII carriage return*followed by a line feed.
SWIEND
Resultà New line on screen (Note:Equivalent to pressingEnter in your computer)
Utility Subroutines Examples
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Example: INCHAR (Input Character)
ORG $1040JSR $FFCD *Goes to subroutine INCHAR. Waits for you to
*type an ASCII character from the keyboard,*and stores the corresponding ASCII number in*accumulator A
SWIEND
Result-> If you type 9 on the keyboard, ASCII number$39 is stored in accumulator A
Utility Subroutines Examples
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Homework 3 Solution
Write an assembly language program to output a string of characters to the screen.
Solution: This particular solution will print “Hello” to the screen using the OUTA subroutine
FCC is used to store a string of characters
FCB is used to store End Of Transmission (EOT)(Note: EOT in ASCII is $04)(Note 2: In this particular example any non-printing character can be used)
OUTA EQU $FFB8
ORG $0100FCC “HELLO”FCB #$04
ORG $1040 LDX #$0100
Loop LDAA $00,XCMPA #$04BEQ QuitJSR OUTA; INXBRA Loop
Quit SWIEND
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
#$04
1
OUTA EQU $FFB8
ORG $0100FCC “Hello”FCB #$04
ORG $1040 LDX #$0100
Loop LDAA $00,XCMPA #$04BEQ QuitJSR OUTA INXBRA Loop
Quit SWIEND
$0104
$0105
$0103
$0102
$0101
$0100
CPU Registers Used in Program:
Memory Locations Used in Program:
Computer Screen:
Program Initialization
$48
$65
$6C
$6C
$6F
$04
CCR Bit Z:
Index X:
Accum A:
#$0100
#$48
0
CCR Z is 0 since Accum A does not equal $04
Does not branch because CCR Z is 0
H
#$0101
Always Branches
#$65
e
#$0102
Skip Ahead Until Last Loop
llo
#$1005CCR Z is 1 sinceAccum A equals$04
Branch since CCR Z is 1
#$6F
AsciiEquivalent of “Hello”
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
Solution 2: This particular solution will print “Hello” to the screen using the OUTSTRG subroutine
OUTSTRG EQU $FFC7
ORG $0100FCC “HELLO”FCB #$04
ORG $1040 LDX #$0100 JSR OUTSTRG SWIEND
(Note: EOT must be used because the OUTSTRG Subroutine uses EOT as the end of the string)
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Homework 3 SolutionWrite a subroutine to save the first 5 odd (8-bit) numbers pointed to by the x-register (passed in) onto the stack.
Solution:
SUB1 LDAA #$05PULY
NEXT BRSET $00,X #$01 ODDINXBRA NEXT
ODD LDAB $00,XPSHBINXDECABNE NEXTPSHYRTS
Note: In order for the subroutine to execute, in the main program, a BSR(branch to subroutine) or JSR (jump to subroutine) command must be used inthe main program.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Homework 4 solution
Write a program to output a square wave thru port D pin 2. The outputcan be observed on the scope, and the period T of the wave shouldbe measured. More than one period wave should be generated. Themachine cycle time of the M68HC11E9 should be estimated. Drawthe square wave.
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405 Homework 4 Solution Cont’dSolution:ORG $1040FCB $XXFCB $04LDAA #$04STAA $1009 LDAB #$04
CYCLE LDAA $1041 4STAA $1008 4EORA #$04 2STAA $1041 4LDAA $1040 4
NEXT DECA 2BNE NEXT 3DECB 2BEQ EXIT 3JMP CYCLE 3
EXIT SWIEND
Prebyte Opcode OperandAddress
1040 XX 1041 011042 86 04 1044 B7 10091045 C6 041049 B6 1041104C B7 1008104F 88 041051 B7 10411054 B6 10401057 4A1058 26 FD105A 5A105B 27 03105D 7E 1049105F 3F
Configures PD2as output. Helpsus to output twoperiods.
Outputs signal from PD2
T/2=Machine Cycle Time*[4+4+2+COUNT*(2+3)+4+4+3+2+3] (Note: Count = #$XX)
Switch $1041(toggle)
Delay
George W. Woodruff School of Mechanical Engineering, Georgia Tech
ME4447/6405ME4447/6405
QUESTIONS???