home *** CD-ROM | disk | FTP | other *** search
- @5
- #### ### ## ######## ######
- ### ## ## ## ## ### ## ### ### ##
- ### ## #### # ## ### ## ## ## ## ##
- ## ## ## # # ### ## ## ## ## ##
- ## ## ## # ## ### # ## ## ## ##
- ## # ## ### #### ## ## ## ##
- ### ######### # ## ### # ## # ######
- #### ## ## ## ######## #### # ## ####
- #### ## # ## ## # ## ### ##
- # ## ######### # ## ## # ## ### ##
- ### ### ## ###### ## ### ## ###### ######### ##
- ###### ##### ###### # ### ##### ##
-
- *3
- @2
- PRESENTS HIS
- *4
- Assembly Language Tutorial
-
- *3
- @4
- ENJOY!!
-
-
- @2]5INTRODUCTION
- *1]1@1
- Before starting this I suggest that you have a good understanding of@4 BASIC@1 in
- any shape or form. You will also need an assembler (NO SHIT!). A reasonable
- understanding of AmigaDOS is also an advantage but not essential.
-
- I am very sorry in advance for any spelling mistakes, the overuse of any
- particular word... and for just living!
-
- You could read the AmigaDOS tutorial that's in this very issue, kindly written
- by Adrian of Future PD.
-
- Having had my Amiga for a few years, well 4 in all. I have had lots of
- experience with it and although I can code. I cannot code the latest mega demo
- with the best "triple whamee" 3D vector type thingy, but I do have a little
- experience with Assembly langauge. This is why I have chosen to do an Assembly
- language tutorial. What I am hoping is that I can teach you the basics and
- then someone with a little more experience will write to me and offer their
- services for a more in-depth tutorial. I don't know if it is the same for
- everyone but when I sit down to read a book on coding, it just seems to go
- right over my head, no matter how much effort I put into it.
-
- Special thanks go to Paul Overaa because without his well written books I
- wouldn't have learnt a thing. Cheers Paul!
- *4@2
- INTRODUCTION INTO PROGRAMMING THE 680X0
- *1@1
- In all the books that I have read, all coding is done the "system legal" way
- which means to say that if you want to learn to program the Amiga the correct
- way, then you really need to get hold of the Amiga Developer's Kit which
- contains all the Commodore include files needed to "talk" to the operating
- system. Essential reading for the would-be coder would include the ROM Kernal
- Manuals, but at £30 quid they aren't cheap. As most of the people reading this
- article will hopefully want to get somewhere by reading this tutorial. I mean
- most people will want to learn so they can code a little intro for a production
- or a little hack that you've always needed. I will not be teaching you how to
- become a master coder because, it's not that I don`t want to, I really wish I
- could, but I don't have a big enough understanding myself yet.
-
- I must first ask that if you are really intent on learning Assembler then it
- has to be worth investing some money in it. I have recommended some books in
- this tutorial and where they can be obtained. If you are like me and don't
- have much of an income then I must recommend the series called HOW TO CODE.
- Although this is now no longer supported it is still about 450K of text
- relating to coding, covering topics including Reading_C, Vectors, Coppers and
- many more. It is only the cost of a disk as well. If should be available from
- any self respecting Public Domain library.
- @2
- HERE GOES!!
- @1]5
- First things first -
- ]1
- In this series of tutorials I intend to deal with the following topics:
- *3@4
- CO-PROCESSOR HARDWARE
- BITPLANES AND PLAYFIELDS
- SPRITE HARDWARE
- AUDIO HARDWARE
- *1@1
- These parts are covered in depth in the Hardware manual and are given a chapter
- each. This book has to be recommended to all future demo coders. As it
- provides essentials on the use of "hardware bashing" or coding the non-system
- way.
-
- These will be the main parts. I may include such things as Blitter, system
- control and interfacing in later issues if the tutorial is going well enough.
- Included in with all this will be the topics of DMA (Direct Memory Access) and
- smaller topics such as the various addressing modes and all the other
- bollocks!! In this chapter we begin with opening libraries and the use of
- copperlists.
- @2]5
- The Basics first -
- @1]1
- Computers have a logical way of accepting data, this data is called Binary and
- consists of loads of 1's and 0's. The computer reads these and interprets them
- as 1(on) or 0(off).
-
- It is these Binary combinations that are used to make other numbers. E.G. If
- we use just one bit there are two combinations 1 and 0. If we use two bits
- there are 4 combinations; 00, 01, 10, 11. Carrying this on to 8-bit gives us
- 256 possible commbinations and therefore 256 possible numbers.
-
- We normally count from 0-9. This is known as Base 10 and you don't really need
- this for Assembler.
-
- 1 byte - 8 bits - a BYTE .b
- 2 bytes - 16bits - a WORD .w
- 4 bytes - 32bits - a LONG WORD .l
-
- Although using Binary is all well and good for your computer it does become a
- bit confusing when trying to decode a string of 1 and 0. This is why, in
- Assembly, we count in Base 16. That is to say we go from 1 to F.
-
- 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
-
- So 10 in Hexadecimal is really 16 in normal Base 10. For numbers over 15 we
- use two digits.
-
- Therefore, work out the following in normal Base 10 numbers as a little puzzle
- to keep you awake!
-
- $0F,$10,$20,$FE,$FF
-
- You should have got 15,16,32,255,256 in that order the $ sign is used to tell
- the Assembler that the number is HEXADEMICAL.
-
- Doing even more we get:
-
- 255 = $00FF
- 256 = $0100
- etc......
- 4096 = $1000
- 65535 = $FFFF
-
- You should recognise some of the numbers. Remember the non-AGA machines, well
- they could only have 4096 colours! It's all to do with numbers!
-
- This is the hardest thing to understand when it comes to learning Assembly
- language.
- *4@2
- Let The Show Begin!
- *1@2
- Opening Libraries -
- @1
- Okay, in this issue I will deal with the fundamentals, and there is nothing
- more fundamental than the use of the Exec run-time libraries. At the very
- basic level an Assembly language program just puts a list of library calls in
- order and these will eventually provide some sort of visible output. When
- coding in C, using function libraries is a damn sight easier because all that's
- needed is an open library call and then all functions are called using the
- function name together with the required parameters.
-
- When coding in Assembler these registers have to be preloaded with any required
- parameters before the subroutine call, this is all transparent in C.
-
- Okay, so how do we open these libraries then? Well, we use an Openlibrary()
- (described later) call. This isn`t actually needed for the Exec library
- because it is permanently available.
-
- The calls are performed by loading the base address of the library into the
- register a6. Then we preform an indirect subroutine call using a displacement
- Value Offset (LVO). The actual code to load the base address of the Exec
- library into a6 and open the lib looks like this:
-
- move.l _AbsExecBase,a6
- jsr _LVOOpenLibrary,(a6)
-
- Okay, so this sounds a bit complicated but just imagine the name of the library
- is loaded in a6 and the next line just opens it. If we imagine the, already
- opened, Exec library as a ladder in memory, the a6 register is loaded in with
- the start (the last rung) of the ladder, then the indirect subrountine tells us
- to go down the ladder until we reach the required function. In this case the
- _LVOOpenLibrary (-552) call.
-
- These text means are resolved in two different ways. Firstly, by the use of
- XREF statements, which tell the Assembler to resolve them at link time. The
- second and easier way is to include a header file of the LVO definitions and
- this is where the include files come in. That's all they are, just a list of
- the LVO functions with the corresponding EQU numbers. This way is a lot easier
- as we don`t have to link with Amiga.lib library to run our program.
-
- Okay, so if you are a fast thinker then you may have noticed a potential
- problem. Well, here it is:
-
- What if the register a6 already contains some data that we want to use and we
- need to open a library. Well, we now come onto the subject of Macros. Macros
- are like large LET BASIC statements, and the LINKLAB one that's in the
- exec/libraries include file, is just what we need. Here is, basically, what it
- does:
-
- move.l a6,(sp) ; Move a6 to the stack pointer to preserve it.
- move.l <LibraryPointer>,a6 ; Get the base mem address of the lib
- into a6.
- jsr (_LVORountineName),a6 ; Make the indirect subrountine call.
- move.l (sp),a6 ; Restore a6.
-
- So with the Exec.library include in the start of the code, we can use simple
- library code like this:
-
- LINKLAB _LVOOpenLibrary,_AbsExecBase
-
- This makes a basis for us to produce well written code as using Macros reduces
- the possibility of errors.
-
- The actual code included on this disk can be loaded into a Word Processor if
- you don't have an Assembler, so you can see what going on. I have also
- included two executable versions of the example sources.
-
- @5
- The Copper Co-processor:
- @1
- The Copper is a graphics co-processor that is inside one of the Amiga's customs
- chips. It gets it's instructions via Direct Memory Access (DMA) or in other
- words the data doesn't have to go through the processor to be executed, making
- it a lot faster. The Copper can control the entire graphics system by itself
- leaving the processor to deal with more intense things.
-
- A copperlist has three possible instructions. These are:
-
- MOVE
- WAIT
- SKIP
- @4
- MOVE-@1 Move data into special purpose register.
- @4
- SKIP-@1 Skip the next instruction if the video beam has reached a certain
- position.
- @4
- WAIT-@1 Wait for screen position.
-
- *4@2
- OKAY, LET'S GO!!
- *1@1
- Load the example source that is on this disk and move to the bottom of the
- screen. The code is heavily commented so to provide a tutorial of what is
- going on. The section at the bottom reads:
- @5
- dc.w $3401,$FFFF.$180.$F00
- dc.w $8001,$FFFF,$180.$FF0
- dc.w $8101,$FFFF,$180.$00F
- dc.w $A001,$FFFF,$180,$0F0
- @1
- This data is our copperlist instructions. The first section is the positions
- of our copperlist, try changing a few of the values to get some different
- positions. The next two sections will be left out for this tutorial. The last
- section deals with the screen colours of the copperlist. Changing these will
- start the copper with different colours on the screen. This technique can be
- used to produce different effects such as a german flag or a landscape (blue
- for the sky and green for the land).
-
- Right, that's the good stuff out of the way. We will now look at each of the
- instructions in our little block of code.
-
- The first four lines are equates. They are like LET statements in BASIC and
- just tell the Assembler that for every FORBID that it comes across, replace it
- with -132.
-
- The first instruction just tells the Assembler that this section is for prog
- code. This is used again in the section cp,data_c which tells the Assembler to
- put the data into Chip RAM. If you only have Chip RAM you don't need this
- instruction.
-
- The next instruction is the Load Effective Address (lea). This allows the
- Assembler to load the address location into a register. In the listing, the
- graphics lib address is loaded into a1.
-
- The next instruction is the most used of the Assembler instructions. It moves
- a 16-bit word value of 4 into the a6 register.
-
- The next instruction, the Jump to SubRoutine (jsr), transfers program control
- over to a subroutine much like GOSUB in BASIC. The -408 is a LVO offset which
- stands for the OldOpenlib function.
-
- The next instruction is now simple and you should be able to tell me what is
- does already. Well, it moves a long word (32-bit) from d0,a1. Simple, eh!
-
- The next instruction copies the contents of the long word, whose address is
- made by adding 38 to the address in a1, into the defined OLDCOP string.
-
- The next instruction is the closelib LVOoffset and is used to close the opened
- graphics library. The next instruction is the same but it is another the
- Forbid call.
-
- The next instruction, if you didn't know. moves the defined #NC, into a
- special copper address at $dff080.
-
- The next instruction, the btst, stands for "test a bit", and it will test an
- operand bit and set the zero flag. In this case it is used to test for a mouse
- button press. It waits until the address at BFE001 is equal to 6. When it is
- the program contines. If the left button is pressed, the value in BFE001
- changes to 6, therefore stopping the program.
-
- The next instruction, the bne, stands for "branch if not equal". The branch is
- taken if the zero bit is 0. This is the opposite to beq which stands for
- "branch if equal". It will continue to loop until the branch is equal.
-
- The next instruction is another LVO function known as Permit.
-
- The next instruction should not cause any problems now I hope. It moves
- 32-bits from the address at OLDCOP into memory location $dff080.
-
- The next instrution is a new one. It stands for "move quick". It is used to
- set a value very quickly. Most Assembler's will optimise any immediate
- addressing moves (i.e. normal moves to moveq's). Thus increasing code speed.
-
- The next instruction is a Return To Subroutine (rts). This instruction returns
- control back to a address given by the stack.
-
- The next instruction, a define instruction graplib, is defined as
- "graphics.library". It also reserves space in memory.
-
- OLDCOP defined as 0 - constant.
-
- NC: This is our new copperlist data. The instruction above this tells us to
- put it in Chip mem.
-
- The lines that are left is the code that does all the visible stuff. This is
- the main point of this little tutorial. I hope this is a help and if you want
- to write to me then my address is in the sourcecode.
-
- The last instruction, the line that reads:
-
- DC.w $FFFF,FFFE ; end
-
- ...is an impossible instruction, and as the computer can't do it, it will stay
- with the copper screen until the mouse button is pressed.
-
- As I can only just code myself I have used some source that was included in an
- old issue of Grapevine. I have updated this code and added a lot of comments
- to it. This is, I believe, enough to justify a release. Writing this has
- helped me to learn the depths of Assembly language programming.
-
- The full copyright of the source remains with the orginal author.
-
- If you want to carry on, then you could change the mouse test bit so that it
- tested for a joystick press or the right mouse button or something else.
- *4@7
- Cheers, and have fun!
- *1@1
- Next issue sees more info on the use of DMA and we will start off with
- bitplanes, or maybe sound, or maybe sprites or interface hardware. Or I may
- even do some intution coding, but I do find that a bit boring and always make
- mistakes. You'll just have to wait and see.
-
- AND REMEMBER ====================>
-
- #0 _# **MMp g### `N## _agN##P0N# _#
- g## jN## j##F J## _dN0" " g##
- _#]## _0 ##L jN##F ### g#/#/ _03##
- gE_j## # 0## jF ##F j##F j## ______ gE_j##
- _0"""N## d" J##L0 ##F 0## 0## "9##F" _0"""5##
- _gF ]## jF ### ##F ##F `##k d## _gF j##
- _g#_ _j##L__g#__ ]N _j##L_ _d##L_ `#Nh___g#N' _g#_ _j##
- `"""" """""'""""' " """""" """""" """"""" `"""" """""
- /\ _ // AMIGA, Amiga Technologies International.
- /..\ \X/ Powered by Motorola. Intel Outside!!.
- / .. \ __________________________________________________
- /_...._\ | __ |
- |....| |___ ___________ ___________ AMIGA 4000/40 == |
- |....| ||_| |_|_|_|_|_| |_|_|_|_|_| |
- |....| |________________________________ _______ _________|
- |....| |[_______________________________ |__|__| [_I_I_I_]|
- |....| |[___I_I_I_I_I_I_I_I_I_I_I_I_] | _ [_I_I_I_]|
- |....| Only The |[__I_I_I_I_I_I_I_I_I_I_I_I_L___| _[_]_ [_I_I_I_]|
- |..<------- |[_____I_I_I_I_I_I_I_I_I_I_I____] [_I_I_] [_I_I_T ||
- |....| Makes | [__I__I_________________I__L_] ________ [___I_I_j|
- |....| It | |
- |....| Possible |__________________________________________________|
-
- @2Refence Material.
-
- Function Name: OpenLibrary()
- Description: Opens a Amiga Run-Time library.
- Call Format: Base_address=Openlibrary(library_name,version);
- Registers: D0 A1 D0
- Arguments: Library_name. Address of a null terminated string version -
- a library version number.
- Return Value: Base_address - the address of the base of the library. If the
- library cannot be opened a NULL (0) value is returned.
-
- Function Name: CloseLibrary()
- Description: Close an open library.
- Call format: CloseLibrary(base_address);
- Registers: A1
- Arguments: base_address - the library base address.
- Return Value: None.
-
- Function Name: Write()
- Description: Write data to a file.
- Call Format: length_written = Write(file,buffer_p,data_length)
- Registers: D0 D1 D2 D3
- Arguments: file - file handle
- buffer_p - pointer to buffer holding the data
- data_length - length of the data
- Return Value: length_written - number of bytes actually written.
- Notes: A length_written value of -1 will indicate and error.