|
1 | 1 | # CPU Assembler |
2 | | -The assembler for the Nibble Knowledge CPU |
| 2 | +The assembler for the Nibble Knowledge CPU. |
| 3 | + |
| 4 | +AS4 |
| 5 | +--- |
| 6 | + |
| 7 | +AS4 is the low level assembler on written in C99 for the Nibble Knowledge CPU. It is under 700 lines of executable code, and recognises only 12 contructs: |
| 8 | + |
| 9 | +### 8 instructions ### |
| 10 | +The Nibble Knowledge CPU has 8 instructions which are split into two different types: |
| 11 | +* Solitary instructions that do not require a memory address (has the format INST) |
| 12 | + * NOP: No operation |
| 13 | + * HLT: Halt CPU |
| 14 | + * CXA: Copy the carry bit (soon to be overflow bit?) and the XOR of that bit and the MSB of the accumulator. |
| 15 | +* Binary instructions that require a memory address (has the formal INST ADDR) |
| 16 | + * ADD: Add the nibble at the specified memory address to the accumulator. |
| 17 | + * NND: NAND the nibble at the specified memory address to the accumulator. |
| 18 | + * JMP: Jump to the specified memory address if the accumulator is 0. |
| 19 | + * LOD: Load the accumulator with the nibble at the specified address. |
| 20 | + * STR: Store the nibble in the accumulator to the specified memory address. |
| 21 | + |
| 22 | +### 2 data types ### |
| 23 | +AS4 recognises two inbuilt data types: |
| 24 | +* Numberical values. Format: ".data SIZE INITIAL VALUE" |
| 25 | +* Strings, both plain and zero terminated. Format: ".ascii "String"" or ".asciiz "String"" |
| 26 | + * Strings must start and end with double quotes. |
| 27 | + * AS4 recognises standard escape characters |
| 28 | + |
| 29 | +### Labels ### |
| 30 | +Labels are of the format "NAME:". They are used to refer to memory locations without having to memorise or calculate number. |
| 31 | +An example of useage would be "number: .data 1 2", which is using the label "number" to point to a data element of 1 nibble in size with the initial value of 2. |
| 32 | + |
| 33 | +### Comments ### |
| 34 | +Comments in AS4 start with a semicolon, ";". |
| 35 | + |
| 36 | +### Example code ### |
| 37 | +; This program reads in integers and adds them together |
| 38 | +; until a negative number is read in. Then it outputs |
| 39 | +; the sum (not including the last number). |
| 40 | + |
| 41 | +;Start: read ; read n -> acc |
| 42 | +; jmpn Done ; jump to Done if n < 0. |
| 43 | +; add sum ; add sum to the acc |
| 44 | +; store sum ; store the new sum |
| 45 | +; jump Start ; go back & read in next number |
| 46 | +;Done: load sum ; load the final sum |
| 47 | +; write ; write the final sum |
| 48 | +; stop ; stop |
| 49 | +; |
| 50 | +;sum: .data 2 0 ; 2-byte location where sum is stored |
| 51 | + |
| 52 | +;; START NIbble-Knowledge code ;; |
| 53 | + |
| 54 | +;; Instruction Section ;; |
| 55 | + |
| 56 | +Start: |
| 57 | + LOD n15 |
| 58 | + ADD n2 |
| 59 | + NND n15 |
| 60 | + NOP |
| 61 | + CXA |
| 62 | + JMP Jump |
| 63 | + STR sum |
| 64 | + |
| 65 | +Jump: HLT |
| 66 | + |
| 67 | + |
| 68 | +;; Data Section ;; |
| 69 | +n0: .data 1 0 ; b0000 |
| 70 | +n1: .data 1 1 ; b0001 |
| 71 | +n2: .data 1 2 ; b0010 |
| 72 | +n3: .data 1 3 ; b0011 |
| 73 | +n4: .data 1 4 ; b0100 |
| 74 | +n5: .data 1 5 ; b0101 |
| 75 | +n6: .data 1 6 ; b0110 |
| 76 | +n7: .data 1 7 ; b0111 |
| 77 | +n8: .data 1 8 ; b1000 |
| 78 | +n9: .data 1 9 ; b1001 |
| 79 | +n10: .data 1 10 ; b1010 |
| 80 | +n11: .data 1 11 ; b1011 |
| 81 | +n12: .data 1 12 ; b1100 |
| 82 | +n13: .data 1 13 ; b1101 |
| 83 | +n14: .data 1 14 ; b1110 |
| 84 | +n15: .data 1 15 ; b1111 |
| 85 | + |
| 86 | +derp: .data 6 0x454534 |
| 87 | + |
| 88 | +derp3: .ascii "hey there" |
| 89 | +derp4: .ascii "hey\" there\n" |
| 90 | +derp5: .asciiz "hey there" |
| 91 | +derp6: .asciiz "hey\" there\n" |
| 92 | + |
| 93 | +sum: .data 1 0 |
| 94 | + |
0 commit comments