<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8197494524072729204</id><updated>2012-02-16T18:06:58.160-08:00</updated><category term='Structures'/><category term='assert'/><category term='if statement'/><category term='bitwise'/><category term='Make'/><category term='debugging'/><category term='Machine'/><category term='Gold'/><category term='Bonus Material'/><category term='C'/><category term='Arrays'/><category term='Branching'/><category term='Files'/><category term='Compiling'/><category term='Streams'/><category term='while loop'/><category term='Testing'/><category term='pointers'/><category term='segfault'/><category term='I/O'/><category term='Assembly'/><category term='Sockets'/><category term='GDB'/><category term='Switch Statement'/><category term='Linking'/><title type='text'>UBC CPSC 213 Tutorial Supplement</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cpsc213.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>20</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-790687074566082626</id><published>2008-03-25T09:44:00.001-07:00</published><updated>2008-03-25T10:11:08.710-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Sockets'/><title type='text'>Socket Overview</title><content type='html'>&lt;p&gt;
A TCP/IP socket is a bi-directional stream between two services.&lt;/p&gt;

&lt;pre&gt;
 Server                Client
--------              --------
 socket                socket
 bind
 listen
   accept   ---------  connect
   | recv*  &lt;--------  send*
   | send*  --------&gt;  recv*
   | close  ---------  close
 close
&lt;/pre&gt;

&lt;dl&gt;
&lt;dt&gt;socket&lt;/dt&gt;
&lt;dd&gt;Gets socket resources (file descriptor, buffers).&lt;/dd&gt;
&lt;dd&gt;Specifies the protocol family.&lt;/dd&gt;

&lt;dt&gt;bind&lt;/dt&gt;
&lt;dd&gt;Gives the socket a local address (port, IP address).&lt;/dd&gt;

&lt;dt&gt;listen&lt;/dt&gt;
&lt;dd&gt;Establish that this socket will be a server socket (a/k/a a welcome socket) for accepting incoming connections.&lt;/dd&gt;
&lt;dd&gt;Sets the length of the listen queue (the maximum number of waiting incoming connections).&lt;/dd&gt;

&lt;dt&gt;accept&lt;/dt&gt;
&lt;dd&gt;Receive an incoming connection.&lt;/dd&gt;
&lt;dd&gt;Returns a new socket connected to the client.&lt;/dd&gt;

&lt;dt&gt;connect&lt;/dt&gt;
&lt;dd&gt;Connect a client socket to a remote host.&lt;/dd&gt;

&lt;dt&gt;send / recv&lt;dt&gt;
&lt;dd&gt;Send and receive data over the connected socket stream.&lt;/dd&gt;

&lt;dt&gt;close&lt;/dt&gt;
&lt;dd&gt;Close the socket.&lt;/dd&gt;

&lt;/dl&gt;

&lt;p&gt;
See: &lt;a href="http://beej.us/guide/bgnet/output/html/multipage/index.html"&gt;Beej's Networking Guide&lt;/a&gt; (especially sections 4 and 5).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-790687074566082626?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/790687074566082626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/790687074566082626'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_03_01_archive.html#790687074566082626' title='Socket Overview'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-63198517776123590</id><published>2008-03-18T12:24:00.000-07:00</published><updated>2008-03-18T13:27:59.742-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='I/O'/><category scheme='http://www.blogger.com/atom/ns#' term='Files'/><title type='text'>Files in C</title><content type='html'>&lt;p&gt;
A file is a flat representation of data as an ordered sequence of bytes.&lt;/p&gt;

&lt;p&gt;
Files are written to and read from as a stream, but a file handle can have the concept of a file cursor.  The file cursor represents the position in the file of the "next" byte to be read or written.&lt;/p&gt;

&lt;p&gt;
In Java, the use of the file cursor for random access to a file is discouraged (seeking is fairly expensive, and often leads to errors).  Most file I/O happens strictly through the stream abstraction.  In C, it's a lot easier to seek backwards and forwards.  There are methods to move the file cursor to different locations in the file, but reading and writing still happens through the stream abstraction.  When you read or write a byte, the file cursor is automatically advanced one byte.&lt;/p&gt;

&lt;p&gt;
C's I/O libraries are set up to be type agnostic so that you can read or write any type to/from the stream without excessive overloading (overloading is actually not allowed in C).  They work by giving the I/O function a pointer to a buffer of unknown type (a &lt;code&gt;void*&lt;/code&gt;) and telling the function how large that buffer is and how many elements it contains.&lt;/p&gt;

&lt;p&gt;
Reading and writing a single integer value looks like this:&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
int val;
fread(&amp;val, sizeof(val), 1, f_in);
fwrite(&amp;val, sizeof(val), 1, f_out);&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;&lt;/p&gt;

&lt;p&gt;
Reading and writing a struct looks like this:&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
struct AStructType val;
fread(&amp;val, sizeof(val), 1, f_in);
fwrite(&amp;val, sizeof(val), 1, f_out);&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;&lt;/p&gt;

&lt;p&gt;
Reading and writing an array looks like this:&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
int[10] val;
fread(val, sizeof(int), 10, f_in);
fwrite(val, sizeof(int), 10, f_out);&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;
Because the read and write values take pointers to buffers, you cannot write any temporary values to a stream.&lt;/p&gt;

&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
frwite(atoi("7"), sizeof(int), 1, f);&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
The above example will generate a compile warning and segfault because the value returned from atoi() is the value 7 and not a pointer to the value 7.  The compiler warns us that we are trying to convert an integer to a pointer without a cast, and it segfaults because fwrite tries to read what is stored at memory location 7 (which is not a valid pointer address).&lt;/p&gt;

&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
frwite(&amp;atoi("7"), sizeof(int), 1, f);&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
The above example does not compile because we are trying to take the address of a temporary value.  This value may not have an address at all, it may only exist in a register, so we cannot take its address.&lt;/p&gt;

&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
int val = atoi("7");
frwite(&amp;val, sizeof(int), 1, f);&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
Corrects the problem and works as intended.&lt;/p&gt;


&lt;p&gt;
See: stdio.h (&lt;a href="http://www.cppreference.com/stdio/index.html"&gt;cppreference.com&lt;/a&gt;, &lt;a href="http://www.cplusplus.com/reference/clibrary/cstdio/"&gt;cplusplus.com&lt;/a&gt;)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-63198517776123590?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/63198517776123590'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/63198517776123590'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_03_01_archive.html#63198517776123590' title='Files in C'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-1895772124401835086</id><published>2008-03-18T11:38:00.000-07:00</published><updated>2008-03-18T12:24:19.978-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='I/O'/><category scheme='http://www.blogger.com/atom/ns#' term='Streams'/><title type='text'>Stream Abstraction</title><content type='html'>&lt;p&gt;
A common abstraction used for I/O is a "&lt;a href="http://en.wikipedia.org/wiki/Stream_%28computing%29"&gt;stream&lt;/a&gt;."  A stream is a reliable connection between two components through which one device can send data to the other.  Streams usually operate on bytes (and are called &lt;a href="http://en.wikipedia.org/wiki/Byte_stream"&gt;byte streams&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;
A stream gets it's name because it is like a river of data.  The data length is unknown, and you cannot randomly access elements in the stream.  When data is added to the stream it flows down the river.  You are only allowed to insert data at the start of the stream -- you cannot change it, remove it, or insert something ahead of it -- once it has gone into the stream, it is out of your control.  When you remove data from a stream, you can only remove it from the end.  You cannot "go back" in the stream (doing so would be the same as pushing a river backwards) and you cannot read a value later in the stream (without reading the data earlier in the stream).  The data pours out of the end, and once it is removed, it is no longer part of the stream.&lt;/p&gt;
&lt;p&gt;
Imagine a stream as a river, and your program is using it to send/receive messages.  Putting data in the stream is the same as putting a message into a bottle and floating it down the river.  Taking data from the stream is the same as removing a bottle from the river.  However, in this case the river is a perfect delivery mechanism.  Bottles always arrive in the same order that they were sent, they are never lost (or missed), and there are no gaps between the bottles.&lt;/p&gt;

&lt;p&gt;
Output streams are occasionally called a "sink," whereas input streams are sometimes called a "source."&lt;/p&gt;

&lt;p&gt;
Streams are a useful abstraction because they hide the underlying transport mechanism used between components, and they hide the details of the component at the other end.  The stream could be a network socket, a &lt;a href="http://en.wikipedia.org/wiki/Pipeline_%28Unix%29"&gt;pipe&lt;/a&gt; to another program, a terminal, a keyboard, a disk, a printer, a modem, a billboard, or a buffer in memory.  Programs written to use streams as their I/O can be easily adapted to substitute one implementation of a stream for another.&lt;/p&gt;

&lt;p&gt;
See: Java's &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html"&gt;InputStream&lt;/a&gt;, &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html"&gt;OutputStream&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Iostream"&gt;C++'s iostream&lt;/a&gt;, and C's &lt;a href="http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_7.html"&gt;I/O with streams&lt;/a&gt; (&lt;a href="http://www.cppreference.com/stdio/index.html"&gt;stdio.h&lt;/a&gt;).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-1895772124401835086?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/1895772124401835086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/1895772124401835086'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_03_01_archive.html#1895772124401835086' title='Stream Abstraction'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-2731233427679603122</id><published>2008-03-06T12:42:00.000-08:00</published><updated>2008-03-18T13:27:49.117-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='debugging'/><category scheme='http://www.blogger.com/atom/ns#' term='GDB'/><title type='text'>Using GDB</title><content type='html'>&lt;p&gt;
&lt;a href="http://en.wikipedia.org/wiki/GDB"&gt;GDB&lt;/a&gt; (&lt;a href="http://sourceware.org/gdb/"&gt;project&lt;/a&gt;, &lt;a href="http://sourceware.org/gdb/documentation/"&gt;documentation&lt;/a&gt;) is a simple debugging tool for GNU command line systems.&lt;/p&gt;

&lt;p&gt;
You can Google around for lots of information about it.  A &lt;a href="http://www.cs.princeton.edu/%7Ebenjasik/gdb/gdbtut.html"&gt;short tutorial&lt;/a&gt;, a &lt;a href="http://www.cs.cmu.edu/%7Egilpin/tutorial/"&gt;longer tutorial&lt;/a&gt;, and a &lt;a href="http://www.cs.dal.ca/studentservices/refcards/gdbref.pdf"&gt;reference card&lt;/a&gt; are pretty easy to find.&lt;/p&gt;

&lt;p&gt;
For GDB to be most effective, you should compile your code with the &lt;code&gt;-g&lt;/code&gt; flag to leave debugging symbols in your executable.  This will do things like leave variable names and line numbers in the executable so that GDB can show you what/were things are.&lt;/p&gt;

&lt;p&gt;
The commands that you will use the most are:&lt;/p&gt;&lt;dl&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_4.html#SEC17"&gt;help&lt;/a&gt;&lt;/dt&gt;

&lt;dt&gt;r &lt;i&gt;arguments&lt;/i&gt; (run)&lt;/dt&gt;
&lt;dd&gt;r 5&lt;/dd&gt;

&lt;dt&gt;file &lt;i&gt;executable&lt;/i&gt;&lt;/dt&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_7.html#SEC48"&gt;bt&lt;/a&gt; (backtrace)&lt;/dt&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_9.html#SEC61"&gt;p&lt;/a&gt; &lt;i&gt;expression&lt;/i&gt; (print)&lt;/dt&gt;
&lt;dd&gt;p argv[1]&lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_6.html#SEC33"&gt;b&lt;/a&gt; &lt;i&gt;file:symbol&lt;/i&gt; (breakpoint)&lt;/dt&gt;
&lt;dd&gt;b main.c:5&lt;/dd&gt;
&lt;dd&gt;b main.c:parseArgs&lt;/dd&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_6.html#SEC36"&gt;d&lt;/a&gt; (delete breakpoints)&lt;/dt&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_6.html#SEC43"&gt;s&lt;/a&gt; (step) [step-into]&lt;/dt&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_6.html#SEC43"&gt;n&lt;/a&gt; (next) [step-over]&lt;/dt&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_6.html#SEC43"&gt;c&lt;/a&gt; (continue)&lt;/dt&gt;

&lt;dt&gt;&lt;a href="http://sourceware.org/gdb/current/onlinedocs/gdb_7.html#SEC49"&gt;up&lt;/a&gt;&lt;/dt&gt;

&lt;dt&gt;q (quit)&lt;/dt&gt;

&lt;/dl&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-2731233427679603122?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2731233427679603122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2731233427679603122'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_03_01_archive.html#2731233427679603122' title='Using GDB'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-4015783791892717858</id><published>2008-03-06T12:29:00.000-08:00</published><updated>2008-03-18T13:27:35.065-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='segfault'/><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='debugging'/><title type='text'>Segmentation Faults and Bus Errors</title><content type='html'>&lt;p&gt;
A &lt;a href="http://en.wikipedia.org/wiki/Segmentation_fault"&gt;segmentation fault&lt;/a&gt; occurs when your program tries to access a region of memory in a way that is not permitted.  A &lt;a href="http://en.wikipedia.org/wiki/Bus_error"&gt;bus error&lt;/a&gt; occurs when your program tries to access a region of memory that does not exist, or it tries to access an unaligned region. See the wiki pages for examples.&lt;/p&gt;

&lt;p&gt;
SegFualts and BusErrors are runtime errors that come from accessing memory through pointers.  Java has protected you from these kinds of errors by abstracting pointers away from the programing language.  The only thing in Java that is similar is an &lt;a href="http://java.sun.com/javase/6/docs/api/java/lang/ArrayIndexOutOfBoundsException.html"&gt;ArrayIndexOutOfBounds&lt;/a&gt; exception that is raised when you try to &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.4"&gt;access an element&lt;/a&gt; that is not contained in an array. In C, buffer accesses are unchecked, so this may or may not raise a segmentation fault depending on how memory has been laid out by the compiler and OS. [Note: &lt;a href="http://en.wikipedia.org/wiki/Buffer_overflow"&gt;unchecked buffers&lt;/a&gt; are arguably the largest source of security bugs in C/C++.]&lt;/p&gt;

Consider the code:
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
int buff[3];
buff[3] = 8;&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
In Java, this would throw an ArrayIndexOutOfBounds. In C, it may or may not segfault.&lt;/p&gt;

&lt;p&gt;
If the buffer &lt;code&gt;buff&lt;/code&gt; was placed in memory next to a &lt;a href="http://en.wikipedia.org/wiki/Memory_page"&gt;memory page&lt;/a&gt; boundary it has a greater chance of raising a segfault.  The buffer &lt;code&gt;buff&lt;/code&gt; ends at &lt;code&gt;buff[2]&lt;/code&gt;; or rather &lt;code&gt;buff&lt;/code&gt; has been reserved space for 3 elements. If &lt;code&gt;buff[3]&lt;/code&gt; (or more precisely, &lt;code&gt;*(buff+3)&lt;/code&gt;, the 4th element) was on a read only page, then the assignment will generate a segfault (but a read would not). If it was on a page marked "no access," then either an attempt to read or to write would raise an error. If it was on a page that is marked as read/write, then no fault signal will be raised and the value 8 will overwrite whatever value was previously at that location.  This may be empty memory (so nothing bad will happen), another variable (the old variable contents will be lost), part of the call stack (possibly causing the program to "return" to an arbitrary location), or even a piece of instruction code.  If the value 8 was inputed from the user, this bug could allow a user to execute arbitrary code within the program.&lt;/p&gt;

&lt;p&gt;
In Java, these errors are always &lt;a href="http://en.wikipedia.org/wiki/Fail-fast"&gt;fail-stop&lt;/a&gt;, so they are easier to find.  In C, they may or may not be fail-stop depending on how memory is arranged, so they are harder to find.&lt;/p&gt;

&lt;p&gt;
Bonus Material: The libraries &lt;a href="http://en.wikipedia.org/wiki/Electric_Fence"&gt;Electric Fence&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Dmalloc"&gt;Dmalloc&lt;/a&gt; change the way in which pages are allocated to ensure that each buffer (in the heap) ends at a page boundary and that the following page is "no access."  The tool &lt;a href="http://en.wikipedia.org/wiki/Valgrind"&gt;Valgrind&lt;/a&gt; translates and instruments code to look for similar (and more) errors, and Die Hard (&lt;a href="http://portal.acm.org/citation.cfm?id=1133981.1134000"&gt;acm&lt;/a&gt;, &lt;a href="http://www.cs.umass.edu/~emery/pubs/diehard-journal-07.pdf"&gt;draft&lt;/a&gt;) takes a probabilistic approach for laying out memory to detect these errors.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-4015783791892717858?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/4015783791892717858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/4015783791892717858'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_03_01_archive.html#4015783791892717858' title='Segmentation Faults and Bus Errors'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-5444024339135901365</id><published>2008-03-04T11:00:00.000-08:00</published><updated>2008-03-18T13:27:23.252-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='assert'/><category scheme='http://www.blogger.com/atom/ns#' term='Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='Bonus Material'/><title type='text'>Bonus: Automated Testing</title><content type='html'>&lt;p&gt;
One of the principles of &lt;a href="http://en.wikipedia.org/wiki/Agile_software_development"&gt;agile development&lt;/a&gt; is that your code should have completely &lt;a href="http://en.wikipedia.org/wiki/Test_automation"&gt;automated&lt;/a&gt; &lt;a href="http://en.wikipedia.org/wiki/Unit_testing"&gt;unit tests&lt;/a&gt;.  Your tests should require no interaction on your part, and they should require no interpretation; they should simply run and say &lt;code&gt;pass&lt;/code&gt; or &lt;code&gt;fail&lt;/code&gt;.  If your tests require input, then they are not repeatable.  If they require interpretation, then they are not automated.&lt;/p&gt;

