Closed Thread
Results 1 to 5 of 5

Thread: Understanding assembler logics

  1. #1
    Walle is offline Learning Programmer
    Join Date
    Mar 2008
    Location
    Stockholm, Sweden
    Posts
    75
    Rep Power
    0

    Understanding assembler logics

    Ok, so I'm learning assembler. What I'm having a hard time grasping is how to do basic arithmetics.

    Addition and Subtraction is no problems ofc, but how to do multiplication and division is harder to get. Multiplication/division by any number that is a power of 2 is easy to go ofc, but what about multiplying a number by say 3?
    To multiply by one I would only shift left one bit, right? To multiply by 4 I would shift left 2 bits, but how do I multiply by 3?

    What I really would like is an application that take C-style operations and output assembler-like code. Ie x=x+2 would output something like:

    Code:
    movlw 0x02
    addwf x,1
    A=B+C would output something like:

    Code:
    movf B,0
    addwf C,1
    movwf A
    you get the picture, right?

    Are there any tools like this? Can anyone point me to some other good resources on learning the logics behind arithmetic operations in assembler?

    /Chris

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: Understanding assembler logics

    movfw? movf? I know x86 nasm style asm, and these aren't any ops I know. Is this for a MIPS machine or a different architecture? ASM is very low level, you need to specify the compiler/architecture your using.

    ALL C compilers that I know of offer a flag to output to asm, since a C compiler will compile to proper asm before its compiled into machine code. For GCC, this flag is -S.

    Why are you not simply using the DIV/MUL-series of instructions?

  4. #3
    Walle is offline Learning Programmer
    Join Date
    Mar 2008
    Location
    Stockholm, Sweden
    Posts
    75
    Rep Power
    0

    Re: Understanding assembler logics

    Sorry, I forgot to mention that I'm programming MCU's, mostly 8-bit MCU's from Microchip. The instruction set is very limited, with a total of 35 ops to play with. The only arithmetic ops available are addition, subtraction, complement, increment, decrement, and the left/right shifts. Then ofc the logic ops.

    Ofc! Thank you! The C-compiler (Hitech) must have an asm-output as well as binary, I will look into that!

  5. #4
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21

    Re: Understanding assembler logics

    For integer multiplication you can always write a loop that does the following.

    Code:
    int mul(int first, int second) {
      int output = 0;
    
      while(second > 0) {
        output += first;
        second--;
      }
    
      return output;
    }
    Last edited by G_Morgan; 03-17-2008 at 09:51 AM.

  6. #5
    appstateguy is offline Newbie
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Re: Understanding assembler logics

    You probably won't gain too much my looking at the assembly of C output since there are multiply and divide instructions for x86.

    Say you have your values you wish to multiply together in two registers (R1 & R2), you will simply need to add the value within R1 R2 times.
    I.E. 2 * 3 = 2 + 2 + 2

    Since i don't know MCU I'll use pseudocode
    Code:
    //R1 * R2 -> R3
    
    MOV 0 to R3 //clears R3
    TEST R1 //set condition codes for R1 -- ADD 0 to R1 would also work
    Branch on zero to End:
    TEST R2 //set condition codes for R2 -- ADD 0 to R2 would also work
    Branch on zero to End:
    MUL:
    ADD R1 to R3
    ADD -1 to R2
    Branch on zero flag to End // done
    Branch Unconditionally to MUL: //continue adding
    End:
    //R3 now contains the result
    Integer division would be similar, but a little more complicated. Hope that helps.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. TASM assembler help
    By M88 in forum Assembly
    Replies: 6
    Last Post: 06-25-2010, 07:38 PM
  2. Assembler
    By fondi in forum C and C++
    Replies: 2
    Last Post: 03-31-2010, 09:26 AM
  3. Assembler guy
    By Slider in forum Introductions
    Replies: 6
    Last Post: 08-27-2009, 09:37 AM
  4. clear my logics
    By Orjan in forum PHP Development
    Replies: 5
    Last Post: 02-04-2009, 08:58 AM
  5. 8086 Assembler HELP
    By BamaBUB1983 in forum General Programming
    Replies: 3
    Last Post: 12-12-2007, 07:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts