Calcolo fattoriale

di il
13 risposte

Calcolo fattoriale

Buona sera a tutti.

Sono nuova di questo sito, spero mi accoglierete e mi aiuterete con l'Assembler e spero di poter ricambiare con la mi esperienza in C# e .Net.

Devo sviluppare un programma in assembler (8086) che :
1)accettare in ingresso DUE NUMERI TRA 0 E 64000
2)RESTITUIRE I FATTORIALI DEI NUMERI COMPRESI TRA I DUE
INSERITI

Tutto ok per quanto riguarda il punto 1; per il punto 2 ho preferito procedere per passi.
Mi sto limitando a calcolare il fattoriale del primo numero inserito:
sono riuscita a fare il ciclo per il calcolo fattoriale e memorizzare i valori nello stack.

A questo punto quando devo riprendere i valori dallo stack per moltiplicarli, questi diventano sempre zeri...sono certa che il problema sia nel fatto che il registro DX è 0.

Vi lascio lo stralcio di codice per il calcolo fattoriale:

FATTORIALE PROC NEAR
XOR AX,AX
XOR BX,BX
XOR CX,CX
XOR DX,DX
MOV DX,[NUM1]
MOVAX,1
FATT: CMP DX,0
JZ FINE ;ESCE DA FATT
PUSH DX ;PRESERVO DX
DEC DX ;DECREMENTO DX
CALL FATT ;RICORSIONE
CMP DX,0 ;se DX == 0 allora lo incremento di 1
JZ INCDX
INCDX: MOV DX,1 ;DX=1 altrimenti il primo membro della moltiplicazione sarebbe 0
POP DX ;RIPRISTINO DX
MUL DX
FINE: MOV DL, DH
MOV AL, DL
MOV AH, DH
CALL STAMPA_NUMERO
RET
FATTORIALE ENDP

Grazie per l'aiuto che vorrete darmi

13 Risposte

  • Re: Calcolo fattoriale

    Il fattoriale di 64000?
  • Re: Calcolo fattoriale


    2.976279049667978 * 10^279803

    inizia con
    2976279049667977941768841727934920732808063900857558848711065686033731\
    4527449576974817622279757905574413867590155615935562123104543214026885\
    1898693877506646312635061053057071719630673309259836801493779631216029\
    2710737670970759955781976742001119618702972902715356647619647603839013\
    5231140924377973551680872343771835582904994330306743449855556334969319\
    0138431992231791177369321885693106770966558187111547177040945262474875\
    6521101492886453268323997468430121443384832925531039143506567402656762\
    6552742992538306581364755512420050977030060740900096159347663646274740\
    7023260437355879811243221451525491188476083927979871510889146186494299\
    6115143780389841330951087654208047646679279012752405390708578674840193\
    9490181821783237338513981421396898131545126290637353998039788354814458\
    3771332439248054425386932944764688229891040876411902081114444595615192\
    7000384236404447290800517749980494814888930240968759774173174876491532\
    1631265503594120580368840392644914033860641275607782025305066266806472\
    0553238670262020295509326578254576326925362244567431260673852677105660\
    1466590795807206049415429311895509869975351691187682609618194444492977\
    8906383268737943253822695500355981104693154215287546621289871791671614\
    4133779058889534011245055937682054588807442355948795069764462616133633\
    8827957418642421093587140115136623561281637151681216214468990520923991\
    2713585735919055606451179558043007321472245578588671637260272450693759\
    1807759316969674414985086470563784388232633523269368715389576493745279\
    8459499016009634559270028961313259454372493064457301217411579764789729\
    5059436325952428571925651353911704793928872021703487372669528760941438\
    0329137156296496168897854738128038003527566457208652504769198385938546\
    2162834664533222467809451053362931588106396326711052481219056434366242\
    7016486295675268648184442242523492329372762879587125869800816945910771\
    1800994194289638296939559324376726530619274497363710669445405150485615\
    8648470759022025534698647115593332433440809683834520550122421874399293\
    7148900613652945253634164046819195416698676956444425300971692882251255\
    6626759852777331383932786635989169581166937700523967063685806325093464\
    2815151534137396531650197549146345148520487548345375837339775483090166\
    54624344

    termina con
    4000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    0000000000000000000000000000000000000000000000000000000000000000000000\
    000000000000000000000000000000000000000
  • Re: Calcolo fattoriale

    Si raga, vi lascio immaginare la mia espressione quando ho letto il testo dell'esercizio fornitomi dal prof. ( ).

    Cmq, ho risolto il problema precedente: ora riesco a stampare la sequenza dei numeri che servono per ilcalcolo fattoriale; esempio: se il numero è 3, visualizzo 123, se inserisco 5, allora stampo 12345.

    Come posso fare per moltiplicare questi valori (salvati in un vettore) e stamparne il prodotto (ossia il mio maledetto fattoriale)?

    Grazie,
  • Re: Calcolo fattoriale

    Non puoi calcolare il fattoriale di 64000 ! Ci deve essere un errore nel testo dell'esercizio.
  • Re: Calcolo fattoriale

    Perche' no, la calcolatrice di windows ci mette meno di un minuto
  • Re: Calcolo fattoriale

    Non è che "non puoi" in assoluto (è ovvio che si può ...). La calcolatrice è un programma che usa un tipo di dato adatto al calcolo di grandi valori e librerie apposite.

    Tu hai a disposizione dei tipi di dati troppo semplici (non parliamo dell'intero ma neanche del virgola mobile ...) per fare quel calcolo. Dovresti usare una libreria apposita o costruirla tu e per un esercizio mi sembra assurdo.
  • Re: Calcolo fattoriale

    mydb ha scritto:


    perche' no, la calcolatrice di windows ci mette meno di un minuto
    Il fattoriale di 64 vale circa 1.3 *10^89 (diconsi, ottantanove)

    il fattoriale di 64000 vale circa 3*10^280000 (diconsi, duecentoottantamila!) come indicato nel post precedente, quattro ordini di grandezza piu' grande.
  • Re: Calcolo fattoriale

    @mydb ... Più precisamente 2,9762790496679779417688417279349e+279803 che non puoi rappresentare con i tipi di dati nativi del sistema ...
  • Re: Calcolo fattoriale

    Ragazzi, state calmi, per favore! Pienamente d'accordo con voi: l'intento non è quello di calcolare il fattoriale di 64000, ma:
    1) usando un numero così grande sono costretta ad usare un vettore per memorizzarlo (primo skill che il prof.vuole valutare); FATTO
    2) sviluppare una funzione che calcoli il fattoriale: Work In Progress con il vostro aiuto;
    3) stampare i risultati a video; Work In Progress;
    4) utilizzare dei 'contatori' che mi segnalino quando smettere di calcolare il fattoriale; Next Step


    Potreste aiutarmi?

    Grazie
  • Re: Calcolo fattoriale

    Proverei ad aiutarti ma non capisco cosa vuoi dire ...
  • Re: Calcolo fattoriale

    oregon ha scritto:


    Proverei ad aiutarti ma non capisco cosa vuoi dire ...

    lamu ha scritto:


    Ragazzi, state calmi, per favore! Pienamente d'accordo con voi: l'intento non è quello di calcolare il fattoriale di 64000, ma:
    1) usando un numero così grande sono costretta ad usare un vettore per memorizzarlo (primo skill che il prof.vuole valutare); FATTO
    2) sviluppare una funzione che calcoli il fattoriale: Work In Progress con il vostro aiuto;
    3) stampare i risultati a video; Work In Progress;
    4) utilizzare dei 'contatori' che mi segnalino quando smettere di calcolare il fattoriale; Next Step


    Potreste aiutarmi?

    Grazie
    Scusa, lamu, ma questo post e' incomprensibile:

    1) a che ti serve un vettore per il calcolo del fattoriale?
    2) di quali contatori stai parlando?
    3) a che ti servono i contatori, se il calcolo del fattoriale di N richiede ESATTAMENTE N (o N+1) passi?

    Nel tuo primo post parlavi di calcolare i fattoriale per tutti i numeri compresi tra A e B. Il vettore ti serve per salvare i rusultati del calcolo del fattoriale dei diversi numeri?

    Quindi, ricominciamo:

    0) separa i concetti. Da una parte 'devo calcolare f(x) per tutti i numeri compresi tra A e B e salvare i risultati in un vettore'. Che sia il fattoriale o qualunque altra funzione con un solo argomento, non cambia assolutamente nulla nella logica da implementare. Dall'altra parte hai 'calcolare il fattoriale di n'

    a) definisci 'fattoriale di n'
    b) decidi come lo vuoi calcolare: in modo iterativo o ricorsivo?
    c) vuoi utilizzare l'aritmetica in precisione arbitraria, gli interi di macchina o i floting point di macchina?
    d) quale e' il range di valori su cui vuoi calcolare il fattoriale?
    e) lo vuoi calcolare in modo esatto o usando le formule approssimate? Esatto, il fattoriale puo' essere calcolato con la approssimazione di stirling

    Ti ricordo, come ti e' gia' stato detto, che il fattoriale di 64000 e' un numero che non e' esprimibile con nessuno dei tipi di numeri di macchina (intero a 8, 16, 32 o 64 bit) ne con i floating point di macchina (32 o 64 bit). Serve una libreria per l'aritmetica in precisione arbitraria. Non difficile da implementare, ma neanche una passeggiata per chi e' alle prime armi.

    Tra l'altro, una cosa che ti consiglio di fare, e' scoprire quale e' il valore massimo di n per cuil il suo fattoriale e' esprimibile con un intero di 8, 16, ..64 bit, o con un float di 32 o 64 bit. E magari postarlo

    Tral'altro, questo ti dovrebbe illuminare sul perche' ottieni sempre 0!
  • Re: Calcolo fattoriale

    Ciao Ragazzi, eccomi ancora qui a chiedere aiuto!!!
    Allora sono sempre ferma al calcolo fattoriale!!!

    In breve il mio problema é che quando riprendo i dati dallo stack il registro DX viene valorizzato anche con dei valori che NON ho inserito nello stack: mi spiego con un esempio.
    1° Giro: valorizzo DX con 8, poi lo decremento e ogni volta inserisco nello stack il valore finchè DX == 1. Mi aspetto uno srack fatto in questo modo 8|7|6|5|4|3|2.
    2°Giro: riprendo i dati dallo stack e li moltiplico per ottenere il fattoriale.
    La prima iterazione carica 2 dallo stack su DX e moltiplicandolo salva in AX 2 (OK!)
    La seconda iterazione carica un valore che non ho mai inserito (esempio 248) dallo stack su DX e moltiplicandolo salva in AX (496): perchè?????Io mi aspettavo che caricasse 3!
    La terza iterazione carica 3 dallo stack su DX e moltiplicandolo salva in AX (che moltiplicato per il valore di prima da 744).
    La quarta iterazione carica un valore che non ho mai inserito, ma lo stesso dell'iterazione 2 (esempio 248) ...!

    Sapete dirmi cosa c'è di sbagliato nel mio codice? ve lo allego.

    Grazie,

    FATTORIALE PROC NEAR
    XOR AX,AX
    XOR DX,DX
    MOV AX, 1 ;AX=1
    MOV DX, 8 ;IL FATTORIALE DI 8 (40320) è L'ULTIMO INFERIORE A 64000
    FATT: CMP DX, 1 ;DX==0?
    JZ FINE ;ESCE DA FATT
    PUSH DX ;PRESERVO DX
    DEC DX ;DECREMENTO DX
    CALL FATT ;RICORSIONE
    CHECK: POP DX ;RIPRISTINO DX
    MUL DL
    CMP AX, [NUM1] ;CONFRONTO IL RISULTATO CON IL LIMITE INFERIORE
    JB CHECK ;SE è INFERIORE CONTINUO A CALCOLARE IL FATTORIALE
    CHECK2: CMP AX,[NUM2] ;CONFRONTO IL FATTORIALE CON IL LIMITE SUPERIORE [NUM2]
    JA FINE ;SE è SUPERIORE ESCO DAL FATTORIALE
    MOV DX,OFFSET RISULTATO ;Sceglie la stringa (DS:DX)
    CALL STAMPA_STRINGA
    MOV AX, DX
    CALL STAMPA_32BIT
    CALL FATT
    FINE: RET
    FATTORIALE ENDP
  • Re: Calcolo fattoriale

    Solo adesso penso di capire cosa intendesse l'esercizio (espresso in un italiano alquanto precario ...)

    A prescindere dai controlli fatti sui valori, questo spezzone di codice funziona correttamente
    
    XOR AX,AX 
    XOR DX,DX
    MOV AX, 1 
    MOV DX, 8 
    FATT: CMP DX, 1 
    JZ FINE 
    PUSH DX 
    DEC DX 
    CALL FATT 
    POP DX
    MUL DX
    FINE:    RET
    
    (Nota il MUL DX )
Devi accedere o registrarti per scrivere nel forum
13 risposte