欧文WriteInt不打印

时间:2013-11-30 03:30:29

标签: assembly x86 masm irvine32

由于某些原因WriteInt PROC在我调用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
Irvines链接库中的

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

编辑:

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

这是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

1 个答案:

答案 0 :(得分:1)

这是错误的:

add ecx, 2
pop eax

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

已写入数字的计数作为第二个参数传递到堆栈(ebp + 12)并保留在EAX的{​​{1}}中。在fibo中存储两个数字后,增加它是显而易见的。因此,请将上面的代码段更改为

fiboNum

请注意,pop eax add eax, 2 必须包含至少3个元素。