0

我正在编写一个程序来显示当前时间和日期 10 秒。我被指示我只能为此使用 smallwin.inc。我编写了一个使用 Irvine32.inc 的程序,但是当我切换到 ONLY smallwin 时,我需要编写一些输出数字的程序。当我添加这些时,一个非常奇怪的行为开始发生!我使用调用“dec repTime”,它会减少 2 而不是 1!我已经尝试过“sub repTime, 1”,它仍然是一样的!我什至将 repTime 移到 eax,然后减去 1,然后将 eax 移回 repTime,它仍然减去 2!

我编写的程序应该运行 10 秒。由于减法 2 的问题,它运行了 4 秒!怎么了?!?

这是我的代码:

; this code demonstrates the usage of Windows API functions to display
; current date and time. It provides a running digital clock that updates
; itself for 10 seconds. The output is displayed starting with cursor position
; (5,5) and uses yellow foreground and blue background.

INCLUDE smallwin.inc

.data
buffer DB 256 DUP(?)
prompt    BYTE "Today is: "                  ; strings used to format the output
slash     BYTE '/'
colon     BYTE ':'
space     BYTE "     Time: "
newLine   WORD 0D0Ah                         
repTime   DWORD 10                           ; display time for 10 seconds   
cursPos   COORD <5,5>                        ; cursor coordinates

.data?
outHandle DWORD ?                            ; storage for console output handle
sysTimeNow   SYSTEMTIME <>                      ; storage for system time structure
sysTimeFuture SYSTEMTIME <>

.code

WriteChar PROC

pushfd
pushad
mov  buffer,al
INVOKE WriteConsole, outHandle, OFFSET buffer, 1, 0, 0

popad
popfd
ret
WriteChar ENDP

outInt PROC USES eax ebx ecx edx,
number: SDWORD               ; method parameter
mov ebx, 10                ; divisor for the radix system
xor ecx, ecx               ; digits counter
cmp number, 0              ; is number >= 0?
jge go                      ; yes - proceed
neg number                 ; negate the number
mov al, '-'
INVOKE WriteChar

go:
mov eax, number   
puDigit:
xor edx, edx
div ebx                    ; get one digit
push edx                   ; save it for further processing
inc ecx                    ; update the digits counter
cmp eax, 0                 ; end of processing
jne puDigit

printIT:
pop eax                     ; get a digit from stack
or al, 30h                 ; ASCII conversion
INVOKE WriteChar           ; print it
loop printIT

ret
outInt ENDP

format2 PROC 
LOCAL zero:BYTE

mov zero, '0'
cmp ax, 10                              ; number < 10 ?
jge L 
push eax                                ; YES - output preceeding 0
INVOKE WriteConsole, outHandle, ADDR zero, 1, 0, 0
pop eax
L:   INVOKE outInt, eax                          ; output the number itself

ret
format2 ENDP


main PROC
INVOKE GetStdHandle, STD_OUTPUT_HANDLE
mov outHandle, eax                      ; get console handle for output

INVOKE SetConsoleTextAttribute, outHandle, 30 ; setup colors
INVOKE GetLocalTime, ADDR sysTimeFuture

;set Future Time to Dec 25, 2013
mov sysTimeFuture.wDay, 25
mov sysTimeFuture.wMonth, 12
mov sysTimeFuture.wYear, 2013
mov sysTimeFuture.wHour, 0
mov sysTimeFuture.wMinute, 0
mov sysTimeFuture.wSecond, 0


startLabel:
INVOKE SetConsoleCursorPosition, outHandle, cursPos

INVOKE GetLocalTime, ADDR sysTimeNow       ; retrieve current date/time

INVOKE WriteConsole, outHandle, ADDR prompt,
SIZEOF prompt, 0, 0                  ; output prompt

mov eax, 0
mov ax, sysTimeNow.wDay                    ; day of the month
call format2
INVOKE WriteConsole, outHandle, ADDR slash, 1, 0, 0 ; output '/'

mov ax, sysTimeNow.wMonth                  ; month number
call format2
INVOKE WriteConsole, outHandle, ADDR slash, 1, 0, 0 ; output '/'

mov ax, sysTimeNow.wYear                   ; print out the year
INVOKE outInt, ax
INVOKE WriteConsole, outHandle, ADDR space, SIZEOF space, 0, 0 

mov ax, sysTimeNow.wHour                   ; output hours
call format2
INVOKE WriteConsole, outHandle, ADDR colon, 1, 0, 0 ; output ':'

mov ax, sysTimeNow.wMinute                 ; output minutes
call format2
INVOKE WriteConsole, outHandle, ADDR colon, 1, 0, 0 ; output ':'

mov ax, sysTimeNow.wSecond                 ; output seconds
call format2

INVOKE Sleep, 1000                      ; wait for 1 second
dec repTime                             ; update the stop watch counter
jnz startLabel

INVOKE SetConsoleTextAttribute, outHandle, 15 ; reset the colors 
INVOKE WriteConsole, outHandle, ADDR newLine, 2, 0, 0 ; start new line
INVOKE ExitProcess, 0 
main ENDP

END main
4

1 回答 1

0

查看原型outint

outInt PROTO number:SDWORD

预期的参数是一个签名的 DWORD -或者SDWORD它可能是DWORD,没关系,看看你作为参数传递的内容:

mov     ax, sysTimeNow.wYear                   ; print out the year
INVOKE  outInt, ax

您将一个WORD大小的寄存器作为参数传递,并eax在过程中使用。

将以上两行更改为:

movzx   eax, sysTimeNow.wYear                   ; print out the year
INVOKE  outInt, eax

或者:

xor     eax, eax
mov     ax, sysTimeNow.wYear                   ; print out the year
INVOKE  outInt, eax

这将使上半部分“归零”eax并使其工作。或者,您可以做的是使过程期望一个WORD大小的参数,因为这是您要传递的,并且使用ax而不是eax在过程中。

于 2013-08-23T01:01:38.263 回答