Announcement

Collapse
No announcement yet.

PowerBASIC inline rotations

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PowerBASIC inline rotations

    One of the very useful thing you can do in PowerBASIC inline assembler
    is perform rotation of bits in a register by a controlled number of
    bits.

    The Intel instructions that will do it are respectively ROR & ROL which
    is "rotate Right" and "rotate Left". One of the many useful things you
    can do with rotation of bits is to retrieve smaller parts of a larger
    register or alternatively load smaller data sizes into a larger register.

    The concept is nearly machanically simple once you understand it and with
    only a little practice, it is an easy to write and simple process that is
    very hard to do any other way.

    The layout of a 32 bit register needs to be understood in the way it is
    writen in binary to make sense but that is simple enough to do. PowerBASIC
    has the &B1111111 notation that makes it easy to read & understand. A 32
    bit variable has the following form,

    Code:
        r32& = &B11111111000000001111111100000000
    That is the "&B" plus 32 digits of either "0" or "1". If you loaded this 32
    bit variable into the EAX register with,

    Code:
        ! mov eax, r32&
    then if you wanted the low BYTE value, it is already in the AL register
    where if you needed the low WORD value, it is already in the AX register.
    This is because different data sizes are possible with Intel processors in
    the SAME register by reading the correct amount of bits from it.

    With that variable loaded into the EAX register, it has the following form,

    Code:
                                 AX
       |<---high word--->|<---low word---->|
        11111111 00000000 11111111 00000000
       |<------>|<------>|<------>|<------>|
                             AH       AL
    This means that you can easily get at AL, AH & AX but it leaves you with a
    problem getting the values in the high word of the EAX register.

    When you use bit rotation, you physically move a set number of bits either
    right or left with wrap around at the ends so you can easily reposition
    the piece of data you need from ther 32 bit register to where you can get
    at it.

    The ROL instruction works like this (slightly simplified),

    Code:
            ,-<-|------- bits ------|-<-,
            |                           |
            '----->--------------->-----'
    The ROR instruction rotates in the reverse direction.

    You can control how many bits you rotate at one time by the actual instruction
    and this allows you to access parts of the 32 bit register that cannot easily
    be got at other ways.

    If you needed to get the high BYTE of the high WORD in the register, you can
    either rotate it LEFT by 8 bits so that it wraps around into AL or you can
    rotate it RIGHT by 24 bits, either will do. You then have the value u need from
    the 32 bit register in the AL register and can place it where you need.

    Code:
        ! mov ByteValue, al
    Putting the high WORD into AX is even easier, rotate it 16 bits in either direction
    and you can then use,

    Code:
        ! mov WordValue, ax
    Because PowerBASIC has both binary display as bin$ and hex display as hex$, this
    is very easy to test in a visual way so it is easy to make yourself a small rotation
    lab to test out rotating values to get a good feel for how it works.

    It is in fact a lot easier to work in HEX once you understand what the bit rotation
    is about as it takes up a lot less space and it is a lot easier to test out. The trick
    when you use either hex$ or bin$ is to use the second parameter for the number of digits
    to display. For 32 bit binary with bin$, use 32 as the number of digits to display and
    there will be no truncation of leading zeros. With 32 bit hex for hex$, use 8 as the number
    of digits.

    Here is an example of putting 3 byte values into COLORREF format for RGB colours in a DWORD
    size variable.

    Code:
        var& = 0
        
        red?   = &HFF
        green? = &HDD
        blue?  = &HAA
        
        ! xor eax, eax      ; 1 zero the register
        ! mov al, blue?     ; 1 blue value
        ! rol eax, 8        ; 3
        ! mov al, green?    ; 1 green value
        ! rol eax, 8        ; 3
        ! mov al, red?      ; 1 red value
        
        ! mov var&, eax
        
        MessageBox hWin,ByCopy hex$(var&,8),"RGB", _
                   %MB_OK or %MB_ICONINFORMATION
    This lets you see visually where the hex bytes are put in the register. It works by
    zeroing the EAX register first, loading a BYTE value into AL and rotating the EAX register
    by 8 bits.

    A small piece of PURE PowerBASIC for your pleasure.

    [email protected]
    hutch at movsd dot com
    The MASM Forum - SLL Modules and PB Libraries

    http://www.masm32.com/board/index.php?board=69.0

  • #2
    Here is a simple conversion of the high and low words of "lParam" as it is
    used in the WM_MOUSEMOVE message to two LONG values that can be used for
    further processing of the message.

    Code:
      
        LOCAL xPos as LONG
        LOCAl yPos as LONG
      
      ' ------------------------------------
      ' Get high & low WORD values in wParam
      ' and convert them to DWORD values
      ' ------------------------------------
        ! mov edx, lParam     ' put lParam in edx
        ! mov ax, dx          ' put loword of edx in ax
        ! cwde                ' convert it to DWORD size
        ! mov xPos, eax       ' copy it to variable
        ! rol edx, 16         ' rotate edx by 16 bits(highword into lowword position)
        ! mov ax, dx          ' put loword of edx in ax
        ! cwde                ' convert it to DWORD size
        ! mov yPos, eax       ' copy it to variable
    The result is two LONG values in xPos & yPos.

    [email protected]

    ------------------
    hutch at movsd dot com
    The MASM Forum - SLL Modules and PB Libraries

    http://www.masm32.com/board/index.php?board=69.0

    Comment


    • #3
      Hutch, keep these examples comming!

      I have cruzed thru many assembler books, but they all seem
      to be Dos based. Can you recommend a book that is more win32'ish?

      I understand that it is possible to write a complete win32
      application in assembler, but I am mostly interested in in-line
      assembly in PBDLL.

      I don't think it would take long for me to learn, since I have 18yrs of 8/16 bit assembly under my size 32 belt.(32! I wish!)

      Thanks!
      Regards, Jules


      ------------------

      Comment


      • #4
        Jules,
        You can start with this page and go from there.
        Hutch's page has a link here and is a must see.
        Also his masm32 is a must download.

        James


        http://203.157.250.93/win32asm/index.html

        ------------------


        [This message has been edited by jcfuller (edited February 20, 2000).]

        Comment


        • #5
          Jules,

          Thanks for the feedback, I am pleased that these litle examples are useful
          to programmers who write PowerBASIC. 18 years is a very formidable
          background in asm so I think that the transfer to 32 bit should be a
          reasonably straight forward one for you. My own experience was one of
          relief at not having to work in segments any more and being free of the
          old 64k segment limit.

          There are a lot more instructions available from 386 up and many of them
          make assembler programming cleaner and faster to write. Unless you are
          writing code that exceeds the 4 gigabyte boundary, you are essentially
          free of numerical restrictions with DWORD size data types and this makes
          for simpler and faster code when you need it.

          The link that James was kind enough to put up is a friend of mine called
          Iczelion, I cannot spell his name as it is a Thai name but he is a very
          experienced assembler language programmer who is fluent in both MASM and
          TASM and he has an emphasis on operating system level code. His tutorials
          are very well done and he modified them slightly so that they could be
          included in my MASM32 project.

          There is a lot of excellent material on his site which reflects some of
          the range of styles available in assembler and the examples come from all
          around the world. Don't be worried about the IRC nicknames used by many
          of the young guys, many of them write very good code.

          I work in both PowerBASIC and MASM and with some practice, it is no big
          deal to port code from one to another because compilers and assemblers
          have different strengths depending on the task being undertaken. I prefer
          to prototype API style code in PowerBASIC as it is very clear and fast to
          develop but it is also useful to be able to develop an algorithm in
          MASM and port it to PowerBASIC.

          With only minor notational differences, PowerBASIC manages MASM style code
          very well so when I need to target an area in my code that the normal
          functions do not address fast enough, I can write an algorithm that more
          accurately targets the task that I have to do.

          Regards,

          [email protected]

          ------------------
          hutch at movsd dot com
          The MASM Forum - SLL Modules and PB Libraries

          http://www.masm32.com/board/index.php?board=69.0

          Comment


          • #6
            Hutch

            Would it not be wonderful if Bob added a stand-alone kick-butt macro assembler to his product line?

            It could start with a clean slate, all new syntax and no legacy baggage whatsoever. Windows and Linux both need a modern assembler that is well supported and is in continuous development.

            Microsoft is not doing MASM justice. If it were not for you and Iczelion we would all be out in the cold. TASM is no longer what it used to be and with problems at Borland I do not think it will have a future. Not much is happening with NASM either.

            We are all grateful to you for keeping MASM alive.

            Siamack


            ------------------


            [This message has been edited by Siamack Yousofi (edited February 21, 2000).]

            Comment


            • #7
              Siamack,

              I don't know if I would be game to try that one out on Bob Zale,
              he may start throwing compiler boxes at you. <g>

              [email protected]

              ------------------
              hutch at movsd dot com
              The MASM Forum - SLL Modules and PB Libraries

              http://www.masm32.com/board/index.php?board=69.0

              Comment


              • #8
                I know, I know

                But then again if he would not do it then who will (or can)?

                maybe I am getting too old for this. Does anyone want to swap a PC for a rocking chair?

                Siamack

                ------------------


                [This message has been edited by Siamack Yousofi (edited February 21, 2000).]

                Comment


                • #9
                  Thanks very much for pointing me to this information!

                  Regards, Jules

                  Comment


                  • #10
                    Hooo! Steve,

                    I dont want to be the guy of the bad news but have you heard the news about the new Intel/HP 64bit processor...

                    It will erase all that you take soo long to learn, the new CPU will use the very long instructions (VLI) of HP CPU . Yes the Intel compatibility will be keeped, but at a price (it will slow down to the half of the capacity of the CPU in the x86 instructions) .

                    Then, you must prepare your mind for the next nitemare!

                    ------------------
                    Francis Beaulieu
                    Prog senior of 17 years.
                    Microsoft Specialist

                    Comment


                    • #11
                      Francis,

                      Perhaps not such bad new, heaps of registers, Bye bye stack, names look
                      unusual with br.XXX = Jxx in x86 but thats just familiarity I guess.

                      Still, I am inclined to wait with no great expectation, the number of
                      prophet's who have been predicting the fall of the ancient architecture of
                      the x86 have been matched by the number of failed prophecies.

                      Pentium's keep getting faster and large scale copper technology is within
                      reach so they will continue to get faster for a while yet. We have seen
                      the rise and fall of 68000 architecture, risc and the PPC range is still
                      current but hardly making headway against the ancient x86 architecture.

                      Think of it this way, at least we are free of segmented architecture but
                      I have no doubt that the operating systems will do more to finish the
                      x86 than the hardware.

                      Regards,

                      [email protected]

                      ------------------
                      hutch at movsd dot com
                      The MASM Forum - SLL Modules and PB Libraries

                      http://www.masm32.com/board/index.php?board=69.0

                      Comment


                      • #12
                        Here is some news about I-64...

                        http://www.inquiry.com/pubs/infoworl...e08/T16-08.asp

                        ------------------
                        Francis Beaulieu
                        Prog senior of 17 years.
                        Microsoft Specialist

                        Comment

                        Working...
                        X