6811 assembler version 2.1 10-Aug-91 please send bugs to Randy Sargent (rsargent@athena.mit.edu) original program by Motorola. 0001 2000 ORG $2000 0002 2000 7e 20 bb JMP START 0003 * Read character from Serial Terminal 0004 ffcd InChar equ $FFCD 0005 * Write character to Serial Terminal 0006 ffaf OutChar equ $FFAF 0007 ffb5 OUTRHF equ $FFB5 0008 ffb2 OUTLHF equ $FFB2 0009 2003 69 6e 76 61 6c 69 invCmdStr fcc 'invalid command' 64 20 63 6f 6d 6d 61 6e 64 0010 2012 00 fcb 0 0011 0010 MAXCHAR EQU 16 0012 2013 tempString rmb 16 0013 2023 00 fcb 0 0014 2024 4e 75 6d 65 72 69 stkUnder FCC 'Numeric Stack underflow' 63 20 53 74 61 63 6b 20 75 6e 64 65 72 66 6c 6f 77 0015 203b 00 FCB 0 0016 203c 4e 75 6d 65 72 69 stkOver fcc 'Numeric stack overflow' 63 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 0017 2052 00 Fcb 0 0018 * SPACE FOR NUMERIC STACK 0019 2053 numEnd rmb 100 0020 20b7 numStart rmb 2 0021 20b9 stkPtr rmb 2 0022 START: 0023 20bb 18 ce 20 b7 ldy #numStart 0024 20bf 18 ff 20 b9 sty stkPtr 0025 0026 cmdloop: 0027 20c3 bd ff cd jsr InChar 0028 20c6 91 6e cmpa 'n' 0029 20c8 20 24 bra getNumber 0030 20ca 91 4e cmpa 'N' 0031 20cc 20 20 bra getNumber 0032 20ce 91 2b cmpa '+' 0033 20d0 20 2c bra stkAdd 0034 20d2 91 2d cmpa '-' 0035 20d4 20 2d bra stkSub 0036 20d6 91 2f cmpa '/' 0037 20d8 20 2b bra stkDiv 0038 20da 91 2a cmpa '*' 0039 20dc 91 70 cmpa 'p' 0040 20de 20 29 bra pop 0041 20e0 91 50 cmpa 'P' 0042 20e2 20 25 bra pop 0043 20e4 20 21 bra stkMul 0044 20e6 fe 20 03 ldx invCmdStr 0045 20e9 bd 20 57 jsr outStringX 0046 20ec 20 d5 bra cmdloop 0047 * 0048 getNumber: 0049 20ee fe 20 13 ldx tempString nild2.asm(50): Suggestion --- Could have used BSR! 0050 20f1 bd 21 62 jsr inStringXB 0051 20f4 c6 10 ldab #MAXCHAR 0052 20f6 bd 20 00 jsr AToI nild2.asm(53): Suggestion --- Could have used BSR! 0053 20f9 bd 21 1a jsr NLDD 0054 20fc 20 c5 bra cmdloop 0055 * 0056 stkAdd: nild2.asm(57): Suggestion --- Could have used BSR! 0057 20fe bd 21 0b jsr add 0058 2101 20 c0 bra cmdloop 0059 * 0060 stkSub: 0061 2103 20 be bra cmdloop 0062 * 0063 stkDiv: 0064 2105 20 bc bra cmdloop 0065 * 0066 stkMul: 0067 2107 20 ba bra cmdloop 0068 * 0069 pop: nild2.asm(70): symbol Undefined on pass 2 nild2.asm(70): Branch out of Range 0070 2109 20 fe bra cmdLoop 0071 * 0072 add: nild2.asm(73): Suggestion --- Could have used BSR! 0073 210b bd 21 3e jsr NSTPD nild2.asm(74): symbol Undefined on pass 2 0074 210e fd 00 00 STD param1 nild2.asm(75): Suggestion --- Could have used BSR! 0075 2111 bd 21 3e jsr NSTPD nild2.asm(76): symbol Undefined on pass 2 0076 2114 f3 00 00 ADDD param1 nild2.asm(77): Suggestion --- Could have used BSR! 0077 2117 bd 21 1a jsr NLDD 0078 * 0079 NLDD: 0080 * Numeric Stack LoaD from D register 0081 211a 18 3c pshy 0082 211c 18 fe 20 b9 ldy stkPtr 0083 2120 18 8c 20 53 cmpy #numEnd 0084 2124 23 11 bls stackOver 0085 2126 18 09 dey 0086 2128 18 e7 00 stab 0,y 0087 212b 18 09 dey 0088 212d 18 a7 00 staa 0,y 0089 2130 18 ff 20 b9 sty stkPtr 0090 2134 18 38 puly 0091 2136 39 rts 0092 0093 stackOver: 0094 2137 fe 20 3c ldx stkOver 0095 213a bd 20 57 jsr outStringX 0096 213d 3f swi 0097 0098 * -------------------------------------------------------------- 0099 * Numeric stack store and pop D 0100 * ------------------------------------------------------------- 0101 NSTPD: 0102 * Numeric Stack STore and Pop to D register 0103 213e 18 3c pshy 0104 2140 18 fe 20 b9 ldy stkPtr 0105 2144 18 8c 20 b7 cmpy #numStart 0106 2148 22 11 bhi stackUnder 0107 214a 18 08 iny 0108 214c 18 a7 00 staa 0,y 0109 214f 18 08 iny 0110 2151 18 e7 00 stab 0,y 0111 0112 2154 18 ff 20 b9 sty stkPtr 0113 2158 18 38 puly 0114 215a 39 rts 0115 0116 stackUnder 0117 215b fe 20 24 ldx stkUnder 0118 215e bd 20 57 jsr outStringX 0119 2161 3f swi 0120 0121 0122 * ------------------------------------------------------------------- 0123 * Console Input inStringXB 0124 * ------------------------------------------------------------------- 0125 * Reads an ASCIIZ string pointed to by Index register X. 0126 * Maximum length to read is specified by Accumulator B. 0127 * Pressing carriage return also terminates input, placing a 0 at the end of the string. 0128 * ------------------------------------------------------------------- 0129 * USAGE: 0130 * inputBuffer fcc "00010" 0131 * FCB 0 0132 * SAMPLE CODE to read 5 characters into the string inputBuffer: 0133 * LDAB #5 0134 * LDX #inputBuffer 0135 * Get a string of at most B characters, into buffer pointed to by X 0136 * jsr inStringXB 0137 * LDX #inputBuffer 0138 * jsr atoiXD * convert as hex number 0139 * ------------------------------------------------------------------- 0140 0141 inStringXB: 0142 2162 3c pshx * Save pointer to string 0143 2163 37 pshb * B = maximum # chars to read 0144 2164 36 psha * Save Acc A 0145 getAgain: 0146 2165 37 pshb * Preserve B 0147 2166 bd ff cd jsr InChar * get character into Acc. A 0148 2169 33 pulb * Recover B 0149 * 0150 216a 81 0d cmpa #13 * is is Carriage Return (CR)? 0151 216c 27 06 beq foundEnter * CR character? - yes: break out of loop 0152 216e a7 00 staa 0,x * no: save read char in buffer 0153 2170 08 inx * point to next buffer position 0154 2171 5a decb * decrement the limit counter 0155 2172 26 f1 bne getAgain * max count not = 0, loop again 0156 * 0157 foundEnter: 0158 2174 6f 00 clr 0,x * ensure string is terminated with a character 0159 * 0160 2176 32 pula * recover Acc A 0161 2177 33 pulb * recover Acc B 0162 2178 38 pulx * recover pointer to string 0163 2179 39 rts 0164 * ------------------------------------------------------------------- 0165 0166 0167 * --------------------------------------------------------- 0168 * SUBROUTINE ASCII to INT16 0169 * Pass a Carriage-Return terminated string to this routine, and it will 0170 * return the converted value in the D register 0171 * 0172 * Upon entry Index Register X points to the beginning of the string to convert 0173 * 0174 * Upon return: 0175 * Accumulator D holds the converted 16 bit Integer 0176 * 0177 * Side effects: Upon return X points to the string terminator 0178 * --------------------------------------------------------------------- 0179 2000 RAM EQU $2000 * Ram starts at $2000 (decimal 8192) 0180 2000 ORG RAM * The origin of our program is the start of RAM ($2000) 0181 * 0182 AToI: 0183 2000 18 3c PSHY * SAVE Y REGISTER 0184 2002 cc 00 00 LDD #0 * Start with 0 as our interim result in accumulator D 0185 MoreChar: 0186 2005 18 8f XGDY * Swap D with Y; Y now contains the interim result 0187 * LDAB 0,X * Get the character pointed to by the X register 0188 2007 c1 00 CMPB #0 * Compare with the String Terminator (Null) 0189 2009 27 19 BEQ AllConverted * Finished conversion 0190 * 0191 * now convert the ASCII letters/numbers -> binary 0192 200b c0 30 SUBB #$30 * ‘0' - $30 => 0, ‘1' - $30 => 1, ... '9'-$30 -> 9, 'A'-$30->17 0193 200d c1 09 CMPB #9 * is result in range 0..9? 0194 200f 2f 02 BLE IsNumeric * If it is 0..9 then Character is now converted to integer 0195 2011 c0 07 SUBB #7 * else assume it was letter & convert letters A..F to binary 0196 IsNumeric: 0197 2013 c4 0f ANDB #$0F * Mask any bits in top nybble of byte - there should be none! 0198 0199 2015 18 8f XGDY * Swap the interim result back into D 0200 0201 2017 05 ASLD * Multiply our interim result by 16, by shifting 4 times 0202 2018 05 ASLD 0203 2019 05 ASLD 0204 201a 05 ASLD 0205 0206 201b 18 8f XGDY * Swap D with Y; Y now contains the interim result 0207 201d 18 3a ABY * IY = IY + B = append the binary value to our interim result 0208 201f 18 8f XGDY * Swap the interim result back into D 0209 * 0210 2021 08 INX * X = X + 1 - point to the next character in the string 0211 2022 20 e1 BRA MoreChar * loop for more characters to convert 0212 * 0213 AllConverted: 0214 2024 18 8f XGDY * Return result in D register 0215 2026 18 38 PULY * RESTORE Y REGISTER 0216 2028 39 RTS * Return from subroutine 0217 * 0218 * 0219 * The following example shows its usage 0220 2040 ORG #$2040 0221 2040 31 32 33 34 MyString FCC '1234' 0222 2044 00 FCB #0 * terminator 0223 * 0224 2050 ORG #$2050 0225 MAIN: 0226 2050 ce 20 40 LDX #MyString * Load the address of MyString 0227 2053 bd 20 00 JSR AToI * AtoI = ASCII to Integer 0228 2056 3f SWI * Call BUFFALO - should show A = 12, B=34 0229 * ------------------------------------------------------------------- 0230 * Console Output - an ASCIIZ string pointed to by Index register X. 0231 * ------------------------------------------------------------------- 0232 outStringX: 0233 2057 3c pshx 0234 again: 0235 2058 a6 00 ldaa 0,x * get the character pointed to by X 0236 205a 81 00 cmpa #0 * Compare with null - character 0 0237 205c 27 06 beq foundNull * found null character so break out of loop 0238 205e bd ff af jsr OutChar * output character in Acc. A 0239 2061 08 inx 0240 2062 20 f4 bra again 0241 0242 foundNull: 0243 2064 38 pulx 0244 2065 39 rts 0245 0246 0247 * ------------------------------------------------------------------- 0248 * Output 2 hex digit version of Accumulator A 0249 * ------------------------------------------------------------------- 0250 outHexA: 0251 2066 36 psha 0252 2067 37 pshb 0253 2068 3c pshx 0254 2069 18 3c pshy 0255 0256 206b 16 TAB 0257 206c bd ff b2 JSR OUTLHF ; output high nybble 0258 206f 17 TBA ; restore it to A 0259 2070 bd ff b5 JSR OUTRHF ; output low nybble 0260 0261 2073 18 38 puly 0262 2075 38 pulx 0263 2076 33 pulb 0264 2077 32 pula 0265 2078 39 rts 0266 0267 0268 * ------------------------------------------------------------------- 0269 * Output 4 hex digit version of Accumulator D 0270 * ------------------------------------------------------------------- 0271 outHexD: 0272 2079 36 psha 0273 207a 37 pshb 0274 0275 207b 37 pshb 0276 207c bd 20 66 jsr outHexA 0277 207f 32 pula 0278 2080 bd 20 66 jsr outHexA 0279 nild2.asm(280): symbol Undefined on pass 2 0280 2083 bd 00 00 JSR CRLF ; output carriage return - line feed 0281 0282 2086 33 pulb 0283 2087 32 pula 0284 2088 39 rts 0285 0286 0287 * ------------------------------------------------------------------------------------ 0288 * This subroutine delays (a little over) 100 micro-seconds 0289 * ------------------------------------------------------------------------------------ 0290 * Busy-wait a number of clock cycles 0291 * ------------------------------------------------------------------------------------ 0292 delay100us: 0293 2089 36 psha ; save Accumulator A 0294 * ; Generate a "short" delay > 100 microsec 0295 208a 86 28 LDAA #40 ; 40 loops for 200 clock cycles at 0.5 micro seconds/clock 0296 DELAYLOOP: 0297 208c 4a DECA ; 0298 208d 26 fd BNE DELAYLOOP 0299 208f 32 pula ; restore Accumulator A 0300 2090 39 RTS ; 0301 0302 0303 0304 0305 0306 0307