Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Basic code

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

  • Basic code

    Can anybody help me write code to add, substract, multiply and divide using add and substract statements only ??

    Thanks

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

  • #2
    you only need shift,add and XOR to perform add/subtract/multip./divide


    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Comment


    • #3
      Hmmm... is this a school assignment? Perhaps you should do it on your own?

      I think it should be simple enough to add by adding, and subtract by subtracting,
      unless your question left out something important?

      Multiplication by addition, well. x * y is the same as x added to itself y times,
      is it not? Likewise, x / y is about the same as subtracting y from x until y is
      less than x...

      I think there are BCD examples of handling this sort of issue in the PBWiz library
      from Information Management Systems, if you need working code. See www.infoms.com
      for details.

      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment


      • #4
        Manuel,

        I'll leave the writing of the code to you, but here is an example
        of adding to achieve multiplying. Let's multiple 126 times 624.

        126
        x 624
        -----

        First add 126 4 times.
        Now add 1260 2 times.
        and now add 12600 6 times.

        The addition problem would look something like this:

        126
        126
        126
        126
        1260
        1260
        12600
        12600
        12600
        12600
        12600
        12600
        --------
        78624
        ========

        Before the days of calculators, this is the way that we had
        to multiply on an adding machine... hope that this gets you
        started.

        Buck

        p.s. Geezzz! did I date myself a little or what...

        Comment


        • #5
          The column of numbers in my previous post should be right
          justified... I didn't realize that the msg base would move
          everything over to the left... grrr! computers and their
          formatting.

          Buck

          Comment


          • #6
            Buck, use [ code ] and [ /code ] statements (without any spaces) to format with fixed-width fonts, eg:
            Code:
            sweet huh?  

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

            Comment


            • #7
              Thats a rather cute technique Buck, I will have to remember that
              one for integer multiplication in assembler as multiply is very
              slow in integer instructions.

              Regards,

              [email protected]

              ------------------
              hutch at movsd dot com
              The MASM Forum

              www.masm32.com

              Comment


              • #8
                Hutch
                Only looks cute in decimal, shift and add if 1 is how its done in most CPU's unless the have a dedicated flash multiplier.
                The only reason the standard instruction would be slow is due to using 2 registers for the result.
                Pity Intel doesnt have an instruction which does an integer multiply resulting with a long in EAX



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

                Comment


                • #9
                  Wayne,

                  Use " [ code ] and [ /code ] ", you say... hmmm.... very
                  intuitive. Wonder why I didn't think of that...

                  Buck

                  Comment


                  • #10
                    Those, and the other UUB formatting options for the BBS, are detailed at http://www.powerbasic.com/support/forums/ubbcode.html which appears in a link next to all "reply" boxes...

                    Likewise, the smilies legend can be found at: http://www.powerbasic.com/support/forums/smilies.html

                    I hope this helps!


                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      You are probably right John, I do a quick & dirty int multiply
                      this way,

                      Code:
                      IntMul proc source :DWORD,multiplier :DWORD
                        
                          LOCAL var1 :DWORD
                        
                          fild source         ; load source
                          fild multiplier     ; load multiplier
                          fmul                ; multiply source by multiplier
                          fist var1           ; store result in variable
                          mov eax, var1
                        
                          ret
                        
                      IntMul endp
                      It translates directly to PowereBASIC inline with no problems.

                      Still, ther are a lot of the old timer tricks that are worth
                      knowing so Bucks technique may be of some use when approaching
                      integer multiply without FP instructions.

                      Regards,

                      [email protected]

                      [/CODE]

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


                      [This message has been edited by Steve Hutchesson (edited March 30, 2001).]
                      hutch at movsd dot com
                      The MASM Forum

                      www.masm32.com

                      Comment

                      Working...
                      X