Mirror of CollapseOS
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

162 строки
5.4KB

  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. On the BEGIN,..AGAIN, side, it's a bit different. You start
  70. with your BEGIN, instruction, and then later you issue a
  71. JRxx, instr followed by AGAIN,. Exactly like you would do
  72. with a label.
  73. On top of that, you have the very nice BREAK, instruction,
  74. which must also be preceded by a JRxx, and will jump to the
  75. PC following the next AGAIN,. Examples:
  76. IFZ, NOP, THEN,
  77. BEGIN, NOP, JR, AGAIN, ( unconditional )
  78. BEGIN, NOP, JRZ, AGAIN, ( conditional )
  79. BEGIN, NOP, JRZ, BREAK, JR, AGAIN, ( break off the loop )
  80. # Z80 Instructions list
  81. Letters in [] brackets indicate "argtype" variants. When the
  82. bracket starts with ",", it means that a "plain" mnemonic is
  83. available. For example, "RET," and "RETc," exist.
  84. Note that assemblers in Collapse OS are incomplete and opcode
  85. words were implemented in a "just-in-time" fashion, when needed.
  86. r => A B C D E H L (HL)
  87. d => BC DE HL AF/SP
  88. c => CNZ CZ CNC CC CPO CPE CP CM
  89. LD [rr, ri, di, (i)HL, HL(i), d(i), (i)d, rIXY, IXYr,
  90. (DE)A, A(DE), (i)A, A(i)]
  91. ADD [r, i, HLd, IXd, IXIX, IYd, IYIY]
  92. ADC [r, HLd]
  93. CP [r, i, (IXY+)]
  94. SBC [r, HLd]
  95. SUB [r, i]
  96. INC [r, d, (IXY+)]
  97. DEC [r, d, (IXY+)]
  98. AND [r, i]
  99. OR [r, i]
  100. XOR [r, i]
  101. OUT [iA, (C)r]
  102. IN [Ai, r(C)]
  103. JP [i, (HL), (IX), (IY)]
  104. JR [, Z, NZ, C, NC]
  105. PUSH POP
  106. SET RES BIT
  107. RL RLC SLA RLA RLCA
  108. RR RRC SRL RRA RRCA
  109. CALL RST DJNZ
  110. DI EI EXDEHL EXX HALT
  111. NOP RET [,c] RETI RETN SCF
  112. # 8086 assembler
  113. Load with "30 LOAD". As with the Z80 assembler, it is incom-
  114. plete.
  115. Mnemonics are followed by argument types. For example, MOVri,
  116. moves 8-bit immediate to 8-bit register.
  117. 'r' = 8-bit register 'x' = 16-bit register
  118. 'i' = 8-bit immediate 'I' = 16-bit immediate
  119. 's' = SREG register
  120. Mnemonics that only have one signature (for example INT,) don't
  121. have operands letters.
  122. For jumps, it's special. 's' is SHORT, 'n' is NEAR, 'f' is FAR.
  123. # 8086 Instructions list
  124. TODO