18. What happens when we run code?#
Important
We reviewed the solution to updating your assigmnment action and how to create issues for missing dates
18.1. Starting the CPU Emulator#
In your terminal:
cd nand2tetris/tools
and then
bash CPUEmulator.sh
CPUEmulator.bat
or in file explorer, Double click on the CPUEmulator.bat
file
and it will open a new window
For more on how the emulator works see the CPU Emulator Tutorial.
For much more detail about how this all works chapter 4 of the related text book has description of the machine code and assembly language.
18.2. Loading a program#
We’re not going to do project 5, which is to build a CPU, but instead to use the test programs designed to check if your personally build CPU works with the CPU Emulator they provide to understand a simple canonical CPU.
We’re going to use the test cases from the book’s project 5:
Load Program
Navigate to
nand2tetris/projects/05
18.3. How does the computer actually add constants?#
We’ll use add.hack
first.
This program adds constants, 2+3.
It is a program, assembly code that we are loading to the simulator’s ROM, which is memory that gets read only by the CPU.
Run the simulator and watch what each line of the program does.
Notice the following:
to compute with a constant, that number only exists in ROM in the instructions
to write a value to memory the address register first has to be pointed to what where in the memory the value will go, then the value can be sent there
The simulator has a few key parts:
address register the
A
program counter the
PC
the ALU
the D register
If you prefer to read, see section 5.2.1- 5.2.6 of nan2tetris book
This program the first instruction puts 2 in the address register from the instructions in ROM.
Then it moves the 2 through the ALU to the data register (D)
then it puts 3 on the address register
then it adds the numbers at D and A
then it puts 0 on the address register
then it write the output from the ALU (D) to memory (at the location indicated by the A register)
18.4. The max between two numbers#
To compute the maximum value max.hack
:
subtracts the two numbers
compares the difference to 0
jumps to different lines based on if it is >0 or not
assigns the address regiter a value accordingly
copies one of the two values to the new location
python
Python 3.11.4 (main, Jul 5 2023, 09:00:44) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a= 2
>>> b =3
>>> c =max(a,b)
>>> c
3
>>> exit()
18.5. File Permissions#
First, we will go back to seawulf
ssh -i ~/seawulf brownsarahm@seawulf.uri.edu
Last login: Thu Mar 28 12:33:16 2024 from 172.20.126.231
First lets make a file:
[brownsarahm@seawulf ~]$ nano hello.sh
with a simple script in it
echo "hello!"
If we want to run the file, we can use bash
directly,
[brownsarahm@seawulf ~]$ bash hello.sh
hello!
this is limited relative to calling our script in other ways.
One thing we could do is to run the script using ./
[brownsarahm@seawulf ~]$ ./hello.sh
-bash: ./hello.sh: Permission denied
By default, files have different types of permissions: read, write, and execute for different users that can access them. To view the permissions, we can use the -l
option of ls
.
[brownsarahm@seawulf ~]$ ls -la
total 138508
drwx------. 5 brownsarahm spring2022-csc392 4096 Apr 2 13:28 .
drwxr-xr-x. 503 root root 16384 Mar 25 16:47 ..
-rw-r--r--. 1 brownsarahm spring2022-csc392 231 Nov 24 2021 .bashrc
drwxr-xr-x. 2 brownsarahm spring2022-csc392 4096 Mar 28 13:33 compilec
-rw-r--r--. 1 brownsarahm spring2022-csc392 77426528 Jan 16 2018 dmel-all-r6.19.gtf
-rw-r--r--. 1 brownsarahm spring2022-csc392 25056938 Jan 25 2016 gene_association.fb
-rw-r--r--. 1 brownsarahm spring2022-csc392 14 Apr 2 13:28 hello.sh
-rw-r--r--. 1 brownsarahm spring2022-csc392 1625262 Jan 25 2016 SRR307023_1.fastq
-rw-r--r--. 1 brownsarahm spring2022-csc392 1625262 Jan 25 2016 SRR307023_2.fastq
-rw-r--r--. 1 brownsarahm spring2022-csc392 1625376 Jan 25 2016 SRR307024_1.fastq
drwx------. 2 brownsarahm spring2022-csc392 36 Oct 25 22:26 .ssh
-rw-r--r--. 1 brownsarahm spring2022-csc392 658 Apr 7 2020 .zshrc
For each file we get 10 characters in the first column that describe the permissions. The 3rd column is the username of the owner, the fourth is the group, then size date revised and the file name.
We are most interested in the 10 character permissions. The fist column indicates if any are directories with a d
or a -
for files. We have no directories, but we can create one to see this.
We can see in the bold line, that the first character is a d.
The next nine characters indicate permission to Read, Write, and eXecute a file. With either the letter or a -
for permissions not granted, they appear in three groups of three, three characters each for owner, group, anyone with access.
we moved the file to make the permissions list shorter
[brownsarahm@seawulf ~]$ mv hello.sh compilec/
[brownsarahm@seawulf ~]$ cd compilec/
[brownsarahm@seawulf compilec]$ ls -l
total 92
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 10032 Mar 28 13:19 demo
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 8360 Mar 28 13:10 demohello
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 8360 Mar 28 13:12 hello
-rw-r--r--. 1 brownsarahm spring2022-csc392 65 Mar 28 13:11 hello.c
-rw-r--r--. 1 brownsarahm spring2022-csc392 16865 Mar 28 12:49 hello.i
-rw-r--r--. 1 brownsarahm spring2022-csc392 1496 Mar 28 12:58 hello.o
-rw-r--r--. 1 brownsarahm spring2022-csc392 433 Mar 28 12:53 hello.s
-rw-r--r--. 1 brownsarahm spring2022-csc392 14 Apr 2 13:28 hello.sh
-rw-r--r--. 1 brownsarahm spring2022-csc392 474 Mar 28 13:14 help.c
-rw-r--r--. 1 brownsarahm spring2022-csc392 3040 Mar 28 13:19 help.o
-rw-r--r--. 1 brownsarahm spring2022-csc392 376 Mar 28 13:17 main.c
-rw-r--r--. 1 brownsarahm spring2022-csc392 3736 Mar 28 13:18 main.o
-rw-r--r--. 1 brownsarahm spring2022-csc392 5 Mar 28 13:33 name.txt
here we can compare files we were able to run (the ones we built on this system)
and see they have the x
permission.
To add execute permission, we can use chmod
[brownsarahm@seawulf compilec]$ chmod +x hello.sh
[brownsarahm@seawulf compilec]$ ls -l
total 92
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 10032 Mar 28 13:19 demo
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 8360 Mar 28 13:10 demohello
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 8360 Mar 28 13:12 hello
-rw-r--r--. 1 brownsarahm spring2022-csc392 65 Mar 28 13:11 hello.c
-rw-r--r--. 1 brownsarahm spring2022-csc392 16865 Mar 28 12:49 hello.i
-rw-r--r--. 1 brownsarahm spring2022-csc392 1496 Mar 28 12:58 hello.o
-rw-r--r--. 1 brownsarahm spring2022-csc392 433 Mar 28 12:53 hello.s
-rwxr-xr-x. 1 brownsarahm spring2022-csc392 14 Apr 2 13:28 hello.sh
-rw-r--r--. 1 brownsarahm spring2022-csc392 474 Mar 28 13:14 help.c
-rw-r--r--. 1 brownsarahm spring2022-csc392 3040 Mar 28 13:19 help.o
-rw-r--r--. 1 brownsarahm spring2022-csc392 376 Mar 28 13:17 main.c
-rw-r--r--. 1 brownsarahm spring2022-csc392 3736 Mar 28 13:18 main.o
-rw-r--r--. 1 brownsarahm spring2022-csc392 5 Mar 28 13:33 name.txt
now we have x permission on the hello.sh
and we can run it!
remember to close the server connection
exit
logout
Connection to seawulf.uri.edu closed.
18.6. Prepare for Next Class#
In fractionalbinary.md use 8 bits to represent the following numbers by using 4 bits as usual (8,4,2,1) and the other 4 bits are 1/2, 1/4, 1/8, 1/16th:
1.5
12.75
7.5625
15.9375
Add to your file some notes about the limitations of representing non integer values this way. How much would using more bits help with, what limitations are not resolved by adding more bits. For example, how could you represent .1?
18.7. Badges#
Run and examine how rect.hack and max.hack in the
nand2tetris/projects/05/
folder work. Make notes and answer the questions below in assemblyexplore.md.1. Write an excerpt of code in a high level compiled language that would compile into this `max.hack``. 1. What does rect.hack do? 1. What did you learn trying to figure out how it works?
File permissions are represented numerically often in octal, by transforming the permissions for each level (owner, group, all) as binary into a number. Add octal.md to your KWL repo and answer the following. Try to think through it on your own, but you may look up the answers, as long as you link to (or ideally cite using jupyterbook citations) a high quality source.
1. Describe how to transform the permissions [`r--`, `rw-`, `rwx`] to octal, by treating each set as three bits. 1. Transform the permission we changed our script to `rwxr-xr-x` to octal. 1. Which of the following would prevent a file from being edited by other people 777 or 755 and why?
File permissions are represented numerically often in octal, by transforming the permissions for each level (owner, group, all) as binary into a number. Add octal.md to your KWL repo and answer the following. Try to think through it on your own, but you may look up the answers, as long as you link to (or ideally cite using jupyterbook citations) a high quality source.
1. Describe how to transform the permissions [`r--`, `rw-`, `rwx`] to octal, by treating each set as three bits. 1. Transform the permission we changed our script to `rwxr-xr-x` to octal. 1. Which of the following would prevent a file from being edited by other people 777 or 755 and why?
Run and examine how rect.hack and max.hack in the
nand2tetris/projects/05/
folder work. Make notes and answer the questions below in assemblyexplore.md.1. What does rect.hack do? 2. What did you learn trying to figure out how it works? 2. Write an excerpt of code in a high level compiled language that would compile into this `max.hack``. Try writing multiple different versions.
18.8. Experience Report Evidence#
no separate evidence
18.9. Questions After Today’s Class#
18.9.1. Is there a way to do assembly like whats done on CPU emulator through a bash terminal? Is it more difficult to do so? Is there a better way to do so?#
Assembly is not interpreted, so it cannto be run interactively, and it has to be assembled to objet code and linked to run on your base computer.
18.9.2. Are there pratical uses for CPU emulator besides being used for testing and understanding?#
This CPU emulator is designed for learning. More complex hardware emulators can be developed for other devices and then are used for testing software on other devices.
18.9.3. Will we look at some low level languages?#
The hack
assembly we saw is one! However, learning more about other assembly languges is a good explore
18.9.4. what does JGT stand for?#
Jumpt if greater than
18.9.5. Is ram dedicated to display located on the GPU?#
Yes, in a system with a GPU (our emulator is a super simple computer witthout that) there is memory on the GPU with dedicated storage for what to display.
18.9.6. Is there a way to make .sh files automatic have X permissions?#
Not that I am aware of, but you can always run it with bash
18.9.7. Are there any useful applications of making your own assembly language?#
It could be more efficient, or it could be needed due to new hardware functionality. It could also be useful for teaching.
18.9.8. What is an emulator used for?#
An emulator allows you to see what a device does without having to have that actual device. In the case of the CPUEmulator we used today, it allows us to simulate runnign code on a simpler compute to get the basic ideas.
18.9.9. What are the exact three categories of users for file permissions?#
owner, group, world/any
18.9.10. What does ASM stand for?#
Assembly
18.9.11. Does the CPU use Assembly?#
It uses the object representation, not the assembly representation, but the assembly matches the operations the CPU can do. It is just easier for us to read assembly than object (binary).
18.9.12. What other components are in a CPU that are like a ALU?#
The other components are not super like an ALU, but the counter is the most similar.
18.9.13. where is this information useful?#
Understanding how your code runs can help debug and it can help you optmize. You will see more about that in CSC411, in this course our goal is to get you ready for that class.
18.9.14. Why does it make me give myself permissions for a bash script if I’m the one who created it?#
This is a good explore.
18.9.15. Are there any restrictions on changing specific restrictions on certain files ?#
Only the file permissions.