Skip to content

Commit

Permalink
Org and end
Browse files Browse the repository at this point in the history
  • Loading branch information
gareththegeek committed Jul 4, 2018
1 parent a814f0c commit cc32385
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Empty file added docs/corewar/loader.md
Empty file.
45 changes: 42 additions & 3 deletions docs/redcode/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ The parser performs the following functions:
* Executes [preprocessor commands](preprocessor)
* Removes white space and [comments](comments)

## Org and End Directives

By default, warriors are loaded into core with the first instruction as the starting instruction.

The `org` directive allows a different starting address to be specified. The `org` directive should be written on its own line and followed by an address indicating the start address relative to the first address in the warrior.

By convention, the `org` directive is usually placed at the top of the warrior's code.

```
org 1
dat #4, #0
add.ab -1, -1
```

The above example uses the `org` directive to set the first instruction to the `add` instruction rather than the `dat` instruction.

The `org` directive was introduced in the [ICWS'94](./#standards) standard. Previous standards used the `end` directive instead, which worked in the same way but was placed at the end of the warrior.

```
dat #4, #0
add.ab -1, -1
end 1
```

Note the `end` and `org` directives are semantically equivalent and either can be used.

## Defaults

All instructions require a [modifier](modifiers) and two [operands](operands) in order to be loaded into the `Simulator`. Each operand must have an [addressing mode](addressing_modes) and a number.
Expand All @@ -18,11 +44,17 @@ However, it is not necessary to specify modifiers or addressing modes in order f

For example when parsed `mov 0, 1` will become `MOV.I $0, $1`.

Under the [ICWS'94 standard](./#standards), the [direct](addressing_modes#direct) (`$`) addressing mode is always the default addressing mode, and the parser will always insert this if the addressing mode is ommitted.
### Addressing Modes

The default addressing mode is [direct](addressing_modes#direct) (`$`) for all [opcodes](opcodes) except [dat](opcodes#dat-data). Instructions with the `dat` opcode default to the [immediate](addressing_modes#immediate) (`#`) addressing mode.

### Operands

If an instruction has only a single operand, the `B` operand is defaulted to zero. Only the `dat`, `jmp`, `spl` and `nop` opcodes can have a single operand specified. Failure to specify a `B` operand for other opcodes will result in a syntax error.

Older standards inserted the [immediate](addressing_modes#immediate) (`#`) addressing mode if the opcode of the instruction was [dat](opcodes#dat-data).
### Modifiers

Which modifier is inserted as a default is dependant upon the instruction's [opcode](opcodes) and `A` and `B` addressing modes as follows:
Which modifier is inserted as a default is dependant upon the instruction's opcode and `A` and `B` addressing modes as follows:

|Opcode|A-mode|B-mode|Default Modifier|
|---|---|---|---|
Expand All @@ -37,3 +69,10 @@ Which modifier is inserted as a default is dependant upon the instruction's [opc
||$@<>{}|any|`.b`|
|`jmp``jmz``jmn``djn``spl``nop`|any|any|`.b`|

### ORG (Starting address)

If no `ORG` or `END` directive is specified, the `ORG` directive will be used to determine the start address.

If multiple `ORG` or `END` directives are detected, the last one will be used (with `ORG` taking precedence over `END`).

Both these situations will generate a parser warning.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pages:
- Intro: corewar/index.md
- Core: corewar/core.md
- Warriors: corewar/warriors.md
- Loader: corewar/loader.md
- Redcode Reference:
- Intro: redcode/index.md
- Execution: redcode/execution.md
Expand Down

0 comments on commit cc32385

Please sign in to comment.