UP ONE LEVEL:
ENCM 369 Home Page
ENCM 369 Winter 2002 Page for Tutorial Section T01
This page is maintained by
Steve Norman
Last modified: Wed Feb 13 15:45:45 MST 2002
Contents
Introduction and Warning
This page is not intended to provide
complete lists of problems and solutions for tutorial
periods.
Instead, it will occasionally be used to provide extra
information, such as solutions in cases where I didn't have time
to present the solution in the tutorial period.
[back to top of document]
February 12, Problems 3 and 4
Below is a solution to Problem 3.
Note that pointers of type char* are needed to access
bytes in memory.
void *my_memcpy(void *dest, const void *src, size_t n)
{
char *d = (char *) dest;
const char *s = (const char *) src;
int i;
for (i = 0; i < n; i++)
d[i] = s[i];
return dest;
}
Here is a version that uses pointer arithmetic instead of array indices:
void *my_memcpy(void *dest, const void *src, size_t n)
{
char *d = (char *) dest;
const char *s = (const char *) src;
while (n > 0) {
*d = *s;
d++;
s++;
n--;
}
return dest;
}
Below is a SPIM version of the pointer-arithmetic-based function;
it doesn't use the local variables d and s;
instead it just treats dest and src as if
they were of type char*.
.text
.globl memcpy
memcpy:
add $v0, $a0, $zero # return value = dest
loop:
sltu $t0, $zero, $a2 # $t0 = (0 < n)
beq $t0, $zero, quit # if (!(0 < n)) goto quit
lbu $t0, ($a1) # $t0 = *src
sb $t0, ($a0) # *dest = $t0
addiu $a0, $a0, 1 # dest++
addiu $a1, $a1, 1 # src++
addiu $a2, $a2, -1 # n--
j loop
quit:
jr $ra
All of the above code is inefficient.
In each case, bytes are copied one at a time.
It would be more efficient to use lw and sw
instructions as much as possible.
But that would be much more complicated to program,
because memcpy does not require that
any of dest, src or n be a multiple of
four.
A highly optimized version of memcpy would look
for opportunities to copy four bytes at a time.
Loop unrolling (Lab 5, Ex A) could also significantly
improve speed.
[back to top of document]