Файл: Introduction to Microcontrollers. Architecture, Programming, and Interfacing of the Motorola 68HC12 (G.J. Lipovski, 1999).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1433
Скачиваний: 0
9.6 Examples from Character String Procedures |
283 |
|
3: char *strchr(char *str, int chr){ |
||
0000088A 3B |
PSHD |
|
4:while (*str) {
QOG0088B 200B |
BRA *+13 ;abs = 0898 |
5:if(*str == chr) return (str);
0000088D |
B715 |
SEX |
B,X |
0000088F |
AE80 |
CPX |
0,SP |
00000891 |
2711 |
BEQ |
*+19 ;abs = 08A4 |
6:-M-str;
00000893 |
EE84 |
LDX |
4,SP |
00000895 |
08 |
INX |
|
00000896 |
6E84 |
STX |
4,SP |
4:while (*str) {
00000898 |
EE84 |
LDX |
4,SP |
0000089A |
E600 |
LDAB |
0,X |
0000089C |
26EF |
BNE |
*-15 ?abs = 088D |
8:if(*str == chr) return str;
0000089E B715 |
SEX |
B,X |
||
000008AO AE80 |
CPX |
0,SP |
||
000008A2 |
2603 |
BNE |
*+5 |
;abs = 08A7 |
000008A4 EC84 |
LDD |
4,SP |
||
000008A6 |
8FC787 |
CPS |
#51079 |
|
10: } |
||||
000008A9 |
30 |
PULX |
||
000008AA |
3D |
RTS |
||
Figure 9.18. The Strchr Procedure
The procedure str chr searches for a character in a null-terminated string. See Figure 9.18. The first argument specifies the string. The second argument is a character. The procedure searches the string for a matching character; if it finds the character, it returns the address of the character in the string, otherwise it retuns a null (0).
We now show strncpy which is used to copy characters from and to a nullterminated string. See Figure 9.19. We show the calling routine for this example to illustrate the passing of more than three arguments. The main procedure calls the strncpy procedure with three arguments. Notice how arguments are pushed in order or their appearance from left to right, so the leftmost string, pushed first, is at 8,SP inside strncpy. You should step through the while loop to see how each C statement is compiled into assembly language. Note, however, that the pointers keep getting reloaded into X and Y registers from their local variable storage locations. You can do a lot better by writing the program in assembler language. But you can use this code, produced by the Hiware C++ compiler, as a starting point for a tightly coded assembler language program.
The procedure strncmp compares characters in two null-terminated strings, specified by the first two arguments, up to a number of characters specified in the third argument. See Figure 9.20. Observe the condition used to execute the while loop. If any of the three conditions are false, the subroutine terminates.
284 |
Chapter 9 Implementationof C Procedures |
char *strncpy(char *str_d,char *str_s,int count){char *sd = str d;
0000088A |
3B |
PSHD |
||
0000088B |
3B |
PSHD |
||
0000088C |
EC88 |
LDD |
8,SP |
|
QQOGQ88E |
6C82 |
STD |
2,SP |
|
51 while(count—) { |
||||
00000890 |
201A |
BRA |
*+28 |
;abs = 08AC |
6:if(*str_s) *str__d++ = *str_s++;
00000892 |
EE86 |
LDX |
6,SP |
|
00000894 E600 |
LDAB |
0,X |
||
00000896 |
270E |
BEQ |
*+16 |
;abs = 08A6 |
00000898 |
EE88 |
LDX |
8,SP |
|
0000089A |
ED86 |
LDY |
6,SP |
|
0000089C |
E670 |
LDAB |
1,Y+ |
|
0000089E |
6B30 |
STAB |
1,X+ |
|
000008AO |
6E88 |
STX |
8,SP |
|
000008A2 |
6D86 |
STY |
6,SP |
|
000008A4 |
2006 |
BRA |
*+8 |
;abs = 08AC |
7:else *str_d++ = '\0';
000008A6 EE88 |
LDX |
8,SP |
|
000008A8 |
6930 |
CLR |
1,X+ |
000008AA |
6E88 |
STX |
8,SP |
5:while(count—) {
000008AC EE80 |
'LDX |
0,SP |
|
000008AE |
191F |
LEAY |
-1,X |
000008BO |
6D80 |
STY |
0,SP |
000008B2 |
0475DD |
TBNE |
X,*-32 ;abs = 0892 |
9:return (sd);
000008B5 |
EC82 |
LDD |
2,SP |
|
10: } |
||||
000008B7 |
1B84 |
LEAS |
4,SP |
|
000008B9 |
3D |
RTS |
||
13: void main() |
{ strncpy(sl, |
s2, 5); |
||
000008BD |
CC080B |
LDD |
#2059 |
; this is si |
000008CO |
3B |
PSHD |
||
000008C1 |
CE0800 |
LDX |
#2048 |
; this is s2 |
000008C4 |
34 |
PSHX |
||
000008C5 |
C605 |
LDAB |
#5 |
; this is the rightmost argument |
000008C7 |
87 |
CLRA |
||
000008C8 |
07CO |
BSR |
*-62 |
;abs = 088A |
000008CA |
1B84 |
LEAS |
4,SP |
|
15: } |
||||
000008D2 |
3D |
RTS |
||
Figure 9.19. The Strncpy Procedure
9.6 Examples from Character String Procedures |
285 |
4: int |
strncmp(char *strl, char *str2, int count) { |
|
0000088A |
6CAE |
STD 2,-SP |
5:if (!count) return 0;
0000088C |
2618 |
BNE |
*+26 |
;abs |
= |
08A6 |
0000088E |
C7 |
CLRB |
||||
0000088F |
87 |
CLRA |
||||
00000890 |
203B |
BRA |
*+61 |
;abs |
= |
08CD |
7:if (*strl != *str2) break;
00000892 |
EE86 |
LDX |
6,SP |
|
00000894 |
E600 |
LDAB |
0,X |
|
00000896 |
EE84 |
LDX |
4,SP |
|
00000898 |
E100 |
CMPB |
0,X |
|
0000089A |
261F |
BNE |
*+33 |
;abs = 08BB |
8:++strl; ++str2;
0000089C |
EE86 |
LDX |
6,SP |
0000089E |
08 |
INX |
|
0000089F |
6E86 |
STX |
6,SP |
000008A1 EE84 |
LDX |
4,SP |
|
000008A3 |
08 |
INX |
|
000008A4 |
6E84 |
STX |
4,SP |
6:while(count— &&*strl && *str2 ){
000008A6 EE80 |
LDX |
0,SP |
||
000008A8 |
191F |
LEAY |
-1,X |
|
000008AA |
6D80 |
STY |
0,SP |
|
000008AC |
04450C |
TBEQ |
X,*+15 |
;abs = 08BB |
000008AF |
EE86 |
LDX |
6,SP |
|
000008B1 |
E600 |
LDAB |
0,X |
|
000008B3 |
2706 |
BEQ |
*+8 |
;abs = 08BB |
000008B5 EE84 |
LDX |
4,SP |
||
000008B7 |
E600 |
LDAB |
0,X |
|
000008B9 |
26D7 |
BNE |
*-39 |
;abs = 0892 |
10:return (*strl - *str2);
000008BB |
EE86 |
LDX |
6,SP |
000008BD |
E600 |
LDAB |
0,X |
000008BF |
B714 |
SEX |
BfD |
000008C1 |
EE84 |
LDX |
4,SP |
000008C3 |
3B |
PSHD |
|
000008C4 |
E600 |
LDAB |
0,X |
000008C6 |
B715 |
SEX |
B,X |
000008C8 |
34 |
PSHX |
|
000008C9 |
EC82 |
LDD |
2,SP |
000008CB |
A3B3 |
SUED |
4,SP+ |
11: } |
|||
000008CD |
30 |
PULX |
|
000008CE |
3D |
RTS |
Figure 9.20. The Strncmp Procedure
288 Chapter 9 Implementation of C Procedures
9. Global variables are declared as struct { unsigned int alpha: 3, beta:?, gamma: 6 } *p; int i;. A struct with bit fields is packed from leftmost bit forthe first field named on the left, through consecutive fields, toward the right. Write a shortest program segment to execute each of the following C statements.
a. i = p->alpha; |
b. p->beti =- i; |
c.p->alpha |
= p->gamma; |
|||||||||||||||||
10. |
Write a shortest program segment to execute each of the following C statements, |
|||||||||||||||||||
a. gui = |
(gui & Oxc7ff) |
+ ((Isc « 11) |
& 0x3800); |
|||||||||||||||||
b. lux |
= |
(lui & Oxffc?) |
\ |
((gsc « 3) & 0x38); |
||||||||||||||||
c. lui |
= |
(lui & Oxc7c7) |
+ |
((gsc«3)&0x38) |
\ |
((Isc«ll)&0x3800); |
||||||||||||||
11. |
Write a shortest program segment to execute each of the following C statements, |
|||||||||||||||||||
a. guc |
= gui |
>= lac |
; « |
|||||||||||||||||
b. luc |
= lui |
< gsc |
; |
|||||||||||||||||
c. lui |
= |
(gui >= Isc) |
\ \ |
(lui |
< gsc) ; |
|||||||||||||||
12. |
Write a shortest program segment to execute each of the following C statements. |
|||||||||||||||||||
a. if{ |
gui |
>= Isc |
) |
lui++; |
||||||||||||||||
b. if( |
! |
( |
gui A |
Isc) |
) |
lui |
*= 10; |
|||||||||||||
c. i f ( ( |
gui >= Isc ) & & |
( ! |
( |
(gui A Isc) |
& gsc) ) |
) |
lui |
A= gui; |
||||||||||||
13. |
Write a shortest program segment to execute each of the following C statements. |
|||||||||||||||||||
a. if(( |
gui |
<= Isc |
) | j ( |
gui |
>=( Isc |
+ 7 ) ) ) |
lui++; |
|||||||||||||
b. if( |
( |
gui |
> Isc) |
&& |
( |
gui |
< (Isc + |
3) |
) |
) |
lui |
*= |
10; |
|||||||
c. i f ( ( |
gui |
>= 0 |
) && ( |
Isi |
< |
0 ) |
&& ( |
gui |
> |
Isi |
) |
) lui |
"= gui; |
|||||||
14.Write the case statement below according to the conventions of Figure 9.9a. switch(guc){case 2: gui = -1; break;case 4:Isc = -1;default:Isi = -1;}
15.Repeat Problem 14 according to the conventions of Figure 9.9b.-
16.Rewrite the assembly-language program of Figure 9.1Ib for a main program, like
Figure 9.11 a, in which the declaration int statement sum += a[i][j]; is replaced by if
sum; is replaced by int k;, and the (k > a[i][j] ) k = a[i][j];
17. Write the C program and the resulting assembly-language program that transposes a two-dimensional matrix of size 4 by 4, following the approach of Figure 9.11.
18. Write the C program and the resulting assembly-language program that stays in a do while loop as long as both bits 7 and 6 of the byte at location $dOare zero, following the approach of Figure 9.13b.