2017-04-22 4 views
-3

Ich versuche, einen faktoriellen Code in der Montage mit JMP und jz und ich bin neu Montage sollteWas mit meinem Assembler-Code falsch

Eingang EAX zu schreiben und die Ausgabe ist ebx

mov eax,3h ;factorial of 3 
    ;initialize registers 
    mov ebx,0h 
    mov ecx,eax 
    dec ecx 
    mov edx,0h 

loopstart: 
    mul ecx ;edx:eax = eax * ecx 
    add ebx,eax ;ebx = ebx + eax 
    dec ecx ;ecx = ecx - 1 
    jz exitloop ;jump to exitloop if the last math is zero (in this ex:dec) 
    jmp loopstart ;jump to loopstart 
exitloop: 
    ;mov eax,ebx 
    call ebxprint; should be 6 
Finden faktoriellen von EAX

input: 2

output: 2

Eingang: 3

Ausgang: 12

Eingang: 4

Ausgang: 60

Also, was ist das Problem hier?

+2

tatsächliche Ausgabe Beschreiben, kommentieren Sie den Code und lernen, einen Debugger zu verwenden. Tipp: Es gibt keine Zugabe in Fakultät, warum haben Sie eine? – Jester

+0

Warum fügen Sie die teilweise berechneten Faktoren immer wieder zu "ebx" hinzu? Ich denke, du willst die 'add ebx, eax'-Anweisung entfernen und 'mov ebx, eax' hinzufügen, wo du momentan die auskommentierte hast'; mov eax, ebx' nach 'exitloop'. –

+0

@PaulHankin Thanks – BaselSayeh

Antwort

0

Dank @PaulHankin

I add ebx, eax und uncomment mov eax,ebx

Hier entfernen muss, ist der Code nach der Bearbeitung:

mov eax,3h ;factorial of 3 
    ;initialize registers 
    mov ebx,0h 
    mov ecx,eax 
    dec ecx 
    mov edx,0h 

loopstart: 
    mul ecx ;edx:eax = eax * ecx 
    dec ecx ;ecx = ecx - 1 
    jz exitloop ;jump to exitloop if the last math is zero (in this ex:dec) 
    jmp loopstart ;jump to loopstart 
exitloop: 
    mov eax,ebx 
    call ebxprint; should be 6