MI-Programming - Leiforg.doc

上传人:来看看 文档编号:5117972 上传时间:2020-02-04 格式:DOC 页数:103 大小:564.50KB
返回 下载 相关 举报
MI-Programming - Leiforg.doc_第1页
第1页 / 共103页
MI-Programming - Leiforg.doc_第2页
第2页 / 共103页
MI-Programming - Leiforg.doc_第3页
第3页 / 共103页
MI-Programming - Leiforg.doc_第4页
第4页 / 共103页
MI-Programming - Leiforg.doc_第5页
第5页 / 共103页
亲,该文档总共103页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《MI-Programming - Leiforg.doc》由会员分享,可在线阅读,更多相关《MI-Programming - Leiforg.doc(103页珍藏版)》请在三一文库上搜索。

1、Leif SvalgaardMI Programming408250 & 408251http:/iSeries400.orgIntroductionWhy Program at the Machine-Level?Leaving aside the precise definition of Machine-Level for a bit, a good reason is that it is just plain ol fun. It certainly is different from cranking out RPG-programs. Then there are all the

2、 standard reasons that have to do with wringing more performance out of your system with the usual caveats about only optimizing what is worth optimizing. There are many APIs on the AS/400 that are just machine-level instructions in disguise, and at times bad ones at that. When you are using such an

3、 API, you are doing machine-level programming, but often without understanding precisely what is going on and often forced into a clumsy and tedious format required by the high-level language you are using. If you know how to program at the machine-level, you will find it easier to use APIs from you

4、r high-level language, and will use them with more confidence. Finally, there are things that you just cant do in any reasonable, other way without going to the machine-level.What is the Machine-Level?One definition is that it simply is the lowest level at which you can work as a programmer. This le

5、vel constitutes an abstract machine, and you program to that abstraction. There are always levels below that you ordinarily dont care about, and in fact often dont want even to know about. The AS/400 is somewhat unusual because there are two machine-levels that play a role in practice. The upper one

6、 is called the MI-level and the lower one is the CISC/RISC platform. MI, or the Machine Interface, is in a very real sense what makes your machine an AS/400 rather than just a souped-up PowerPC. This book will show you how to program to the MI-level. We shall also examine the RISC platform in detail

7、 so you will understand something of what goes on “under the covers”.Above and Below the MIThe operating system that controls your machine has two parts. Over time some misleading nomenclature has been used. You have probably heard about “horizontal” and “vertical” micro-code. These names have falle

8、n out of favor, mainly for legal reasons. Since “micro-code” is considered part of the hardware you can own micro-code. IBM doesnt want you to own the operating system, so requires you to license it instead, hence had to change the names. Today, names like OS/400 and SLIC (System Licensed Internal C

9、ode) are used. Basically, OS/400 is programmed to the MI-level and SLIC is programmed to the PowerPC RISC platform. This is often expressed by saying that OS/400 (and your applications) are above the MI and SLIC is below the MI, hence justifying talking about the machine interface.Old MI and New MIJ

10、ust as the AS/400 hardware has evolved, MI has too. MI was designed to be extensible and new operations and functionality have been added over time as needed. We are at a point now where one can talk about the “old” or classic MI supporting the “old programming model”, OPM, and the “new” MI supporti

11、ng the ILE programming model with its emphasis on C-style programs. Todays RISC-based AS/400 only support the ILE programming model, but a special module in SLIC takes care of transforming OPM program objects into ILE modules bound into an ILE program. The module that does that has been called the “

12、Magic” module. There is this notion that there is some magic involved in MI-programming. I dont like magic. There is a famous quote from the Science Fiction master Arthur C. Clarke that “any sufficiently advanced technology is indistinguishable from magic”. One purpose of this book is to dispel some

13、 of the magic by seeking an actual understanding of what is happening.Is MI Hard and Arcane?It is a common misconception that machine-level programming is hard. There may be some truth to that at the RISC level, but that certainly is not so at the MI-level. MI is a very expressive language with powe

14、rful data structuring facilities that makes programming easy and straightforward. I can still remember my very first MI-program (some time back in 1989). I converted a 2000-line COBOL program into MI in less than a week and it ran the first time dropping the time it took to generate a 5250 datastrea

15、m from 0.337 seconds to 0.017 seconds for a speed-up factor of 20. If I can do it, so can you. What About All Those MI-Instructions?Analysis of several large production-type MI-programs containing thousands of instructions show that only 10 instructions comprise almost 80% of all instructions used:M

16、nemonic Freq% of TotalInstruction DescriptionCPYBLA19.89 % 19.89 %Copy Bytes Left AdjustedCPYNV10.86 % 30.75 % Copy Numeric ValueB10.03 % 40.78 % BranchCMPNV 9.79 % 50.57 % Compare Numeric valueADDN 7.95 % 58.52 % Add Numeric CMPBLA 6.53 % 65.05 % Compare Bytes Left AdjustedCPYBLAP 4.27 % 69.32 %Cop

17、y Bytes Left Adjusted with PadCALLX 3.80 % 73.12 % Call External (program)SUBN 3.62 % 76.74 %Subtract Numeric CALLI 2.67 % 79.41 %Call Internal (subroutine)Fundamental operations include copying characters (CPYBLA and CPYBLAP), copying numbers (CPYNV), branching (B), and comparisons (CMPBLA for char

18、acters and CMPNV for numbers). These alone make up more than 61% of all instructions coded. The moral of this exercise was to show that a lot could be accomplished with a little, so you should be able to quickly become productive. Ovid said “Add little to little and there will be a big pile”, same t

19、hing here.Small Programs or Large ProgramsSome people advocate only writing very small programs in MI. Useful programs can be as short as a single instruction (with an implied return instruction). The argument is that maintaining MI programs is hard. This is actually not the case, rather, well-writt

20、en MI-programs are easy to maintain (as are most well-written programs in any language). What was true, was that finding people with the skills needed was hard. That is another one of the reasons for this very book. When you have worked your way through the book, you will find that acquiring MI-skil

21、ls was not all that hard.I know of whole applications written solely in MI, comprising tens of thousands of lines of source code. Experience shows that these applications are not any harder to maintain than applications written in other languages. Because of the interoperability of programs on the A

22、S/400 (one of the delivered promises of ILE) it probably would make good sense to write pieces of the application in languages best suited for that particular piece, with MI taking its place among the others on an equal footing doing what it does best.Why is MI so Secret?As we all know, IBM has not

23、been very helpful in supplying information about MI-programming. The hoped for support, expressed in NEWS 3X/400 September 1989, that “You may decide that you would like IBM to let the S/38 and AS/400 “be all that they can be” by openly supporting MI” did not come to pass. There have been a handful

24、of articles in the various AS/400 publications, and only recently has there been a mailing list (MI400MIDRANGE.COM) to cater for the curious-minded AS/400 programmers. The IBM public documentation for the MI language is very sparse. The MI Functional Reference Manual, which describes each (of a subs

25、et, only) MI instruction doesnt even have one single MI example within its many pages. The System API Reference manual has a tiny chapter, dedicated to the MI language syntax, but all of this information not really enough to be a working set of reference materials for programming in MI. Getting Your

26、 Own MI-CompilerMI-CompilersIf you want to program in MI, you of course will need a compiler. While you could purchase an MI-compiler for the S/38 from IBM (the “PRPQ”), IBM claimed than no MI-compiler for the AS/400 was available. Several people discovered early on that contrary to this claim, ever

27、y AS/400 was, in fact, shipped with an MI-compiler built in. What is missing is a convenient way to invoke the compiler, i.e. there is no CRTMIPGM command. Some people would you sell a simple front-end program as “An MI-compiler” for several thousand dollars. Maybe to stop that practice (or for reas

28、ons unknown), IBM finally documented an API named QPRCRTPG (Create Program) to invoke the compiler. You can find this information (at least until IBM pulls it again) at this web-site: “http:/ The method described at the above URL is needlessly complex and even wrong if you follow the instructions to

29、 the letter (hint: one of the CL-programs blows up because the source presented to it exceeds the 2000-character limit imposed on a variable in CL) and as such we shall not duplicate that material here. Instead we shall write an RPG-program that will invoke the API. If you do not have an RPG compile

30、r, you can download a ready-made savefile with the resulting compiler front-end from our website at this URL: http:/iSeries400.org/micomp.exe. You will learn more from trying to build one yourself following the steps given in this book.The Create Program (QPRCRTPG) API converts the symbolic represen

31、tation (read: simple source text) of a machine interface (MI) program into an OPM program object. This symbolic representation is known as the intermediate representation of a program. The QPRCRTPG API creates a program object that resides in the *USER domain and runs in the *USER state. In later ch

32、apters we shall examine the domain/state concepts in detail.Create Program (QPRCRTPG) APIThe QPRCRTPG API is documented in the books for V3R2 and in the System API manual for later releases. Here is the an on-line reference http:/ Below I excerpt here the relevant information for your convenience (w

33、ho knows for how long the above link is still good?). Much of the detailed description (shown in a smaller font) may not make much sense to your at this stage, so you should just consider it to be reference material, skim it, and continue with confidence.What makes the API a little tricky to use is

34、that you do not pass it the name of a member in a source file as you would ordinarily do, but instead, you pass the API a large character array containing the source. For this reason, a front-end is usually required to read the source records into an array. Here is first the parameter list for the A

35、PI: _ | 1 | Program source statements | In | Char(*) | | 2 | Length of program source statements | In | Binary(4) | | 3 | Qualified program name | In | Char(20) | | 4 | Program text | In | Char(50) | | 5 | Qualified source file name | In | Char(20) | | 6 | Source file member information | In | Char(

36、10) | | 7 | Source file last changed date and time | In | Char(13) | | 8 | Qualified printer file name | In | Char(20) | | 9 | Starting page number | In | Binary(4) | | 10 | Public authority | In | Char(10) | | 11 | Option template | In | Char(*) | | 12 | Number of option template entries | In | Bin

37、ary(4) | | 13 | Error code (optional) | I/O | Char(*) | |_|_|_|_|The MI-Compiler Front-EndWe must first decide which language to use for our compiler front-end. Every AS/400 has a CL-compiler, but the limitation of 2000 characters for the size of the variable to hold the MI source string is severe.

38、Most AS/400 shops have an RPG-compiler, but even RPG has limitations. Other languages are not used broadly enough to be viable for my purpose, so I decided to write the front-end in MI! Of course, I still have to compile the front-end, but since the front-end code is small (about 200 lines) I can em

39、bed the code in an RPG array and have a simple RPG-program call the QPRCRTPG API to generate the MI front-end program. /*= * This program creates MI compiler front-end CRTMIPGM in *CURLIB= * Source statements for the MI compiler are found in array MI. = *= E MI 1 208 80 I DS I B 1 40#SRCLN I I CRTMI

40、PGM *CURLIB 5 24 #PGMLB I 25 74 #TEXT I I *NONE 75 94 #SRCFL I 95 104 #MBR I 105 117 #CHGDT I 105 105 #CENT I 106 107 #YY I 108 111 #MMDD I 112 117 #HMS I 118 137 #PRTFL I B 138 1410#STRPG I 142 151 #AUT I 152 327 #OP I B 328 3310#NOOPT C CALL QPRCRTPG C PARM MI C PARM 16640 #SRCLN C PARM #PGMLB C P

41、ARM MI Comp #TEXT C PARM #SRCFL C PARM #MBR C PARM #CHGDT C PARM #PRTFL C PARM 0 #STRPG C PARM *USE #AUT C PARM *REPLACE#OP C PARM 1 #NOOPT C MOVE *ON *INLRYou can easily recognize the parameters to the QPRCRTPG API. All we have to do now is to populate the array MI with the appropriate code (as sho

42、wn below), compile the RPG program and run it. The result is the CRTMIPGM program object. Now, you should try this right away.Below are the contents of the MI array. The curious */ in the first line has a matching /* at the beginning of the RPG-program. Comments in MI are free-form text (can be spre

43、ad over several lines) starting with /* and ending with */. That makes the entire code part of the RPG-program an MI-comment. In fact, the program is at the same time a valid RPG-program and a valid MI-program. You can give it to either compiler and it will compile and run. Why would I do a thing like this? Maintenance. I can en

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程管理


经营许可证编号:宁ICP备18001539号-1