Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.0KB

  1. # Assembling Z80 binaries
  2. (All assembers in Collapse OS follow the same basic principles.
  3. There are sections, below, for each supported architectures, but
  4. you should read this first section first to be familiar with
  5. those common, basic principles)
  6. Words in the Z80 assembler, loaded with "5 LOAD" allow you to
  7. assemble z80 binaries. Being Forth words, opcode assembly is a
  8. bit different than with a typical assembler. For example, what
  9. would traditionally be "ld a, b" would become "A B LDrr,".
  10. Those opcode words, of which there is a complete list below, end
  11. with "," to indicate that their effect is to write (,) the cor-
  12. responding opcode.
  13. The "argtype" suffix after each mnemonic is needed because the
  14. assembler doesn't auto-detect the op's form based on arguments.
  15. It has to be explicitly specified. "r" is for 8-bit registers,
  16. "d" for 16-bit ones, "i" for immediate, "c" is for conditions.
  17. Be aware that "SP" and "AF" refer to the same value: some 16-
  18. bit ops can affect SP, others, AF. If you use the wrong argu-
  19. ment on the wrong op, you will affect the wrong register.
  20. Mnemonics having only a single form, such as PUSH and POP,
  21. don't have argtype suffixes.
  22. In addition to opcode words, some variables are also defined by
  23. this program:
  24. BIN( is the addr at which the compiled binary will live. It is
  25. often 0.
  26. ORG is H@ offset at which we begin spitting binary. Used to
  27. compute PC. To have a proper PC, call "H@ ORG !" at the
  28. beginning of your assembly process. PC is H@ - ORG + BIN(.
  29. Labels are a convenient way of managing relative jump
  30. calculations. Backward labels are easy. It is only a matter or
  31. recording "HERE" and do subtractions. Forward labels record the
  32. place where we should write the offset, and then when we get to
  33. that point later on, the label records the offset there.
  34. To avoid using dict memory in compilation targets, we
  35. pre-declare label variables here, which means we have a limited
  36. number of it. We have 4: L1, L2, L3, L4.
  37. # Ad-hoc assembly
  38. A frequent usage of the assembler is to cross-assemble a new
  39. Collapse OS binary. However, it can also be used to assemble
  40. code for immediate usage on the machine that assemble.
  41. A frequent mistake when doing so is to forget to set BIN( and
  42. ORG. BIN( must match your system's binary offset (you get get
  43. it with "0 BIN+") even for the most simple of code because
  44. otherwise, ";CODE" will jump to the wrong offset.
  45. ORG is less critical, but can lead to problems if not set, so
  46. you should take the habit of setting it with "H@ ORG !".
  47. # Flow
  48. There are 2 label types: backward and forward. For each type,
  49. there are two actions: set and write. Setting a label is
  50. declaring where it is. Words for this are BSET and FSET. It has
  51. to be performed at the label's destination. Writing a label is
  52. writing its offset difference to the binary result. It has to be
  53. done right after a relative jump operation. Word for this are
  54. BWR and FWR. Yes, those words are only for relative jumps.
  55. For backward labels, set happens before write. For forward
  56. labels, write happen before set. The write operation writes a
  57. dummy placeholder, and then the set operation writes the offset
  58. at that placeholder's address.
  59. Important limitation: Flow words are broken when PC reaches
  60. 0x8000. The BREAK, word relies on that 15th bit as a flag.
  61. Variable actions are expected to be called with labels in
  62. front of them. Examples:
  63. L1 BSET NOP, JR, L1 BWR ( backward jump )
  64. JR, L1 FWR NOP, L1 FSET ( forward jump )
  65. If you look at the code for those words, you'll notice a mys-
  66. terious "1-". z80 relative jumps receives "e-2", that is, the
  67. offset that *counts the 2 bytes of the jump itself*. Because we
  68. set the label *after* the jump OP1 itself, that's 1 byte that is
  69. taken care of. We still need to adjust by another byte before
  70. writing the offset.
  71. Can you use labels with JP, and CALL,? Yes, but only backwards
  72. jumps, and in that case, you use the label's value directly.
  73. Example: L2 @ CALL,
  74. # Structured flow
  75. z80a also has words that behave similarly to IF..THEN and
  76. BEGIN..UNTIL.
  77. On the IF side, we have IFZ, IFNZ, IFC, IFNC, and THEN,. When
  78. the opposite condition is met, a relative jump is made to
  79. THEN,'s PC. For example, if you have IFZ, a jump is made when
  80. Z is unset.
  81. There can be an ELSE, in the middle of an IF, and THEN,. When
  82. present, IF, jumps to it when the condition is unmet. When the
  83. condition is met, upon reaching the ELSE, we unconditionally
  84. jump to the THEN,.
  85. On the BEGIN,..AGAIN, side, it's a bit different. You start
  86. with your BEGIN, instruction, and then later you issue a
  87. JRxx, instr followed by AGAIN,. Exactly like you would do
  88. with a label.
  89. On top of that, you have the very nice BREAK, instruction,
  90. which must also be preceded by a JRxx, and will jump to the
  91. PC following the next AGAIN,. Examples:
  92. IFZ, NOP, ELSE, NOP, THEN,
  93. BEGIN, NOP, JR, AGAIN, ( unconditional )
  94. BEGIN, NOP, JRZ, AGAIN, ( conditional )
  95. BEGIN, NOP, JRZ, BREAK, JR, AGAIN, ( break off the loop )
  96. # Z80 Instructions list
  97. Letters in [] brackets indicate "argtype" variants. When the
  98. bracket starts with ",", it means that a "plain" mnemonic is
  99. available. For example, "RET," and "RETc," exist.
  100. Note that assemblers in Collapse OS are incomplete and opcode
  101. words were implemented in a "just-in-time" fashion, when needed.
  102. r => A B C D E H L (HL)
  103. d => BC DE HL AF/SP
  104. c => CNZ CZ CNC CC CPO CPE CP CM
  105. LD [rr, ri, di, (i)HL, HL(i), d(i), (i)d, rIXY, IXYr,
  106. (DE)A, A(DE), (i)A, A(i)]
  107. ADD [r, i, HLd, IXd, IXIX, IYd, IYIY]
  108. ADC [r, HLd]
  109. CP [r, i, (IXY+)]
  110. SBC [r, HLd]
  111. SUB [r, i]
  112. INC [r, d, (IXY+)]
  113. DEC [r, d, (IXY+)]
  114. AND [r, i]
  115. OR [r, i]
  116. XOR [r, i]
  117. OUT [iA, (C)r]
  118. IN [Ai, r(C)]
  119. JP [i, (HL), (IX), (IY)]
  120. JR [, Z, NZ, C, NC]
  121. PUSH POP
  122. SET RES BIT
  123. RL RLC SLA RLA RLCA
  124. RR RRC SRL RRA RRCA
  125. CALL RST DJNZ
  126. DI EI EXDEHL EXX HALT
  127. NOP RET [,c] RETI RETN SCF
  128. Macros:
  129. SUBHLd PUSH [0,1,Z,A] HLZ DEZ
  130. LDDE(HL) OUT [HL,DE]
  131. # 8086 assembler
  132. Load with "30 LOAD". As with the Z80 assembler, it is incom-
  133. plete.
  134. Mnemonics are followed by argument types. For example, MOVri,
  135. moves 8-bit immediate to 8-bit register.
  136. 'r' = 8-bit register 'x' = 16-bit register
  137. 'i' = 8-bit immediate 'I' = 16-bit immediate
  138. 's' = SREG register
  139. Mnemonics that only have one signature (for example INT,) don't
  140. have operands letters.
  141. For jumps, it's special. 's' is SHORT, 'n' is NEAR, 'f' is FAR.
  142. # 8086 Instructions list
  143. r -> AL BL CL DL AH BH CH DX
  144. x -> AX BX CX DX SP BP SI DI
  145. s -> ES CS SS DS
  146. [] -> [SI] [DI] [BP] [BX] [BX+SI] [BX+DI] [BP+SI] [BP+DI]
  147. RET CLI STI HLT CLD STD NOP CBW REPZ REPNZ
  148. LODSB LODSW CMPSB SMPSW MOVSB MOVSW SCASB SCASW STOSB STOSW
  149. CALL J[Z,NZ,C,NC] JMP[s,n,r,f]
  150. INC[r,x,[w],[b],[w]+,[b]+]
  151. DEC[r,x,[w],[b],[w]+,[b]+]
  152. POP[x,[w],[w]+]
  153. PUSH[x,[w],[w]+,s]
  154. MUL[r,x]
  155. DIV[r,x]
  156. XOR[rr,xx]
  157. OR[rr,xx]
  158. AND[rr,xx,ALi,AXI]
  159. ADD[rr,xx,ALi,AXI,xi]
  160. SUB[rr,xx,ALi,AXI,xi]
  161. INT
  162. CMP[rr,xx,r[],x[],r[]+,x[]+]
  163. MOV[rr,xx,r[],x[],[]r,[]x,r[]+,x[]+,[]+r,[]+x,ri,xI,sx,rm,xm
  164. mr,mx]
  165. ("1" means "shift by 1", "CL" means "shift by CL")
  166. ROL[r1,x1,rCL,xCL]
  167. ROR[r1,x1,rCL,xCL]
  168. SHL[r1,x1,rCL,xCL]
  169. SHR[r1,x1,rCL,xCL]
  170. # AVR assembler
  171. Load with "50 LOAD". As with the Z80 assembler, it is incom-
  172. plete.
  173. All mnemonics in AVR have a single signature. Therefore, we
  174. don't need any "argtype" suffixes.
  175. Registers are referred to with consts R0-R31. There is
  176. X, Y, Z, X+, Y+, Z+, X-, Y-, Z- for appropriate ops (LD, ST).
  177. XL, XH, YL, YH, ZL, ZH are simple aliases to R26-R31.
  178. Branching works differently. Instead of expecting a byte to be
  179. written after the naked op, branching words expect a displace-
  180. ment argument.
  181. This is because there's bitwise ORing involved in the creation
  182. of the final opcode, which makes z80a's approach impractical.
  183. This makes labelling a bit different too. Instead of expecting
  184. label words after the naked branching op, we rather have label
  185. words expecting branching wordref as an argument. Examples:
  186. L2 ' BRTS FLBL! ( branch forward to L2 )
  187. L1 ' RJMP LBL, ( branch backward to L1 )
  188. # Model-specific constants
  189. Model-specific constants must be loaded separately. Here is a
  190. list of units:
  191. - ATMega328P: B65-B66
  192. Those units contain register constants such as PORTB, DDRB, etc.
  193. Unlike many moder assemblers, they do not include bit constants.
  194. Here's an example use:
  195. DDRB 5 SBI,
  196. PORTB 5 CBI,
  197. R16 TIFR0 IN,
  198. R16 0 ( TOV0 ) SBRS,
  199. # AVR instructions list
  200. OPRd (B53)
  201. ASR COM DEC INC LAC LAS LAT LSR NEG POP PUSH
  202. ROR SWAP XCH
  203. OPRdRr (B54)
  204. ADC ADD AND CP CPC CPSE EOR MOV MUL OR SBC
  205. SUB
  206. OPRdA (B54)
  207. IN OUT
  208. OPRdK (B55)
  209. ANDI CPI LDI ORI SBCI SBR SUBI
  210. OPAb (B55)
  211. CBI SBI SBIC SBIS
  212. OPNA (B56)
  213. BREAK CL[C,H,I,N,S,T,V,Z] SE[C,H,I,N,S,T,V,Z] EIJMP ICALL
  214. EICALL IJMP NOP RET RETI SLEEP WDR
  215. OPb (B57)
  216. BCLR BSET
  217. OPRdb (B57)
  218. BLD BST SBRC SBRS
  219. Special (B57,B60)
  220. CLR TST LSL LD ST
  221. Flow (B58)
  222. RJMP RCALL
  223. BR[BC,BS,CC,CS,EQ,NE,GE,HC,HS,ID,IE,LO,LT,MI,PL,SH,TC,TS,VC,VS]
  224. Flow macros (B61)
  225. LBL! LBL, SKIP, TO, FLBL, FLBL! BEGIN, AGAIN? AGAIN, IF, THEN,