Computer Science 237 |
Lab 7: Langton's Ant
Due: Monday, October 30, 2006 at 9:00 AM
This week, your task is to translate the C-based ant-movement function in a simulator for Langton's Ant that runs on the Palm. This function is called CMoveAnt in the file ant.c. You should write an assembly subroutine MoveAnt in the file MoveAnt.s, and turn in only that file.
Here are the specific steps:
tar xvfz /usr/cs-local/share/cs237/labs/ant/ant.tar.gzThis creates a subdirectory, ant, that contains all of the usual files, including ant.c and MoveAnt.s
void (*MOVEANT)(int*,int*,int*,UInt8[],int) = CMoveAnt;to read
void (*MOVEANT)(int*,int*,int*,UInt8[],int) = MoveAnt;This declares a pointer to a function, initialized to point to CMoveAnt. When you change the pointer value, any calls to MOVEANT become calls to your code. You might find the call to MOVEANT--it looks pretty much like any other call. That's because a function is normally represented by a pointer to its first instruction.
turnin -c 237 MoveAnt.s
Here's the code you are to convert, found at the top of the ant.c file:
extern void CMoveAnt(int*,int*,int*,UInt8[],int); extern void MoveAnt(int*,int*,int*,UInt8[],int); /* Change the following assignment to be ... = MoveAnt to use your code */ void (*MOVEANT)(int*,int*,int*,UInt8[],int) = CMoveAnt; ... /* Ant is located at (Antx,Anty), pointing in direction AntDir, where * 0 => North, 1 => East, 2 => South, 3 => West * world is the toroidal array of bits. Bits (0,0) through (0,7) are stored * in world[0]; bits (0,8) through (0,15) are stored in world[1], etc. * This is a packed "row-major ordering." * size is the width of each row of this logical 2D array, in bits * WARNING: size may be a value other than a power of 2. */ void CMoveAnt(int *Antx, int *Anty, int *AntDir, UInt8 world[], int size) { int i; int x = *Antx, y = *Anty, d = *AntDir; InvertAnt(x,y,d); i = x+y*size; if (world[i/8]&(1<<(i%8))) { d = (d+1)%4; } else { d = (d+3)%4; } world[i/8] ^= (1<<(i%8)); InvertPt(x,y); if (d & 1) { x = (x+size-d+2)%size; } else { y = (y+size+d-1)%size; } InvertAnt(x,y,d); *Antx = x; *Anty = y; *AntDir = d; }