This repository is a component of a larger project: Open-Machine - an open-source computer developed from scratch.
Β Β Β Β 1. π Definition and Explanation
Β Β Β Β 2. π’ Instructions
Β Β Β Β 3. π Code Flow and Tips
Β Β Β Β 4. π‘ Code syntax
Β Β Β Β 5. β¨οΈ Code Example
Β Β Β Β 6. βΆοΈ Setup and Run
Β Β Β Β 7. π» Assembler CLI
Β Β Β Β 9. π Contributing Guidelines
Assembly is basically the most basic programming language of a certain hardware. There's a very strong correspondence between the instructions in the language and the architecture's machine code instructions: every instruction in the language is a machine code instruction and vice-versa. It was created so that humans don't had to memorize the machine code instructions which are many numbers.
From the Wikipedia:
In computer programming, assembly language, often abbreviated asm, is any low-level programming language in which there is a very strong correspondence between the instructions in the language and the architecture's machine code instructions.
Because of this strong correspondence, the translating process is called assembling instead of compiling, which is the same process but for high-end languages. Those languages do not have this strong correspondence that assembly languages have.
The core of the assembling process is to identify the assembly instructions and translate them to the circuit's instruction binary equivalent. Similarly, it also has to convert each variable to a memory address.
If you are interested in knowing more how this process works don't be afraid to read at the code.
If you are interested in knowing more about the actual circuit that runs the code you write, click here.
If you are interested in knowing more about the Open-Computer project, click here.
Let's take a close look at the instructions available. Don't worry about syntax right now, we will talk about it later.
Warning
Some symbols are used in the Instructions Table. Here you can see their meaning.
Symbol | Explanation |
---|---|
ACC | The ACC register |
variable | A variable from the memory |
label | Jump label |
[ ] | "Value of" |
${memAddr} |
Memory address parameter |
${jumpTo} |
Instruction index or jump label parameter |
Assembly Command | Short Instruction Description | Long Instruction Description | Short Param Description | Long Param Description |
---|---|---|---|---|
nop |
- | This instruction doesn't perform any action | - | No parameter is required |
copy ${memAddr} |
[ACC] = [variable] | A value from the memory is copied to the ACC register | variable | It's the name of the variable that will be used in the instruction |
store ${memAddr} |
[variable] = [ACC] | The value from the ACC register is stored into memory | variable | It's the name of the variable that will be used in the instruction |
add ${memAddr} |
[ACC] = [ACC] + [variable] | The sum of the value of the ACC register and a value from the memory is stored in the ACC register | variable | It's the name of the variable that will be used in the instruction |
sub ${memAddr} |
[ACC] = [ACC] - [variable] | The difference between the value of the ACC register and a value from the memory is stored in the ACC register | variable | It's the name of the variable that will be used in the instruction |
input ${memAddr} |
[variable] = input value | The input value is copied to the memory | variable | It's the name of the variable that will be used in the instruction |
output ${memAddr} |
Output [variable] | Outputs a value from the memory into the circuit LEDs | variable | It's the name of the variable that will be used in the instruction |
kill |
Finishes program | When this instruction is encountered, the program is finished and no more instructions will be executed | - | No parameter is required |
jmp ${jumpTo} |
Jump to EE | Jump to another line of code | label | The jump label the program will jump to |
jg ${jumpTo} |
Jump to EE if [ACC] > 0 | Jump to another line of code if the value of the ACC register is positive | label | The jump label the program will jump to if the condition is right |
je ${jumpTo} |
Jump to EE if [ACC] = 0 | Jump to another line of code if the value of the ACC register is zero | label | The jump label the program will jump to if the condition is right |
jl ${jumpTo} |
Jump to EE if [ACC] < 0 | Jump to another line of code if the value of the ACC register is negative | label | The jump label the program will jump to if the condition is right |
This section will help you think more in an assembly way.
Because Open-Machine's Circuit only has very simple commands and very few registers, the way to think about your assembly code will be very different.
Click here to go the section!
Warning
Read the specifications below to learn the code syntax.
- Case sensitive;
- Tabs and spaces can be used interchangeably;
- Blank or empty lines won't be considered;
- Numbers can be written in hexadecimal in the form of
0xff
or in decimal as255
;
- A label name should start with a letter and the rest of the name can have more letters and numbers;
- Every name should obey the following regex:
[a-z][a-zA-Z0-9]*
; - Snake-case is not allowed and the use of camel-case is encouraged.
- Definition: it marks the line for possible jumps to that line;
- Form:
{labelName}:
- Remember to follow the naming practices
Definition
An instruction line is a line that contains an instruction call.
Components
instruction
is the actual instruction that will be executed, it must be one of the following in the instruction table;arg
can be a jump label or a number (depending on the instruction)
Form
- A instruction line should be in the following form
{instruction} [arg]
; - An instruction line should obey the following regex:
^[\t ]*(((nop)|(copy)|(store)|(add)|(sub)|(input)|(output)|(kill)|(jmp)|(jg)|(je)|(jl))(([\t ]+[a-z][a-zA-Z0-9]*)|()))[\t ]*$
Instructions List
Check out here the instruction table to know what instructions you can use and their parameters.
The following assembly code gets two numbers from input and outputs the sum of them. If the sum is greater than zero it will output zero.
ps: Since the input
instruction doesn't wait for a change, expect the output to be zero.
# data inputs
input 0x55
input 0x56
# sum
copy 0x55
add 0x56
store 0x57
# output
output 0x57
# if output higher than zero, it will output zero
copy 0x57
je finish # if
jl finish # if
output 0xff # [0xff] = 0 since we didn't change it
finish:
kill
These are the steps to setup and run Open-Computer's Assembler.
You can find more information about the assembler CLI here and about running the circuit here.
-
Build the GoLang project
./setup.sh
-
Clone Open-Computer's Circuit Repository
You will need this repository to run the assembled program.
If you have git installed in your terminal, run:
git clone https://github.com/Open-Machine/Circuits/
Assemble your code
./assembler assemble ${main.asm}
There are two ways of running your application from the machine code generated by the assemble
command.
In this mode, you will be able to see everything that is happening to the circuits in real time and interact with it by changing the inputs.
You can watch this video as an introduction to Logisim-Evolution, which is the program that we will be using to simulate the circuit.
-
Navigate to the Circuits repository
-
Start the circuit: follow the steps to Start the Circuit
-
Right click in the RAM and click "Load Image"
-
Select the assembled file
You should select the file generated by the assemble program, not the file with the assembly code.
-
To run the program (start the clock simulation), follow the steps to Run the Circuit
In this mode, you will only be able to see the outputs of your application. You just have to run:
java -jar logisim-evolution.jar main.circ -load ${assembled_file} -tty table
Remember to write the name of the file that was generated by the assembler command instead of ${assembled_file}
.
The outputs will appear on the console following this pattern: {16 bits of the main output} {4 bit ouptut counter}
.
The first output can be ignored.
go test ./...
cd go_scripts/format_circuit_output
go test ./...
You can use the flag --help
to see all the options.
./assembler assemble --help
usage: assembler assemble [<flags>] <file-name>
Assemble assembly code
Flags:
--help Show context-sensitive help (also try --help-long and
--help-man).
-r, --rename-exec="" Provide the name of the executable file that will be created
(if empty, the name will be the same as the assembly code
file)
Args:
<file-name> Provide the name of file with the assembly code
./assembler syntax --help
usage: assembler syntax [<flags>]
Help with the syntax of this assembly language
Flags:
--help Show context-sensitive help (also try --help-long
and --help-man).
-e, --example Assembly code example with explanation
-l, --ls List all available instructions
-c, --instruction=INSTRUCTION Explanation of an specific instruction
Check out the contributing guidelines here.