Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,24 @@ 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

### Instructions Table
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 | [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 | [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 | [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 | [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 | [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 | 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 | Jump to EE | Jump to another line of code | label | The jump label the program will jump to
jg | 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 | 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 | 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
```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

<br/>

Expand Down Expand Up @@ -137,10 +139,19 @@ Read the specifications below to learn the code syntax.
- **Numbers** can be written in hexadecimal in the form of ```0xff``` or in decimal as ```255```;

## Naming Practices
- A **label name** should start with a letter and the rest of the name can have more letters and numbers;
- A **variable or 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.

## Declaring Variables
- The variables should be declared between the imports and instructions.
- Form: ```declare {variableName} {number}```
- Example
```sh
declare variable 0xff
```
- Remember to follow the [naming practices](#naming-practices)

## Jump Label
- Definition: it marks the line for possible jumps to that line;
- Form: ```{labelName}:```
Expand All @@ -154,7 +165,7 @@ 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 either a jump label or a
- ```arg``` can be a jump label, variable or a number (depending on the instruction)

***Form***
- A instruction line should be in the following form ```{instruction} [arg]```;
Expand All @@ -171,22 +182,30 @@ Check out [here](#-instructions) the instruction table to know what instructions
<br/>

# 👨🏻‍💻 Code Example
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.*
```sh
# Let's get two numbers as input and print the result of the sum of them
# data inputs
input 0x55
input 0x56

# sum
copy 0x55
add 0x56
store 0x57

# output
output 0x57

procedure procSub
copy 0xff # copy value in the address ff in RAM
store 0x0a # stores the value of ACC in the address 0a
end
# 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

start:
procSub
finish:

copy 0xff # copy value in the address ff in RAM
store 0x0a # stores the value of ACC in the address 0a
je start # jumps to the 'start' label if ACC is 0
kill
```

Expand Down
26 changes: 26 additions & 0 deletions commands/instruction/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package instruction

import (
"github.com/open-machine/assembler/commands/instruction/engine"
"github.com/open-machine/assembler/core/commandsassembler"
)

func GetInstructionsConfig() map[string]commandsassembler.CommandAssembler {
return instructions
}

// TODO: test if any instruction has the same code
var instructions = map[string]commandsassembler.CommandAssembler{
"nop": engine.NewNoParamInstruct(0x0),
"copy": engine.NewOneParamInstruct(0x1, !engine.ACCEPT_STRING_PARAM),
"store": engine.NewOneParamInstruct(0x2, !engine.ACCEPT_STRING_PARAM),
"add": engine.NewOneParamInstruct(0x3, !engine.ACCEPT_STRING_PARAM),
"sub": engine.NewOneParamInstruct(0x4, !engine.ACCEPT_STRING_PARAM),
"input": engine.NewOneParamInstruct(0x7, !engine.ACCEPT_STRING_PARAM),
"output": engine.NewOneParamInstruct(0x8, !engine.ACCEPT_STRING_PARAM),
"kill": engine.NewNoParamInstruct(0x9),
"jmp": engine.NewOneParamInstruct(0xA, engine.ACCEPT_STRING_PARAM),
"jg": engine.NewOneParamInstruct(0xB, engine.ACCEPT_STRING_PARAM),
"je": engine.NewOneParamInstruct(0xD, engine.ACCEPT_STRING_PARAM),
"jl": engine.NewOneParamInstruct(0xF, engine.ACCEPT_STRING_PARAM),
}
Loading