------------------------------------------------------- The PowerBASIC InLine-Assembler contains the functions of the Intel 8086 CPU. This means that you may have to adapt the InLine-Assembler- Code of other High-Language-Compilers or true Assembler-Code to the PowerBASIC InLine-Assembler, because they quite often contain 80286 commands. Usually the following commands have to be converted: Source - > PowerBASIC shr ax, 2 ! shr ax, 1 ! shr ax, 1 or like this: ----------------------------------------------------------- shl ax, 3 ! push cx ! mov cl, 3 ! shl ax, cl ! pop cx ----------------------------------------------------------- pusha ! push ax ! push bx and so on until all registers are saved ----------------------------------------------------------- popa analog, only restore registers 5.2. Assembler Syntax Error --------------------------- When we forget the 'true' Syntax Errors, which usually occurs when you are not quite used to Assemblercommands, there still is an 'obvious' Syntax Error which can be marked by the InLine-Assembler of PowerBASIC. This is the case when the Compiler can't use a Variable in the InLine-Assembler because it isn't defined in any way. PowerBASIC usually creates used Variables within true BASIC-Source by itself and allocates memory for it. You have to do that yourself within the InLine-Assembler. Example: ! mov ax, Demo% This causes a Syntax Error because the Compiler can't do anything with the Variable 'Demo%'. You should first set the Variable to a value: Demo% = 1 ! mov ax, Demo% Now the Compiler accepts the Assemblerline. You don't have to assign a value to a Variable every time, a simple DIM or SHARED, PUBLIC, LOCAL etc. is enough and initialises 'Demo%'. 5.3. Faulty passing of Variables in the InLine-Assembler -------------------------------------------------------- You probably often swore around because a working routine with REG(x) didn't work crrectly after conversion to the InLine-Assembler or when your Testroutine wouldn't do its job in a SUB/FUNCTION. The solution is relatively simple: You have to pass the variables to the InLine-Assembler BYVAL. Example: Demo 1 FUNCTION Demo(BYVAL i%) public ! mov ax, i% ! inc ax ! mov i%, ax PRINT i% END FUNCTION This little Demo simply adds the value '1' using the InLine-Assembler and then prints it on the screen. Simply leave out the BYVAL and then test the Demo again! 5.4. Problems with LDS/LES -------------------------- Compareable to the passing of the parameters is the function of the commands LDS/LES. It is also essential whether a variable is passed 'BY COPY', 'BY REFERENCE' or 'BY VALUE'. The followingcan be taken as rule of thumb: BY REFERENCE: - default in the main program - or when a variable is declared SHARED/PUBILC etc. BY COPY: - default in a SUB/FUNCTION, if the variables aren't passed BY VALUE. BY VALUE: - always interpreted by the InLine-Assembler as BY VALUE You should only pass variables of type BY COPY to LDS/LES, because only then the DS/ES Segmentaddresses will be loaded and the Offsetaddresses into the other Registers. When passing BY REFERENCE DS/ES will be loaded with the high value contents of the variable, if of type Long/DWord, else the DS/ES Register will contain an irrelevant value. The other Registers will contain the low value of the variable. Example: SHARED DemoSeg%, DemoOff% i& = &h12345678 Demo1 i& Demo2 i& Demo3 i& FUNCTION Demo1(i&) public PRINT "PB-Adresse : ";: PRINT HEX$(VARSEG(i&));":"; HEX$(VARPTR(i&)) END FUNCTION FUNCTION Demo2(i&) public ! les bx, i& ! mov DemoSeg%, es ! mov DemoOff%, bx PRINT "LES /BY COPY : ";: PRINT HEX$(DemoSeg%);":"; HEX$(DemoOff%) END FUNCTION FUNCTION Demo3(BYVAL i&) public ! les bx, i& ! mov DemoSeg%, es ! mov DemoOff%, bx PRINT "LES /BY VALUE: ";: PRINT HEX$(DemoSeg%);":"; HEX$(DemoOff%) END FUNCTION 5.5. Crash after calling own INT-Functions ------------------------------------------ You will ask yourself: Why this section in the FAQ? Does the PowerBASIC InLine-Assembler have any Bugs? The answer is definetly: NO. But many calls using INT-Functions of the BIOS/DOS are connected to some trouble, because they change important Segments or specially address them. Many buffers that have to be passed to a function look for their pointer in the Datasegment-Register (DS). PowerBASIC also addresses its variables using DS, so that conflicts are 'programmed' here. It should not be like this, for example: ! mov ax, &h3D90 ; Function File open ! mov ds, FileSeg?? ; Load segment of Filename, ! mov dx, FileOff?? ; First error, because FileOff?? ; can't be addressed using DS. DS ; is already pointing somewhere else. ! int &h21 ; INT-Call ! mov Handle%, ax ; Because DS still points to nowhere ; for PowerBASIC, this is wrong, too, ; and PowerBASIC will crash sooner or ; later. A clean listing should look like this: ! push ds ; Save DS ! mov ax, &h3D90 ! mov dx, FileOff?? ; Load Offset of Filename ! mov ds, fileSeg?? ; Load Segment of Filename, not needed ; by PowerBASIC anymore ! int &h21 ; INT-Call ! pop ds ; Restore PowerBASIC Segment ! mov Handle%, ax ; Save Handle% (Or Errorcode) ! jnc ... ; Check Carry-Flag 5.6. Fixup Overflow ------------------- The problem is relatively easy and simple: The 8086 CPU only allows jumps of type SHORT, meaning that you can only jump to labels within -127/+128 OpCodes directly. The following example also creates such an error: DemoLabel: ! jc DemoLabel To get around this, you'll just have to address the whole thing a little different. This is principally no problem once you know it: DemoLabel: ! jnc DemoWeiter ! jmp near DemoLabel DemoWeiter: But this can be found in any good Assembler book... 5.7. Dividing variables from WORD to BYTE ----------------------------------------- Should you still turn your 16bit variables into its 8bit parts with mathematical work, it is time to finally stop it! The CPU can do it by itsself: Example: DIM Demo AS WORD DIM DemoHigh AS BYTE DIM DemoLow AS BYTE Demo?? = &H1234 ! mov ax, Demo?? ! mov DemoLow? , al ! mov DemoHigh?, ah 5.8. Dividing variables frob DWORD to WORD