程序集x86上的阶乘(Ubuntu上的NASM)

时间:2015-01-18 15:05:28

标签: assembly x86 nasm

我正在尝试编写一个计算阶乘的程序,但是这个代码会导致无限循环,因为inttostring函数。 我认为问题在于div的功能,但我没有看到解决方案

  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

1 个答案:

答案 0 :(得分:1)

在进行分割之前,您需要清除EDX。

此程序仅在EAX = 0时启动。 EAX在哪里初始化?

无限循环来自于一遍又一遍地重新初始化ESI! 在标签mov esi,3之前移动inttostring