【发布时间】:2013-05-03 18:50:19
【问题描述】:
您好,这是我第一次在这里发帖,但我正在做一个家庭作业,即使用以下给定规则设计一个汇编函数 syracuse(N, sequence): 1.如果N为1,结束循环。 2.如果N为偶数,则N=N/2,跳转到循环开头 3.如果N为奇数,则N=3N+1,跳转到循环开头
很简单,然后他希望我们显示一些信息并创建报告。但是,我已经盯着这段代码几个小时了,我不知道出了什么问题。一旦我注释掉调用,程序运行良好,不会崩溃,否则会崩溃。我想我只是忽略了一些简单而基本的东西,你们中的任何人都可以提供帮助吗?
代码如下:
.586
.MODEL FLAT
INCLUDE io.h
cr EQU 0dh;carriage return
Lf EQU 0ah;line feed
.STACK 4096
.DATA
array DWORD ?
n DWORD 0
steps DWORD 0
prompt BYTE "Enter N: ", 0
count BYTE cr, Lf, "Total Numbers: "
string BYTE 40 DUP (?)
result BYTE cr, Lf, "N: "
;result2 BYTE cr, Lf, "Steps: "
lbl BYTE 11 DUP (?)
BYTE cr, Lf, 0
.CODE
_start PROC
output prompt ;ask for n
input string, 40
atod string ; convert to int
mov n, eax
dtoa lbl, n ;convert to ascii
output result; print out n
push n
push array
call syracuse
add esp, 8
ret
_start ENDP
syracuse PROC ; syracuse(n, array)
push ebp
mov ebp, esp
push ebx;save ebx
push eax;save eax
push esi
mov eax, [ebp+8] ;first parameter
lea esi, [ebp+12] ;beginning of the array
mov ecx, 0
whileLoop: inc ecx; ecx++
mov [esi+4], eax
cmp eax, 1
je endLoop ;if n = 1, then end
mov ebx, 2
idiv ebx
cmp edx, 0
je evenProc ; if n is even
;if n is odd then 3N + 1
shl eax, 1
add eax, 2
jmp whileLoop
evenProc: ;if n is even then N = N/2
mov ebx, 2
idiv eax
jmp whileLoop
endLoop:
dtoa lbl, ecx
output count;display count
pop esi
pop eax
pop ebx
pop ebp
ret
syracuse ENDP
END
【问题讨论】: