home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaDemoCD2.iso / ASCII / TEXTE / SCENE / HPA / HACKING / tut2.txt < prev    next >
Encoding:
Text File  |  1994-03-26  |  21.6 KB  |  442 lines

  1.  
  2.                          Coding Tutorial Part Two
  3.           Written And Documented By Digit Of Allstarz In May 1995
  4.          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5.              «« Another Great Release For Cement City BBS!! »»
  6.  
  7.   Introduction
  8.  ~~~~~~~~~~~~~~
  9.  Welcome to the second edition of my coding tutorial. After many requests
  10. and praises from various people I have decided to get something into gear
  11. (that being me) and write the second part of this tutorial. I myself will
  12. not be writing to much stuff for the Amiga for a while because as some of
  13. you may know I have come across a few problems which I shall not mention
  14. here at the moment, maybe later on?
  15.  I will continue where we last left off and that was to open and close
  16. libraries incorperating a mouse wait between each of the two routines used,
  17. after we opened the library and before we closed it. If you have never
  18. managed to get hold of a copy of the last tutorial that I did then have a
  19. look around on some of the top boards or try to get hold of it on Cement
  20. City or from someone that has access to it. As usual this tutorial and any
  21. future releases will be on Cement City before any other board so if you
  22. want these quick style then keep your eyes peeled there.
  23.  
  24.  I was informed of a mistake that was made during the last tutorial about
  25. the Z flag in the mouse wait routine. I said that it was always kept at
  26. zero and that changes are only reported when that value changes from zero
  27. to one. Unfortunatly I was wrong (thanks to Mars Bar for reporting it to
  28. me!) because the Z flag is always at one but changes are only notified
  29. when the value turns to zero. It may not sound much but in fact it can be
  30. quite a big mistake to learn and can become quite important to know about
  31. it properly before you really scramble your head trying to locate a bug or
  32. fault of some sort.
  33.  Just a word of warning before we begin and that is I have a splitting
  34. head-ache and feel completly knackered because I got terribly pissed last
  35. night due to free drinks being dished out all night. So in the off chance
  36. that something in this tutorial is wrong please let me know as soon as
  37. possible, I will try to find and destroy any mistakes that might be here
  38. before release.
  39.  
  40.   Opening A Window
  41.  ~~~~~~~~~~~~~~~~~~
  42.  In the last tutorial we did a little coding but nothing really happened
  43. visually so in this tutorial we will break that barrier and make things a
  44. little more interesting for us all. After we have gone through it and you
  45. know what is going on you should have a little play about with a few of the
  46. commands, these will be stated latter on for you so don`t worry too much.
  47.  
  48.  Before we begin there are various ways on opening a window but because we
  49. are going to learn system code then we really only have three basic types
  50. to deal with, these are Inuition (two types) and Dos (one type). In the
  51. last tutorial we opened an intuition library but in this tutorial we will
  52. be opening a dos window because it is the easiest of the three to
  53. begin with so for this we need to open the dos library.  One of the
  54. reasons why it is one of the easiest to open and to use is because most of
  55. the window handling is controlled through the Amiga`s operating system.
  56. Most of the routines are in the Amiga`s built in libraries which are
  57. located in the ROM, these will do most of the work for you so it is only
  58. the most important parts that you have control over, ie. what size the
  59. window should be and where you would like it, etc. This means that you,
  60. "The Programmer", can now concentrate on the most vital parts in your
  61. program and this is why the Amiga is the most friendliest machine to work
  62. with when writting GUI programs.
  63.  Okay so for opening a dos window we need to open the "dos library"
  64. because we will be calling offsets from it and these cannot be gained
  65. from any other library (unless a library is specifically written like
  66. that of the dos library). So if you followed the last issue then see if
  67. you can open the dos library yourself before you continue any further.
  68.  
  69.  Lets have a talk through opening a window using the dos library before we
  70. have a look at the code so that you can firmiliarize yourself with it`s
  71. characteristics.
  72.  
  73.  So what is a dos window? Well any window is usually called a console and a
  74. console window can only be opened when a channel has been linked to it. A
  75. channel is where input and output data is sent through, to and from a
  76. source such as window, keyboard, etc. We have to firstly open a channel
  77. before we can use our console (window) to input or output data to.
  78.  A dos window and an intuition window are similar but they work in very
  79. different ways to each other and so different routines have to be written
  80. to do roughly the same thing. Because of this you "The Programmer" will
  81. have to decide what you are going to be coding and what you want your
  82. program to do and contain, hopefully this should have been sorted during
  83. planning out your program. A dos window is very simple and very easy to
  84. open and apply input and output routines to, but unfortunalty it does not
  85. allow for many (if at all) gadgets without a great deal of hassle while
  86. intuition windows allow for such things. The only gadgets that a dos
  87. window will be able to use is the left corner of the window (the close
  88. gadget) and the size symbol which is located in the lower right corner, so
  89. as you can see a dos window would not do the job for gadget based programs.
  90. The two gadgets on a dos window can be controlled by the programmer to do
  91. specific things if you wish, making use of these will be told later in the
  92. tutorial`s series.
  93.  There are two similar types of dos window and they are "CON:" and "RAW:",
  94. there are a few more types that can be put into place but at the moment we
  95. have no need for them. If you change the windows parameters in the code
  96. listed in this tutorial then you will not see any change to the screen. The
  97. differences between the two will be told later on as we talk through each
  98. line of code. The two differences though are quite surprisingly large and
  99. care should be taken when deciding to use a "RAW:" window instead of a
  100. normal "CON:" based window.
  101.  When a window has been opened then it is possible to do lots of different
  102. things with it. We can input and output data to and from it through the use
  103. of our channel and because the dos library is very versatile we can cut
  104. down the work load on the programmer.
  105.  
  106.  Okay as usual lets have a look at the commands and instructions that we
  107. will be using to open this window of ours and then lets talk through them.
  108.  
  109. ****************************************************************************
  110. ; === Setup assembler (Devpac)
  111.  
  112.     section     Window,code_c       ;Stick in chipmem, obvious!
  113.     opt         o+,ow-              ;Optimize Devpack code, Lame eh?
  114.  
  115. ; === Save registers
  116.  
  117.     movem.l     a0-a6/d0-d7,-(a7)   ;Save registers
  118.     move.l      SP,StackPointer     ;Save saved register
  119.  
  120. ; === Open dos library
  121.  
  122.     move.l      _ExecBase,a6        ;Get execbase address
  123.     lea         DosName(pc),a1      ;Get library name
  124.     moveq       #0,d0               ;Any old version
  125.     jsr         _OpenLib(a6)        ;Open 'dos.library' - eeek!
  126.     move.l      d0,DosBase          ;Store base address for later!
  127.  
  128. ; === Open a window onto the Workbench screen
  129.  
  130.     move.l      DosBase,a6          ;Get base address of library
  131.     move.l      #ConsoleName,d1     ;Window pointers etc.
  132.     move.l      #Mode_Old,d2        ;Load old file
  133.     jsr         _Open(a6)           ;Open window to screen
  134.     move.l      d0,ConHandle        ;Save window base address
  135.     beq.w       Exit                ;Quit if window never opened
  136.  
  137. ; === Test for left mouse button press
  138.  
  139. Mice:
  140.     btst        #6,$bfe001          ;Is it the one on the left?
  141.     beq.s       Exit                ;Exit program
  142.     bra         Mice                ;Lets check again shall we?
  143.  
  144. ; === If the mouse has been pressed then close our window!
  145.  
  146. Exit:
  147.     move.l      ConHandle,d1        ;Get window base
  148.     move.l      DosBase,a6          ;Get base address of library
  149.     jsr         _Close(a6)          ;Close window down
  150.  
  151. ; === Close dos library
  152.  
  153.     move.l      DosBase,a1          ;Get base address again
  154.     move.l      _ExecBase,a6        ;Get exec base address
  155.     jsr         _CloseLib(a6)       ;Close the doslibrary
  156.  
  157. ; === Return saved pointers!
  158.  
  159.     move.l      StackPointer,SP     ;Restore stack pointer
  160.     movem.l     (a7)+,a0-a6/d0-d7   ;Return saved registers
  161.     rts                             ;Exit from the program
  162.  
  163. *************************************************************************
  164. * WINDOW NAME POINTER, ATTRIBUTES, ETC                                  *
  165. *************************************************************************
  166. DosName:        dc.b    'dos.library',0
  167.                 EVEN
  168.  
  169. ConsoleName:    dc.b    'CON:100/80/430/70/Our Window!',0
  170.                 EVEN
  171.  
  172. ConHandle:      dc.l    0
  173. DosBase:        dc.l    0
  174. StackPointer:   dc.l    0
  175.  
  176. *************************************************************************
  177. * THE OFFSETS FOR THE LIBRARY(S) WE ARE GOING TO USE                    *
  178. *************************************************************************
  179. ; === Exec offsets
  180.  
  181. _ExecBase:  =   4
  182. _CloseLib:  =   -414
  183. _OpenLib:   =   -552
  184.  
  185. ; === Dos offsets
  186.  
  187. _Open:      =   -30
  188. _Close:     =   -36
  189. Mode_Old:   =   1005
  190.  
  191. END
  192.  
  193. ****************************************************************************
  194.  
  195.  Okay that is all we need to open a window onto the Workbench screen using
  196. the dos library. There are a lot more new commands and instructions that we
  197. are going to have to talk through before you should start to play around,
  198. like the saving of registers and the assembler setup instructions, etc.
  199.  
  200.   Assembler Setup Instructions
  201.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  202.  These instructions are not so important to most people and you are not
  203. forced to use them but I prefer to use them because I can make sure that
  204. the instructions after the command are going to the right parts in memory.
  205. All that the first line is doing is labelling this section of code as
  206. "Window" while the command after the commor is telling the computer to put
  207. all of the instructions after it into chip memory.
  208.  
  209.     section     Window,code_c       ;Stick in chipmem, obvious!
  210.     opt         o+,ow-              ;Optimize Devpack code, Lame eh?
  211.  
  212.  The next line should be avoided for lots of people because it really is
  213. only aimed at Devpac users and is an internal assembler function in Devapc
  214. which other assemblers like AsmOne, etc. will not understand and so report
  215. as an unknown command/instruction. All that it is doing is telling Devpac
  216. to optimize the code. Note that with the new versions of Devpac you can set
  217. the settings so that it will always optimize your code along with various
  218. other things.
  219.  
  220.   Saving The Registers
  221.  ~~~~~~~~~~~~~~~~~~~~~~
  222.  Again these can be used by choice but it is strongly advised to use them
  223. in all of your programs however little or big they are. All these two lines
  224. do is to move the contents of various registers to a storage area while the
  225. program is using and abusing them and so when you exit the program they can
  226. be recalled to stop your machine from having to meditate!
  227.  
  228.     movem.l     a0-a6/d0-d7,-(a7)   ;Save registers
  229.     move.l      SP,StackPointer     ;Save saved register
  230.  
  231.  The first line is just moving all of the contents in the address registers
  232. and the data registers to register a7. A7 is sometimes referred to as the
  233. stackpointer and this is referred to as SP in most assemblers. Note the
  234. computer is told to save the registers by making a7 negative. After that
  235. the next line will move the stackpointer (SP) contents to your temporary
  236. storage area, in this case I have used a label called "StackPointer". This
  237. label can be found further down towards the end of the source which just
  238. says, "dc.l    0", this is a clear line for saving your data to.
  239.  
  240.   Opening The Dos Library
  241.  ~~~~~~~~~~~~~~~~~~~~~~~~~
  242.  If you have been following the tutorial you will know what this does
  243. already and how it goes about the job so I will not say anything more about
  244. it. If for no apparent reason you never recieved a copy of the last
  245. tutorial that I did then you have missed out because it explained all of
  246. the commands and ways to open a library. For those guys that did read it
  247. then you will have to make sure that you are opening the dos library and
  248. not the intuition like before because the offsets that we will be calling
  249. have no use and so your machine will probably end up meditating.
  250.  
  251.   Opening A Dos Window
  252.  ~~~~~~~~~~~~~~~~~~~~~~
  253.  This is one of the new routines that you will not have come across if you
  254. have been following my tutorial so far. As stated in the last issue the
  255. first command here should be straight forward, all it is doing is telling
  256. the computer that we are going to be calling our offsets from the dos
  257. library.
  258.  
  259.     move.l      DosBase,a6          ;Get base address of library
  260.  
  261.  The next line (see below) move`s the window information into data register
  262. one so that when the "Open" offset in the dos library is called the computer
  263. will know where and what to open. All the information about the window size
  264. etc. is located in the "ConsoleName", the next paragraph goes into deeper
  265. detail about the consolename.
  266.  
  267.     move.l      #ConsoleName,d1     ;Window pointers etc.
  268.  
  269.  The line below is what the computer will get, it is our pointer which we
  270. have labelled as "ConsoleName". If you look closely you may figure out what
  271. is going on already but all it contains is the windows parameters and the
  272. title for the window. Lets have a look more closely shall we?
  273.  
  274. ConsoleName:    dc.b    'CON:100/80/430/70/Our Window!',0
  275.  
  276.  "CON:" = This is short for Console and is similar to that of a drive like
  277.           "DF0:" or similar but not surprisingly it works in a different
  278.           way, so care should be taken. All this is saying is that we are
  279.           going to supply a screen with channels for input and output. Note
  280.           that when using the "CON:" you have control over line editing and
  281.           standard ASCII codes. "RAW:" will just echo the input and output
  282.           commands so this type of window is very limited and can only be
  283.           used in particular ways.
  284.  
  285.  "100/80/430/70/" = This is the X,Y,W,H coordinates for the window. Now
  286.           depending on what screen size is being used you can have the
  287.           window as big as you like but be careful. Usually if the window
  288.           is bigger than the actual screen you may find that the computer
  289.           will crash. In this line you can also specify the top left and
  290.           the bottom right corners so that you can position the window any
  291.           where that you wish.
  292.  
  293.  "Our Window!" = This is just the title of the window which will be located
  294.           in the top bar.
  295.  
  296.     move.l      #Mode_Old,d2        ;Load old file
  297.  
  298.  For this next line we are telling the computer that we are going to use
  299. this opened channel for reading. To do this we have to send a message to the
  300. computer to tell it so, in this case this message is found at "Mode_Old"
  301. which is "1005" and the pointer is put into register "D2". There are two
  302. other pointers that can be used and these are:
  303.  
  304.     "Mode_New"      = Used for writing only, and
  305.  
  306.     "Mode_ReadWrite"= Used for both reading and writing to a channel.
  307.  
  308.  Again if you were paying attention to the last tutorial you should under-
  309. stand what this next line is doing. For those that have not then all it is
  310. doing is calling the "Open" offset in the dos library which we have set at
  311. "-552". Note that if the offset is wrong then it is highly likely that the
  312. computer will crash and meditate.
  313.  
  314.     jsr         _Open(a6)           ;Open window to screen
  315.  
  316.  This is a simple one this and hopefully even the beginners will know what
  317. it does? Oh and why not? Okay, well all it does is save the address of the
  318. window that we have opened so that if we want to write any text out to it we
  319. can just locate the destination in the buffer "ConHandle". If the computer
  320. does not have this then it will not know where it is supposed to be writing
  321. the text to and so it will crash and meditate. Another factor is that it is
  322. an easy way of finding the destination address.
  323.  
  324.     move.l      d0,ConHandle        ;Save window base address
  325.  
  326.  Just asks the computer to check to see if the window has indeed opened. If
  327. it has not then the computer will send a NULL byte to "D0" so that the user
  328. can check and decide what to do next. In our case we have decided to exit
  329. the program to stop any faffing about but if you wish you could ask it to go
  330. and do other things, like display your own message - who knows?
  331.  
  332.     beq.w       Exit                ;Quit if window never opened
  333.  
  334.   Testing For The Left Mouse Button
  335.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  336.  The same as it was in the last tutorial, I will not go into any detail but
  337. all it does is to check for the left mouse button press. Once that has been
  338. found by pressing it the computer will then jump to it`s next routine that
  339. you want. In our case it will then close the window and the libraries used.
  340.  
  341.   Closing Our Window
  342.  ~~~~~~~~~~~~~~~~~~~~
  343.     move.l      ConHandle,d1        ;Get window base
  344.  
  345.  All this simply does is to get the address of the window that we have just
  346. opened. As we saved the address information before into our buffer then we
  347. do not have to go about it the long way. As you may already have guessed
  348. the data was stored in the "ConHandle" and is being requested to go to data
  349. register "D1".
  350.  
  351.     move.l      DosBase,a6          ;Get base address of library
  352.  
  353.  Just lets the computer know that we are still dealing with the dos library
  354. before we call any more offsets. Not really necessary in this small program
  355. because we are only using the one library. If there was more than one
  356. library being used then we may have to let the computer know which to call
  357. and we do this by sending the library address into register "A6" before we
  358. call the offset. When we call an offset the computer will check the "A6"
  359. register to find out which library to look at.
  360.  
  361.     jsr         _Close(a6)          ;Close window down
  362.  
  363.  The above line will tell the computer to call the "Close" offset in the
  364. dos library. When that is called then the window will shut down and continue
  365. with the next instructions listed in the program. The Close offset will look
  366. for the address of the file to close in register "D1".
  367.  
  368.   Closing The Dos Library
  369.  ~~~~~~~~~~~~~~~~~~~~~~~~~
  370.  Exactly the same as in the last tutorial, all this routine does is to close
  371. the dos library down before we finally exit from the program. If you read
  372. the last tutorial then please make sure that it is the dos library you are
  373. going to close and not intuition!
  374.  
  375.   Return All Of The Saved Pointers
  376.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  377.  When we first started the program the first real things we did was to save
  378. the registers and the stackpointer. All we are going to do now is to return
  379. them and believe it or not all we have to basically do is swap a few of the
  380. label`s around.
  381.  
  382.     move.l      StackPointer,SP     ;Restore stack pointer
  383.  
  384.  This is one of them, all we do is tell the computer to spill the contents
  385. of the buffer called "StackPointer" into SP (register a7).
  386.  
  387.     movem.l     (a7)+,a0-a6/d0-d7   ;Return saved registers
  388.  
  389.  Once we haved emptied our buffer out we can now return the values of the
  390. registers by making a7 positive which will let the computer know we want to
  391. return our original values when they were saved.
  392.  
  393.     rts                             ;Exit from the program
  394.  
  395.  Exits the program!!
  396.  
  397.   Okay How Was It For You?
  398.  ~~~~~~~~~~~~~~~~~~~~~~~~~~
  399.  Okay an hour has just passed and I am feeling slightly better but not well.
  400. Everthing seems to be okay and sorry for the lack of indepth information but
  401. as I keep stating if you read the last tutorial you will know most of the
  402. minor nitty gritty because I can now concentrate more on the general
  403. instructions rather than keep describing each little bit. Of course when we
  404. move further on to more complicated instructions and routines I will be
  405. going in depth but for now we will deal in general and try to get you
  406. familarized with most of the basic instructions and how to apply them best
  407. in your own programs. Hopefully you should know what most of the
  408. instructions do and what each little ".l", ".w", ".s" do after the "move"
  409. and others. You should now know how to check to see if a requested task has
  410. been done, how to open libraries, dos window, how to check for a mouse press
  411. and save the registers properly.
  412.  
  413.  For those that want to know what the future is going to hold for us then I
  414. will be going through all the minor routines like opening windows using the
  415. two commonly used libraries (dos, intuition) and then we will advance by
  416. writing our own buttons and making our own screens etc. I am thinking of
  417. planning on heading for a calculator type program which will have buttons
  418. etc. which can also have input from the keyboard and allow print out`s on
  419. results. I think if we head for something like that then we will be covering
  420. a lot of the more newly common system code and offsets so then you (the
  421. readers) will hopefully then be able to write their own code easily.
  422.  
  423.  Well thanks to all the guys asking where the second tutorial was coming as
  424. it`s nice to know that there are still people around who are willing to put
  425. praise into my work. I would have thought a few of my so called friends
  426. could have asked sometime or other, just how many where bothered to read it?
  427. Anyway thanks to Shy, Measle, Heath, Quackers, Phantom and some other guys
  428. who have either pestered me or praised me and go shag your teddies to the
  429. guys who never!!
  430.  
  431.      HOPE YOU ENJOYED THIS AND WATCH OUT FOR THE NEXT TUTORIAL - DIGIT!!
  432. @BEGIN_FILE_ID.DIZ
  433.  ******************************************
  434.      NUT AND BOLT ARE PROUD TO PRESENT
  435.  ******************************************
  436.     Second part to Digit's system coding
  437. Tutorial. This one goes through opening and
  438.    closing windows and many more .......
  439.  Straight from the source - Narcosis in 95!
  440.  ******************************************
  441. @END_FILE_ID.DIZ
  442.