⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdk_tut.texi

📁 汇编语言编程源代码
💻 TEXI
📖 第 1 页 / 共 4 页
字号:
@end ftable@node Input-output operators, Conversion operators, Jump operators, MIX instruction set@comment  node-name,  next,  previous,  up@subsubsection Input-output operators@cindex input-output operatorsAs explained in previous sections (@pxref{MIX architecture}), the MIXcomputer can interact with a series of block devices. To that end, youhave at your disposal the following instructions:@ftable @code@item INTransfer a block of words from the specified unit to memory, starting ataddress M.OPCODE = 36, MOD = I/O unit.@item OUTTransfer a block of words from memory (starting at address M) to thespecified unit.OPCODE = 37, MOD = I/O unit.@item IOCPerfom a control operation (given by M) on the specified unit.OPCODE = 35, MOD = I/O unit.@item JREDJump to M if the specified unit is ready.OPCODE = 38, MOD = I/O unit.@item JBUSJump to M if the specified unit is busy.OPCODE = 34, MOD = I/O unit.@end ftable@noindentIn all the above instructions, the @samp{MOD} subfile must be in therange 0-20, since it denotes the operation's target device. The@samp{IOC} instruction only makes sense for tape devices (@samp{MOD} =0-7 or 20): it shifts the read/write pointer by the number of wordsgiven by @samp{M} (if it equals zero, the tape is rewound)@footnote{InKnuth's original definition, there are other control operationsavailable, but they do not make sense when implementing the blockdevices as disk files (as we do in @sc{mdk} simulator). For the samereason, @sc{mdk} devices are always ready, since all input-outputoperations are performed using synchronous system calls.}.@node Conversion operators, Shift operators, Input-output operators, MIX instruction set@comment  node-name,  next,  previous,  up@subsubsection Conversion operators@cindex conversion operatorsThe following instructions convert between numerical values and theircharacter representations.@ftable @code@item NUMConvert rAX, assumed to contain a character representation of a number,to its numerical value and store it in rA.OPCODE = 5, MOD = 0.@item CHARConvert the number stored in rA to a character representation and storeit in rAX.OPCODE = 5, MOD = 1.@end ftable@noindentDigits are represented in MIX by the range of values 30-39 (digits0-9). Thus, if the contents of @samp{rA} and @samp{rX} are, for instance,@example[rA] = + 30 30 31 32 33[rX] = + 31 35 39 30 34@end example@noindentthe represented number is 0012315904, and @samp{NUM} will store thisvalue in @samp{rA} (i.e., we end up with @samp{[rA]} = @w{+ 0 46 62 520} = 12315904. @samp{CHAR} performs the inverse operation.@node Shift operators, Miscellaneous operators, Conversion operators, MIX instruction set@comment  node-name,  next,  previous,  up@subsubsection Shift operators@cindex shift@cindex shift operatorsThe following instructions perform byte-wise shifts of the contents of@samp{rA} and @samp{rX}.@ftable @code@item SLA@itemx SRA@itemx SLAX@itemx SRAX@itemx SLC@itemx SRCShift rA or rAX left, right, or rAX circularly (see example below)left or right. M specifies the number of bytes to be shifted.OPCODE = 6, MOD = 0, 1, 2, 3, 4, 5.@end ftable@noindentIf we begin with, say, @samp{[rA]} = @w{- 01 02 03 04 05}, we wouldhave the following modifications to @samp{rA} contents when performingthe instructions on the left column:@multitable {SLA 00} {[rA] = - 00 00 00 00 00}@item SLA 2 @tab [rA] = - 03 04 05 00 00@item SRA 1 @tab [rA] = - 00 01 02 03 04@item SLC 3 @tab [rA] = - 04 05 01 02 03@item SRC 24 @tab [rA] = - 05 01 02 03 04@end multitable@noindentNote that the sign is unaffected by shift operations. On the other hand,@samp{SLAX} and @samp{SRAX} treat @samp{rA} and @samp{rX} as a single10-bytes register (ignoring again the signs), so that, if we begin with@samp{[rA]} = @w{+ 01 02 03 04 05} and @samp{[rX]} = @w{- 06 07 08 0910}, executing @samp{SLAX 3} would yield:@example[rA] = + 04 05 06 07 08  [rX] = - 09 10 00 00 00@end example@node Miscellaneous operators, Execution times, Shift operators, MIX instruction set@comment  node-name,  next,  previous,  up@subsubsection Miscellaneous operators@cindex miscellaneous operatorsFinally, we list in the following table three miscellaneous MIXinstructions which do not fit in any of the previous subsections:@ftable @code@item MOVEMove MOD words from M to the location stored in rI1.OPCODE = 7, MOD = no. of words.@item NOPNo operation. OPCODE = 0, MOD = 0.@item HLTHalt. Stops instruction fetching. OPCODE = 5, MOD = 2.@end ftable@noindentThe only effect of executing @samp{NOP} is increasing the locationcounter, while @samp{HLT} usually marks program termination.@node Execution times,  , Miscellaneous operators, MIX instruction set@comment  node-name,  next,  previous,  up@subsubsection Execution times@cindex exection time@cindex timeWhen writing MIXAL programs (or any kind of programs, for thatmatter), whe shall often be interested in their executiontime. Loosely speaking, we will interested in the answer to thequestion: how long takes a program to execute? Of course, thisexecution time will be a function of the input size, and the answer toour question is commonly given as the asymptotic behaviour as afunction of the input size. At any rate, to compute this asymptoticbehaviour, we need a measure of how long execution of a singleinstruction takes in our (virtual) CPU. Therefore, each MIXinstruction will have an associated execution time, given in arbitraryunits (in a real computer, the value of this unit will depend on thehardware configuration). When our MIX virtual machine executesprograms, it will (optionally) give you the value of their executiontime based upon the execution time of each single instruction.In the following table, the execution times (in the above mentionedarbitrary units) of the MIX instructions are given.@multitable {INSSSS} {01} {INSSSS} {01} {INSSSS} {01} {INSSSS} {01}@item @code{NOP} @tab 1 @tab @code{ADD} @tab 2 @tab @code{SUB}@tab 2 @tab @code{MUL} @tab 10@item @code{DIV} @tab 12 @tab @code{NUM} @tab 10 @tab @code{CHAR}@tab 10 @tab @code{HLT} @tab 10@item @code{SLx} @tab 2 @tab @code{SRx} @tab 2 @tab @code{LDx}@tab  2 @tab @code{STx} @tab 2 @item @code{JBUS} @tab 1 @tab @code{IOC} @tab 1 @tab @code{IN}@tab  1@tab @code{OUT} @tab 1@item @code{JRED} @tab 1 @tab @code{Jx} @tab 1 @tab @code{INCx}@tab  1 @tab @code{DECx} @tab 1 @item @code{ENTx} @tab 1 @tab @code{ENNx} @tab 1 @tab @code{CMPx}@tab  1 @tab @code{MOVE} @tab 1+2F  @end multitableIn the above table, 'F' stands for the number of blocks to be moved(given by the @code{FSPEC} subfield of the instruction); @code{SLx} and@code{SRx} are a short cut for the byte-shifting operations; @code{LDx}denote all the loading operations; @code{STx} are the storingoperations; @code{Jx} stands for all the jump operations, and so on withthe rest of abbreviations.@node MIXAL,  , The MIX computer, MIX and MIXAL tutorial@comment  node-name,  next,  previous,  up@section MIXAL@cindex MIXAL@cindex MIX assembly language@cindex assemblyIn the previous sections we have listed all the available MIX binaryinstructions. As we have shown, each instruction is represented by aword which is fetched from memory and executed by the MIX virtualCPU. As is the case with real computers, the MIX knows how to decodeinstructions in binary format (the so--called machine language), but ahuman programmer would have a tough time if she were to write herprograms in machine language. Fortunately, the MIX computer can beprogrammed using an assembly language, MIXAL, which provides a symbolicway of writing the binary instructions understood by the imaginary MIXcomputer. If you have used assembler languages before, you will findMIXAL a very familiar language. MIXAL source files are translatedto machine language by a MIX assembler, which produces a binary file (theactual MIX program) which can be directly loaded into the MIX memory andsubsequently executed.In this section, we describe MIXAL, the MIX assembly language. Theimplementation of the MIX assembler program and MIX computer simulatorprovided by @sc{mdk} are described later on (@pxref{Getting started}).@menu* Basic structure::             Writing basic MIXAL programs.* MIXAL directives::            Assembler directives.* Expressions::                 Evaluation of expressions.* W-expressions::               Evaluation of w-expressions.* Local symbols::               Special symbol table entries.* Literal constants::           Specifying an immediate operand.@end menu@node Basic structure, MIXAL directives, MIXAL, MIXAL@comment  node-name,  next,  previous,  up@subsection Basic program structureThe MIX assembler reads MIXAL files line by line, producing, whenrequired, a binary instruction, which is associated to a predefinedmemory address. To keep track of the current address, the assemblermaintains an internal location counter which is incremented each time aninstruction is compiled. In addition to MIX instructions, you caninclude in MIXAL file assembly directives (or pseudoinstructions)addressed at the assembler itself (for instance, telling it where theprogram starts and ends, or to reposition the location counter; see below).MIX instructions and assembler directives@footnote{We shall call them,collectively, MIXAL instructions.} are written in MIXAL (one persource file line) according to the following pattern:@example[LABEL]   MNEMONIC  [OPERAND]   [COMMENT]@end example@noindentwhere @samp{OPERAND} is of the form@example[ADDRESS][,INDEX][(MOD)]@end exampleItems between square brackets are optional, and@table @code@item LABELis an alphanumeric identifier (a @dfn{symbol}) which gets the currentvalue of the location counter, and can be used in subsequentexpressions,@item MNEMONICis a literal denoting the operation code of the instruction(e.g. @code{LDA}, @code{STA}; see @pxref{MIX instruction set}) or anassembly pseudoinstruction (e.g. @code{ORG}, @code{EQU}),@item ADDRESSis an expression evaluating to the address subfield of the instruction,@item INDEXis an expression evaluating to the index subfield of the instruction, whichdefaults to 0 (i.e., no use of indexing) and can only be used when @code{ADDRESS} is present,@item MODis an expression evaluating to the mod subfield of the instruction. Itsdefault value, when omitted, depends on @code{OPCODE},@item COMMENTany number of spaces after the operand mark the beggining of a comment,i.e. any text separated by white space from the operand is ignored bythe assembler (note that spaces are not allowed within the@samp{OPERAND} field).@end tableNote that spaces are @emph{not} allowed between the @code{ADDRESS},@code{INDEX} and @code{MOD} fields if they are present. White space isused to separate the label, operation code and operand parts of theinstruction@footnote{In fact, Knuth's definition of MIXAL restricts thecolumn number at which each of these instruction parts must start. TheMIXAL assembler included in @sc{mdk}, @code{mixasm}, does not imposesuch restriction.}.We have already listed the mnemonics associated will each MIXinstructions; sample MIXAL instructions representing MIX instructionsare:@exampleHERE     LDA  2000         HERE represents the current location counter         LDX  HERE,2(1:3)  this is a comment         JMP  1234@end example@node MIXAL directives, Expressions, Basic structure, MIXAL@comment  node-name,  next,  previous,  up@subsection MIXAL directivesMIXAL instructions can be either one of the MIX machine instructions(@pxref{MIX instruction set}) or one of the following assemblypseudoinstructions:@ftable @code@item ORIGSets the value of the memory address to which following instructionswill be allocated after compilation.@item EQUUsed to define a symbol's value, e.g. @w{@code{SYM  EQU  2*200/3}}.@item CONThe value of the given expression is copied directly into the currentmemory address.@item ALFTakes as operand five characters, constituting the five bytes of a wordwhich is copied directly into the current memory address.@item ENDMarks the end of the program. Its operand gives the start address forprogram execution.@end ftableThe operand of @code{ORIG}, @code{EQU}, @code{CON} and @code{END} can beany expression evaluating to a constant MIX word, i.e., either a simpleMIXAL expression (composed of numbers, symbols and binary operators,@pxref{Expressions}) or a w-expression (@pxref{W-expressions}).All MIXAL programs must contain an @code{END} directive, with a twofoldend: first, it marks the end of the assembler job, and, in the secondplace, its (mandatory) operand indicates the start address for the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -