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:
A=B+C would output something like:Code:movlw 0x02 addwf x,1
you get the picture, right?Code:movf B,0 addwf C,1 movwf A
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
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?
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!
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.
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
Integer division would be similar, but a little more complicated. Hope that helps.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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks