Mirror of CollapseOS
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

284 Zeilen
8.3KB

  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. # Flow
  38. There are 2 label types: backward and forward. For each type,
  39. there are two actions: set and write. Setting a label is
  40. declaring where it is. Words for this are BSET and FSET. It has
  41. to be performed at the label's destination. Writing a label is
  42. writing its offset difference to the binary result. It has to be
  43. done right after a relative jump operation. Word for this are
  44. BWR and FWR. Yes, those words are only for relative jumps.
  45. For backward labels, set happens before write. For forward
  46. labels, write happen before set. The write operation writes a
  47. dummy placeholder, and then the set operation writes the offset
  48. at that placeholder's address.
  49. Variable actions are expected to be called with labels in
  50. front of them. Examples:
  51. L1 BSET NOP, JR, L1 BWR ( backward jump )
  52. JR, L1 FWR NOP, L1 FSET ( forward jump )
  53. If you look at the code for those words, you'll notice a mys-
  54. terious "1-". z80 relative jumps receives "e-2", that is, the
  55. offset that *counts the 2 bytes of the jump itself*. Because we
  56. set the label *after* the jump OP1 itself, that's 1 byte that is
  57. taken care of. We still need to adjust by another byte before
  58. writing the offset.
  59. Can you use labels with JP, and CALL,? Yes, but only backwards
  60. jumps, and in that case, you use the label's value directly.
  61. Example: L2 @ CALL,
  62. # Structured flow
  63. z80a also has words that behave similarly to IF..THEN and
  64. BEGIN..UNTIL.
  65. On the IF side, we have IFZ, IFNZ, IFC, IFNC, and THEN,. When
  66. the opposite condition is met, a relative jump is made to
  67. THEN,'s PC. For example, if you have IFZ, a jump is made when
  68. Z is unset.
  69. There can be an ELSE, in the middle of an IF, and THEN,. When
  70. present, IF, jumps to it when the condition is unmet. When the
  71. condition is met, upon reaching the ELSE, we unconditionally
  72. jump to the THEN,.
  73. On the BEGIN,..AGAIN, side, it's a bit different. You start
  74. with your BEGIN, instruction, and then later you issue a
  75. JRxx, instr followed by AGAIN,. Exactly like you would do
  76. with a label.
  77. On top of that, you have the very nice BREAK, instruction,
  78. which must also be preceded by a JRxx, and will jump to the
  79. PC following the next AGAIN,. Examples:
  80. IFZ, NOP, ELSE, NOP, THEN,
  81. BEGIN, NOP, JR, AGAIN, ( unconditional )
  82. BEGIN, NOP, JRZ, AGAIN, ( conditional )
  83. BEGIN, NOP, JRZ, BREAK, JR, AGAIN, ( break off the loop )
  84. # Z80 Instructions list
  85. Letters in [] brackets indicate "argtype" variants. When the
  86. bracket starts with ",", it means that a "plain" mnemonic is
  87. available. For example, "RET," and "RETc," exist.
  88. Note that assemblers in Collapse OS are incomplete and opcode
  89. words were implemented in a "just-in-time" fashion, when needed.
  90. r => A B C D E H L (HL)
  91. d => BC DE HL AF/SP
  92. c => CNZ CZ CNC CC CPO CPE CP CM
  93. LD [rr, ri, di, (i)HL, HL(i), d(i), (i)d, rIXY, IXYr,
  94. (DE)A, A(DE), (i)A, A(i)]
  95. ADD [r, i, HLd, IXd, IXIX, IYd, IYIY]
  96. ADC [r, HLd]
  97. CP [r, i, (IXY+)]
  98. SBC [r, HLd]
  99. SUB [r, i]
  100. INC [r, d, (IXY+)]
  101. DEC [r, d, (IXY+)]
  102. AND [r, i]
  103. OR [r, i]
  104. XOR [r, i]
  105. OUT [iA, (C)r]
  106. IN [Ai, r(C)]
  107. JP [i, (HL), (IX), (IY)]
  108. JR [, Z, NZ, C, NC]
  109. PUSH POP
  110. SET RES BIT
  111. RL RLC SLA RLA RLCA
  112. RR RRC SRL RRA RRCA
  113. CALL RST DJNZ
  114. DI EI EXDEHL EXX HALT
  115. NOP RET [,c] RETI RETN SCF
  116. Macros:
  117. SUBHLd PUSH [0,1,Z,A] HLZ DEZ
  118. LDDE(HL) OUT [HL,DE]
  119. # 8086 assembler
  120. Load with "30 LOAD". As with the Z80 assembler, it is incom-
  121. plete.
  122. Mnemonics are followed by argument types. For example, MOVri,
  123. moves 8-bit immediate to 8-bit register.
  124. 'r' = 8-bit register 'x' = 16-bit register
  125. 'i' = 8-bit immediate 'I' = 16-bit immediate
  126. 's' = SREG register
  127. Mnemonics that only have one signature (for example INT,) don't
  128. have operands letters.
  129. For jumps, it's special. 's' is SHORT, 'n' is NEAR, 'f' is FAR.
  130. # 8086 Instructions list
  131. r -> AL BL CL DL AH BH CH DX
  132. x -> AX BX CX DX SP BP SI DI
  133. s -> ES CS SS DS
  134. [] -> [SI] [DI] [BP] [BX] [BX+SI] [BX+DI] [BP+SI] [BP+DI]
  135. RET CLI STI HLT CLD STD NOP CBW REPZ REPNZ
  136. LODSB LODSW CMPSB SMPSW MOVSB MOVSW SCASB SCASW STOSB STOSW
  137. CALL J[Z,NZ,C,NC] JMP[s,n,r,f]
  138. INC[r,x,[w],[b],[w]+,[b]+]
  139. DEC[r,x,[w],[b],[w]+,[b]+]
  140. POP[x,[w],[w]+]
  141. PUSH[x,[w],[w]+,s]
  142. MUL[r,x]
  143. DIV[r,x]
  144. XOR[rr,xx]
  145. OR[rr,xx]
  146. AND[rr,xx,ALi,AXI]
  147. ADD[rr,xx,ALi,AXI,xi]
  148. SUB[rr,xx,ALi,AXI,xi]
  149. INT
  150. CMP[rr,xx,r[],x[],r[]+,x[]+]
  151. MOV[rr,xx,r[],x[],[]r,[]x,r[]+,x[]+,[]+r,[]+x,ri,xI,sx,rm,xm
  152. mr,mx]
  153. ("1" means "shift by 1", "CL" means "shift by CL")
  154. ROL[r1,x1,rCL,xCL]
  155. ROR[r1,x1,rCL,xCL]
  156. SHL[r1,x1,rCL,xCL]
  157. SHR[r1,x1,rCL,xCL]
  158. # AVR assembler
  159. Load with "50 LOAD". As with the Z80 assembler, it is incom-
  160. plete.
  161. All mnemonics in AVR have a single signature. Therefore, we
  162. don't need any "argtype" suffixes.
  163. Registers are referred to with consts R0-R31. There is
  164. X, Y, Z, X+, Y+, Z+, X-, Y-, Z- for appropriate ops (LD, ST).
  165. XL, XH, YL, YH, ZL, ZH are simple aliases to R26-R31.
  166. Branching works differently. Instead of expecting a byte to be
  167. written after the naked op, branching words expect a displace-
  168. ment argument.
  169. This is because there's bitwise ORing involved in the creation
  170. of the final opcode, which makes z80a's approach impractical.
  171. This makes labelling a bit different too. Instead of expecting
  172. label words after the naked branching op, we rather have label
  173. words expecting branching wordref as an argument. Examples:
  174. L2 ' BRTS FLBL! ( branch forward to L2 )
  175. L1 ' RJMP LBL, ( branch backward to L1 )
  176. # Model-specific constants
  177. Model-specific constants must be loaded separately. Here is a
  178. list of units:
  179. - ATMega328P: B65-B66
  180. Those units contain register constants such as PORTB, DDRB, etc.
  181. Unlike many moder assemblers, they do not include bit constants.
  182. Here's an example use:
  183. DDRB 5 SBI,
  184. PORTB 5 CBI,
  185. R16 TIFR0 IN,
  186. R16 0 ( TOV0 ) SBRS,
  187. # AVR instructions list
  188. OPRd (B53)
  189. ASR COM DEC INC LAC LAS LAT LSR NEG POP PUSH
  190. ROR SWAP XCH
  191. OPRdRr (B54)
  192. ADC ADD AND CP CPC CPSE EOR MOV MUL OR SBC
  193. SUB
  194. OPRdA (B54)
  195. IN OUT
  196. OPRdK (B55)
  197. ANDI CPI LDI ORI SBCI SBR SUBI
  198. OPAb (B55)
  199. CBI SBI SBIC SBIS
  200. OPNA (B56)
  201. BREAK CL[C,H,I,N,S,T,V,Z] SE[C,H,I,N,S,T,V,Z] EIJMP ICALL
  202. EICALL IJMP NOP RET RETI SLEEP WDR
  203. OPb (B57)
  204. BCLR BSET
  205. OPRdb (B57)
  206. BLD BST SBRC SBRS
  207. Special (B57,B60)
  208. CLR TST LSL LD ST
  209. Flow (B58)
  210. RJMP RCALL
  211. BR[BC,BS,CC,CS,EQ,NE,GE,HC,HS,ID,IE,LO,LT,MI,PL,SH,TC,TS,VC,VS]
  212. Flow macros (B61)
  213. LBL! LBL, SKIP, TO, FLBL, FLBL! BEGIN, AGAIN? AGAIN, IF, THEN,