Shop OBEX P1 Docs P2 Docs Learn Events
Propeller Assembly Q&A - Page 3 — Parallax Forums

Propeller Assembly Q&A

13»

Comments

  • SailerManSailerMan Posts: 337
    edited 2009-09-30 00:58
    Kuroneko... Thanks!! Now that you've given me the answer, It seems so clear now.

    @localroger... I'm feeling that way.. I'm actually learning more in the past week than ever. I have ..obviously a long way to go..But this time I'm really interested.
  • SailerManSailerMan Posts: 337
    edited 2009-10-05 01:20
    I know what Bitwise operators are but what are the most common uses for them. AND/OR and Such.

    I read the manual I can comprehend what they do... but I am not sure how some commands will help me.

    Maybe some examples of their usefulness.

    TEST for instance.. I understand the manual.. but what is a practical use for it?

    Eric

    Post Edited (SailerMan) : 10/5/2009 12:25:32 PM GMT
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-10-05 02:10
    Not a problem, but the "FIT 496" statement should be after the "destination res 1" line as what you are checking to see you have not overflowed the cog memory and the destination is actually a part of this code. It does nothing other than forcing a compiler check.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • SailerManSailerMan Posts: 337
    edited 2009-10-05 17:26
    I hope I'm not being a pain but, Does anyone have any good resources that would show me uses for bitwise operators... See my previous post.

    Thanks in advance for any help.

    Eric
  • potatoheadpotatohead Posts: 10,261
    edited 2009-10-05 17:58
    I don't have a link, but I do have a moment.

    Bit operators, or booleans as some will refer to them, let you manupulate bits in a structured way. Assembly language has three very fundemental components:

    1. Moving (or really copying) numbers around.

    2. Performing additions (and really, everything is addition, even though we have lots of instructions that hide that from us)

    3. Bit operators.

    You've got two of the three under your belt! If you get all three, you will progress nicely with assembly code. That's some nice motivation maybe [noparse]:)[/noparse]

    To grok bit operators, you've got to understand truth tables. Truth tables are an expression of the bit operator rules. Truth tables can be used for lots of other things too.

    The core operators are: OR, AND, ANDN, XOR on propeller. NOT is combined with AND, and can be performed with XOR on propeller.

    I've got the basics in the latter portion of my beginner document. Some truth tables are there for you to reference. Be sure you know what they do.

    So, why use them?

    The most common use is to insure that specific bits, which are likely to be controlling hardware, or acting as display data, are in a specific state. Without these operators, we would have to combine many instructions and tests and branching to achieve the same results. In this, the bit operators are extremely powerful.

    Let's take a graphics display. If that display has one bit per pixel, then a bit set to a 1 shows us something, and a bit set to a zero shows us nothing. Simple.

    00000000       11000011      11000011
    00110000   OR  10000001  =   10110001
    00011000       10000001      10011001
    00000000       11000011      11000011
    
    11000011       10000001      10000001
    10110001   AND 00110000 =    00110000
    10011001       00011000      00011000
    11000011       10000001      10000001
    
    



    An OR instruction can make sure a bit is always a 1, regardless of the other bits. The combination of bits you use to control the target bits is called a mask. In the example above, the bits to be operated on are at left. The mask is in the middle, and the result is on the right. With OR, we are making sure some bits are set to 1, leaving the other bits as they are. Graphically, this adds shapes together on the screen. In terms of hardware control, an OR could make sure the bit to turn an LED on, is set to the on state, leaving the other bits alone. In the mask, 0 means leave it alone, and 1 means set it, no matter what it was before.

    An AND instruction can make sure that bits are only set to a one, if they are already a 1, otherwise make them 0. Again, the source bits are at left, mask in the middle, result at right.

    See how only the bits set to a 1 in the mask stay 1, but all other bits are zero?

    This is what bit operators do.

    Another example for building a table index. Say there is a counter. I want to take that counter, modulo 7, multiply by 2, and use it to reference entries in a table.

    In English then, I would move the counter to a temporary working register, perform an AND %00000111 to mask off all but the lower bits. That's the modulo operation. Then perform a SHL to do the multiply, then MOVs it to function as my index.

                    mov   temp, counter        'prepare to operate on counter
                    and   temp, #%00000111     'Take lower three bits only
                    shl   temp, #1             'Multiply by 2
                    movs  :index, temp         'Self modify index
                    nop                        'Deal with pipeline
    :index          mov   destination, #0-0    'Fetch table entry for further processing
    
    



    The key to this whole bit is the bit operators. The AND literally lets me make sure only the three lower bits of the counter are being used in the operations. It does this because of the rules (see tutorial truth tables) by which it works. The mask, is our expression of how we want to apply those rules (%00000111), leaving us with a known result, in this case one where only the three least significant bits are in play for the subsequent operations.

    If AND were not available, performing this kind of operation would be very difficult, requiring lots of testing, branching, etc....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    Chat in real time with other Propellerheads on IRC #propeller @ freenode.net
    Safety Tip: Life is as good as YOU think it is!

    Post Edited (potatohead) : 10/5/2009 6:08:17 PM GMT
  • SailerManSailerMan Posts: 337
    edited 2009-10-06 13:07
    Potatohead,

    This really makes sense. Thanks for taking the time to explain.

    So how would I add two Binary numbers besides Add Dest,SRC .. I guess the long way. with ASM.

    Thanks,
    Eric
  • potatoheadpotatohead Posts: 10,261
    edited 2009-10-06 15:40
    The add instruction is built in. That's what you use. When I said everything else is adds, I was referring to math. Because of how registers "wrap around", like your car odometer does when full, subtraction can be done with addition. For a byte, adding $FF is like subtracting 1, by way of example. With addition and subtraction done, multiply is possible, and of course, division can be done with a multiplication, which you know from basic primary school math, same as all of us do. From there, the world of math is opened up, with the speed of the processor, and the number of built in instructions limiting the time it takes to get anything done. The Propeller does not have a multiply built in, so this must be done via short cut, like with shift, or by a short loop that gets the necessary operations done, or through a table. There are some tables in the Propeller ROM that can be used for rough advanced computations. My favorite way to think of processors is in terms of adds per second, because of this.

    I suppose the bit operators could be used with branching to do the addition, shuffling through each digit. Go look at some of the multiply routines for an example of doing math the long way. Where there isn't an instruction, it's the long way, or shortcuts.

    Like shift is a multiply or divide by power of two is a shortcut. 'shl #2' = multiply by 4.

    This behavior, by the way, is why the powers of two show up so often in computing. When a power of two is involved, it's almost always possible to arrive at a computational solution that is optimal, involving a bit operator.

    Finally, I forgot to mention flags and testing. Let's say you've got a long and it's digits all represent conditions! That's basically 32 extra longs of data storage, where often the lazy and easy way is to just use an entire long for a flag. Flags are yes, no, sometimes maybe so, indicators that your program tests to make decisions. Bit operators allow the programmer to mask off all but a flag, test it, then act upon the result of that test.

    A compare (CMP) is really a subtraction, where the result is either 0, more than 0, or less than 0, and the Propeller flags (Z & C) are set. So, a move to obtain a temp working copy of something, followed by a bit operation to mask off all but what is tested, followed by a subtract with flag conditions set, can isolate a bit, test it's state, and branch. Other instructions, like TEST, wrap this up into one operation where the test is not destructive to the data, for optimal speed and overall instruction use.

    Many assembly language instructions are this way. I have always preferred to start simple, then use the advanced instructions when they become obvious they are needed. Usually this is a matter of speed or memory addressing space. One of those two comes up, then I go looking for tricks in the instruction set. The basics, addition, bit operators, branching, are sufficient to do anything. Doing it well, or efficiently comes with coding over time.

    IMHO, you are well down that road to "the assembly mind set". [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    Chat in real time with other Propellerheads on IRC #propeller @ freenode.net
    Safety Tip: Life is as good as YOU think it is!
  • SailerManSailerMan Posts: 337
    edited 2009-10-06 17:52
    I'm really trying to stick with it this time and I appreciate all the time you've taken to answer my questions.. I have read your tutorial so I see some parallels but you've given me more details.

    Do you have any simple & short ASM code that I can pick apart..and then ask questions about it. That would give me a common reference.

    Thanks again!!

    Eric
Sign In or Register to comment.