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

  • SEARCH
  • Home
  • 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 8914851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:49:52+00:00 2026-06-15T04:49:52+00:00

I’ve written a program in assembly language (MASM) to allow students to practice calculating

  • 0

I’ve written a program in assembly language (MASM) to allow students to practice calculating combinations. The program calculates the factorials recursively. I use a procedure called combinations which receives n and r, then gets (n=r)!, n!, and r! by calling a procedure called factorial. Factorial is recursive.

.data
n       DWORD   ?
r       DWORD   ?
result  DWORD   ?
answer            DWORD ?
divisor DWORD   ?

.code
MAIN    PROC

(some preliminary procedure calls)

push OFFSET divisor     ;ebp+20
push n              ;ebp+16
push r              ;ebp+12
push OFFSET result      ;ebp+8
call    combinations

;*************************************************
; combinations calls factorial (3 times) to calculate n!, r!, and (n-r)!.
; combinations calculates n!/(r!(n-r)!) , and stores the value in result.
; receives:  accepts n and r by value and result by address.
; returns: none
; preconditions: none
; registers changed:  eax, ebx, edx
;*************************************************
combinations    PROC
push        ebp
mov     ebp,esp

mov     eax, [ebp+16]   ;find (n-r)!
sub     eax, [ebp+12]
mov     ebx, eax
push        ebx
call        factorial
pop     ebx
mov     edx,[ebp+20]    ;move (n-r)! into result
mov     [edx],eax

mov     ebx, [ebp+12]        ;find r!
push        ebx
call        factorial
pop     ebx

mov     edx,[ebp+20]
mov     ebx, [edx]
mul     ebx         ;r!*(n-r)!, store product in eax
mov     ebx, [ebp+20]
mov     [ebx], eax          ;store product in divisor variable

mov     ebx, [ebp+16]   ;find n!
push        ebx
call        factorial
pop     ebx
mov     edx,[ebp+20]            
mov     ebx,[edx]           ;move value of divisor into ebx

mov     edx, 0
div     ebx         ;divide n! by divisor (r!*(n-r)!)
mov     ebx, [ebp+8]
mov     [ebx],eax                 ;move quotient into result

pop     ebp
ret     16
combinations    ENDP

;*************************************************
; calculates factorial recursively
; receives: 
; returns: factorial solution in eax
; preconditions: none
; registers changed: eax
;*************************************************
factorial   PROC
mov eax,dword ptr [esp+4]
cmp eax,1
jle endRecursive
dec eax
push eax
call factorial
mov esi,dword ptr [esp+4]
mul esi
endRecursive:
    ret 4
factorial   ENDP

All goes as expected and I’m getting the values needed. However, when all the calculations are done and the program reaches the statement “ret 16” at the end of the combinations procedure, I get the following exception:

Unhandled exception at 0x76f915de in Project.exe: 0xC0000005: Access violation.

I’ve run it through debugger and tried changing the return statement in the event that I miscalculated, but nothing I’ve tried thus far has worked. Maybe it’s a case of having stared at this too long, but any ideas would be appreciated. Thanks in advance.

Update: Just tracking ebp and esp in debugger: it looks like when the program comes out of the last factorial call, esp is +12 of ebp, so it’s adding +4 with each call of factorial. As a result, when the program hits pop ebp, ebp is pointing at r instead of where it should. Any suggestions for how to fix this?

  • 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-06-15T04:49:54+00:00Added an answer on June 15, 2026 at 4:49 am

    You need to remove the three pop ebx instructions that appear after the three top-level calls tofactorial. Since factorial already pops its argument off the stack (through the use of ret 4), trying to remove the argument again by doing pop ebx messes up the stack pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.