📄 mdk_gstart.texi
字号:
@c -*-texinfo-*-@c This is part of the GNU MDK Reference Manual.@c Copyright (C) 2000, 2001, 2002@c Free Software Foundation, Inc.@c See the file mdk.texi for copying conditions.@c $Id: mdk_gstart.texi,v 1.13 2002/04/08 00:26:38 jao Exp $@node Getting started, mixvm.el, MIX and MIXAL tutorial, Top@chapter Getting started@cindex tutorialIn this chapter, you will find a sample code-compile-run-debug sessionusing the @sc{mdk} utilities. Familiarity with the MIX mythical computerand its assembly language MIXAL (as described in Knuth's TAOCP) isassumed; for a compact reminder, see @ref{MIX and MIXAL tutorial}. @menu* Writing a source file:: A sample MIXAL source file.* Compiling:: Using @code{mixasm} to compile source files into binary format.* Running the program:: Running and debugging your programs.* Using mixguile:: Using the Scheme interpreter to run and debug your programs.* Using Scheme in mixvm and gmixvm:: @end menu@node Writing a source file, Compiling, Getting started, Getting started@section Writing a source file@cindex MIXAL@cindex source file@cindex .mixal fileMIXAL programs can be written as ASCII files with your editor of choice.Here you have the mandatory @emph{hello world} as written in the MIXALassembly language:@example* (1)* hello.mixal: say 'hello world' in MIXAL (2)* (3)* label ins operand comment (4)TERM EQU 19 the MIX console device number (5) ORIG 1000 start address (6)START OUT MSG(TERM) output data at address MSG (7) HLT halt execution (8)MSG ALF "MIXAL" (9) ALF " HELL" (10) ALF "O WOR" (11) ALF "LD " (12) END START end of the program (13)@end example @noindent MIXAL source files should have the extension @file{.mixal}when used with the @sc{mdk} utilities. As you can see in the abovesample, each line in a MIXAL file can be divided into four fieldsseparated by an arbitrary amount of whitespace characters (blanks and ortabs). While Knuth's definition of MIXAL each field must start at afixed pre-defined column number, the @sc{mdk} assembler loosens thisrequirement and lets you format the file as you see fit. The onlyrestrictions retained are for comment lines (like 1-4) which must beginwith an asterisk (*) placed at column 1, and for the label field (seebelow) which, if present, must also start at column 1. The four fieldsin each non-comment line are:@itemize @minus@iteman optional label, which either refers to the current memory address (as@code{START} and @code{MSG} in lines 7 and 9) or a defined symbol(@code{TERM}) (if present, the label must always start at the firstcolumn in its line, for the first whitespace in the line maks thebeginning of the second field),@iteman operation mnemonic, which can represent either a MIX instruction(@code{OUT} and @code{HLT} in lines 7 and 8 above), or an assemblypseudoinstruction (e.g., the @code{ORIG} pseudoinstruction in line6@footnote{If an @code{ORIG} directive is not used, the program willbe loaded by the virtual machine at address 0. @code{ORIG} allowsallocating the executable code where you see fit.}.@iteman optional operand for the (pseudo)instruction, and@iteman optional free text comment.@end itemize@noindent Lines 9-12 of the @file{hello.mixal} file above also show thesecond (and last) difference between Knuth's MIXAL definition and ours:the operand of the @code{ALF} pseudoinstruction (a word of fivecharacters) must be quoted with using ""@footnote{In Knuth's definition,the operand always starts at a fixed column number, and the use ofquotation is therefore unnecessary. As @code{mixasm} releases thisrequirement, marking the beginning and end of the @code{ALF} operanddisambiguates the parser's recognition of this operand when it includesblanks. Note that double-quotes (") are not part of the MIX characterset, and, therefore, no escape characters are needed within@code{ALF}'s operands.}. The workings of this sample program should be straightforward if you arefamiliar with MIXAL. See TAOCP vol. 1 for a thorough definition or@ref{MIX and MIXAL tutorial}, for a tutorial.@node Compiling, Running the program, Writing a source file, Getting started@section Compiling@cindex compiling@cindex binary programs@cindex virtual machine@cindex assembler@cindex @code{mixasm}Three simulators of the MIX computer, called @code{mixvm}, @code{gmixvm}and @code{mixguile}, are included in the @sc{mdk} tools. They are able torun binary files containing MIX instructions written in their binaryrepresentation. You can translate MIXAL source files into this binaryform using @code{mixasm}, the MIXAL assembler. So, in order to compilethe @file{hello.mixal} file, you can type the following command at yourshell prompt:@examplemixasm -g hello @key{RET}@end example@cindex .mix fileIf the source file contains no errors, this will produce a binary filecalled @file{hello.mix} which can be loaded and run by the MIX virtualmachine. The @code{-g} flag tells the assembler to include debuginformation in the executable file (for a complete description of allthe compilation options, see @ref{mixasm}). Now, your are ready to runyour first MIX program, as described in the following section.@node Running the program, Using mixguile, Compiling, Getting started@section Running the program@cindex @code{mixvm}@cindex non-interactive mode@cindex interactive modeMIX is a mythical computer, so it is no use ordering it from yourfavorite hardware provider. @sc{mdk} provides three software simulators ofthe computer, though. They are@itemize @bullet@item@code{mixvm}, a command line oriented simulator,@item@code{gmixvm}, a GTK based graphical interface to @code{mixvm}, and@item@code{mixguile}, a Guile shell with a built-in MIX simulator.@end itemizeAll three simulators accept the same set of user commands, but offer adifferent user interface, as noted above. In this section we shalldescribe some of these commands, and show you how to use them from@code{mixvm}'s command line. You can use them as well at @code{gmixvm}'scommand prompt (@pxref{gmixvm}), or using the built-in Scheme primitivesof @code{mixguile} (@pxref{Using mixguile}).Using the MIX simulators, you can run your MIXAL programs, aftercompiling them with @code{mixasm} into binary @file{.mix}files. @code{mixvm} can be used either in @dfn{interactive} or@dfn{non-interactive} mode. In the second case, @code{mixvm} will loadyour program into memory, execute it (producing any output due toMIXAL @code{OUT} instructions present in the program), and exit whenit encounters a @code{HLT} instruction. In interactive mode, you willenter a shell prompt which allows you issuing commands to the runningvirtual machine. This commands will permit you to load, run and debugprograms, as well as to inspect the MIX computer state (registercontents, memory cells contents and so on).@menu* Non-interactive mode:: Running your programs non-interactively.* Interactive mode:: Running programs interactively.* Debugging:: Commands for debugging your programs.@end menu@node Non-interactive mode, Interactive mode, Running the program, Running the program@comment node-name, next, previous, up@subsection Non-interactive mode@cindex non-interactive modeTo make @code{mixvm} work in non-interactive mode, use the @code{-r}flag. Thus, to run our @file{hello.mix} program, simply type@examplemixvm -r hello @key{RET}@end example@noindent at your command prompt, and you will get the following output:@exampleMIXAL HELLO WORLD@end example@noindent Since our hello world program uses MIX's device number 19 asits output device (@pxref{Writing a source file}), the output isredirected to the shell's standard output. Had you used any other MIXoutput devices (disks, drums, line printer, etc.), @code{mixvm} wouldhave created a file named after the device used (e.g. @file{disk4.dev})and written its output there@footnote{The device files are stored, bydefault, in a directory called @file{.mdk}, which is created in yourhome directory the first time @code{mixvm} is run. You can change thisdefault directory using the command @code{devdir} when running@code{mixvm} in interactive mode (@pxref{Configuration commands})}. The virtual machine can also report the execution time of the program,according to the (virtual) time spent in each of the binary instructions(@pxref{Execution times}). Printing of execution time statistics isactivated with the @code{-t} flag; running@examplemixvm -t -r hello @key{RET}@end example@noindentproduces the following output:@exampleMIXAL HELLO WORLD** Execution time: 11@end exampleSometimes, you will prefer to store the results of your program in MIXregisters rather than writing them to a device. In such cases,@code{mixvm}'s @code{-d} flag is your friend: it makes @code{mixvm} todump the contents of its registers and flags after executing the loadedprogram. For instance, typing the following command at your shell'sprompt@examplemixvm -d -r hello@end example@noindent you will obtain the following output:@exampleMIXAL HELLO WORLDrA: + 00 00 00 00 00 (0000000000)rX: + 00 00 00 00 00 (0000000000)rJ: + 00 00 (0000)rI1: + 00 00 (0000) rI2: + 00 00 (0000) rI3: + 00 00 (0000) rI4: + 00 00 (0000) rI5: + 00 00 (0000) rI6: + 00 00 (0000) Overflow: FCmp: E@end example@noindent which, in addition to the program's outputs and executiontime, gives you the contents of the MIX registers and the values of theoverflow toggle and comparison flag (admittedly, rather uninteresting inour sample).As you can see, running programs non-interactively has manylimitations. You cannot peek the virtual machine's memory contents, notto mention stepping through your program's instructions or settingbreakpoints@footnote{The @code{mixguile} program allows you to executearbitrary combinations of @code{mixvm} commands (using Scheme)non-interactively. @xref{Scheme scripts}.}. Enter interactive mode.@node Interactive mode, Debugging, Non-interactive mode, Running the program@comment node-name, next, previous, up@subsection Interactive mode@cindex interactive modeTo enter the MIX virtual machine interactive mode, simply type@examplemixvm @key{RET}@end example@noindent at your shell command prompt. This command enters the@code{mixvm} command shell. You will be presented the following commandprompt:@exampleMIX >@end example@noindent The virtual machine is initialised and ready to accept yourcommands. The @code{mixvm} command shell uses GNU's readline, so thatyou have at your disposal command completion (using @key{TAB}) andhistory functionality, as well as other line editing shortcuts common toall utilities using this library (for a complete description ofreadline's line editing usage, see @ref{Command LineEditing,,,Readline}.)@cindex @code{load}Usually, the first thing you will want to do is loading a compiled MIXprogram into memory. This is acomplished by the @code{load} command,which takes as an argument the name of the @file{.mix} file to beloaded. Thus, typing@exampleMIX > load hello @key{RET}Program loaded. Start address: 3000MIX >@end example@noindent will load @file{hello.mix} into the virtual machine's memoryand set the program counter to the address of the first instruction. Youcan obtain the contents of the program counter using the command@code{pc}:@cindex @code{pc}@exampleMIX > pcCurrent address: 3000MIX >@end example@cindex @code{run}After loading it, you are ready to run the program, using, as you surelyhave guessed, the @code{run} command:@exampleMIX > runRunning ...MIXAL HELLO WORLD ... doneElapsed time: 11 /Total program time: 11 (Total uptime: 11)MIX > @end example@noindent Note that now the timing statistics are richer. You obtain theelapsed execution time (i.e., the time spent executing instructionssince the last breakpoint), the total execution time for the program upto now (which in our case coincides with the elapsed time, since therewere no breakpoints), and the total uptime for the virtual machine (youcan load and run more than one program in the samesession)@footnote{Printing of timing statistics can be disabled usingthe command @code{timing} (@pxref{Configuration commands}).}. Afterrunning the program, the program counter will point to the address afterthe one containing the @code{HLT} instruction. In our case, asking thevalue of the program counter after executing the program will give us@exampleMIX > pcCurrent address: 3002MIX >@end example@cindex @code{pmem}@noindent You can check the contents of a memory cell giving its addressas an argument of the command @code{pmem}, like this@exampleMIX > pmem 30013001: + 00 00 00 02 05 (0000000133)MIX >@end example@noindentand convince yourself that address 3001 contains the binaryrepresentation of the instruction @code{HLT}. An address range of the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -