Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4247270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:05:07+00:00 2026-05-21T04:05:07+00:00

I wrote a simple program which takes a predefined number to a predefined power

  • 0

I wrote a simple program which takes a predefined number to a predefined power and returns the result. It’s in NASM assembly, for Linux. I’ve been trying to get my head around how to use the stack to pass variables to functions, and wanted to make sure I did this correctly. It returns the correct number, but I have no idea if it’s correct:

section .text
    global _start

_start:
    push dword 3        ;(power)
    push dword 2        ;(num)

    call power

    mov ebx, eax
    mov eax, 1
    int 0x80

power:
    push ebp
    mov ebp, esp
    sub esp, 8      ;reserve space for two local vars
    push dword [ebp+8]
    pop dword [ebp-4]   ;store original num as a local var
    push dword [ebp+12] 
    pop dword [ebp-8]   ;store power in a local counter var
    dec dword [ebp-8]
    mov eax, [ebp-4]    ;store original num in eax (current val)    
    jmp power_loop  

power_loop:
    imul eax, dword [ebp-4] ;multiply current val by original num
    dec dword [ebp-8]   ;decrement counter

    cmp dword [ebp-8], 0
    jne power_loop

    mov esp, ebp        ;restore stack pointer
    pop ebp
    ret 

Any advice would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:05:08+00:00Added an answer on May 21, 2026 at 4:05 am

    It looks good for the most part. However, after the power returns, you should remove the variables from the stack. It doesn’t matter in these circumstances since _start doesn’t return, but it will matter if you try to call a function from a function that returns and doesn’t clean up. The return address of a function is stored on the stack and popped off by the ret instruction, so if you have something else on the top of the stack you will return to the wrong location.

    _start:
        push dword 3
        push dword 2
        call power
        add esp,8 ; Removes two dwords from stack
    

    If you write a function that calls a lot of other functions, it is better to allocate space for stack arguments at the beginning of the function, write to it before each function call, and remove it from the stack at the end of the function. That way, you spend less time pushing and popping because you can use mov with the proper address instead.

    _start:
        sub esp,8 ; Make room for two dwords
        mov dword [esp+4], 3
        mov dword [esp], 2
        call power
        add esp,8 ; Removes two dwords from stack
    

    As a comment on your power function: It currently only works if the power is at least 2. You could change the minimum power to 0 by:

    • Start eax at 1
    • Don’t decrement the counter variable before the loop
    • Check to see if the counter is 0 at the beginning of the loop

    Example:

        ; dec dword [ebp-8] ; Don't do this
        mov eax, dword 1
        jmp power_loop  
    
    power_loop:
        cmp dword [ebp-8], 0
        je end_loop
    
        imul eax, dword [ebp-4] ;multiply current val by original num
        dec dword [ebp-8]   ;decrement counter
        jmp power_loop
    
    end_loop:
        mov esp, ebp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program in VC++ which will just initialize the
I have a simple Ruby program which takes the command line arguments and posts
I wrote a simple Windows Forms program in C#. I want to be able
I'm just starting out writing trying to write a simple program in C and
I wrote a simple web service in C# using SharpDevelop (which I just got
I wrote a simple javascript image rotator which picks a random image on each
I am very newbie to Perl. I wrote a very simple Perl program (script):
I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do
I wrote a simple batch file as a PowerShell script, and I am getting
I wrote a simple tool to generate a DBUnit XML dataset using queries that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.