• 0

[ASM] How to write a formula for instruction count


Question

Right up front: this is for my computer science class, so don't directly give me the answer. Guide me. :p

We're working in assembly right now, and are using a simulated machine that has a much simpler instruction set than a real instruction set. The current project has us writing 3 different programs that do the same thing in different ways: given an exponent and a base, calculate the appropriate result. The first program is just a simple loop, multiplying the base by itself exponent number of times.

The second one uses this formula (in Java because it's fairly readable):

public static double power(double b, int e) {
        double power = 1;
        while (e > 0) {
            if ((e % 2) == 0) {
                b = b * b;
                e = e / 2;
            } else {
                power = power * b;
                e = e - 1;
            }
        }
        return power;
    }

I'm trying to find a formula for the number of instructions run for any given exponent. For the first program, coming up with the formula was fairly straightforward. But this one has added complexity, in that if the number is divisible by 2, it divides the exponent by two. I can't for the life of me figure out how to come up with the formula. The simulated machine tells me exactly how many instructions are run each time, but I need a formula to predict that.

Here's the assembly code I've written. It works, and it's been checked by my prof.

;;performs exponation given a base and a non-negative integer exponent

allocate-registers base, exponent       ;;inputs
allocate-registers one, two	                 ;;constants
allocate-registers expMod2                 ;;temporary register
allocate-registers exponate, done, if   ;;labels
allocate-registers output                    ;;holds the result


	read base
	read exponent
	li one 1
	li two 2	
	li exponate exponateL
	li done doneL
	li if ifL
	li output 1

exponateL:
	jeqz exponent done         ;;while exponent is greater than one do this loop
	rem expMod2 exponent two
	jeqz expMod2 if            ;;if exponent%2 == 0, jump to ifL
	mul output output base
	sub exponent exponent one
	j exponate


doneL:
	write output
	halt


ifL:
	mul base base base
	div exponent exponent two
	j exponate

The lines that have a colon at the end (doneL, etc) are line labels and aren't counted as instructions. Also, the register allocations do not count. jeqz is a conditional jump, will jump to the label given by the second argument if the content of the first argument (a register) is zero.

j is an unconditional jump.

li loads the second argument into the register specified by the first argument.

add, sub, mul, div are standard operations. rem is remainder.

Can you help me?

2 answers to this question

Recommended Posts

  • 0

The only thing I could think off would be to wrap each asm instruction into a "function" (called via a jmp) so you can manually increment your counter based on how many instructions it takes to do whatever takes you need it to do. It will be hard coded and slow but thats the only thing I can think of.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • End of an era? Kubuntu is removing default support for X11 in new installs by David Uzondu X11, the old window system whose days have long felt numbered, just saw another one of its major supporters head for the exit. Kubuntu has decided to follow its parent distro's lead, making its next release, version 25.10, a Wayland-only affair for fresh installs. It seems many Linux developers see Wayland as the future. Just recently, Linux Mint started working to improve support for the protocol in Cinnamon, tackling lingering issues with keyboard layouts and input methods. You can even see the progress in KDE's development, where an upgrade to Wayland PiP is planned for KDE Plasma 6.5. So what's the logic behind dropping a session that, for the most part, still works? According to Kubuntu's Rik Mills, the team wants to "rip off this sticking plaster" now, in an interim release, rather than ###### off a lot of people by doing it in the next Long-Term Support version, 26.04. The developers feel that maintaining code for the aging X11 system holds back progress on security and new features that Wayland can enable more easily. Plus, supporting two separate display servers is a massive undertaking. Of course, this change might have some people worried, but relax; all is not lost if you still need the old session. If you're running hardware that acts up, like some older NVIDIA cards, or who relies on an ancient application that doesn't play nicely with the XWayland compatibility layer, you can still get your familiar session back. Just enter the following command in your terminal: sudo apt install plasma-session-x11 Once that command finishes, the X11 session will appear as an option on the login screen, so you can carry on as before. As OMGUbuntu notes, not everyone in the Ubuntu family is following its lead just yet. Other official flavors like Xubuntu, Ubuntu Budgie, and Ubuntu Cinnamon are expected to keep offering an X11 session on their default installs for this cycle.
    • Mangohud hasn't been built into "Steam Deck", it has been built into SteamOS. I understand that your goal is to try and praise MS for a simple feature that everyone else has, but we are comparing OS vs OS. Hardware does not have anything "built-in". Software does. Like it or not, SteamOS has it "built-in". And it is far superior to XBOX game bar's information.
    • Please don't inject yourself into a discussion you did not read or understand what was said. No where did I say Windows was hard to use. I knew when seen two notifications with your handle the replies they were going to be a waste of time to read.
    • Exactly. The UI feels a lot snappier when animations are disabled. I think I might actually give it a try for a while, I'm liking it so far.
    • keep in mind some things like chrome look at this setting to disable some animations in browsers... its an accessibility thing
  • Recent Achievements

    • First Post
      Johnny Mrkvička earned a badge
      First Post
    • Week One Done
      viraltui earned a badge
      Week One Done
    • One Month Later
      serfegyed earned a badge
      One Month Later
    • Dedicated
      firey earned a badge
      Dedicated
    • Dedicated
      fettermanj earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      648
    2. 2
      Michael Scrip
      223
    3. 3
      ATLien_0
      221
    4. 4
      Xenon
      144
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!