Optimizing ASM

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Optimizing ASM

Krozu
Going to try this without giving too many hints away. And i think you only understand what i'm about to say if you already solved it anyway.
Short question:
          Is this actually done on assembly level?

The somewhat longer question:
          I'm going to use Mult.asm as an example.
          Let's say that R0 = 5 and R1 = 2000, for calculation 1.
          For calculation 2, let's make R0=2000, and R1=5

          Won't this make one of the calculations really long depending on how you did your code?
          Making it shorter would require you to find the lowest/highest value and calculate with the help of
          this. So, is this done on OS level in most if not all cases, or are there reasons to do it on assembly
          level?
Reply | Threaded
Open this post in threaded view
|

Re: Optimizing ASM

cadet1620
Administrator
This is a good observation that the order of the operands vastly affects the run time of the "repeated addition" multiplication algorithm.

There are better algorithms for multiplication, one of which is presented in chapter 12 where you will be writing the OS's multiply function. These algorithms take approximately the same time to run regardless of the operand values.

This is analogous to the way that the standard decimal multiplication algorithm takes about the same amount of work to multiply 123*987 as it does for 987*123
       123       987
     x 987     x 123
    ------    ------
       861      2961
      984      1974
    1107       987
    ------    ------
    121401    121401
Usually OS and library functions are optimized so that application programmers don't need to worry about how things like argument order may affect performance.

--Mark