• 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

    • Indiana Jones and the Great Circle: The Order of Giants DLC launches this September by Pulasthi Ariyasinghe The Xbox Games Showcase gave a look at the next Indiana Jones adventure today. MachineGames showed up with a trailer for the first DLC pack coming to Indiana Jones and the Great Circle: The Order of Giants. Set in Italy, the fresh story will introduce new locations, puzzles, enemies and more. Watch the reveal trailer above. This Indy journey will be taking place during the main campaign and dive deeper into the tribe of giants, Nephilim Order, that players see in the base game. The story will kick off with Indiana Jones offering help to a young priest named Father Ricci, who is attempting to track down a mysterious artifact. The resulting journey expands into this separate storyline that takes the player beyond Vatican City to explore Rome's streets and the crypts beneath it. “One of the most satisfying parts of developing Indiana Jones and the Great Circle was the opportunity to create the Nephilim Order, the secret society of giants formed by descendants of fallen angels working to atone for their ancestral sins,” says Production Director John Jennings. “That story spans thousands of years and this DLC has given us the opportunity to develop further upon their lore and look at other aspects not covered in the initial story campaign. There’s something new and unexpected to tell here for fans of the game, and we can’t wait for people to be able to play it for themselves.” The Indiana Jones and the Great Circle: The Order of Giants DLC will be releasing on September 4, 2025. It will be available on PC, Xbox Series X|S, and PlayStation 5. Premium Edition, Premium Upgrade, Collector’s Edition, and Collector’s Bundle owners will be receiving the DLC for no extra charge at launch.
    • Could we have a desktop version of this lightweight bloat free Windows 11?
    • So they admit that Windows 11 is slow and bloated. Good to know!
    • Real handheld Xbox hardware was canceled, this is just a PC with a screen and controller built-in.
    • Amazon Deal: JBL BAR 1000 7.1.4, BAR 700 5.1 Dolby Atmos wireless subwoofer soundbars by Sayan Sen If you are in the market for an audio system and are after smaller bookshelf speakers delivering highly accurate sound, then take a look at KEF and Polk Audio's Q Concerto Meta and Reserve R200 speakers, respectively, as both of them are up for sale at their lowest ever prices. However, if you are more into shaking your house, which is not possible without a subwoofer, then Samsung has its Q900F, Q800F, and Q600F soundbar systems with wireless subwoofers at the lowest prices. These are the latest 2025 models, and you can take a look at them in this article here. JBL BAR 1000 For those looking for additional options, JBL's BAR 1000 and Bar 700 are also available. The former has hit its lowest ever price too, while the latter is back to its cheapest (purchase links down below). JBL claims that its BAR 1000 model goes as low as 33Hz which is crucial for movie-watching or even some genres of music. The 10-inch subwoofer is rated at 300 watts of RMS power. The total power output of the system is 880 watts at THD (total harmonic distortion) of 1%. JBL BAR 1000 rear view Unlike the 7.1.4 JBL BAR 1000, the BAR 700 is a 5.1 system which means it lacks true Dolby Atmos, but it should still provide an Atmos-like experience. DTS:X is also not supported. The BAR 700 is rated at 620 watts. It is good to see some power ratings, as companies like Samsung, Sonos, Bose, and more tend not to mention them all too often nowadays. Interestingly, both the BAR systems have similarly-specced subwoofers so if bass is what you are looking for and do not care about the Atmos experience so much, you can opt for the BAR 700 too. Get them at the links below: JBL Bar 1000: 7.1.4-Channel soundbar with Detachable Surround Speakers, MultiBeam™, True Dolby Atmos®, and DTS:X®, Black: $799.95 (Shipped and Sold by Amazon US) JBL Bar 700: 5.1-Channel soundbar with Detachable Surround Speakers and Dolby Atmos®, Black: $549.95 (Amazon US) + you also get free 90-day Amazon Music This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      475
    2. 2
      +FloatingFatMan
      264
    3. 3
      ATLien_0
      236
    4. 4
      snowy owl
      228
    5. 5
      Edouard
      174
  • Tell a friend

    Love Neowin? Tell a friend!