Page 312 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 312
PROBLEMS 289
19, Write the C program and the resulting assembly-language program that calls a
procedure with prototype unsigned int par (unsigned int Rl, unsigned int
R2 ) ; to compute the resistance of two parallel resistors Rl and R2, following the
approach of Figure 9.14, returning the result in accumulator D.
2 0, Write the C program and the resulting assembly-language program that calls a
procedure with prototype unsigned int inner (unsigned int *v, unsigned int
*w); to compute the inner product of two two-element vectors v and w, following
the approach of Figure 9.14, returning the result in accumulator D.
21. Hand-compile the C procedure strncat below. Put the 6812 instructions under
each C statement that generates them, main calls strncat which concatenates the
second argument string on the end of the first argument string, but copies at most the
number of characters given in the third argument. For full credit, store ail parameters and
local variables on the stack, even though they can be left in registers to make the
program shorter, and do not optimize between statements, but provide the most statically
efficient assembler language code for each C statement. Your solution should be
reentrant, but need not be position independent. Assume that arguments which are put on
the stack are pushed in the order that they appear from left to right.
char *strncat(char *str_d,char *str_s,int count){char *sd=str_d;
while (*str_d++) ;
str_d—-;
while (—count) { if (!(*str_d++ = *str_s++)) return sd; }
*str_d = ' \0'; return sd;
}
22. Hand-compile the C procedure memchr below. Put the 6812 instructions under
each C statement that generates them, main calls memchr which searches the first
argument string for the second argument character, but searches at most the number of
characters given in the third argument. If it finds the second argument, it returns the
address of that character in the string. Otherwise it returns a null (0). For full credit, store
all parameters and local variables on the stack, even though they can be left in registers
to make the program shorter, and do not optimize between statements, but provide the
most statically efficient assembler language code for each C statement. Your solution
should be reentrant, but need not be position independent. Assume that arguments which
are put on the stack are pushed in the order that they appear from left to right.
char *memchr(char *buffer,char chr,int count){char *ptr=buffer;
while(count—) { if( *ptr == chr ) return ptr; ++ptr; }
return 0;
}