[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PLEASE HELP: recursive factorial in assembly

Posted on 2003-12-11
5
Medium Priority
?
3,911 Views
Last Modified: 2012-06-22
Hi, I am trying to convert the following into 8086 assembly.

#include <stdio.h>

int i;

int fact(n) /* ANSI C version: */
    int n;  /* int fact(int n) */
{
    if (n == 1)
        return 1;
    else
        return n * fact(n-1);
}

void main()
{
    printf("input a number 1-9 : ");
    scanf("%d",&i);

    printf("factorial %d = %d\n",i,fact(i));
}

I have already done the parts that read a number from the keyboard and the result, so please ignore those parts of the code that concern these operations.

I have spend a lot of time, but I cannot implement recursion in the above code.

can you please help me. thanx in advance

below is the code i've done, but it doesnt give me results


      .MODEL TINY     ; Data and code fit in one 64K segment
        .STACK          ; allocate a stack
        .CODE           ; start of code segment

result      equ    [bp+6]
param      equ    [bp+4]


      sub sp,2
      push w[times]
      call fact
      
        jmp l0
      


fact:      push bp
      mov bp,sp
      push ax
      push bx
      push dx
      mov dx,param      
      cmp dx,1
      je base
      
      mov ax,param
      mov cx,ax
      dec cx
      
      mov bx,result
      push cx
      mul bx
      mov result,ax
      call fact

      
base:      mov dx,1      
      mov result,dx
      jmp end


end:      pop cx      
      pop dx
      pop bx
      pop ax
      sub sp,bp
      pop bp
      ret 2
      
l0:          mov ax,4c00h
        int 21h            ; return control to DOS with errorlevel 0

        .data

n       db ?            ; uninitialised short n
k       db ?
times      dw 4
prompt  db 'type a number from 1 to 9: $'

        end

0
Comment
Question by:espadana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 9924540
Recursion suppose that you will use stack for your parameters.
They can not be global in the way you implemented them.
This should be correct code for your fact(n):
      assume      cs:_TEXT
_fact      proc      near
      push      bp
      mov      bp,sp
      push      si
      mov      si,word ptr [bp+4]
      cmp      si,1
      jne      short callFact
      mov      ax,1
      pop      si
      pop      bp
      ret      
callFact:
      mov      ax,si
      dec      ax
      push      ax
      call      near ptr _fact
      add      sp,2
      imul      si
      pop      si
      pop      bp
      ret      
_fact      endp
0
 
LVL 11

Expert Comment

by:dimitry
ID: 9924624
Sorry, I didn't pay attention that you have "define" of param as [bp+4].
I think that your code has 2 problems:
1) You are trying to keep result in stack too... And it should be in AX...
2) This code is wrong:
    mov result, ax
    call fact
base:    
    mov dx,1             <--- Always set result to 1
    mov result,dx
    jmp end



0
 
LVL 3

Accepted Solution

by:
terageek earned 1000 total points
ID: 9925256
You can pass return values back on the stack.

; This block is wrong.  
; You need to reserve space on the stack for making a recursive call,
; make the recursive call passing in n-1,
; multiply n by the result from the recursive call,
; and return the result back on the stack
; This code doesn't do that.
     mov bx,result
     push cx
     mul bx
     mov result,ax
     call fact

...    

end:     pop cx   ; you never pushed cx  
     pop dx
     pop bx
     pop ax
     sub sp,bp ; I have no clue what this does except mess up your stack pointer
     pop bp
     ret 2        ; You reserved 4 bytes on the stack for the call, 2 for param and 2 for result
0
 

Author Comment

by:espadana
ID: 10140276
Thanx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With the evolution of technology, we have finally reached a point where it is possible to have home automation features like having your thermostat turn up and door lock itself when you leave, as well as a complete home security system. This is a st…
If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question