. CSci320 Programming Languages -- laboratory 09 -- C++ Control structures . Duff's Disgusting Device Look at this .See http://groups.google.com/groups?selm=2748%40alice.UUCP Duff's original code assumes that the place that the data is being moved to is not RAM but an address of a peripheral! When run with "to" pointing at RAM it needs a fix. I am grateful to an alert reader who corrected me: .As_is > Given the "8 times", I think you're considering the "*to" (instead .As_is > of "*to++") being an error. [As was] .As_is > .As_is > May I point out that Tom Duff's application of that routine was to .As_is > send data to a device register which was memory-mapped at a fixed .As_is > address, so his code is (for *his* application) correct? This is .As_is > explained in Duff's original posting[2] as well as (slightly more .As_is > elaborate) a later comment[3]. .As_is > .As_is > Of course, the application (memory-to-memory copy vs. .As_is > copy-to-device-register) is orthogonal to Duff's discussion .As_is > of language properties. .As_is > .As_is > Regards, .As_is > Ignatios Souvatzis Here is a complete working program in C .See ../c/duff.c that uses this device to copy integer arrays. What changes must you make to move from C to C++? Notice that it uses a C function header that expects you to provide the address of two arrays: write a main program that lets you test it. Publish your working C++ code and link it to a lab page explaining what is good and bad about Duff's device. By the way, people .See http://groups.google.com/groups/search?q=%22duff%27s+device%22&start=0&scoring=d&hl=en&lr=&safe=off&num=10 are still discussing this trick. . Jackson's Inversion Here is the design of a co-function that accepts a series of integers and detects the first and later occurence of the number 2: input ::= #(int~2) 2 #(int~2). .As_is string fun(int i) .As_is { .As_is restart(); .As_is while( i != 2) .As_is { .As_is resume "no two yet"; .As_is } .As_is resume "two"; .As_is while(true) .As_is { .As_is resume("there was a two"); .As_is } .As_is } Unfortunately the above uses restart() and resume() which are nonstandard C++ functions. Resume exits a function but remembers where it was, and restart goes to that place the `next time the function is call`. So here is a sample test run .Table i returned value .Row 1 no two yet .Row 3 no two yet .Row 2 two .Row 1 there was a two .Row 2 there was a two .Row 3 there was a two .Close.Table M. A. Jackson publicised a way to use switch, goto, etc to implement restart and resume. Here is a file that shows it coded in standard C++. Download, compile, run and test... .See ../restart.cpp Download, compile, and test (99 is the terminating sentinal for input...). If you have time you can try changing it... Add to your web page a comment on this style of "inverted code" plus a link to an example. . Evil C Macros to make Co-Routines Co-routines are an alternative way of getting the same effect as Jackson's inverted code. Here .See http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html is a link into a way to get the same effect in C by using some possible dangerous macros. It also has a good discussion of why we need co-routines. . Check the Preparation for next class .See ../10.html . If you have time .See http://c2.com/cgi/wiki?ProgrammerLiteracy