Assignment 6, Solutions
Part of
the homework for 22C:50, Spring 2003
|
Modify the count shell script that was given so that it outputs the first n members of the Fibonacci series. members). (1.0)
#tcsh # count n # where n is a number, # count up to but not including n; # outputs the successive fibonacci numbers 10 per line # set limit = $1 set count = 0 set components = 10 set component = 1 set linebuf = set previous = 1 -- added set this = 0 -- added while ( $count < $limit ) set linebuf = "${linebuf} ${this}" -- modified @ next = $previous + $this -- added set previous = $this -- added set this = $next -- added @ count ++ @ component = $component + 1 if ( $component > $components ) then echo $linebuf set linebuf = set component = 1 endif end if ( $component > 1 ) echo $linebuf
Do problem 5 (1.0) at the end of chapter 8 of the notes.
On a system where the procedure call stack grows down relocatable code is loaded as if it was on a stack growing up, the available free space is exhausted when the calling stack collides with the loader's location counter. Therefore, whenever a program is loaded, the location counter and stack pointer should be compared as each byte is loaded, and the loader should raise an exception if the location counter is ever greater than or equal to the stack pointer.
Ideally, application programs should also check this condition whenever they push an activation record on the stack, comparing it with the address of the highest loaded location in the current program. This requires cooperation from the assembly language programmer or the compiler, so it is difficult to rely on this check being done properly.
Do problem 2 (1.0) at the end of chapter 9 of the notes.
Using the data from Figure 9.4, and using the style of Figure 9.5, write code to initialize the COM1 port to 9600 baud, 8-bit data, 1 stop bit, odd parity.
/* in a real application, symbolic names ought to be given to all the bits needed to initialize these, but only the relevant ones are given here */ #define bits8 0x4 #define stop1 0x0 #define parity_odd 0x08 #define setbaud 0x80 #define baud9600L 0x0C #define baud9600H 0x00 /* given these defines, this code suffices */ outp( COM1LCR, setbaud ); outp( COM1BRDH, baud9600H ); outp( COM1BRDL, baud9600L ); outp( COM1LCR, parity_odd | stop1 | bits8 );
Do problem 6 (1.0) at the end of chapter 9 of the notes.
Write the "comreadblock" routine to match the other keyboard routines given in Figure 9.8 and preceeding figures, and then comment on why this routine would be very unlikely to be used for reading from the keyboard.
void comreadblock( struct filevariable * f, char buf[], int len ); { int i; for (i = 0; i < len; i++) { buf[i] = f->read(f); } }
If the com port is connected to an asynchronous keyboard, a remote dialup session using a terminal emulator, or anything else that logically connects the keyboard to the input file, a service to read the next n characters without echoing won't have much use, except perhaps for reading passwords.