+ Reply to Thread
Results 1 to 5 of 5

Thread: Understanding assembler logics

  1. #1
    Learning Programmer Walle is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Stockholm, Sweden
    Age
    28
    Posts
    75

    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. #2
    The Crazy One TkTech will become famous soon enough TkTech's Avatar
    Join Date
    Jun 2006
    Location
    Canada
    Age
    18
    Posts
    1,549
    Blog Entries
    1

    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?

  3. #3
    Learning Programmer Walle is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Stockholm, Sweden
    Age
    28
    Posts
    75

    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!

  4. #4
    Guru G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough
    Join Date
    Oct 2007
    Age
    25
    Posts
    537

    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 11:51 AM.

  5. #5
    Newbie appstateguy is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    1

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Clear Window in 8086 Assembler, no Ints
    By omniscian in forum General Programming
    Replies: 1
    Last Post: 02-25-2008, 09:58 PM
  2. 8086 Assembler HELP
    By BamaBUB1983 in forum General Programming
    Replies: 3
    Last Post: 12-12-2007, 09:11 PM
  3. Assignment help understanding
    By lmc059 in forum Java Help
    Replies: 5
    Last Post: 11-29-2007, 03:38 AM
  4. Problems Using Assembler In C++
    By dargueta in forum C and C++
    Replies: 8
    Last Post: 10-15-2007, 08:48 PM
  5. understanding classes
    By Chan in forum C# Programming
    Replies: 3
    Last Post: 07-17-2006, 12:19 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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