user3478498
user3478498

Reputation: 29

factorial on assembly x86 (NASM on Ubuntu)

I'm trying to write a program counting factorial, but this code leads to an infinite loop because inttostring functions. I think that the problem is with the function of the div but I do not see a solution

  section .text
    global _start

_start:
    inc eax     
mov ebx, 2  
mov ecx, 3  
jmp count    

count:  
imul eax, ebx       
cmp ebx, ecx    
je inttostring  
inc ebx     
jmp count

inttostring:    
mov ebx, 10     
mov esi, 3  
div ebx     
add [result+esi], dl    
dec esi     
cmp esi, 0  
je displey  
jmp inttostring         

 displey:
        mov eax, 4
        mov ebx, 1  mov ecx, result
        mov edx, size
        int 80h
        mov eax, 1  int 80h 

    section .data

    result times 4 db 48 
    size equ $ - result

Upvotes: 1

Views: 1859

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

You need to clear EDX before doing the division.

This program will only work if EAX=0 to start. Where is EAX initialized?

An infinite loop comes from re-initializing ESI over and over again!
Move mov esi,3 before the label inttostring.

Upvotes: 1

Related Questions