📄 math.lst
字号:
ASEM-51 V1.3 Copyright (c) 2002 by W.W. Heinz PAGE 1
MCS-51 Family Macro Assembler A S E M - 5 1 V 1.3
=====================================================
Source File: E:\MiCrOConTroller\Coba_M-IDE\Math 8051\math.asm
Object File: E:\MiCrOConTroller\Coba_M-IDE\Math 8051\math.hex
List File: E:\MiCrOConTroller\Coba_M-IDE\Math 8051\math.lst
Line I Addr Code Source
1: ;*****************************************************************
2: ;* *
3: ;* Maths Subroutines for the 8051 microcontroller *
4: ;* W.G.Marshall 2002 *
5: ;* *
6: ;*****************************************************************
7:
8: ; All parameters in Register bank 0, (r0 to r7)
9: ; Bits 21H and 22H reserved for sign bits
10:
11: ;=================================================================
12: ; subroutine Cr0
13: ; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
14: ;
15: ; input: r0 = signed byte
16: ;
17: ; output: r0 = magnitude
18: ; Bit 21H = sign (21H is set if r0 is a negative number)
19: ;
20: ; alters: acc
21: ;=================================================================
22:
23: 0000 E8 Cr0: mov a, r0 ; read X into accumulator
24: 0001 20 E7 03 jb acc.7, Cr0a ; X is negative if bit 7 is 1
25: 0004 C2 21 clr 21H ; clear sign bit if 'positive'
26: 0006 22 ret ; done
27:
28: 0007 F4 Cr0a: cpl a ; X negative, find abs value
29: 0008 04 inc a ; XA = complement(XT)+1
30: 0009 F8 mov r0, a ; save magnitude
31: 000A D2 21 setb 21H ; set sign bit if 'negative'
32: 000C 22 ret
33:
34:
35: ;=================================================================
36: ; subroutine Cr1
37: ; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
38: ;
39: ; input: r1 = signed byte
40: ;
41: ; output: r1 = magnitude
42: ; Bit 22H = sign (22H is set if r1 is a negative number)
43: ;
ASEM-51 V1.3 Copyright (c) 2002 by W.W. Heinz PAGE 2
Line I Addr Code Source
44: ; alters: acc
45: ;=================================================================
46:
47: 000D E9 Cr1: mov a, r1 ; read X into accumulator
48: 000E 20 E7 03 jb acc.7, Cr1a ; X is negative if bit 7 is 1
49: 0011 C2 22 clr 22H ; clear sign bit if 'positive'
50: 0013 22 ret ; done
51:
52: 0014 F4 Cr1a: cpl a ; X negative, find abs value
53: 0015 04 inc a ; XA = complement(XT)+1
54: 0016 F9 mov r1, a ; save magnitude
55: 0017 D2 22 setb 22H ; set sign bit if 'negative'
56: 0019 22 ret
57:
58:
59: ;===================================================================
60: ; subroutine Cr0r1
61: ; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
62: ;
63: ; input: r1, r0 = signed word
64: ;
65: ; output: r1, r0 = magnitude
66: ; Bit 21H = sign (21H is set if negative number)
67: ;
68: ; alters: acc, C
69: ;===================================================================
70:
71: 001A E9 Cr0r1: mov a, r1 ; high byte into accumulator
72: 001B 20 E7 03 jb acc.7, c0a ; negative if bit 7 is 1
73: 001E C2 21 clr 21H ; clear sign bit if 'positive'
74: 0020 22 ret ; done
75:
76: 0021 D2 21 c0a: setb 21H ; set sign flag
77: 0023 E8 mov a, r0 ; number is negative
78: 0024 F4 cpl a ; complement
79: 0025 24 01 add a, #1 ; and add +1
80: 0027 F8 mov r0, a
81: 0028 E9 mov a, r1 ; get next byte
82: 0029 F4 cpl a ; complement
83: 002A 34 00 addc a, #0
84: 002C F9 mov r1, a
85: 002D 22 ret
86:
87:
88: ;====================================================================
89: ; subroutine Cr2r3
90: ; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
91: ;
92: ; input: r3, r2 = signed word
93: ;
94: ; output: r3, r2 = magnitude
95: ; Bit 22H = sign (22H is set if negative number)
96: ;
97: ; alters: acc, C
98: ;====================================================================
99:
ASEM-51 V1.3 Copyright (c) 2002 by W.W. Heinz PAGE 3
Line I Addr Code Source
100: 002E EB Cr2r3: mov a, r3 ; read high into accumulator
101: 002F 20 E7 03 jb acc.7, c1a ; negative if bit 7 is 1
102: 0032 C2 22 clr 22H ; clear sign bit if 'positive'
103: 0034 22 ret ; done
104:
105: 0035 D2 22 c1a: setb 22H ; set sign flag
106: 0037 EA mov a, r2 ; number is negative
107: 0038 F4 cpl a ; complement
108: 0039 24 01 add a, #1 ; and add +1
109: 003B FA mov r2, a
110: 003C EB mov a, r3 ; get next byte
111: 003D F4 cpl a ; complement
112: 003E 34 00 addc a, #0
113: 0040 FB mov r3, a
114: 0041 22 ret
115:
116:
117: ;====================================================================
118: ; subroutine Cr4r5
119: ; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
120: ;
121: ; input: r5, r4 = signed word
122: ;
123: ; output: r5, r4 = magnitude
124: ; Bit 22H = sign (22H is set if negative number)
125: ;
126: ; alters: acc, C
127: ;====================================================================
128:
129: 0042 ED Cr4r5: mov a, r5 ; read high into accumulator
130: 0043 20 E7 03 jb acc.7, c3a ; negative if bit 7 is 1
131: 0046 C2 22 clr 22H ; clear sign bit if 'positive'
132: 0048 22 ret ; done
133:
134: 0049 D2 22 c3a: setb 22H ; set sign flag
135: 004B EC mov a, r4 ; number is negative
136: 004C F4 cpl a ; complement
137: 004D 24 01 add a, #1 ; and add +1
138: 004F FC mov r4, a
139: 0050 ED mov a, r5 ; get next byte
140: 0051 F4 cpl a ; complement
141: 0052 34 00 addc a, #0
142: 0054 FD mov r5, a
143: 0055 22 ret
144:
145:
146: ;====================================================================
147: ; subroutine Cr0r3
148: ; 32-Bit 2's Complement -> magnitude / Sign Bit Conversion
149: ;
150: ; input: r3, r2, r1, r0 = signed word
151: ;
152: ; output: r3, r2, r1, r0 = magnitude
153: ; Bit 21H = sign (21H is set if negative number)
154: ;
155: ; alters: acc
ASEM-51 V1.3 Copyright (c) 2002 by W.W. Heinz PAGE 4
Line I Addr Code Source
156: ;====================================================================
157:
158: 0056 EB Cr0r3: mov a, r3 ; read high into accumulator
159: 0057 20 E7 03 jb acc.7, c2a ; negative if bit 7 is 1
160: 005A C2 21 clr 21H ; clear sign flag if 'positive'
161: 005C 22 ret ; done
162:
163: 005D D2 21 c2a: setb 21H ; set sign flag
164: 005F E8 mov a, r0 ; number is negative
165: 0060 F4 cpl a ; complement
166: 0061 24 01 add a, #1 ; and add +1
167: 0063 F8 mov r0, a
168: 0064 E9 mov a, r1 ; get next byte
169: 0065 F4 cpl a ; complement
170: 0066 34 00 addc a, #0
171: 0068 F9 mov r1,a
172: 0069 EA mov a, r2 ; get next byte
173: 006A F4 cpl a ; complement
174: 006B 34 00 addc a, #0
175: 006D FA mov r2,a
176: 006E EB mov a, r3 ; get next byte
177: 006F F4 cpl a ; complement
178: 0070 34 00 addc a, #0
179: 0072 FB mov r3, a
180: 0073 22 ret ; done
181:
182:
183: ;==================================================================
184: ; subroutine Mr0
185: ; 8-Bit magnitude / Sign Bit -> 2's Complement Conversion
186: ;
187: ; input: r0 = magnitude
188: ; Bits 21H & 22H = sign bits of operands X and Y
189: ; (set if negative)
190: ;
191: ; output: r0 = signed byte
192: ;
193: ; alters: acc
194: ;==================================================================
195:
196: 0074 20 21 04 Mr0: jb 21H, Mr0b ; test X sign
197: 0077 20 22 05 jb 22H, Mr0a ; test Y sign
198: 007A 22 ret
199:
200: 007B 30 22 01 Mr0b: jnb 22H, Mr0a
201: 007E 22 ret
202:
203: 007F E8 Mr0a: mov a, r0 ; if r0 negative, get abs value
204: 0080 F4 cpl a ; complement magnitude of X
205: 0081 04 inc a ; r0 = complement(r0)+1
206: 0082 F8 mov r0, a ; save in 2's complement
207: 0083 22 ret ; done
208:
209:
210: ;====================================================================
211: ; subroutine Mr0r1
ASEM-51 V1.3 Copyright (c) 2002 by W.W. Heinz PAGE 5
Line I Addr Code Source
212: ; 16-Bit magnitude / Sign Bit -> 2's Complement Conversion
213: ;
214: ; input: r1, r0 = magnitude
215: ; Bits 21H & 22H = sign bits of operands X and Y
216: ; (set if negative)
217: ;
218: ; output: r1, r0 = signed word
219: ;
220: ; alters: acc, C
221: ;====================================================================
222:
223: 0084 20 21 04 Mr0r1: jb 21H, Mr0r1b ; test X sign
224: 0087 20 22 05 jb 22H, Mr0r1a ; test Y sign
225: 008A 22 ret
226:
227: 008B 30 22 01 Mr0r1b: jnb 22H, Mr0r1a
228: 008E 22 ret
229:
230: 008F E8 Mr0r1a: mov a, r0 ; negate number
231: 0090 F4 cpl a ; complement
232: 0091 24 01 add a, #1 ; and add +1
233: 0093 F8 mov r0, a
234: 0094 E9 mov a, r1 ; get next byte
235: 0095 F4 cpl a ; complement
236: 0096 34 00 addc a, #0
237: 0098 F9 mov r1, a
238: 0099 22 ret
239:
240:
241: ;====================================================================
242: ; subroutine Mr0r3
243: ; 32-Bit magnitude / Sign Bit -> 2's Complement Conversion
244: ;
245: ; input: r3, r2, r1, r0 = magnitude
246: ; Bits 21H & 22H = sign bits of operands X and Y
247: ; (set if negative)
248: ;
249: ; output: r3, r2, r1, r0 = signed word
250: ;
251: ; alters: acc, C
252: ;====================================================================
253:
254: 009A 20 21 04 Mr0r3: jb 21H, Mr0r3b ; test X sign
255: 009D 20 22 05 jb 22H, Mr0r3a ; test Y sign
256: 00A0 22 ret
257:
258: 00A1 30 22 01 Mr0r3b: jnb 22H, Mr0r3a
259: 00A4 22 ret
260:
261: 00A5 E8 Mr0r3a: mov a, r0 ; negate number
262: 00A6 F4 cpl a ; complement
263: 00A7 24 01 add a, #1 ; and add +1
264: 00A9 F8 mov r0, a
265: 00AA E9 mov a, r1 ; get next byte
266: 00AB F4 cpl a ; complement
267: 00AC 34 00 addc a, #0
ASEM-51 V1.3 Copyright (c) 2002 by W.W. Heinz PAGE 6
Line I Addr Code Source
268: 00AE F9 mov r1, a
269: 00AF EA mov a, r2 ; get next byte
270: 00B0 F4 cpl a ; complement
271: 00B1 34 00 addc a, #0
272: 00B3 FA mov r2, a
273: 00B4 EB mov a, r3 ; get next byte
274: 00B5 F4 cpl a ; complement
275: 00B6 34 00 addc a, #0
276: 00B8 FB mov r3, a
277: 00B9 22 ret ; done
278:
279:
280: ;====================================================================
281: ; subroutine CONV816
282: ; 8-bit Signed number to 16-Bit Signed number conversion
283: ;
284: ; input: r0 = X
285: ;
286: ; output: r1, r0 = X with sign extended to 16 bits
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -