## Jupyter at Bryn Mawr College

Public notebooks: /services/public/dblank / CS240 Computer Organization / 2015-Fall / Assignments

# Assignment1: Welcome aboard!¶

## CS240Bryn Mawr CollegeProfessor Blank¶

Welcome aboard to CS240: Priciples of Computer Organization!

Due: Wednesday, Sep 2, 2015, 2:40pm. Print out, and bring to class.

This first assignment introduces a number of new things:

• Jupyter notebooks, like this one
• can enter text and code
• Two Machine instructions:
• 1111 0000 00100101 - halt
• Eight Registers:
• 000, 001, 002, etc.
• .ORIG
• .END
• Magic, meta-commands
• %exe
• %reg

Read chapter 1, and write down 6 questions for discussion in class on Wednesday, Sep 2:

• Three original questions: review-type questions, that cover the material in the chapter
• Three original questions: clarifying questions, that answer a real question that you may be confused about

## Our first Machine Language Program¶

For this, our "Hello, World" program in Machine Language, our goal is to:

• write a machine language program
• put it in memory location x3000
• add 1 to the current value in the first register, 000 also known as R0
• stop

We create a machine language program by putting machine language code in a Jupyter cell, and pressing Shift+Enter, or pressing the triangle button at top of page. This will assemble the code and place it into memory.

A machine language program always starts with where in memory the code should be placed, its "origin". We'll often use x3000 as a generally open place in memory, but it could be anywhere. We use the .ORIG directive.

Then, we enter all of the machine language instructions (see next).

Finally, we signal to the assembler that this is the end, using the .END directive.

### Instructions¶

For our first program, we will use two instructions: add and halt.

The add instruction always starts out with 0001. Then it is followed by the destination register, as source register, the number 1, and a 5-bit binary number.

In shorthand, it does this:

Destination Register <- Source Register + 5-bit binary number

#### Halt¶

The halt instruction is long, but specific:

1111 0000 00100101

There are variations of this command, but for now, we'll just use it as it is.

### Putting it all together¶

Now we just put the .ORIG, code, and .END together in a cell, and press Shift+Enter:

In [13]:
.ORIG x3000
0001 000 000 1 00001
1111 0000 00100101
.END

============================================================
Memory dump:
============================================================
x3000: x1021
x3001: xF025

============================================================
Registers:
============================================================
PC: x3002
N: 0 Z: 0 P: 1
R0: x0000 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x3002


There, you just assembled your first program! What you are seeing is:

• a "Memory dump" showing the two instructions, but now in hexidecimal format.
• the "registers" and their values. R0 through r7 should all be zero.

But we didn't actually run the program. To do that, we use a Jupyter Calysto LC3 magic, %exe. Enter %exe in a cell, and press Shift+Enter:

In [14]:
%exe

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1
R0: x0001 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x3002
============================================================
Computation completed
============================================================
Instructions: 2
Cycles: 15 (0.000008 milliseconds)


You just ran your first machine language program! What did it do? Just what we told it:

Destination Register (R0) <- Source Register (R0) + 5-bit binary number (00001)

So, if it worked correctly, you should see R0 has the value 1.

You can run it again:

In [15]:
%exe

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1
R0: x0002 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x3002
============================================================
Computation completed
============================================================
Instructions: 2
Cycles: 15 (0.000008 milliseconds)


And now R0 contains 2.

To reset the register, use:

%reg REGISTER VALUE

So, to reset register 000 to 0 use:

In [17]:
%reg 000 0

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1
R0: x0000 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x0000


You can continue to run it again, or set registers to different values.

## Questions¶

For the following questions, using only the add instruction, do the following: