2

出于某种原因,WriteInt PROC在我调用 my 之后它不起作用fibo PROC,但如果我注释掉它可以打印数组fibo PROC。整个程序运行,但从不打印值。以下是我正在使用的链接库:http: //kipirvine.com/asm/examples/IrvineExamplesVS2012.zip

我想知道为什么它不打印到控制台?这是我写的程序:

INCLUDE Irvine32.inc

printArray PROTO pArray:PTR DWORD, count:DWORD

.data
fiboNum DWORD 1,2,3,4,5,6,7,8,9,10

.code
main PROC
push LENGTHOF fiboNum
push 0
push OFFSET fiboNum
call fibo
Invoke printArray, ADDR fiboNum, LENGTHOF fiboNum

exit
main ENDP

fibo PROC
push ebp
mov ebp, esp
pushad

mov eax, [ebp + 12]
mov ecx, [ebp + 16]
cmp ecx, eax
je leaveNOW

cmp eax, 0 ; Does array need to be setup?
jne calc

push eax
mov eax, [ebp + 8] ; Setup array
mov DWORD PTR [eax], 1
mov DWORD PTR [eax + 4], 1
add ecx, 2
pop eax

calc:
mov ebx, [ebp + 8]
mov edx, [ebx]
add edx, [ebx + 4]
mov [ebx + 8], edx
lea ebx, [ebx + 4]

inc eax
push ecx
push eax
push ebx

call fibo

leaveNow:
popad
pop ebp
ret 12
fibo ENDP

printArray PROC USES ecx edx eax,
pArray:PTR DWORD,
count:DWORD

mov ecx, count
mov edx, pArray

L1:
mov eax, [edx]
call WriteInt
call CrLf
add edx, 4

loop L1

ret
printArray ENDP

END main

WriteInt PROC在欧文链接库中:

;-----------------------------------------------------
WriteInt PROC
;
; Writes a 32-bit signed binary integer to the console window
; in ASCII decimal.
; Receives: EAX = the integer
; Returns:  nothing
; Comments: Displays a leading sign, no leading zeros.
; Last update: 7/11/01
;-----------------------------------------------------
WI_Bufsize = 12
true  =   1
false =   0
.data
buffer_B  BYTE  WI_Bufsize DUP(0),0  ; buffer to hold digits
neg_flag  BYTE  ?

.code
pushad
CheckInit

mov   neg_flag,false    ; assume neg_flag is false
or    eax,eax             ; is AX positive?
jns   WIS1              ; yes: jump to B1
neg   eax                ; no: make it positive
mov   neg_flag,true     ; set neg_flag to true

WIS1:
mov   ecx,0              ; digit count = 0
mov   edi,OFFSET buffer_B
add   edi,(WI_Bufsize-1)
mov   ebx,10             ; will divide by 10

WIS2:
mov   edx,0              ; set dividend to 0
div   ebx                ; divide AX by 10
or    dl,30h            ; convert remainder to ASCII
dec   edi                ; reverse through the buffer
mov   [edi],dl           ; store ASCII digit
inc   ecx                ; increment digit count
or    eax,eax             ; quotient > 0?
jnz   WIS2              ; yes: divide again

; Insert the sign.

dec   edi   ; back up in the buffer
inc   ecx                   ; increment counter
mov   BYTE PTR [edi],'+'    ; insert plus sign
cmp   neg_flag,false        ; was the number positive?
jz    WIS3                  ; yes
mov   BYTE PTR [edi],'-'    ; no: insert negative sign

WIS3:   ; Display the number
mov  edx,edi
call WriteString

popad
ret
WriteInt ENDP

编辑:

好的,所以我有点修复它,但我不知道为什么(特别是)。显然,当我从程序的一个过程中调用 WriteInt Proc 时,CheckInit 宏没有初始化控制台句柄。因此,我只是将 WriteInt 称为程序中的第一条指令,它正确设置了控制台句柄。不知道为什么它在我的过程中没有初始化控制台句柄。有人对它为什么会这样做有任何想法吗?

这是 CheckInIt 宏:

;-------------------------------------------------------------
CheckInit MACRO
;
; Helper macro
; Check to see if the console handles have been initialized
; If not, initialize them now.
;-------------------------------------------------------------
LOCAL exit
cmp InitFlag,0
jne exit
call Initialize
exit:
ENDM
4

1 回答 1

1

这是错误:

add ecx, 2
pop eax

ECXfiboNum是在堆栈上传递的定义长度( EBP+16) 和 的中断条件fibo。如果你增加它,你fibo就有机会写在后面fiboNum并覆盖其他数据——在这种情况下是 Irvine 的InitFlag. 因此CheckInit认为 STDOUT 句柄已经保存并继续而不保存它。以下WriteConsole得到了错误的句柄,不写入并用 EAX=0 发出错误信号。GetLastError给出错误代码 6 (ERROR_INVALID_HANDLE)。

已写入数字的计数作为堆栈 () 上的第二个参数传递ebp + 12并保留EAXfibo. 很明显,在将两个数字存储在fiboNum. 所以将上面的代码段更改为

pop eax
add eax, 2

考虑一下,它fiboNum必须至少包含 3 个元素。

于 2015-12-20T10:25:01.797 回答