# HW0: Basic Circuits & Machine Code

CS 641 Homework, Dr. Lawlor

This homework is mostly background material--things you in theory already know.  Of course, theory and practice do tend to diverge!

## Part 1: Digital Logic Gates from FETs (Logisim)

Build an XOR gate from individual field effect transistors.  It may be useful to review the building logic gates lecture from CS 441.

XOR is a little more complex to implement than an AND or OR gate.  There are several ways to do it, but I recommend starting by building and debugging AND and OR gates, then implementing  XOR(A,B) = AND(OR(A,B), NAND(A,B)).
 A   B XOR OR NAND 0 0 0 0 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 0

Rather than deal with physical transistors, which have a bad habit of hiding in the carpet, killing themselves when hooked up backwards or spontaniously via static electricity, and costing actual money, I'd recommend building a virtual model.  My favorite free circuit simulation program is the digital circuit simulator Logisim, which does a decent job with FETs.

Turn in your Logisim .circ file (or other simulation output), and a screenshot of your XOR circuit, on Blackboard.  If you choose to build a physical model, bring it into class so we may all bask in your spare free time!

## Part 2: Writing x86 machine code

I'd like you to load up 7 into one register, load 3 into another register, and add the two registers.  The catch is I'd like this in x86 machine code, for use in this function:
`const unsigned char machinecode[]={	/* your comma-separated bytes of x86 machine code here! */};int foo(void) {	return ((int (*)())machinecode)();}`

(Try this in NetRun now!)

If you don't remember much x86 assembly (or never knew much!), you can learn a lot by writing some simple C or C++ code and watching the disassembly.  In particular, "register int x=3;" should nearly do the right thing as long as optimization is off.  Keep in mind that most disassembly already includes the machine code (on the left) as well as the assembler instructions (on the right).

Tons of x86 documentation is also available on my CS 301 assembly language homepage.