An app that contacts you to the authority in just 1 click!
Explore the docs Β»
Report Bug
Β·
Request Feature
Table of Contents
I have had that question since the first day I participated in Competitive Trading in high school. How can my laptop read those C++ or Python lines and just βdo itβ?
Or how does the computer know what is 1+1? (I initially thought somehow it searched the internet and sent us the result lol). But actually, every single thing comes from the Computer or the CPU.
In this project, I will build a computer from scratch, from CPU to high-level language to gain an understanding of how computer work but also how we can increase efficiency in a low-level scope.
Specifically, I implemented:
- Operating System Classes: Math, Sys, Memory, Array, Linked List Classes
- Basic Arithmetic Function: Add, Multiply by Assembly
- Basic Computer Function: Draw a Line, Circle, or a Character by Assembly
By implementing these essential functions of the computer, I understood how a computer allocates or deallocates memory efficiently, how it does multiply in log time.
This code accounts for Add Implementation
// Computes R0 = 2 + 3 (R0 refers to RAM[0])
@2
D=A
@3
D=D+A
@0
M=D
// Pushes and adds two constants.
push constant 7
push constant 8
add
class Array {
/** Constructs a new Array of the given size. */
function Array new(int size) {
return Memory.alloc(size);
}
/** Disposes this array. */
method void dispose() {
// we need an Array for deAlloc(). So get the this
//var Array that;
//let that = Memory.peek(4);
do Memory.deAlloc(this);
return;
}
}
The Hack Assembly Language mainly consists of 3 types of instructions. It ignores whitespace and allows programs to declare symbols with a single symbol declaration instruction. Symbols can either be labels or variables.
- A: Address Register.
- D: Data Register.
- M: Refers to the register in Main Memory whose address is currently stored in A.
- SP: RAM address 0.
- LCL: RAM address 1.
- ARG: RAM address 2.
- THIS: RAM address 3.
- THAT: RAM address 4.
- R0-R15: Addresses of 16 RAM Registers, mapped from 0 to 15.
- SCREEN: Base address of the Screen Map in Main Memory, which is equal to 16384.
- KBD: Keyboard Register address in Main Memory, which is equal to 24576.
- A-Instruction: Addressing instructions.
- C-Instruction: Computation instructions.
- L-Instruction: Labels (Symbols) declaration instructions.
@value
, where value is either a decimal non-negative number or a Symbol.
Examples:
@21
@R0
@SCREEN
0xxxxxxxxxxxxxxx
, where x
is a bit, either 0 or 1. A-Instructions always have their MSB set to 0.
Examples:
000000000001010
011111111111111
Sets the contents of the A register to the specified value. The value is either a non-negative number (i.e. 21) or a Symbol. If the value is a Symbol, then the contents of the A register is set to the value that the Symbol refers to but not the actual data in that Register or Memory Location.
Symbols can be either variables or labels. Variables are symbolic names for memory addresses to make remembering these addresses easier. Labels are instructions addresses that allow multiple jumps in the program easier to handle. Symbols declaration is not a machine instruction because machine code doesn't operate on the level of abstraction of that of labels and variables, and hence it is considered a pseudo-instruction.
Declaring variables is a straightforward A-Instruction, example:
@i
M=0
The instruction @i
declares a variable "i", and the instruction M=0
sets the memory location of "i" in Main Memory to 0, the address "i" was automatically generated and stored in A Register by the instruction.
To declare a label we need to use the command (LABEL_NAME)
, where "LABEL_NAME" can be any name we desire to have for the label, as long as it's wrapped between parentheses. For example:
(LOOP)
// ...
// instruction 1
// instruction 2
// instruction 3
// ...
@LOOP
0;JMP
The instruction (LOOP)
declares a new label called "LOOP", the assembler will resolve this label to the address of the next instruction (A or C instruction) on the following line.
The instruction @LOOP
is a straight-forward A-Instruction that sets the contents of A Register to the instruction address the label refers to, whereas the 0; JMP
instruction causes an unconditional jump to the address in A Register causing the program to execute the set of instructions between (LOOP)
and 0; JMP
infinitely.
dest = comp ; jmp, where:
- dest: Destination register in which the result of computation will be stored.
- comp: Computation code.
- jmp: The jump directive.
Examples:
D=0
M=1
D=D+1;JMP
M=M-D;JEQ
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
, where:
111
bits: C-Instructions always begin with bits111
.a
bit: Chooses to load the contents of either A register or M (Main Memory register addressed by A) into the ALU for computation.- Bits
c1
throughc6
: Control bits expected by the ALU to perform arithmetic or bit-wise logic operations. - Bits
d1
throughd3
: Specify which memory location to store the result of ALU computation into: A, D or M. - Bits
j1
throughj3
: Specify which JUMP directive to execute (either conditional or uncoditional).
Performs a computation on the CPU (arithmetic or bit-wise logic) and stores it into a destination register or memory location, and then (optionally) JUMPS to an instruction memory location that is usually addressed by a value or a Symbol (label).
I built this project by Assembly Language
Get used to Assembly Language and Operating System
- Clone the repo
git clone [https://github.com/your_username_/Project-Name.git](https://github.com/DuyNguyenPhuong/Low-latency-Trading-Program.git)
- Check the repo named features
You can run this project by clicking the Make Project Button (Green Arrow) in the top right of the project or type this in the terminal
- Finish the Product
- Add back to top links
- Add Additional Templates w/ Examples
- Add "components" document to easily copy & paste sections of the readme
- Multi-language Support
- Vietnamese
- Spanish
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!