&lt;p&gt;
By building an automated regression suite, you can use the tests as an enabler of change to allow you to test every change that you make to your code-base with little effort.  Ideally, you would make running tests as part of your build process. In most agile environments, the automated execution of tests is at least part of the &lt;a href="http://en.wikipedia.org/wiki/Continuous_integration"&gt;integration&lt;/a&gt; process (when you commit your code changes to the repository).&lt;/p&gt;

&lt;p&gt;
There are many testing tools available for agile developers.  The most popular is the xUnit framework. (Java: &lt;a href="http://www.junit.org/"&gt;JUnit&lt;/a&gt;, .NET: &lt;a href="http://www.nunit.org/index.php"&gt;NUnit&lt;/a&gt;, C++: &lt;a href="http://cppunit.sourceforge.net/cppunit-wiki"&gt;CppUnit&lt;/a&gt;).  These tools may be too heavyweight to learn just for testing your cs213 assignment, so we're going to see how to write an automated testing framework using some more basic primitives.  The C language includes a basic assertion package &lt;a href="http://www.cplusplus.com/reference/clibrary/cassert/assert.html"&gt;&amp;lt;assert.h&amp;gt;&lt;/a&gt;.&lt;/p&gt;

Consider the file:&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;testable.c:
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;assert.h&amp;gt;

int isBiggerThanFive(const int x) {
    return x &gt; 5;
}

int main() {

    assert(true == isBiggerThanFive(10));
    assert(true == isBiggerThanFive(6));
    assert(false == isBiggerThanFive(5));
    assert(false == isBiggerThanFive(4));
    assert(false == isBiggerThanFive(-10));

    return 0;
}&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
Compiling and running this file with &lt;code&gt;gcc -Wall testable.c -o testable; ./testable&lt;/code&gt; produces no output since all of the tests pass.&lt;/p&gt;

&lt;p&gt;
[Note: an &lt;a href="http://en.wikipedia.org/wiki/Assertion_%28computing%29"&gt;assertion&lt;/a&gt; is a statement that something is believed to be true, so all of your assertions should pass.]&lt;/p&gt;

&lt;p&gt;
If we add the incorrect assertion that &lt;code&gt;assert(false == isBiggerThanFive(50));&lt;/code&gt; to the bottom of our method, we get the output:&lt;/p&gt;

&lt;blockquote&gt;&lt;code&gt;testable: testable.c:15: main:
Assertion `0 == isBiggerThanFive(50)' failed.&lt;/code&gt;&lt;/blockquote&gt;

&lt;p&gt;
This is telling us that for the executable &lt;code&gt;testable&lt;/code&gt;, in the file &lt;code&gt;testable.c&lt;/code&gt;, on line &lt;code&gt;15&lt;/code&gt;, in method &lt;code&gt;main&lt;/code&gt;, our assertion that &lt;code&gt;false == isBiggerThanFive(50)&lt;/code&gt; failed. In this case, it's because our test is invalid, but it could have just as easily been an error in our implementation.&lt;/p&gt;

&lt;p&gt;
If we add another incorrect assertion to our test method &lt;code&gt;true == isBiggerThanFive(0)&lt;/code&gt; and re-build, we still get the same output.  There is no output from the second test, even though it should fail.  This is the expected behavior.&lt;/p&gt;

&lt;p&gt;
In unit testing, it's desired that a test harness fails on the first error for each test.  There is no sense in continuing to run a test after a failure since it means that the unit under test (or the test itself) has entered an unknown state, and therefore we can no longer make assertions (our belief was wrong).&lt;/p&gt;

&lt;p&gt;
Failing an &lt;code&gt;assert&lt;/code&gt; in C causes the program to abort execution.  This has the unfortunate effect that only one unit test can fail per executable (subsequent tests will not run if there was a failure).  The xUnit testing frameworks handle testing multiple units better, but &lt;code&gt;assert&lt;/code&gt; is good enough for testing small programs.&lt;/p&gt;

&lt;p&gt;
If you don't want to use the &lt;code&gt;assert&lt;/code&gt; library, you can write your test harnesses to use the &lt;code&gt;printf&lt;/code&gt; function, but all that you should be printing is &lt;code&gt;pass&lt;/code&gt; or &lt;code&gt;fail [details]&lt;/code&gt;; don't print things that require you to read them to verify that they are correct, as this will destroy any chance of test automation.&lt;/p&gt;


See also:
&lt;ul&gt;
 &lt;li&gt;&lt;a href="http://www.extremeprogramming.org/rules/unittests.html"&gt;Extreme Rules: Unit Tests&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href="http://radio.weblogs.com/0100190/stories/2002/07/25/sixRulesOfUnitTesting.html"&gt;Six Rules of Unit Testing&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href="http://www.acm.org/ubiquity/views/t_burns_1.html"&gt;Effective Unit Testing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-5444024339135901365?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/5444024339135901365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/5444024339135901365'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_03_01_archive.html#5444024339135901365' title='Bonus: Automated Testing'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-2313934540752673841</id><published>2008-02-26T23:38:00.000-08:00</published><updated>2008-02-28T11:10:54.839-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Compiling'/><category scheme='http://www.blogger.com/atom/ns#' term='Make'/><title type='text'>Make</title><content type='html'>&lt;p&gt;
A &lt;a href="http://en.wikipedia.org/wiki/Make_%28software%29"&gt;make&lt;/a&gt; file consists of a set of make rules.  Each rule has three parts: a target, a set of zero or more dependencies, and a list of zero or more commands.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
target: dependencies
(tab) command1
(tab) command2&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;
Make is invoked by specifying a target on the command line. If no target is given, then the first target in the file is taken as the default.  To compete a target, all dependencies are evaluated first.  If a dependency is another target in the makefile, then those dependent targets are evaluated.  If the dependency is not a target in the makefile, then the file system is checked for the existence of a file with the name of the dependency.  If the file has been modified more recently than the file matching the target, then the commands for that target are run.&lt;/p&gt;

Consider the files:&lt;code&gt;&lt;pre&gt;
main.c:
  #include "bar.h"
  void main() {
    bar();
  }
bar.c:
  #include "bar.h"
  void bar() {
  }
bar.h:
  void bar();&lt;/code&gt;&lt;/pre&gt;


The corresponding naive makefile would be:
&lt;code&gt;&lt;pre&gt;
main.exe: main.o bar.o
(tab) gcc main.o bar.o -o main.exe

main.o: main.c bar.h
(tab) gcc -c man.c

bar.o: bar.c bar.h
(tab) gcc -c bar.c

clean:
(tab) rm *.o main.exe&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;
Note: the "-o" flag specifies the name of the output file.  The "-c" flag tells the compiler to stop after the "compile" stage and before the "link" stage, generating an object file as the output.&lt;/p&gt;

&lt;p&gt;
Typing "make" on the command line would build this project and produce an executable named "main.exe".  Typing "make" again would do nothing since all of the dependencies would evaluate to false since none of the source files had changed.  Changing any of the source files and then typing "make" would cause only the required segments of the project to be recompiled.  The make utility knows what files need to be rebuilt based on the dependencies that we have given it, so it is important that these dependencies are correct.&lt;/p&gt;

&lt;p&gt;
See also: &lt;a href="http://www.gnu.org/software/make/manual/html_node/index.html"&gt;GNU make documentation&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-2313934540752673841?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2313934540752673841'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2313934540752673841'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#2313934540752673841' title='Make'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-2372395733557459425</id><published>2008-02-26T22:05:00.000-08:00</published><updated>2008-02-26T23:45:14.486-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Compiling'/><category scheme='http://www.blogger.com/atom/ns#' term='Linking'/><title type='text'>C: Declaration vs Definition</title><content type='html'>&lt;p&gt;
In computer science, there is a subtle but important &lt;a href="http://en.wikipedia.org/wiki/Declaration_%28computer_science%29"&gt;distinction between a declaration and a definition&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;
A declaration is a statement that something exists and what its characteristics are (name, and &lt;a href="http://en.wikipedia.org/wiki/Data_type"&gt;type&lt;/a&gt;).  A definition is saying what that thing is.&lt;/p&gt;

&lt;p&gt;
A function declaration in C looks like this: &lt;code&gt;void foo();&lt;/code&gt;.  Note the semicolon.  If we were to create a variable like that (&lt;code&gt;int i;&lt;/code&gt;) we would declare that the variable exists without assigning it a value.  When we declare a method without giving it a body, was are saying that this method exists somewhere, but we are not saying what it is. [Note: the variable in the example would still be "defined" since it was allocated space.]  In C, these are referred to as &lt;a href="http://en.wikipedia.org/wiki/Function_prototype"&gt;function prototypes&lt;/a&gt;.  In Java, they are called &lt;a href="http://en.wikipedia.org/wiki/Method_signature"&gt;method signatures&lt;/a&gt;.  They are functionally and conceptually the same.&lt;/p&gt;

&lt;p&gt;
To define a function, we must give it a body: &lt;code&gt;void foo() {...}&lt;/code&gt;.  This is saying what the function is and is a shorthand for the concept of saying: &lt;code&gt;void foo() &lt;b&gt;=&lt;/b&gt; {...}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;
You can think of all of the symbols in a program as entries in a dictionary.  The declarations are listing all of the words, and the definitions are assigning them meanings.  Because programs must be unambiguous (so that they can be machine executed), each word that we declare must have exactly one definition.  It would be ambiguous to have words with no definition (no meaning) or multiple definitions (multiple meanings) -- the machine would not know what to do.&lt;/p&gt;

&lt;p&gt;
Declarations can be used by the compiler to verify that function calls and function definitions match the exposed prototypes.  C can use the function declarations (separated from their definitions) to achieve &lt;a href="http://www.neu.edu.cn/cxsj/materal/otherc/imada/subsubsection3_14_4_1.html"&gt;seperate compilation&lt;/a&gt;.

&lt;/p&gt;
&lt;p&gt;
Separate compilation allows the differing functional units of a program to be compiled independently.  If the function "foo" calls the function "bar," the two of them can be compiled separately if they both know the prototype for the "bar" method.  Bar does not have to know anything about foo, and foo only has to know the declaration for bar.  The linker can link the definition of bar to the call to bar in the method foo.&lt;/p&gt;

&lt;p&gt;
Separate compilation allows for large projects to be compiled without having to recompile every function every time a change is made.  In the previous example, a change to foo would not require bar to be recompiled, and a change to bar would not require foo to be recompiled.  A change to the declaration of bar would require both foo and bar to be recompiled though.&lt;/p&gt;


&lt;p&gt;
See also: &lt;a href="http://www-ee.eng.hawaii.edu/~tep/EE150/book/chap14/subsection2.1.1.4.html"&gt;Variable Definition vs Declaration&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-2372395733557459425?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2372395733557459425'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2372395733557459425'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#2372395733557459425' title='C: Declaration vs Definition'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-7427152927868875360</id><published>2008-02-26T21:37:00.001-08:00</published><updated>2008-12-11T16:43:25.998-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Compiling'/><category scheme='http://www.blogger.com/atom/ns#' term='Linking'/><title type='text'>Stages of Compilation</title><content type='html'>&lt;p&gt;
Compilation in C has two major stages.  The first stage, "&lt;a href="http://en.wikipedia.org/wiki/Compiler"&gt;compiling&lt;/a&gt;" consists of preprocessing, lexical, syntactic, and semantic analysis, followed by optimization and code generation.  All of the compile errors that you are familiar with in Java come from this first stage.&lt;/p&gt;

&lt;p&gt;
The input to the first stage is your .c and .h source files, and the output is a .o object file.  The object file consists of two parts, the machine code equivalent of the source file, and a symbol table which lists all of the symbols (functions, variable, and a few other things) that this object files provides and requires.&lt;/p&gt;

&lt;p&gt;
The second stage of C compilation is "&lt;a href="http://en.wikipedia.org/wiki/Linker"&gt;linking&lt;/a&gt;."  A linker takes in a set of object files and "links" the symbols in the symbol tables together.  If one symbol table requires a "bar" function, the linker connects it to the object file that provides it.  The output from the linker is an executable file.&lt;/p&gt;

&lt;p&gt;
In Java, there is very little static linking -- most is done dynamically at run-time, so this will be a new source of errors for you. Fortunately, the errors generated by the linker are usually very straightforward.&lt;/p&gt;

&lt;p&gt;
There are two major sources of errors from the linker.  The first occurs when a symbol is required by one object file, but not provided by any other. In this case, the linker cannot make a required link, so it will generate a "symbol not defined" error.  [The closest equivalent in Java is a "NoClassDefFoundError," a "NoSuchFieldError," and a "NoSuchMethodError" at runtime.]  The other kind of error comes when a symbol has been provided by more than one object file.  In this case, the linker does not know which symbol to link to, so it will generate a "multiply defined symbol" error.&lt;/p&gt;

&lt;p&gt;
Linking errors for methods come about because of the distinction between method declaration and method definition in C, which is the subject of the next post.&lt;/p&gt;

&lt;a href="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R8T3QewtKVI/AAAAAAAAACs/Y_0ATCLJvJA/s1600-h/compilation.png"&gt;&lt;img src="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R8T3QewtKVI/AAAAAAAAACs/Y_0ATCLJvJA/s400/compilation.png" border="1" alt=""id="BLOGGER_PHOTO_ID_5171530134635489618" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-7427152927868875360?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/7427152927868875360'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/7427152927868875360'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#7427152927868875360' title='Stages of Compilation'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_pBSp-BH_Ayo/R8T3QewtKVI/AAAAAAAAACs/Y_0ATCLJvJA/s72-c/compilation.png' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-5957617161906975612</id><published>2008-02-12T12:02:00.000-08:00</published><updated>2008-02-12T13:26:24.053-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='while loop'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Branching'/><title type='text'>Branches (While Loop - !0)</title><content type='html'>&lt;p&gt;
Consider the code block:&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
int x = 0;

while(x &lt;= 10) {
  x++;

}
x += 4;

while(x &gt; 10) {
  x--;
}
x -= 4;  &lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;
When we have a loop condition that involves a non-zero argument, we need to re-express that condition in terms of a zero argument.  The code block above translates to:&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
int x = 0;

while(x -10 &lt;= 0) {         // or [ 0 &lt;= 10 - x]
  x++;
}
x += 4;

while(x -10 &gt; 0) {          // or [ 0 &gt; 10 - x]
  x--;
}
x -= 4;  &lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;
Converting to assembly gives:&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
0:   ld $0x0000, r0       # x = 0

# first loop, false conditional at top
2:   ld $0xfffffff6, r1   # r1 = -10
8:   mov r0, r2            
     add r1, r2           # r2 = x - 10
     bgt r2, 0x0012       # if x-10 &gt; 0, x &gt; 10, !(x &lt;= 10) goto +3

     inc r0               # x++
     br 0x0008            # goto -4

12:  add $4, r0           # x += 4

# second loop, true conditional at bottom
14:  ld $0xfffffff6, r1   # r1 = -10
1a:  br 0x0022            # goto +4
1c:  dec r0               # x--;

     mov r0, r2
     add r1, r2           # r2 = x - 10;
22:  bgt r2, 0x001c       # if x-10 &gt; 0, x &gt; 10 goto -3

     add $-4, r0          # x -= 4  &lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;
Note: the condition being evaluated in each case is identical, it is just treated differently.  In one block, it is used as a condition to exit the loop; in the other, it is used as a condition to continue the loop.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-5957617161906975612?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/5957617161906975612'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/5957617161906975612'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#5957617161906975612' title='Branches (While Loop - !0)'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-466307337344026405</id><published>2008-02-07T11:58:00.000-08:00</published><updated>2008-02-07T14:31:48.157-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Switch Statement'/><category scheme='http://www.blogger.com/atom/ns#' term='Bonus Material'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Branching'/><title type='text'>Bonus: Advanced Branching (Switch Statement)</title><content type='html'>&lt;p&gt;Consider a switch statement:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
switch(r0) {
  case 0: ~~~0;
          break;
  case 1: ~~~1;
          break;
  ...
  case 0xf: ~~~f;
            break;
  default:
            ~~~default;
}
~~~done&lt;/pre&gt;&lt;/code&gt;

&lt;h3&gt;If / Else Naive Implementation&lt;/h3&gt;

&lt;code&gt;&lt;pre&gt;
  0: rr move r1, r0     # r1 = r0
     beq r1, 0x??0      # goto "0" case
     dec r1             # r1 = r0 - 1
     beq r1, 0x??1      # goto "1" case
     ...
     dec r1             # r1 = r0 - f
     beq r1, 0x??f      # goto "f" case
     ~~~default         # execute "default" case
     br 0x???           # exit
??0: ~~~0               # execute "0" case
     br 0x???           # exit
??1: ~~~1               # execute "1" case
     br 0x???           # exit
...
??f: ~~~f               # execute "f" case
???: ~~~done            # outside the switch&lt;/pre&gt;&lt;/code&gt;

&lt;pre&gt;
Case     # conditions evaluated      # branches taken
  0                 1                      2
  1                 2                      2
 ..
  e                15                      2
  f                16                      1
 &lt;0                17                      1
 &gt;f                17                      1
Sum:              170                     31&lt;/pre&gt;

&lt;p&gt;
Every additional case adds progressively more and more to the number of conditions evaluated.  The number of branches taken remains constant for each execution path.&lt;/p&gt;

&lt;h3&gt;Switch / Jump Table Implementation&lt;/h3&gt;

&lt;code&gt;&lt;pre&gt;
   0:  move r1, r0          # r1 = r0
       dec r1               
       bgt r1 0x08          # ensure &gt;=0 (in range)
       br "def"             # if not, goto "default" case
   8:  move r1, r0          # r1 = r0
       ld $0xFFFFFFF1, r2   # r2 = -f
       add r2, r1           # r1 = r0 - f
       bgt r1 "def"         # goto "default" case if &gt; f
       move r1, r0          # r1 = r0 (and r1 is in range)
       ld $0x00001000, r2   # r2 = &amp; jump table
       jmp *(4*r1, r2)      # jump to the right case
"c0":  ~~~0                 # case "0"
       br "done"            # exit
"c1":  ~~~1                 # case "1"
       br "done"            # exit
...
"cf":  ~~~f                 # case "f"
       br "done"            # exit
"def": ~~~~default         # "default" case
"done" ~~~~done            # outside the switch
...
1000:  &amp;"c0"    # jump table, filled with addresses of cases
       &amp;"c1"
       ...
       &amp;"cf"&lt;/pre&gt;&lt;/code&gt;

&lt;pre&gt;
Case     # conditions evaluated      # branches taken
  0                 3                      3
  1                 3                      3
 ..
  e                 3                      3
  f                 3                      3
 &lt;0                 1                      1
 &gt;f                 2                      1
Sum:               48                     47&lt;/pre&gt;

&lt;p&gt;
More cases do not grow the number of conditions evaluated per path, and the number of branches taken per path remains constant.&lt;/p&gt;

&lt;p&gt;
Special Note: the number of branches taken in the above example is higher than that of the if/else example due to the branch on the "&gt;0" case.  This branch can be eliminated by reversing the test:&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
   0:  move r1, r0          # r1 = r0
       not r1               # 
       inc r1               # r1 = -r0
       bgt r1 "def"         # goto "default" if &lt;0
   8:  move r1, r0
       ld $0xFFFFFFF1, r2   # r2 = -f
       add r2, r1           # r1 = r0 - f
       bgt r1 "def"         # goto "default" case if &gt; f
       move r1, r0          # r1 = r0 (and r1 is in range)
       ld $0x00001000, r2   # r2 = &amp; jump table
       jmp *(4*r1, r2)      # jump to the right case
...&lt;/pre&gt;&lt;/code&gt;

&lt;pre&gt;
Case     # conditions evaluated      # branches taken
  0                 3                      2
  1                 3                      2
 ..
  e                 3                      2
  f                 3                      2
 &lt;0                 1                      1
 &gt;f                 2                      1
Sum:               48                     32&lt;/pre&gt;

&lt;p&gt;
For more information, see &lt;a href="http://webster.cs.ucr.edu/AoA/Windows/HTML/LowLevelControlStructsa2.html#1000309"&gt;Implementing Common Control Structures in Assembly Language&lt;/a&gt; and/or &lt;a href="http://www.eventhelix.com/RealtimeMantra/Basics/CToAssemblyTranslation3.htm"&gt;C to Assembly Translation III&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-466307337344026405?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/466307337344026405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/466307337344026405'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#466307337344026405' title='Bonus: Advanced Branching (Switch Statement)'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-6323732540314630206</id><published>2008-02-07T09:58:00.000-08:00</published><updated>2008-02-07T10:57:35.892-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='while loop'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Branching'/><title type='text'>Branches (While Loop - false)</title><content type='html'>&lt;code&gt;&lt;pre&gt;
while(x != 0 ) {
  ~~~i
  ~~~i
  ~~~i
}
~~~o
&lt;/pre&gt;&lt;/code&gt;

&lt;table border="1"&gt;&lt;tr&gt;
  &lt;th&gt;Address&lt;/th&gt;
  &lt;th&gt;Machine&lt;/th&gt;
  &lt;th&gt;Assembly&lt;/th&gt;
  &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td colspan="3"&gt;assume r0 = x&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;0:&lt;/td&gt;
  &lt;td&gt;9005&lt;/td&gt;
  &lt;td&gt;beq r0, 0x10&lt;/td&gt;
  &lt;td&gt;branch to exit&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;2:&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td rowspan="3"&gt;true case&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;8:&lt;/td&gt;
  &lt;td&gt;80FC&lt;/td&gt;
  &lt;td&gt;br 0x00&lt;/td&gt;
  &lt;td&gt;loop&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;10:&lt;/td&gt;
  &lt;td&gt;~~~o&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;exited loop&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;
Note 1: 2 branches total, 1 branch taken on critical path.&lt;/p&gt;

&lt;p&gt;
Note 2: conditional at top.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-6323732540314630206?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/6323732540314630206'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/6323732540314630206'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#6323732540314630206' title='Branches (While Loop - false)'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-4373194997199085708</id><published>2008-02-05T16:19:00.000-08:00</published><updated>2008-02-07T10:57:18.046-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='while loop'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Branching'/><title type='text'>Branches (While Loop - true)</title><content type='html'>&lt;code&gt;&lt;pre&gt;
while( x == 0 ) {
 ~~i
 ~~i
 ~~i
}
~~ o
&lt;/pre&gt;&lt;/code&gt;

&lt;h3&gt;Naive / Literal&lt;/h3&gt;

&lt;table border="1"&gt;&lt;tr&gt;
  &lt;th&gt;Address&lt;/th&gt;
  &lt;th&gt;Machine&lt;/th&gt;
  &lt;th&gt;Assembly&lt;/th&gt;
  &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td colspan="3"&gt;assume r0 = x&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;0:&lt;/td&gt;
  &lt;td&gt;9002&lt;/td&gt;
  &lt;td&gt;beq r0, 0x04&lt;/td&gt;
  &lt;td&gt;conditional&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;2:&lt;/td&gt;
  &lt;td&gt;8005&lt;/td&gt;
  &lt;td&gt;br 0x0c&lt;/td&gt;
  &lt;td&gt;exit&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;4:&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td rowspan="3"&gt;true case&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;a:&lt;/td&gt;
  &lt;td&gt;80FB&lt;/td&gt;
  &lt;td&gt;br 0x00&lt;/td&gt;
  &lt;td&gt;loop&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;c:&lt;/td&gt;
  &lt;td&gt;~~~o&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;exited loop&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;
Note: 3 branches total, 2 branches in critical path.&lt;/p&gt;

&lt;h3&gt;Preferred&lt;/h3&gt;

&lt;table border="1"&gt;&lt;tr&gt;
  &lt;th&gt;Address&lt;/th&gt;
  &lt;th&gt;Machine&lt;/th&gt;
  &lt;th&gt;Assembly&lt;/th&gt;
  &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td colspan="3"&gt;assume r0 = x&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;0:&lt;/td&gt;
  &lt;td&gt;8004&lt;/td&gt;
  &lt;td&gt;br 0x08&lt;/td&gt;
  &lt;td&gt;prime (need to check condition)&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;2:&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td rowspan="3"&gt;true case&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~i&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;8:&lt;/td&gt;
  &lt;td&gt;90FD&lt;/td&gt;
  &lt;td&gt;beq r0, 0x02&lt;/td&gt;
  &lt;td&gt;loop conditional&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;~~~o&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;exited loop&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;
Note 1: 2 branches total, 1 branch in critical path.&lt;/p&gt;

&lt;p&gt;
Note 2: conditional at bottom.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-4373194997199085708?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/4373194997199085708'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/4373194997199085708'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#4373194997199085708' title='Branches (While Loop - true)'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-1589173013523294231</id><published>2008-02-05T14:00:00.000-08:00</published><updated>2008-02-06T13:10:31.419-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='if statement'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Branching'/><title type='text'>Branches (If Statements)</title><content type='html'>&lt;h3&gt;If without Else&lt;/h3&gt;

&lt;code&gt;&lt;pre&gt;
if( x == 0 ) {
  x = 1;
}
x++;
&lt;/pre&gt;&lt;/code&gt;

&lt;table border="1"&gt;&lt;tr&gt;
  &lt;th&gt;Address&lt;/th&gt;
  &lt;th&gt;Machine&lt;/th&gt;
  &lt;th&gt;Assembly&lt;/th&gt;
  &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td colspan="3"&gt;assume r0 = x&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;0:&lt;/td&gt;
  &lt;td&gt;9002&lt;/td&gt;
  &lt;td&gt;beq r0, 0x0004 &lt;/td&gt;
  &lt;td&gt;if true, skip false&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;2:&lt;/td&gt;
  &lt;td&gt;8004&lt;/td&gt;
  &lt;td&gt;br 0x000a&lt;/td&gt;
  &lt;td&gt;false case, skip true&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;4:&lt;/td&gt;
  &lt;td&gt;0000 0000 0001&lt;/td&gt;
  &lt;td&gt;ld $0x0001, r0&lt;/td&gt;
  &lt;td&gt;true case&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;a:&lt;/td&gt;
  &lt;td&gt;6300&lt;/td&gt;
  &lt;td&gt;inc r0&lt;/td&gt;
  &lt;td&gt;after if block&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;
Note: the false case comes first since we branch somewhere else when true.&lt;/p&gt;

&lt;h3&gt;If with Else&lt;/h3&gt;

&lt;code&gt;&lt;pre&gt;
if( x == 0 ) {
  x = 1;
} else {
  x = 2;
}
x++;&lt;/pre&gt;&lt;/code&gt;

&lt;table border="1"&gt;&lt;tr&gt;
  &lt;th&gt;Address&lt;/th&gt;
  &lt;th&gt;Machine&lt;/th&gt;
  &lt;th&gt;Assembly&lt;/th&gt;
  &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td colspan="3"&gt;assume r0 = x&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;0:&lt;/td&gt;
  &lt;td&gt;9005&lt;/td&gt;
  &lt;td&gt;beq r0, 0x000a&lt;/td&gt;
  &lt;td&gt;if true, skip false&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;2:&lt;/td&gt;
  &lt;td&gt;0000 0000 0002&lt;/td&gt;
  &lt;td&gt;ld $0x0002, r0&lt;/td&gt;
  &lt;td&gt;false case&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;8:&lt;/td&gt;
  &lt;td&gt;8004&lt;/td&gt;
  &lt;td&gt;br 0x0010&lt;/td&gt;
  &lt;td&gt;skip true&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;a:&lt;/td&gt;
  &lt;td&gt;0000 0000 0001&lt;/td&gt;
  &lt;td&gt;ld $0x0001, r0&lt;/td&gt;
  &lt;td&gt;true case&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;10:&lt;/td&gt;
  &lt;td&gt;6300&lt;/td&gt;
  &lt;td&gt;inc r0&lt;/td&gt;
  &lt;td&gt;after if block&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-1589173013523294231?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/1589173013523294231'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/1589173013523294231'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#1589173013523294231' title='Branches (If Statements)'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-2214092700006755070</id><published>2008-02-05T10:22:00.000-08:00</published><updated>2008-02-05T13:59:22.996-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='pointers'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Structures'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Arrays'/><title type='text'>Structures in Machine Memory</title><content type='html'>&lt;code&gt;&lt;pre&gt;
typedef struct {
  int a;
  int b;
} T;

T foo;
T bar[2];
T* h = (T*) malloc( 3* sizeof(T) );  // reserve heap space
&lt;/pre&gt;&lt;/code&gt;

&lt;table border="1"&gt;&lt;tr&gt;
  &lt;th&gt;address&lt;/th&gt;
  &lt;th&gt;value&lt;/th&gt;
  &lt;th&gt;offset&lt;/th&gt;
  &lt;th&gt;c name&lt;/th&gt;
  &lt;th&gt;region&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;0:&lt;/td&gt;
  &lt;td&gt;~~~~~&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;program&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;...&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;1000:&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;00&lt;/td&gt;
  &lt;td&gt;foo.a&lt;/td&gt;
  &lt;td rowspan="7"&gt;global variables&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;01&lt;/td&gt;
  &lt;td&gt;foo.b&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;1008:&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;00&lt;/td&gt;
  &lt;td&gt;bar[0].a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;01&lt;/td&gt;
  &lt;td&gt;bar[0].b&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;02&lt;/td&gt;
  &lt;td&gt;bar[1].a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;03&lt;/td&gt;
  &lt;td&gt;bar[1].b&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;1018:&lt;/td&gt;
  &lt;td&gt;0000 2000&lt;/td&gt;
  &lt;td&gt;00&lt;/td&gt;
  &lt;td&gt;h&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;...&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;2000:&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;00&lt;/td&gt;
  &lt;td&gt;h[0].a&lt;/td&gt;
  &lt;td rowspan="6"&gt;heap&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;01&lt;/td&gt;
  &lt;td&gt;h[0].b&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;02&lt;/td&gt;
  &lt;td&gt;h[1].a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;03&lt;/td&gt;
  &lt;td&gt;h[1].b&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;04&lt;/td&gt;
  &lt;td&gt;h[2].a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;???? ????&lt;/td&gt;
  &lt;td&gt;05&lt;/td&gt;
  &lt;td&gt;h[2].b&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-2214092700006755070?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2214092700006755070'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2214092700006755070'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#2214092700006755070' title='Structures in Machine Memory'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-830457347513890556</id><published>2008-02-04T23:07:00.000-08:00</published><updated>2008-02-05T22:53:26.240-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><category scheme='http://www.blogger.com/atom/ns#' term='Branching'/><title type='text'>Gold Machine's Branching Instructions</title><content type='html'>&lt;table border=1&gt;&lt;tr&gt;
  &lt;th&gt;opcode&lt;/th&gt;
  &lt;th&gt;format&lt;/th&gt;
  &lt;th&gt;semantics&lt;/th&gt;
  &lt;th&gt;example&lt;/th&gt;
  &lt;th&gt;example assembly (RISC)&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;branch&lt;/th&gt;
  &lt;td&gt;8-oo&lt;/td&gt;
  &lt;td&gt;pc = pc-2 + 2*o&lt;/td&gt;
  &lt;td&gt;1000: 8006&lt;/td&gt;
  &lt;td&gt;br 0x100c&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;branch if equal&lt;/th&gt;
  &lt;td&gt;9roo&lt;/td&gt;
  &lt;td&gt;if r[r] == 0,&lt;br/&gt;
      pc = pc-2 + 2*o&lt;/td&gt;
  &lt;td&gt;1000: 9206&lt;/td&gt;
  &lt;td&gt;beq r2, 0x100c&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;branch if greater&lt;/th&gt;
  &lt;td&gt;aroo&lt;/td&gt;
  &lt;td&gt;if r[r] &gt; 0,&lt;br/&gt;
      pc = pc-2 + 2*o&lt;/td&gt;
  &lt;td&gt;1000: a206&lt;/td&gt;
  &lt;td&gt;bgt r2, 0x100c&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;jump&lt;/th&gt;
  &lt;td&gt;b--- aaaaaaaa&lt;/td&gt;
  &lt;td&gt;pc = a&lt;/td&gt;
  &lt;td&gt;1000: b000 00008000&lt;/td&gt;
  &lt;td&gt;jmp 0x8000&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;get pc&lt;/th&gt;
  &lt;td&gt;6f-d&lt;/td&gt;
  &lt;td&gt;r[d] = pc-2&lt;/td&gt;
  &lt;td&gt;6f01&lt;/td&gt;
  &lt;td&gt;gpc r1&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;jump indirect&lt;/th&gt;
  &lt;td&gt;croo&lt;/td&gt;
  &lt;td&gt;pc = r[r] + 2*o&lt;/td&gt;
  &lt;td&gt;c103&lt;/td&gt;
  &lt;td&gt;jmp 0x3(r1)&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;jump indirect, b+o&lt;/th&gt;
  &lt;td&gt;droo&lt;/td&gt;
  &lt;td&gt;pc = m[ r[r] + 4*o ]&lt;/td&gt;
  &lt;td&gt;d103&lt;/td&gt;
  &lt;td&gt;jmp *0x3(r1)&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;jump indirect, index&lt;/th&gt;
  &lt;td&gt;eir-&lt;/td&gt;
  &lt;td&gt;pc = m r[r] + 4*r[i] ]&lt;/td&gt;
  &lt;td&gt;e120&lt;/td&gt;
  &lt;td&gt;jmp *(4*r1,r2)&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;
Note 1: All "branch" machine language instructions are relative to the current PC (though their assembly versions are absolute).  All "jump" instructions are absolute.&lt;/p&gt;

&lt;p&gt;
Note 2: the semantics for each branch instruction include a -2.  This is to undo the +2 increment to PC during the fetch stage.&lt;/p&gt;

&lt;p&gt;
Corollary to Note 2: a branch of "0" should make the branch instruction branch to itself.  So if you ever branch by 0, you will enter an infinite loop.  This simplifies branching forwards and backwards.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-830457347513890556?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/830457347513890556'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/830457347513890556'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_02_01_archive.html#830457347513890556' title='Gold Machine&apos;s Branching Instructions'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-2437396645758772416</id><published>2008-01-22T12:47:00.000-08:00</published><updated>2008-01-29T11:12:44.750-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='bitwise'/><title type='text'>Bitwise Operations</title><content type='html'>&lt;p&gt;
Given a number in hex, &lt;code&gt;0x1234&lt;/code&gt; &lt;br /&gt;
to extract an individual digit, you need to do bitwise manipulations.&lt;/p&gt;

WLOG, extract the third lowest digit (third from the right):&lt;pre&gt;
int x = 0x1&lt;b&gt;2&lt;/b&gt;34;
int third = (x &gt;&gt; (2*4)) &amp; 0xf;
assert (third == 0x2);&lt;/pre&gt;

&lt;p&gt;
The "&gt;&gt;" &lt;a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5121"&gt;shift operator&lt;/a&gt; moves the number to the right by the specified number of bits. So:&lt;pre&gt;
   01001000110100b          0x1234
&gt;&gt;              8        &gt;&gt; 0x0008
------------------       ---------
           010010b          0x0012&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;
The "&amp;" &lt;a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5233"&gt;integer bitwise operator&lt;/a&gt; masks the number. So:&lt;pre&gt;
  00010010b           0x0012
&amp; 00001111b         &amp; 0x000f
-----------         --------
  00000010b           0x0002&lt;/pre&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-2437396645758772416?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2437396645758772416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2437396645758772416'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_01_01_archive.html#2437396645758772416' title='Bitwise Operations'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-2263902663649227033</id><published>2008-01-22T12:38:00.000-08:00</published><updated>2008-01-24T10:19:37.834-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='pointers'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><title type='text'>Pointer Example</title><content type='html'>&lt;table border=1&gt;&lt;tr&gt;
 &lt;th&gt;C&lt;/th&gt;
 &lt;th&gt;Gold Assembly&lt;/th&gt;
 &lt;th&gt;Gold Machine&lt;/th&gt;
 &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int a = -1;&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;1000: ffffffff&lt;/td&gt;
 &lt;td&gt;&amp;a = 1000&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int b = -1;&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;1004: ffffffff&lt;/td&gt;
 &lt;td&gt;&amp;b = 1004&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int* p = &amp;a;&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;2000: 00001000&lt;/td&gt;
 &lt;td&gt;&amp;p = 2000&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;void foo() {&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;*p = 5;&lt;/td&gt;
 &lt;td&gt;ld $0x5, r0 &lt;br/&gt;
     ld $0x2000, r1 &lt;br /&gt;
     ld 0x0(r1), r2 &lt;br /&gt;
     st r0, 0x0(r2)&lt;/td&gt;
 &lt;td&gt;0000 0000 0005 &lt;br/&gt;
     0100 0000 2000 &lt;br /&gt;
     1012 &lt;br /&gt;
     3002&lt;/td&gt;
 &lt;td&gt;r0 ~ *p &lt;br /&gt;
     r1 = &amp;p &lt;br /&gt;
     r2 = p &lt;br /&gt;
     *p = *p&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;p = &amp;c;&lt;/td&gt;
 &lt;td&gt;ld $0x1004, r2 &lt;br /&gt;
     st r2, 0x0(r1)&lt;/td&gt;
 &lt;td&gt;0200 0000 1004&lt;br /&gt;
     3201&lt;/td&gt;
 &lt;td&gt;r2 = &amp;c &lt;br /&gt;
     p = &amp;c&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;*p = 8;&lt;/td&gt;
 &lt;td&gt;ld $0x8, r0 &lt;br /&gt;
     st r0, 0x0(r2)&lt;/td&gt;
 &lt;td&gt;0000 0000 0008 &lt;br /&gt;
     3002&lt;/td&gt;
 &lt;td&gt;r0 ~ *p &lt;br /&gt;
     *p = *p&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;}&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

TODO: insert slide show&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-2263902663649227033?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2263902663649227033'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/2263902663649227033'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_01_01_archive.html#2263902663649227033' title='Pointer Example'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-1373035201259133297</id><published>2008-01-15T16:33:00.001-08:00</published><updated>2008-12-11T16:43:26.085-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><title type='text'>Tracing Machine Execution</title><content type='html'>&lt;script type="text/javascript"&gt;

function SlideShow(aPlace) {
  this.place = aPlace;
  this.images = new Array();
  this.cur = -1;

  this.add = SlideShow_addImage;
  this.next = SlideShow_next;
  this.prev = SlideShow_prev;
}

function SlideShow_addImage(imgsrc) {
  var img = new Image();
  img.src = imgsrc;
  this.images[this.images.length] = img;
}

function SlideShow_next() {
 this.cur = (this.cur+1) % this.images.length;
 document[this.place].src = this.images[this.cur].src;
}

function SlideShow_prev() {
 this.cur = (this.cur-1) % this.images.length;
 if(this.cur &lt; 0) this.cur = 0;
 document[this.place].src = this.images[this.cur].src;
}

&lt;/script&gt;



&lt;h3&gt;Short Example&lt;/h3&gt;

&lt;img name="slide1" src="" border="1" /&gt;&lt;br /&gt;
&lt;script type="text/javascript"&gt;
var s1 = new SlideShow("slide1");
s1.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42V7PCMbSI/AAAAAAAAAAM/KpJOgzYYjQs/s1600/gold+machine+short+1.png");
s1.add("http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42V7fCMbTI/AAAAAAAAAAU/9xeNXMcXy2Q/s1600/gold+machine+short+2.png");
s1.add("http://2.bp.blogspot.com/_pBSp-BH_Ayo/R42V7vCMbUI/AAAAAAAAAAc/pdXcIIBJ3pU/s1600/gold+machine+short+3.png");
s1.add("http://2.bp.blogspot.com/_pBSp-BH_Ayo/R42V7vCMbVI/AAAAAAAAAAk/t3QhkgdCLFA/s1600/gold+machine+short+4.png");
s1.add("http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42V7_CMbWI/AAAAAAAAAAs/aJvz6XITsv8/s1600/gold+machine+short+5.png");
s1.add("http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42WD_CMbXI/AAAAAAAAAA0/PFL-CgplSOo/s1600/gold+machine+short+6.png");
s1.add("http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42WD_CMbYI/AAAAAAAAAA8/iSEFSM7vBBM/s1600/gold+machine+short+7.png");
s1.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WEPCMbZI/AAAAAAAAABE/Ufo-qkjSlKo/s1600/gold+machine+short+8.png");
s1.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WEPCMbaI/AAAAAAAAABM/34j1y6UDRYI/s1600/gold+machine+short+9.png");
s1.add("http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42WEfCMbbI/AAAAAAAAABU/p3KEctR1Qj0/s1600/gold+machine+short+10.png");
s1.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WKPCMbcI/AAAAAAAAABc/fGSD3exmUq0/s1600/gold+machine+short+11.png");
s1.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WKPCMbdI/AAAAAAAAABk/Ma8yv-wKpvk/s1600/gold+machine+short+12.png");
s1.add("http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42WKfCMbeI/AAAAAAAAABs/W2YkFeO_lJo/s1600/gold+machine+short+13.png");
s1.next();
&lt;/script&gt;
&lt;a href="#slide1" onClick="s1.prev()"&gt;previous&lt;/a&gt;
&lt;a href="#slide1" onClick="s1.next()"&gt;next&lt;/a&gt;
&lt;p&gt;
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42V7PCMbSI/AAAAAAAAAAM/KpJOgzYYjQs/s1600-h/gold+machine+short+1.png"&gt;1&lt;/a&gt;,
&lt;a href="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42V7fCMbTI/AAAAAAAAAAU/9xeNXMcXy2Q/s1600-h/gold+machine+short+2.png"&gt;2&lt;/a&gt;,
&lt;a href="http://2.bp.blogspot.com/_pBSp-BH_Ayo/R42V7vCMbUI/AAAAAAAAAAc/pdXcIIBJ3pU/s1600-h/gold+machine+short+3.png"&gt;3&lt;/a&gt;,
&lt;a href="http://2.bp.blogspot.com/_pBSp-BH_Ayo/R42V7vCMbVI/AAAAAAAAAAk/t3QhkgdCLFA/s1600-h/gold+machine+short+4.png"&gt;4&lt;/a&gt;,
&lt;a href="http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42V7_CMbWI/AAAAAAAAAAs/aJvz6XITsv8/s1600-h/gold+machine+short+5.png"&gt;5&lt;/a&gt;,
&lt;a href="http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42WD_CMbXI/AAAAAAAAAA0/PFL-CgplSOo/s1600-h/gold+machine+short+6.png"&gt;6&lt;/a&gt;,
&lt;a href="http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42WD_CMbYI/AAAAAAAAAA8/iSEFSM7vBBM/s1600-h/gold+machine+short+7.png"&gt;7&lt;/a&gt;,
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WEPCMbZI/AAAAAAAAABE/Ufo-qkjSlKo/s1600-h/gold+machine+short+8.png"&gt;8&lt;/a&gt;,
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WEPCMbaI/AAAAAAAAABM/34j1y6UDRYI/s1600-h/gold+machine+short+9.png"&gt;9&lt;/a&gt;,
&lt;a href="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42WEfCMbbI/AAAAAAAAABU/p3KEctR1Qj0/s1600-h/gold+machine+short+10.png"&gt;10&lt;/a&gt;,
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WKPCMbcI/AAAAAAAAABc/fGSD3exmUq0/s1600-h/gold+machine+short+11.png"&gt;11&lt;/a&gt;,
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42WKPCMbdI/AAAAAAAAABk/Ma8yv-wKpvk/s1600-h/gold+machine+short+12.png"&gt;12&lt;/a&gt;,
&lt;a href="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42WKfCMbeI/AAAAAAAAABs/W2YkFeO_lJo/s1600-h/gold+machine+short+13.png"&gt;13&lt;/a&gt;.&lt;/p&gt;



&lt;h3&gt;Store Example&lt;/h3&gt;

&lt;img name="slide2" src="" border="1" /&gt;&lt;br /&gt;
&lt;script type="text/javascript"&gt;
var s2 = new SlideShow("slide2");
s2.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42XaPCMbfI/AAAAAAAAAB0/n6VvjbH6NAU/s1600/gold+machine+store+01.png");
s2.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42XaPCMbgI/AAAAAAAAAB8/oHowfRnfH5M/s1600/gold+machine+store+02.png");
s2.add("http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42XafCMbhI/AAAAAAAAACE/VplDBenqc-k/s1600/gold+machine+store+03.png");
s2.add("http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42XafCMbiI/AAAAAAAAACM/jjLn2M-ziQA/s1600/gold+machine+store+04.png");
s2.add("http://2.bp.blogspot.com/_pBSp-BH_Ayo/R42XavCMbjI/AAAAAAAAACU/niCGtIrqHfs/s1600/gold+machine+store+05.png");
s2.add("http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42Xd_CMbkI/AAAAAAAAACc/BE42WvSnViM/s1600/gold+machine+store+06.png");
s2.add("http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42XePCMblI/AAAAAAAAACk/lEQasV5gLQM/s1600/gold+machine+store+07.png");
s2.next();
&lt;/script&gt;
&lt;a href="#slide2" onClick="s2.prev()"&gt;previous&lt;/a&gt;
&lt;a href="#slide2" onClick="s2.next()"&gt;next&lt;/a&gt;

&lt;p&gt;
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42XaPCMbfI/AAAAAAAAAB0/n6VvjbH6NAU/s1600-h/gold+machine+store+01.png"&gt;1&lt;/a&gt;,
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42XaPCMbgI/AAAAAAAAAB8/oHowfRnfH5M/s1600-h/gold+machine+store+02.png"&gt;2&lt;/a&gt;,
&lt;a href="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42XafCMbhI/AAAAAAAAACE/VplDBenqc-k/s1600-h/gold+machine+store+03.png"&gt;3&lt;/a&gt;,
&lt;a href="http://1.bp.blogspot.com/_pBSp-BH_Ayo/R42XafCMbiI/AAAAAAAAACM/jjLn2M-ziQA/s1600-h/gold+machine+store+04.png"&gt;4&lt;/a&gt;,
&lt;a href="http://2.bp.blogspot.com/_pBSp-BH_Ayo/R42XavCMbjI/AAAAAAAAACU/niCGtIrqHfs/s1600-h/gold+machine+store+05.png"&gt;5&lt;/a&gt;,
&lt;a href="http://3.bp.blogspot.com/_pBSp-BH_Ayo/R42Xd_CMbkI/AAAAAAAAACc/BE42WvSnViM/s1600-h/gold+machine+store+06.png"&gt;6&lt;/a&gt;,
&lt;a href="http://4.bp.blogspot.com/_pBSp-BH_Ayo/R42XePCMblI/AAAAAAAAACk/lEQasV5gLQM/s1600-h/gold+machine+store+07.png"&gt;7&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-1373035201259133297?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/1373035201259133297'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/1373035201259133297'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_01_01_archive.html#1373035201259133297' title='Tracing Machine Execution'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8197494524072729204.post-6859519201988610241</id><published>2008-01-15T11:51:00.000-08:00</published><updated>2008-01-17T10:15:54.923-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='Assembly'/><category scheme='http://www.blogger.com/atom/ns#' term='Machine'/><category scheme='http://www.blogger.com/atom/ns#' term='Gold'/><title type='text'>Mapping C to Assembly to Machine Language</title><content type='html'>&lt;h3&gt;Short Example&lt;/h3&gt;

&lt;table border=1&gt;&lt;tr&gt;
 &lt;th&gt;C&lt;/th&gt;
 &lt;th&gt;Gold Assembly&lt;/th&gt;
 &lt;th&gt;Gold Machine&lt;/th&gt;
 &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;void foo() {&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int a = 0;&lt;/td&gt;
 &lt;td&gt;ld $0x0, r1&lt;/td&gt;
 &lt;td&gt;0100 0000 0000&lt;/td&gt;
 &lt;td&gt;r1 ~ a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;a++;&lt;/td&gt;
 &lt;td&gt;inc r1&lt;/td&gt;
 &lt;td&gt;6301&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;a *= 2;&lt;/td&gt;
 &lt;td&gt;add r1, r1&lt;/td&gt;
 &lt;td&gt;6111&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;}&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;


&lt;h3&gt;Store Example&lt;/h3&gt;

&lt;table border=1&gt;&lt;tr&gt;
 &lt;th&gt;C&lt;/th&gt;
 &lt;th&gt;Gold Assembly&lt;/th&gt;
 &lt;th&gt;Gold Machine&lt;/th&gt;
 &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int a = 2;&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;1000: 0000 0002&lt;/td&gt;
 &lt;td&gt;&amp;a = 1000&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;void foo() {&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;ld $0x1000, r0&lt;br/&gt;
     ld 0x0(r0), r1&lt;/td&gt;
 &lt;td&gt;0: 0000 0000 1000&lt;br/&gt;
     1001&lt;/td&gt;
 &lt;td&gt;r0 = &amp;amp;a&lt;br/&gt;
     r1 = a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;a++;&lt;/td&gt;
 &lt;td&gt;inc r1&lt;/td&gt;
 &lt;td&gt;6301&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;a *= 2;&lt;/td&gt;
 &lt;td&gt;add r1, r1&lt;/td&gt;
 &lt;td&gt;6111&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;st r1, 0x0(r0)&lt;/td&gt;
 &lt;td&gt;3001&lt;/td&gt;
 &lt;td&gt;a = r1&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;}&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;


&lt;h3&gt;Array Example&lt;/h3&gt;

&lt;table border=1&gt;&lt;tr&gt;
 &lt;th&gt;C&lt;/th&gt;
 &lt;th&gt;Gold Assembly&lt;/th&gt;
 &lt;th&gt;Gold Machine&lt;/th&gt;
 &lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int[] a =&lt;br/&gt;
     {0,1,2,3}; &lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;1000: 0000 0000&lt;br/&gt;
     0000 0001&lt;br/&gt;
     0000 0002&lt;br/&gt;
     0000 0003&lt;/td&gt;
 &lt;td&gt;&amp;a[] = 1000&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;int b = -1;&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;2000: ffff ffff&lt;/td&gt;
 &lt;td&gt;&amp;b = 2000&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;void sum() {&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;b = 0;&lt;/td&gt;
 &lt;td&gt;ld $0x0, r2&lt;/td&gt;
 &lt;td&gt;0: 0200 0000 0000&lt;/td&gt;
 &lt;td&gt;r2 ~ b&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;ld $0x1000, r0&lt;/td&gt;
 &lt;td&gt;0000 0000 1000&lt;/td&gt;
 &lt;td&gt;r0 = &amp;a&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;b += a[0];&lt;/td&gt;
 &lt;td&gt;ld 0x0(r0), r1&lt;br/&gt;
     add r1, r2&lt;/td&gt;
 &lt;td&gt;1001&lt;br/&gt;
     6112&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;b += a[1];&lt;/td&gt;
 &lt;td&gt;ld 0x4(r0), r1&lt;br/&gt;
     add r1, r2&lt;/td&gt;
 &lt;td&gt;1101&lt;br/&gt;
     6112&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;b += a[2];&lt;/td&gt;
 &lt;td&gt;ld 0x8(r0), r1&lt;br/&gt;
     add r1, r2&lt;/td&gt;
 &lt;td&gt;1201&lt;br/&gt;
     6112&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;b += a[3];&lt;/td&gt;
 &lt;td&gt;ld 0xc(r0), r1&lt;br/&gt;
     add r1, r2&lt;/td&gt;
 &lt;td&gt;1301&lt;br/&gt;
     6112&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td&gt;ld $0x2000, r0&lt;br/&gt;
     st r2, 0x0(r0)&lt;/td&gt;
 &lt;td&gt;0000 0000 2000&lt;br/&gt;
     3002&lt;/td&gt;
 &lt;td&gt;r0 = &amp;b&lt;br/&gt;
     b = r2&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
 &lt;td&gt;}&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8197494524072729204-6859519201988610241?l=cpsc213.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/6859519201988610241'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8197494524072729204/posts/default/6859519201988610241'/><link rel='alternate' type='text/html' href='http://cpsc213.blogspot.com/2008_01_01_archive.html#6859519201988610241' title='Mapping C to Assembly to Machine Language'/><author><name>Jeremy H</name><uri>http://www.blogger.com/profile/03549304318485464635</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
