02. Introduction to C, More About Unix, Application Installation and Logisim Setup (2)
02. Introduction to C, More About Unix, Application Installation and Logisim Setup (2)
The command installs a bunch of new packages including gcc, g++ and make.
● You may also want to install the manual pages about using GNU/Linux for
development:
benjamin@benjamin-ThinkPad-T570:~$ sudo apt-get install manpages-dev
● To validate that the GCC compiler is successfully installed, use the gcc --version
command which prints the GCC version:
benjamin@benjamin-ThinkPad-T570:~$ gcc --version
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
2
Compile the program by calling the standard C compiler program, gcc:
The gcc command takes a list of source files to compile (in this case, just hello.c) and
outputs the compiled executable (or a list of errors if the program does not compile). In the
above command, we are passing two flags in addition to the filename: -Wall (Warnings: all)
says to turn on all compiler warnings, and -o hello says we want the output executable file
to be named hello. If we omit the -o hello option, then gcc defaults to producing an
executable named a.out.
benjamin@benjamin-ThinkPad-T570:~$ ./hello
Note that the ./ indicates that we're running a program located in the current directory.
Output redirection allows you to save the output of a command in a file instead of writing
it in the terminal window. To do so, use > operator.
After the execution of the first command, the output appeared below the prompt (standard
output). In the case of output redirection, the effective user id (the output of whoami) is stored in
the textual file selfawareness.txt (with cat we checked the contents of file), Figure 1.
3
When operator > is used for redirection, if the file (where we want our output to appear)
already exists, its contents will be overwritten.
Example 2: Output our working directory in the same file as the effective user id from the
previous example.
We can notice that the contents of our file are overwritten (the effective user id is not there
anymore). To avoid this, use the >> operator, Figure 2.
Accessing Rights
Every file has owner, group, and other permissions that define what actions a specific user
can perform on file. The “other” permissions refers to permissions of any other user that is
not an owner nor within a group. To the information related to file permissions use
command ls -l (optionally name of directory of files you want to inspect), Figure 4.
Figure 4. Permissions
4
We can see that in the directory os there is one directory “thedirectory” and one .txt file
“thefile.txt”. Except that, the output has different access modes (the first column), name of
the owner, name of the group, date. In the first column we can see (for thedirectory)
drwxrwxr-x, where the d at the beginning states that it is a directory not a file (in case of
file there is just dash at that place); the following three letters rwx are the owner’s
permissions; the next three characters (rwx) arthat allows the users to either control the
flow of the command or to specify the input data for the command, and the final three
characters (r-x) are the permissions of everyone else. Those r, w, and x stand for read,
write, and execute (respectively).
Explore: Create a text file with and check out what the following programs do less, head,
tail, grep, wc. Note: You can also ask for help (--help, apropos, man).
Piping
Piping is a form of output redirection where the command output is used as an input of the
next command.
Example 3: Store the manual of less program in a file less_a_man.txt. Count how many
times is the word version used in this manual.
To do this we will first redirect the output of man less to the file less_a_man.txt using man less >
less_a_man.txt. After that we will pipe the output of grep to program wc: grep version
less_a_man.txt | wc, Figure 3.
Figure 3. Piping
Analyze what each of the three numbers in output means? Contemplate how to solve the
same problem without piping? (Use --help, man, apropos to enhance your contemplation.)
Introduction to Logisim
In this lab session, we will be diving into the world of digital circuits with the help of
Logisim—an incredibly interactive and visual tool that brings circuits to life! This
5
educational software is designed to make it easier for you to design and simulate digital
circuits, making complex concepts more tangible and fun to explore. Whether you're
building logic gates, flip-flops, or entire processors, Logisim will guide you through the
process, helping you understand how these systems work.
1.
Once everything is set up, you’ll be greeted with a window that’s ready to become your
creative playground. Let’s get ready to turn theory into reality by simulating the circuits
you’ll be designing!
Digital Circuits
6
Logical Gates
-Building blocks for digital circuits
-They make decisions based on the combination of digital signals that come from the
input.
-In most cases, we will deal with two inputs which generate a single output.
An example of logical gates in digital circuits (exclusive-or relationship between X and Y).
-In logical gates, we have two binary conditions: false (0) and true (1).
-Light switch analogy (when the switch is off, we can consider it to be false/0. Otherwise it
is true/1).
-Output depends on the type of the logical gate.
7
AND Gate
-True when all inputs are true, otherwise it is false.
8
OR Gate
-True if either one or both inputs are true. Otherwise it is false.
XOR Gate
-True if inputs are different.
-False if inputs are the same.
NOT Gate
-Known as an inverter, it is used to differentiate between different electronic inverters.
-The simplest gate because it reverses the logic state of the input.
-Has only one input.
9
NAND gate
-Behaves as an AND gate followed by a NOT gate.
-True if both inputs are false .
-False if both inputs are true.
NOR gate
-Behaves as OR gate followed by an inverter (NOT).
-True if both inputs are false.
-False if both inputs are true or different.
10
XNOR gate
-Behaves as XOR gate followed by an inverter (NOT).
-True if both inputs are the same.
-False if both inputs are different.
11
Logisim Demo
The blue section of the screen is the Menu bar. In the Menu bar, we have many different
options that allow the user to open different Logisim file, save the existing project file,
simulate the project, generate certain tables and conclusions from the circuit (will come to
this part later), as well as gain access to Logisim’s help center.
The green section of the screen contains some of the most common and important options
and tools which we will use during our circuit creation process. The options that you see
are the following:
12
Poke Tool - allows you to change values (1 or 0) that are sent to the circuit (mostly
used when sending values to the input pin).
Select Tool - allows you to select a certain part of the circuit (gate, pin, wire).
Text Tool - allows you to add a text box to the circuit’s canvas.
Input Pin - allows you to send a certain input to the circuit (is manipulated by the Poke
Tool).
NOT gate - important gate which implements logical negation. The gate works in the
way that it outputs the bit opposite of the bit that was sent to it.
AND gate - gate which implements logical conjunction. This means that we will get 1
as an output only if all inputs are equal to 1 as well as for 0.
OR gate - logic gate which accepts two or more inputs and based on the fact if they are
high, produces an output of 1 or ON.
The yellow part of the screen is the explorer pane. In the explorer pane, you can find all
different resources which you can use in your circuit development (different types of
circuits, gates, transistors, LED diodes etc.)
13
The orange part of the screen is the attribute table, which contains basic details about the
resource which you are using at the moment. Let us, for example, use the NOT Gate from
the Gates section. By clicking on the resource, you should be able to see the basic
information related to it.
14
The red part of the screen is the canvas, where you can draw your circuit. By simply
choosing any resource from the Explorer pane or one of them from the green section of
the screen, you can simply click on the canvas which will later draw your selected resource
on it.
15
Building Logisim Circuits
After we had gone through the Logisim software, let us now build one circuit. The circuit
which we will build is displayed below on the picture and depicts the relationship of x and
y in the form of x XOR y.
16
The goal of this task is to recreate the circuit in Logisim as well as to generate the truth
table for this circuit (also in the software).
The first step that you should take is to place the necessary gates to the canvas. In case
you are not clear how you can do it, you can follow the animation.
After we had placed the gates, let us now place the input and the output pins. Because we
have the exclusive or relationship between x and y, this means that we will have two input
pins (for x and y) as well as one output pin that will give us the respective result (if the
value of x and y differ, we will get 1 or 0 in the opposite case).
17
Now after we have placed all gates and pins, we are ready to wire them. In case your menu
bar does not have a wiring option, you can easily find it under Base > Wiring Tool. After
choosing the tool, you should connect the components by pulling the wire from the end of
one component to the end of the other one.
As you can see, all of our components are wired which means that we are now ready to
test our circuit. In order to test the circuit, we should select the Poke Tool and then click
on the input pins (which will change the value to either 1 and 0) and should pay attention
to the output pin in order to see whether our circuit satisfies the x XOR y relationship.
18
As visible in the animation above, we can conclude that our x XOR y circuit works
correctly. Now, in order to generate a truth table for this circuit, we can easily do that by
selecting Project > Analyze Circuit > Table.
19
TASKS:
1) Recreate the following circuit and generate its truth table in Logisim.
2) Draw the circuit and generate the truth table for the expression 𝐴𝐵 + 𝐶 in
Logisim.
3) Draw the circuit and generate the truth table for the expression 𝐴𝐵 + 𝐶 in
Logisim.
4) Draw the circuit and generate the truth table for the expression 𝐴 * (𝐵 + 𝐶) in
Logisim.
PRACTICE:
5) Draw the circuit and generate the truth table for the expression (𝐴𝐵 + 𝐶) * 𝐷 in
Logisim.
20
Challenge exercise:
1. On the lecture you have been working on a 3-input MUX logic circuit (if you don’t
remember you can refresh your knowledge by going through the lecture).
Your task is to implement the 4-input MUX logic circuit in Logisim software,
simulate and test it.
21