asm.ms 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. .ft CW
  2. .ta 8n +8n +8n +8n +8n +8n +8n
  3. .ft
  4. .TL
  5. A Manual for the Plan 9 assembler
  6. .AU
  7. .I "Rob Pike"
  8. .AI
  9. rob@plan9.bell-labs.com
  10. .SH
  11. Machines
  12. .PP
  13. There is an assembler for each of the MIPS, SPARC, Intel 386,
  14. Motorola 68020 and 68000, IBM Power PC, DEC Alpha, and ARM.
  15. The 68020 assembler,
  16. .CW 2a ,
  17. is the oldest and in many ways the prototype.
  18. The assemblers are really just variations of a single program:
  19. they share many properties such as left-to-right assignment order for
  20. instruction operands and the synthesis of macro instructions
  21. such as
  22. .CW MOVE
  23. to hide the peculiarities of the load and store structure of the machines.
  24. To keep things concrete, the first part of this manual is
  25. specifically about the 68020.
  26. At the end is a description of the differences among
  27. the other assemblers.
  28. .ig
  29. .PP
  30. The document, ``How to Use the Plan 9 C Compiler'', by Rob Pike,
  31. is a prerequisite for this manual.
  32. ..
  33. .SH
  34. Registers
  35. .PP
  36. All pre-defined symbols in the assembler are upper-case.
  37. Data registers are
  38. .CW R0
  39. through
  40. .CW R7 ;
  41. address registers are
  42. .CW A0
  43. through
  44. .CW A7 ;
  45. floating-point registers are
  46. .CW F0
  47. through
  48. .CW F7 .
  49. .PP
  50. A pointer in
  51. .CW A6
  52. is used by the C compiler to point to data, enabling short addresses to
  53. be used more often.
  54. The value of
  55. .CW A6
  56. is constant and must be set during C program initialization
  57. to the address of the externally-defined symbol
  58. .CW a6base .
  59. .PP
  60. The following hardware registers are defined in the assembler; their
  61. meaning should be obvious given a 68020 manual:
  62. .CW CAAR ,
  63. .CW CACR ,
  64. .CW CCR ,
  65. .CW DFC ,
  66. .CW ISP ,
  67. .CW MSP ,
  68. .CW SFC ,
  69. .CW SR ,
  70. .CW USP ,
  71. and
  72. .CW VBR .
  73. .PP
  74. The assembler also defines several pseudo-registers that
  75. manipulate the stack:
  76. .CW FP ,
  77. .CW SP ,
  78. and
  79. .CW TOS .
  80. .CW FP
  81. is the frame pointer, so
  82. .CW 0(FP)
  83. is the first argument,
  84. .CW 4(FP)
  85. is the second, and so on.
  86. .CW SP
  87. is the local stack pointer, where automatic variables are held
  88. (SP is a pseudo-register only on the 68020);
  89. .CW 0(SP)
  90. is the first automatic, and so on as with
  91. .CW FP .
  92. Finally,
  93. .CW TOS
  94. is the top-of-stack register, used for pushing parameters to procedures,
  95. saving temporary values, and so on.
  96. .PP
  97. The assembler and loader track these pseudo-registers so
  98. the above statements are true regardless of what has been
  99. pushed on the hardware stack, pointed to by
  100. .CW A7 .
  101. The name
  102. .CW A7
  103. refers to the hardware stack pointer, but beware of mixed use of
  104. .CW A7
  105. and the above stack-related pseudo-registers, which will cause trouble.
  106. Note, too, that the
  107. .CW PEA
  108. instruction is observed by the loader to
  109. alter SP and thus will insert a corresponding pop before all returns.
  110. The assembler accepts a label-like name to be attached to
  111. .CW FP
  112. and
  113. .CW SP
  114. uses, such as
  115. .CW p+0(FP) ,
  116. to help document that
  117. .CW p
  118. is the first argument to a routine.
  119. The name goes in the symbol table but has no significance to the result
  120. of the program.
  121. .SH
  122. Referring to data
  123. .PP
  124. All external references must be made relative to some pseudo-register,
  125. either
  126. .CW PC
  127. (the virtual program counter) or
  128. .CW SB
  129. (the ``static base'' register).
  130. .CW PC
  131. counts instructions, not bytes of data.
  132. For example, to branch to the second following instruction, that is,
  133. to skip one instruction, one may write
  134. .P1
  135. BRA 2(PC)
  136. .P2
  137. Labels are also allowed, as in
  138. .P1
  139. BRA return
  140. NOP
  141. return:
  142. RTS
  143. .P2
  144. When using labels, there is no
  145. .CW (PC)
  146. annotation.
  147. .PP
  148. The pseudo-register
  149. .CW SB
  150. refers to the beginning of the address space of the program.
  151. Thus, references to global data and procedures are written as
  152. offsets to
  153. .CW SB ,
  154. as in
  155. .P1
  156. MOVL $array(SB), TOS
  157. .P2
  158. to push the address of a global array on the stack, or
  159. .P1
  160. MOVL array+4(SB), TOS
  161. .P2
  162. to push the second (4-byte) element of the array.
  163. Note the use of an offset; the complete list of addressing modes is given below.
  164. Similarly, subroutine calls must use
  165. .CW SB :
  166. .P1
  167. BSR exit(SB)
  168. .P2
  169. File-static variables have syntax
  170. .P1
  171. local<>+4(SB)
  172. .P2
  173. The
  174. .CW <>
  175. will be filled in at load time by a unique integer.
  176. .PP
  177. When a program starts, it must execute
  178. .P1
  179. MOVL $a6base(SB), A6
  180. .P2
  181. before accessing any global data.
  182. (On machines such as the MIPS and SPARC that cannot load a register
  183. in a single instruction, constants are loaded through the static base
  184. register. The loader recognizes code that initializes the static
  185. base register and treats it specially. You must be careful, however,
  186. not to load large constants on such machines when the static base
  187. register is not set up, such as early in interrupt routines.)
  188. .SH
  189. Expressions
  190. .PP
  191. Expressions are mostly what one might expect.
  192. Where an offset or a constant is expected,
  193. a primary expression with unary operators is allowed.
  194. A general C constant expression is allowed in parentheses.
  195. .PP
  196. Source files are preprocessed exactly as in the C compiler, so
  197. .CW #define
  198. and
  199. .CW #include
  200. work.
  201. .SH
  202. Addressing modes
  203. .PP
  204. The simple addressing modes are shared by all the assemblers.
  205. Here, for completeness, follows a table of all the 68020 addressing modes,
  206. since that machine has the richest set.
  207. In the table,
  208. .CW o
  209. is an offset, which if zero may be elided, and
  210. .CW d
  211. is a displacement, which is a constant between -128 and 127 inclusive.
  212. Many of the modes listed have the same name;
  213. scrutiny of the format will show what default is being applied.
  214. For instance, indexed mode with no address register supplied operates
  215. as though a zero-valued register were used.
  216. For "offset" read "displacement."
  217. For "\f(CW.s\fP" read one of
  218. .CW .L ,
  219. or
  220. .CW .W
  221. followed by
  222. .CW *1 ,
  223. .CW *2 ,
  224. .CW *4 ,
  225. or
  226. .CW *8
  227. to indicate the size and scaling of the data.
  228. .IP
  229. .TS
  230. l lfCW.
  231. data register R0
  232. address register A0
  233. floating-point register F0
  234. special names CAAR, CACR, etc.
  235. constant $con
  236. floating point constant $fcon
  237. external symbol name+o(SB)
  238. local symbol name<>+o(SB)
  239. automatic symbol name+o(SP)
  240. argument name+o(FP)
  241. address of external $name+o(SB)
  242. address of local $name<>+o(SB)
  243. indirect post-increment (A0)+
  244. indirect pre-decrement -(A0)
  245. indirect with offset o(A0)
  246. indexed with offset o()(R0.s)
  247. indexed with offset o(A0)(R0.s)
  248. external indexed name+o(SB)(R0.s)
  249. local indexed name<>+o(SB)(R0.s)
  250. automatic indexed name+o(SP)(R0.s)
  251. parameter indexed name+o(FP)(R0.s)
  252. offset indirect post-indexed d(o())(R0.s)
  253. offset indirect post-indexed d(o(A0))(R0.s)
  254. external indirect post-indexed d(name+o(SB))(R0.s)
  255. local indirect post-indexed d(name<>+o(SB))(R0.s)
  256. automatic indirect post-indexed d(name+o(SP))(R0.s)
  257. parameter indirect post-indexed d(name+o(FP))(R0.s)
  258. offset indirect pre-indexed d(o()(R0.s))
  259. offset indirect pre-indexed d(o(A0))
  260. offset indirect pre-indexed d(o(A0)(R0.s))
  261. external indirect pre-indexed d(name+o(SB))
  262. external indirect pre-indexed d(name+o(SB)(R0.s))
  263. local indirect pre-indexed d(name<>+o(SB))
  264. local indirect pre-indexed d(name<>+o(SB)(R0.s))
  265. automatic indirect pre-indexed d(name+o(SP))
  266. automatic indirect pre-indexed d(name+o(SP)(R0.s))
  267. parameter indirect pre-indexed d(name+o(FP))
  268. parameter indirect pre-indexed d(name+o(FP)(R0.s))
  269. .TE
  270. .in
  271. .SH
  272. Laying down data
  273. .PP
  274. Placing data in the instruction stream, say for interrupt vectors, is easy:
  275. the pseudo-instructions
  276. .CW LONG
  277. and
  278. .CW WORD
  279. (but not
  280. .CW BYTE )
  281. lay down the value of their single argument, of the appropriate size,
  282. as if it were an instruction:
  283. .P1
  284. LONG $12345
  285. .P2
  286. places the long 12345 (base 10)
  287. in the instruction stream.
  288. (On most machines,
  289. the only such operator is
  290. .CW WORD
  291. and it lays down 32-bit quantities.
  292. The 386 has all three:
  293. .CW LONG ,
  294. .CW WORD ,
  295. and
  296. .CW BYTE .
  297. The AMD64 adds
  298. .CW QUAD
  299. for 64-bit values.)
  300. .PP
  301. Placing information in the data section is more painful.
  302. The pseudo-instruction
  303. .CW DATA
  304. does the work, given two arguments: an address at which to place the item,
  305. including its size,
  306. and the value to place there. For example, to define a character array
  307. .CW array
  308. containing the characters
  309. .CW abc
  310. and a terminating null:
  311. .P1
  312. DATA array+0(SB)/1, $'a'
  313. DATA array+1(SB)/1, $'b'
  314. DATA array+2(SB)/1, $'c'
  315. GLOBL array(SB), $4
  316. .P2
  317. or
  318. .P1
  319. DATA array+0(SB)/4, $"abc\ez"
  320. GLOBL array(SB), $4
  321. .P2
  322. The
  323. .CW /1
  324. defines the number of bytes to define,
  325. .CW GLOBL
  326. makes the symbol global, and the
  327. .CW $4
  328. says how many bytes the symbol occupies.
  329. Uninitialized data is zeroed automatically.
  330. The character
  331. .CW \ez
  332. is equivalent to the C
  333. .CW \e0.
  334. The string in a
  335. .CW DATA
  336. statement may contain a maximum of eight bytes;
  337. build larger strings piecewise.
  338. Two pseudo-instructions,
  339. .CW DYNT
  340. and
  341. .CW INIT ,
  342. allow the (obsolete) Alef compilers to build dynamic type information during the load
  343. phase.
  344. The
  345. .CW DYNT
  346. pseudo-instruction has two forms:
  347. .P1
  348. DYNT , ALEF_SI_5+0(SB)
  349. DYNT ALEF_AS+0(SB), ALEF_SI_5+0(SB)
  350. .P2
  351. In the first form,
  352. .CW DYNT
  353. defines the symbol to be a small unique integer constant, chosen by the loader,
  354. which is some multiple of the word size. In the second form,
  355. .CW DYNT
  356. defines the second symbol in the same way,
  357. places the address of the most recently
  358. defined text symbol in the array specified by the first symbol at the
  359. index defined by the value of the second symbol,
  360. and then adjusts the size of the array accordingly.
  361. .PP
  362. The
  363. .CW INIT
  364. pseudo-instruction takes the same parameters as a
  365. .CW DATA
  366. statement. Its symbol is used as the base of an array and the
  367. data item is installed in the array at the offset specified by the most recent
  368. .CW DYNT
  369. pseudo-instruction.
  370. The size of the array is adjusted accordingly.
  371. The
  372. .CW DYNT
  373. and
  374. .CW INIT
  375. pseudo-instructions are not implemented on the 68020.
  376. .SH
  377. Defining a procedure
  378. .PP
  379. Entry points are defined by the pseudo-operation
  380. .CW TEXT ,
  381. which takes as arguments the name of the procedure (including the ubiquitous
  382. .CW (SB) )
  383. and the number of bytes of automatic storage to pre-allocate on the stack,
  384. which will usually be zero when writing assembly language programs.
  385. On machines with a link register, such as the MIPS and SPARC,
  386. the special value -4 instructs the loader to generate no PC save
  387. and restore instructions, even if the function is not a leaf.
  388. Here is a complete procedure that returns the sum
  389. of its two arguments:
  390. .P1
  391. TEXT sum(SB), $0
  392. MOVL arg1+0(FP), R0
  393. ADDL arg2+4(FP), R0
  394. RTS
  395. .P2
  396. An optional middle argument
  397. to the
  398. .CW TEXT
  399. pseudo-op is a bit field of options to the loader.
  400. Setting the 1 bit suspends profiling the function when profiling is enabled for the rest of
  401. the program.
  402. For example,
  403. .P1
  404. TEXT sum(SB), 1, $0
  405. MOVL arg1+0(FP), R0
  406. ADDL arg2+4(FP), R0
  407. RTS
  408. .P2
  409. will not be profiled; the first version above would be.
  410. Subroutines with peculiar state, such as system call routines,
  411. should not be profiled.
  412. .PP
  413. Setting the 2 bit allows multiple definitions of the same
  414. .CW TEXT
  415. symbol in a program; the loader will place only one such function in the image.
  416. It was emitted only by the Alef compilers.
  417. .PP
  418. Subroutines to be called from C should place their result in
  419. .CW R0 ,
  420. even if it is an address.
  421. Floating point values are returned in
  422. .CW F0 .
  423. Functions that return a structure to a C program
  424. receive as their first argument the address of the location to
  425. store the result;
  426. .CW R0
  427. is unused in the calling protocol for such procedures.
  428. A subroutine is responsible for saving its own registers,
  429. and therefore is free to use any registers without saving them (``caller saves'').
  430. .CW A6
  431. and
  432. .CW A7
  433. are the exceptions as described above.
  434. .SH
  435. When in doubt
  436. .PP
  437. If you get confused, try using the
  438. .CW -S
  439. option to
  440. .CW 2c
  441. and compiling a sample program.
  442. The standard output is valid input to the assembler.
  443. .SH
  444. Instructions
  445. .PP
  446. The instruction set of the assembler is not identical to that
  447. of the machine.
  448. It is chosen to match what the compiler generates, augmented
  449. slightly by specific needs of the operating system.
  450. For example,
  451. .CW 2a
  452. does not distinguish between the various forms of
  453. .CW MOVE
  454. instruction: move quick, move address, etc. Instead the context
  455. does the job. For example,
  456. .P1
  457. MOVL $1, R1
  458. MOVL A0, R2
  459. MOVW SR, R3
  460. .P2
  461. generates official
  462. .CW MOVEQ ,
  463. .CW MOVEA ,
  464. and
  465. .CW MOVESR
  466. instructions.
  467. A number of instructions do not have the syntax necessary to specify
  468. their entire capabilities. Notable examples are the bitfield
  469. instructions, the
  470. multiply and divide instructions, etc.
  471. For a complete set of generated instruction names (in
  472. .CW 2a
  473. notation, not Motorola's) see the file
  474. .CW /sys/src/cmd/2c/2.out.h .
  475. Despite its name, this file contains an enumeration of the
  476. instructions that appear in the intermediate files generated
  477. by the compiler, which correspond exactly to lines of assembly language.
  478. .PP
  479. The MC68000 assembler,
  480. .CW 1a ,
  481. is essentially the same, honoring the appropriate subset of the instructions
  482. and addressing modes.
  483. The definitions of these are, nonetheless, part of
  484. .CW 2.out.h .
  485. .SH
  486. Laying down instructions
  487. .PP
  488. The loader modifies the code produced by the assembler and compiler.
  489. It folds branches,
  490. copies short sequences of code to eliminate branches,
  491. and discards unreachable code.
  492. The first instruction of every function is assumed to be reachable.
  493. The pseudo-instruction
  494. .CW NOP ,
  495. which you may see in compiler output,
  496. means no instruction at all, rather than an instruction that does nothing.
  497. The loader discards all
  498. .CW NOP 's.
  499. .PP
  500. To generate a true
  501. .CW NOP
  502. instruction, or any other instruction not known to the assembler, use a
  503. .CW WORD
  504. pseudo-instruction.
  505. Such instructions on RISCs are not scheduled by the loader and must have
  506. their delay slots filled manually.
  507. .SH
  508. MIPS
  509. .PP
  510. The registers are only addressed by number:
  511. .CW R0
  512. through
  513. .CW R31 .
  514. .CW R29
  515. is the stack pointer;
  516. .CW R30
  517. is used as the static base pointer, the analogue of
  518. .CW A6
  519. on the 68020.
  520. Its value is the address of the global symbol
  521. .CW setR30(SB) .
  522. The register holding returned values from subroutines is
  523. .CW R1 .
  524. When a function is called, space for the first argument
  525. is reserved at
  526. .CW 0(FP)
  527. but in C (not Alef) the value is passed in
  528. .CW R1
  529. instead.
  530. .PP
  531. The loader uses
  532. .CW R28
  533. as a temporary. The system uses
  534. .CW R26
  535. and
  536. .CW R27
  537. as interrupt-time temporaries. Therefore none of these registers
  538. should be used in user code.
  539. .PP
  540. The control registers are not known to the assembler.
  541. Instead they are numbered registers
  542. .CW M0 ,
  543. .CW M1 ,
  544. etc.
  545. Use this trick to access, say,
  546. .CW STATUS :
  547. .P1
  548. #define STATUS 12
  549. MOVW M(STATUS), R1
  550. .P2
  551. .PP
  552. Floating point registers are called
  553. .CW F0
  554. through
  555. .CW F31 .
  556. By convention,
  557. .CW F24
  558. must be initialized to the value 0.0,
  559. .CW F26
  560. to 0.5,
  561. .CW F28
  562. to 1.0, and
  563. .CW F30
  564. to 2.0;
  565. this is done by the operating system.
  566. .PP
  567. The instructions and their syntax are different from those of the manufacturer's
  568. manual.
  569. There are no
  570. .CW lui
  571. and kin; instead there are
  572. .CW MOVW
  573. (move word),
  574. .CW MOVH
  575. (move halfword),
  576. and
  577. .CW MOVB
  578. (move byte) pseudo-instructions. If the operand is unsigned, the instructions
  579. are
  580. .CW MOVHU
  581. and
  582. .CW MOVBU .
  583. The order of operands is from left to right in dataflow order, just as
  584. on the 68020 but not as in MIPS documentation.
  585. This means that the
  586. .CW Bcond
  587. instructions are reversed with respect to the book; for example, a
  588. .CW va
  589. .CW BGTZ
  590. generates a MIPS
  591. .CW bltz
  592. instruction.
  593. .PP
  594. The assembler is for the R2000, R3000, and most of the R4000 and R6000 architectures.
  595. It understands the 64-bit instructions
  596. .CW MOVV ,
  597. .CW MOVVL ,
  598. .CW ADDV ,
  599. .CW ADDVU ,
  600. .CW SUBV ,
  601. .CW SUBVU ,
  602. .CW MULV ,
  603. .CW MULVU ,
  604. .CW DIVV ,
  605. .CW DIVVU ,
  606. .CW SLLV ,
  607. .CW SRLV ,
  608. and
  609. .CW SRAV .
  610. The assembler does not have any cache, load-linked, or store-conditional instructions.
  611. .PP
  612. Some assembler instructions are expanded into multiple instructions by the loader.
  613. For example the loader may convert the load of a 32 bit constant into an
  614. .CW lui
  615. followed by an
  616. .CW ori .
  617. .PP
  618. Assembler instructions should be laid out as if there
  619. were no load, branch, or floating point compare delay slots;
  620. the loader will rearrange\(em\f2schedule\f1\(emthe instructions
  621. to guarantee correctness and improve performance.
  622. The only exception is that the correct scheduling of instructions
  623. that use control registers varies from model to model of machine
  624. (and is often undocumented) so you should schedule such instructions
  625. by hand to guarantee correct behavior.
  626. The loader generates
  627. .P1
  628. NOR R0, R0, R0
  629. .P2
  630. when it needs a true no-op instruction.
  631. Use exactly this instruction when scheduling code manually;
  632. the loader recognizes it and schedules the code before it and after it independently. Also,
  633. .CW WORD
  634. pseudo-ops are scheduled like no-ops.
  635. .PP
  636. The
  637. .CW NOSCHED
  638. pseudo-op disables instruction scheduling
  639. (scheduling is enabled by default);
  640. .CW SCHED
  641. re-enables it.
  642. Branch folding, code copying, and dead code elimination are
  643. disabled for instructions that are not scheduled.
  644. .SH
  645. SPARC
  646. .PP
  647. Once you understand the Plan 9 model for the MIPS, the SPARC is familiar.
  648. Registers have numerical names only:
  649. .CW R0
  650. through
  651. .CW R31 .
  652. Forget about register windows: Plan 9 doesn't use them at all.
  653. The machine has 32 global registers, period.
  654. .CW R1
  655. [sic] is the stack pointer.
  656. .CW R2
  657. is the static base register, with value the address of
  658. .CW setSB(SB) .
  659. .CW R7
  660. is the return register and also the register holding the first
  661. argument to a C (not Alef) function, again with space reserved at
  662. .CW 0(FP) .
  663. .CW R14
  664. is the loader temporary.
  665. .PP
  666. Floating-point registers are exactly as on the MIPS.
  667. .PP
  668. The control registers are known by names such as
  669. .CW FSR .
  670. The instructions to access these registers are
  671. .CW MOVW
  672. instructions, for example
  673. .P1
  674. MOVW Y, R8
  675. .P2
  676. for the SPARC instruction
  677. .P1
  678. rdy %r8
  679. .P2
  680. .PP
  681. Move instructions are similar to those on the MIPS: pseudo-operations
  682. that turn into appropriate sequences of
  683. .CW sethi
  684. instructions, adds, etc.
  685. Instructions read from left to right. Because the arguments are
  686. flipped to
  687. .CW SUBCC ,
  688. the condition codes are not inverted as on the MIPS.
  689. .PP
  690. The syntax for the ASI stuff is, for example to move a word from ASI 2:
  691. .P1
  692. MOVW (R7, 2), R8
  693. .P2
  694. The syntax for double indexing is
  695. .P1
  696. MOVW (R7+R8), R9
  697. .P2
  698. .PP
  699. The SPARC's instruction scheduling is similar to the MIPS's.
  700. The official no-op instruction is:
  701. .P1
  702. ORN R0, R0, R0
  703. .P2
  704. .SH
  705. i386
  706. .PP
  707. The assembler assumes 32-bit protected mode.
  708. The register names are
  709. .CW SP ,
  710. .CW AX ,
  711. .CW BX ,
  712. .CW CX ,
  713. .CW DX ,
  714. .CW BP ,
  715. .CW DI ,
  716. and
  717. .CW SI .
  718. The stack pointer (not a pseudo-register) is
  719. .CW SP
  720. and the return register is
  721. .CW AX .
  722. There is no physical frame pointer but, as for the MIPS,
  723. .CW FP
  724. is a pseudo-register that acts as
  725. a frame pointer.
  726. .PP
  727. Opcode names are mostly the same as those listed in the Intel manual
  728. with an
  729. .CW L ,
  730. .CW W ,
  731. or
  732. .CW B
  733. appended to identify 32-bit,
  734. 16-bit, and 8-bit operations.
  735. The exceptions are loads, stores, and conditionals.
  736. All load and store opcodes to and from general registers, special registers
  737. (such as
  738. .CW CR0,
  739. .CW CR3,
  740. .CW GDTR,
  741. .CW IDTR,
  742. .CW SS,
  743. .CW CS,
  744. .CW DS,
  745. .CW ES,
  746. .CW FS,
  747. and
  748. .CW GS )
  749. or memory are written
  750. as
  751. .P1
  752. MOV\f2x\fP src,dst
  753. .P2
  754. where
  755. .I x
  756. is
  757. .CW L ,
  758. .CW W ,
  759. or
  760. .CW B .
  761. Thus to get
  762. .CW AL
  763. use a
  764. .CW MOVB
  765. instruction. If you need to access
  766. .CW AH ,
  767. you must mention it explicitly in a
  768. .CW MOVB :
  769. .P1
  770. MOVB AH, BX
  771. .P2
  772. There are many examples of illegal moves, for example,
  773. .P1
  774. MOVB BP, DI
  775. .P2
  776. that the loader actually implements as pseudo-operations.
  777. .PP
  778. The names of conditions in all conditional instructions
  779. .CW J , (
  780. .CW SET )
  781. follow the conventions of the 68020 instead of those of the Intel
  782. assembler:
  783. .CW JOS ,
  784. .CW JOC ,
  785. .CW JCS ,
  786. .CW JCC ,
  787. .CW JEQ ,
  788. .CW JNE ,
  789. .CW JLS ,
  790. .CW JHI ,
  791. .CW JMI ,
  792. .CW JPL ,
  793. .CW JPS ,
  794. .CW JPC ,
  795. .CW JLT ,
  796. .CW JGE ,
  797. .CW JLE ,
  798. and
  799. .CW JGT
  800. instead of
  801. .CW JO ,
  802. .CW JNO ,
  803. .CW JB ,
  804. .CW JNB ,
  805. .CW JZ ,
  806. .CW JNZ ,
  807. .CW JBE ,
  808. .CW JNBE ,
  809. .CW JS ,
  810. .CW JNS ,
  811. .CW JP ,
  812. .CW JNP ,
  813. .CW JL ,
  814. .CW JNL ,
  815. .CW JLE ,
  816. and
  817. .CW JNLE .
  818. .PP
  819. The addressing modes have syntax like
  820. .CW AX ,
  821. .CW (AX) ,
  822. .CW (AX)(BX*4) ,
  823. .CW 10(AX) ,
  824. and
  825. .CW 10(AX)(BX*4) .
  826. The offsets from
  827. .CW AX
  828. can be replaced by offsets from
  829. .CW FP
  830. or
  831. .CW SB
  832. to access names, for example
  833. .CW extern+5(SB)(AX*2) .
  834. .PP
  835. Other notes: Non-relative
  836. .CW JMP
  837. and
  838. .CW CALL
  839. have a
  840. .CW *
  841. added to the syntax.
  842. Only
  843. .CW LOOP ,
  844. .CW LOOPEQ ,
  845. and
  846. .CW LOOPNE
  847. are legal loop instructions. Only
  848. .CW REP
  849. and
  850. .CW REPN
  851. are recognized repeaters. These are not prefixes, but rather
  852. stand-alone opcodes that precede the strings, for example
  853. .P1
  854. CLD; REP; MOVSL
  855. .P2
  856. Segment override prefixes in
  857. .CW MOD/RM
  858. fields are not supported.
  859. .SH
  860. AMD64
  861. .PP
  862. The assembler's conventions are similar to those for the 386, above.
  863. The architecture provides extra fixed-point registers
  864. .CW R8
  865. to
  866. .CW R15 .
  867. All registers are 64 bit, but instructions access low-order 8, 16 and 32 bits
  868. as described in the processor handbook.
  869. For example,
  870. .CW MOVL
  871. to
  872. .CW AX
  873. puts a value in the low-order 32 bits and clears the top 32 bits to zero.
  874. Literal operands are limited to signed 32 bit values, which are sign-extended
  875. to 64 bits in 64 bit operations; the exception is
  876. .CW MOVQ ,
  877. which allows 64-bit literals.
  878. MMX registers are
  879. .CW M0
  880. to
  881. .CW M7 ,
  882. and
  883. XMM registers are
  884. .CW X0
  885. to
  886. .CW X15 .
  887. .PP
  888. There are many new instructions, including the MMX and XMM media instructions,
  889. and conditional move instructions.
  890. As with the 386 instruction names,
  891. all new 64-bit integer instructions, and the MMX and XMM instructions
  892. uniformly use
  893. .CW L
  894. for `long word' (32 bits) and
  895. .CW Q
  896. for `quad word' (64 bits).
  897. Some instructions use
  898. .CW O
  899. (`octword') for 128-bit values, where the processor handbook
  900. variously uses
  901. .CW O
  902. or
  903. .CW DQ .
  904. The assembler also consistently uses
  905. .CW PL
  906. for `packed long' in
  907. XMM instructions, instead of
  908. .CW Q ,
  909. .CW DQ
  910. or
  911. .CW PI .
  912. Either
  913. .CW MOVL
  914. or
  915. .CW MOVQ
  916. can be used to move values to and from control registers, even when
  917. the registers might be 64 bits.
  918. The assembler often accepts the handbook's name to ease conversion
  919. of existing code (but remember that the operand order is uniformly
  920. source then destination).
  921. .PP
  922. C's
  923. .CW "long long"
  924. type is 64 bits, but passed and returned by value, not by reference.
  925. More notably, C pointer values are 64 bits, and thus
  926. .CW "long long"
  927. and
  928. .CW "unsigned long long"
  929. are the only integer types wide enough to hold a pointer value.
  930. The C compiler and library use the XMM floating-point instructions, not
  931. the old 387 ones, although the latter are implemented by assembler and loader.
  932. The compiler provides external registers,
  933. allocated from
  934. .CW R15
  935. down.
  936. .PP
  937. The calling conventions are different from the 386.
  938. .CW CALL
  939. pushes, and
  940. .CW RET
  941. pops a 64-bit return address on the stack.
  942. The first integer or pointer argument is passed in a register, which is
  943. .CW BP
  944. for an integer or pointer (it can be referred to in assembly code by the pseudonym
  945. .CW RARG ).
  946. .CW AX
  947. holds the return value from subroutines as before.
  948. Floating-point results are returned in
  949. .CW X0 ,
  950. although currently the first parameter is not passed in a register if floating-point.
  951. All parameters less than 8 bytes in length have 8 byte slots reserved on the stack
  952. to preserve alignment and simplify variable-length argument list access,
  953. including the first parameter when passed in a register,
  954. although bytes 4 to 7 are not initialized.
  955. .PP
  956. The assembler assumes 64-bit mode unless a
  957. .CW MODE
  958. pseudo-operation is given:
  959. .P1
  960. MODE $32
  961. .P2
  962. to change to 32-bit mode.
  963. The effect is mainly to diagnose instructions that are illegal in
  964. the given mode, but the loader will also assume 32-bit operands and addresses,
  965. and 32-bit PC values for call and return.
  966. .SH
  967. Alpha
  968. .PP
  969. On the Alpha, all registers are 64 bits. The architecture handles 32-bit values
  970. by giving them a canonical format (sign extension in the case of integer registers).
  971. Registers are numbered
  972. .CW R0
  973. through
  974. .CW R31 .
  975. .CW R0
  976. holds the return value from subroutines, and also the first parameter.
  977. .CW R30
  978. is the stack pointer,
  979. .CW R29
  980. is the static base,
  981. .CW R26
  982. is the link register, and
  983. .CW R27
  984. and
  985. .CW R28
  986. are linker temporaries.
  987. .PP
  988. Floating point registers are numbered
  989. .CW F0
  990. to
  991. .CW F31 .
  992. .CW F28
  993. contains
  994. .CW 0.5 ,
  995. .CW F29
  996. contains
  997. .CW 1.0 ,
  998. and
  999. .CW F30
  1000. contains
  1001. .CW 2.0 .
  1002. .CW F31
  1003. is always
  1004. .CW 0.0
  1005. on the Alpha.
  1006. .PP
  1007. The extension character for
  1008. .CW MOV
  1009. follows DEC's notation:
  1010. .CW B
  1011. for byte (8 bits),
  1012. .CW W
  1013. for word (16 bits),
  1014. .CW L
  1015. for long (32 bits),
  1016. and
  1017. .CW Q
  1018. for quadword (64 bits).
  1019. Byte and ``word'' loads and stores may be made unsigned
  1020. by appending a
  1021. .CW U .
  1022. .CW S
  1023. and
  1024. .CW T
  1025. refer to IEEE floating point single precision (32 bits) and double precision (64 bits), respectively.
  1026. .SH
  1027. PowerPC
  1028. .PP
  1029. The PowerPC follows the Plan 9 model set by the MIPS and SPARC,
  1030. not the elaborate ABIs.
  1031. The 32-bit instructions of the 60x and 8xx PowerPC architectures are supported;
  1032. there is no support for the older POWER instructions.
  1033. Registers are
  1034. .CW R0
  1035. through
  1036. .CW R31 .
  1037. .CW R0
  1038. is initialized to zero; this is done by C start up code
  1039. and assumed by the compiler and loader.
  1040. .CW R1
  1041. is the stack pointer.
  1042. .CW R2
  1043. is the static base register, with value the address of
  1044. .CW setSB(SB) .
  1045. .CW R3
  1046. is the return register and also the register holding the first
  1047. argument to a C function, with space reserved at
  1048. .CW 0(FP)
  1049. as on the MIPS.
  1050. .CW R31
  1051. is the loader temporary.
  1052. The external registers in Plan 9's C are allocated from
  1053. .CW R30
  1054. down.
  1055. .PP
  1056. Floating point registers are called
  1057. .CW F0
  1058. through
  1059. .CW F31 .
  1060. By convention, several registers are initialized
  1061. to specific values; this is done by the operating system.
  1062. .CW F27
  1063. must be initialized to the value
  1064. .CW 0x4330000080000000
  1065. (used by float-to-int conversion),
  1066. .CW F28
  1067. to the value 0.0,
  1068. .CW F29
  1069. to 0.5,
  1070. .CW F30
  1071. to 1.0, and
  1072. .CW F31
  1073. to 2.0.
  1074. .PP
  1075. As on the MIPS and SPARC, the assembler accepts arbitrary literals
  1076. as operands to
  1077. .CW MOVW ,
  1078. and also to
  1079. .CW ADD
  1080. and others where `immediate' variants exist,
  1081. and the loader generates sequences
  1082. of
  1083. .CW addi ,
  1084. .CW addis ,
  1085. .CW oris ,
  1086. etc. as required.
  1087. The register indirect addressing modes use the same syntax as the SPARC,
  1088. including double indexing when allowed.
  1089. .PP
  1090. The instruction names are generally derived from the Motorola ones,
  1091. subject to slight transformation:
  1092. the
  1093. .CW . ' `
  1094. marking the setting of condition codes is replaced by
  1095. .CW CC ,
  1096. and when the letter
  1097. .CW o ' `
  1098. represents `OE=1' it is replaced by
  1099. .CW V .
  1100. Thus
  1101. .CW add ,
  1102. .CW addo.
  1103. and
  1104. .CW subfzeo.
  1105. become
  1106. .CW ADD ,
  1107. .CW ADDVCC
  1108. and
  1109. .CW SUBFZEVCC .
  1110. As well as the three-operand conditional branch instruction
  1111. .CW BC ,
  1112. the assembler provides pseudo-instructions for the common cases:
  1113. .CW BEQ ,
  1114. .CW BNE ,
  1115. .CW BGT ,
  1116. .CW BGE ,
  1117. .CW BLT ,
  1118. .CW BLE ,
  1119. .CW BVC ,
  1120. and
  1121. .CW BVS .
  1122. The unconditional branch instruction is
  1123. .CW BR .
  1124. Indirect branches use
  1125. .CW "(CTR)"
  1126. or
  1127. .CW "(LR)"
  1128. as target.
  1129. .PP
  1130. Load or store operations are replaced by
  1131. .CW MOV
  1132. variants in the usual way:
  1133. .CW MOVW
  1134. (move word),
  1135. .CW MOVH
  1136. (move halfword with sign extension), and
  1137. .CW MOVB
  1138. (move byte with sign extension, a pseudo-instruction),
  1139. with unsigned variants
  1140. .CW MOVHZ
  1141. and
  1142. .CW MOVBZ ,
  1143. and byte-reversing
  1144. .CW MOVWBR
  1145. and
  1146. .CW MOVHBR .
  1147. `Load or store with update' versions are
  1148. .CW MOVWU ,
  1149. .CW MOVHU ,
  1150. and
  1151. .CW MOVBZU .
  1152. Load or store multiple is
  1153. .CW MOVMW .
  1154. The exceptions are the string instructions, which are
  1155. .CW LSW
  1156. and
  1157. .CW STSW ,
  1158. and the reservation instructions
  1159. .CW lwarx
  1160. and
  1161. .CW stwcx. ,
  1162. which are
  1163. .CW LWAR
  1164. and
  1165. .CW STWCCC ,
  1166. all with operands in the usual data-flow order.
  1167. Floating-point load or store instructions are
  1168. .CW FMOVD ,
  1169. .CW FMOVDU ,
  1170. .CW FMOVS ,
  1171. and
  1172. .CW FMOVSU .
  1173. The register to register move instructions
  1174. .CW fmr
  1175. and
  1176. .CW fmr.
  1177. are written
  1178. .CW FMOVD
  1179. and
  1180. .CW FMOVDCC .
  1181. .PP
  1182. The assembler knows the commonly used special purpose registers:
  1183. .CW CR ,
  1184. .CW CTR ,
  1185. .CW DEC ,
  1186. .CW LR ,
  1187. .CW MSR ,
  1188. and
  1189. .CW XER .
  1190. The rest, which are often architecture-dependent, are referenced as
  1191. .CW SPR(n) .
  1192. The segment registers of the 60x series are similarly
  1193. .CW SEG(n) ,
  1194. but
  1195. .I n
  1196. can also be a register name, as in
  1197. .CW SEG(R3) .
  1198. Moves between special purpose registers and general purpose ones,
  1199. when allowed by the architecture,
  1200. are written as
  1201. .CW MOVW ,
  1202. replacing
  1203. .CW mfcr ,
  1204. .CW mtcr ,
  1205. .CW mfmsr ,
  1206. .CW mtmsr ,
  1207. .CW mtspr ,
  1208. .CW mfspr ,
  1209. .CW mftb ,
  1210. and many others.
  1211. .PP
  1212. The fields of the condition register
  1213. .CW CR
  1214. are referenced as
  1215. .CW CR(0)
  1216. through
  1217. .CW CR(7) .
  1218. They are used by the
  1219. .CW MOVFL
  1220. (move field) pseudo-instruction,
  1221. which produces
  1222. .CW mcrf
  1223. or
  1224. .CW mtcrf .
  1225. For example:
  1226. .P1
  1227. MOVFL CR(3), CR(0)
  1228. MOVFL R3, CR(1)
  1229. MOVFL R3, $7, CR
  1230. .P2
  1231. They are also accepted in
  1232. the conditional branch instruction, for example
  1233. .P1
  1234. BEQ CR(7), label
  1235. .P2
  1236. Fields of the
  1237. .CW FPSCR
  1238. are accessed using
  1239. .CW MOVFL
  1240. in a similar way:
  1241. .P1
  1242. MOVFL FPSCR, F0
  1243. MOVFL F0, FPSCR
  1244. MOVFL F0, $7, FPSCR
  1245. MOVFL $0, FPSCR(3)
  1246. .P2
  1247. producing
  1248. .CW mffs ,
  1249. .CW mtfsf ,
  1250. or
  1251. .CW mtfsfi
  1252. as appropriate.
  1253. .SH
  1254. ARM
  1255. .PP
  1256. The assembler provides access to
  1257. .CW R0
  1258. through
  1259. .CW R14
  1260. and the
  1261. .CW PC .
  1262. The stack pointer is
  1263. .CW R13 ,
  1264. the link register is
  1265. .CW R14 ,
  1266. and the static base register is
  1267. .CW R12 .
  1268. .CW R0
  1269. is the return register and also the register holding
  1270. the first argument to a subroutine.
  1271. The assembler supports the
  1272. .CW CPSR
  1273. and
  1274. .CW SPSR
  1275. registers.
  1276. It also knows about coprocessor registers
  1277. .CW C0
  1278. through
  1279. .CW C15 .
  1280. Floating registers are
  1281. .CW F0
  1282. through
  1283. .CW F7 ,
  1284. .CW FPSR
  1285. and
  1286. .CW FPCR .
  1287. .PP
  1288. As with the other architectures, loads and stores are called
  1289. .CW MOV ,
  1290. e.g.
  1291. .CW MOVW
  1292. for load word or store word, and
  1293. .CW MOVM
  1294. for
  1295. load or store multiple,
  1296. depending on the operands.
  1297. .PP
  1298. Addressing modes are supported by suffixes to the instructions:
  1299. .CW .IA
  1300. (increment after),
  1301. .CW .IB
  1302. (increment before),
  1303. .CW .DA
  1304. (decrement after), and
  1305. .CW .DB
  1306. (decrement before).
  1307. These can only be used with the
  1308. .CW MOV
  1309. instructions.
  1310. The move multiple instruction,
  1311. .CW MOVM ,
  1312. defines a range of registers using brackets, e.g.
  1313. .CW [R0-R12] .
  1314. The special
  1315. .CW MOVM
  1316. addressing mode bits
  1317. .CW W ,
  1318. .CW U ,
  1319. and
  1320. .CW P
  1321. are written in the same manner, for example,
  1322. .CW MOVM.DB.W .
  1323. A
  1324. .CW .S
  1325. suffix allows a
  1326. .CW MOVM
  1327. instruction to access user
  1328. .CW R13
  1329. and
  1330. .CW R14
  1331. when in another processor mode.
  1332. Shifts and rotates in addressing modes are supported by binary operators
  1333. .CW <<
  1334. (logical left shift),
  1335. .CW >>
  1336. (logical right shift),
  1337. .CW ->
  1338. (arithmetic right shift), and
  1339. .CW @>
  1340. (rotate right); for example
  1341. .CW "R7>>R2" or
  1342. .CW "R2@>2" .
  1343. The assembler does not support indexing by a shifted expression;
  1344. only names can be doubly indexed.
  1345. .PP
  1346. Any instruction can be followed by a suffix that makes the instruction conditional:
  1347. .CW .EQ ,
  1348. .CW .NE ,
  1349. and so on, as in the ARM manual, with synonyms
  1350. .CW .HS
  1351. (for
  1352. .CW .CS )
  1353. and
  1354. .CW .LO
  1355. (for
  1356. .CW .CC ),
  1357. for example
  1358. .CW ADD.NE .
  1359. Arithmetic
  1360. and logical instructions
  1361. can have a
  1362. .CW .S
  1363. suffix, as ARM allows, to set condition codes.
  1364. .PP
  1365. The syntax of the
  1366. .CW MCR
  1367. and
  1368. .CW MRC
  1369. coprocessor instructions is largely as in the manual, with the usual adjustments.
  1370. The assembler directly supports only the ARM floating-point coprocessor
  1371. operations used by the compiler:
  1372. .CW CMP ,
  1373. .CW ADD ,
  1374. .CW SUB ,
  1375. .CW MUL ,
  1376. and
  1377. .CW DIV ,
  1378. all with
  1379. .CW F
  1380. or
  1381. .CW D
  1382. suffix selecting single or double precision.
  1383. Floating-point load or store become
  1384. .CW MOVF
  1385. and
  1386. .CW MOVD .
  1387. Conversion instructions are also specified by moves:
  1388. .CW MOVWD ,
  1389. .CW MOVWF ,
  1390. .CW MOVDW ,
  1391. .CW MOVWD ,
  1392. .CW MOVFD ,
  1393. and
  1394. .CW MOVDF .