Skip to content

Instantly share code, notes, and snippets.

@yunchih
Last active August 29, 2015 14:10
Show Gist options
  • Save yunchih/0e3a50e1c96d6a6f2f56 to your computer and use it in GitHub Desktop.
Save yunchih/0e3a50e1c96d6a6f2f56 to your computer and use it in GitHub Desktop.

#The TOY Assembly Language

The TOY language is an experimental ( educational ) assembly language target for TOY achitecture, and is a part of our final project C14-Assembler. Primary contributors to this project include: Andy , Tony and Borris.

##Directive

  • .DATA
  • .TEXT

Case-insensitive

##Data

  • All variables must be declared within .DATA
  • Declaration must abide to the following form:

< variable name > < size > < value > where

  • < variable name > := An alphanumeric symbol with length no more than 10. It should not start with number. _ is allowed. For example: my_var is ok, while 0_0_My_Var is strictly forbidden.
  • < size > := BYTE or WORD or DWORD
  • < value > := Any integer within the range of 0 to 4294967295 , i.e. 32 bits, unsigned integer, binary number unsupported. Prefix with 0x if the value is encoded hexadecimally. If you want to declare uninitialized value, set value to ?. You can also declare array by Array WORD DUP(10), which reserves 10 words for Array.

####Legal declaration

.DATA
   myVar WORD 32766
   foo   BYTE -10
   myArray WORD DUP(10)
   bar   DWORD ?

##Complete Instruction Specification

####Terminology

  • <reg> := R<i> where i in range 1 to F. R case-sensitive. For example: R1, RA are valid register namerreby r1, R17 are invalid.
  • <con> := Any 16-bit integer.
  • <var> := Any variable declared in .data
  • <mem> := [<reg>] [<var>] or [<con>]. For example: [R1],[myVariable],[0xF8] are valid, but [ R1 ] is invalid.
  • <instr> := Instruction.
  • <dest> := Destination operand.
  • <src> := Source operand.

####Rule

  • Ternary instructions: <instr> <dest> <src1> <src2>. For example: add R2 R1 R1.
  • Binary instructions: <instr> <dest> <src>. For example: ld R1 0xF1.

####Instruction

The table is transcribed from CYY as seen here

Name Opcode Mnemonic Format Pseudocode
Halt 0 hlt hlt Exit
Add 1 add add <reg>, <reg>, <reg> R[d] = R[s] + R[t]
Sub 2 sub sub <reg>, <reg>, <reg> R[d] = R[s] - R[t]
And 3 and and <reg>, <reg>, <reg> R[d] = R[s] & R[t]
Xor 4 xor xor <reg>, <reg>, <reg> R[d] = R[s] ^ R[t]
Shift left 5 shl shl <reg>, <reg>, <reg> R[d] = R[s] << R[t]
Shift right 6 shr shr <reg>, <reg>, <reg> R[d] = R[s] >> R[t]
Load address 7 lda lda <reg>, <con> R[d] = addr or constant
Load 8 ld ld <reg>, <mem> R[d] = mem[addr]
Store 9 st st <mem>, <reg> mem[addr] = R[d]
Load indirect A ldi ldi <reg>, [<reg>] R[d] = mem[ R[t] ]
Store indirect B sti sti [<reg>], <reg> mem[ R[t] ] = R[d]
Branch zero C bz bz <reg>, <con> if( R[d] == 0 ) pc = addr
Branch positive D bp bp <reg>, <con> if(R[d] > 0 ) pc = addr
Jump register E jr jr <reg>, (<reg>) pc = R[t]
Jump and link F jl jl <reg>, <con> R[d] = pc , pc = addr
Read read read <reg> Read from Stdin
Print print print <reg> Output to stdout
All instructions are case-insensitive

##Constant

Behave like the preprocessed #define in C.

    myConstant EQU 100

##Symbol / label

  • Update: Label can share line with instruction.
  • Symbol can be referenced even before it's declared.
  • Symbol contains only alphanumeric characters and _ and must not conflict with register name.
  • Symbol name must not start with digits.
  • A symbol must contains no more than 20 characters.

####Invalid symbol name

R8 ; conflicts with register name
myLongLongLongLongLongSymbol ; too long
2BeOrNot2Be ; starts with digit

##Comment

Anything prefixed by ; is regarded as comment and will not parsed by the assembler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment