我的任务是编写一个 NASM 程序,该程序获取下个月第一天的星期几。举个例子:如果今天是 6 月 4 日,那么程序应该这样说:
July 1st is a Thursday.
我正在使用 mktime 函数以及其他一些时间/日期函数。这是我的代码:
extern time
extern localtime
extern exit
extern printf
extern mktime
extern ctime
global main
section .data
sSun: db "Sunday", 0
sMon: db "Monday", 0
sTue: db "Tuesday", 0
sWed: db "Wednesday", 0
sThu: db "Thursday", 0
sFri: db "Friday", 0
sSat: db "Saturday", 0
format_1: db "%d", 10, 0
string: db "%s", 10, 0
section .bss
timestamp: resd 1
tmstruct: resd 1
section .text
main:
pusha
push dword 0 ; fetch the timestamp
call time
add esp, 4
mov [timestamp], eax
push timestamp
call localtime
add esp, 4
;change the localtime struct to indicate first day of next month.
;seconds, minutes, hours, day of month from 1.
mov [eax], dword 0
mov [eax + 4], dword 0
mov [eax + 8], dword 0
mov [eax + 12], dword 1
;get month # from 0, to ecx.
mov ecx, [eax + 16]
cmp ecx, 11
jne notDecember
;its december. Set date to January of next year.
mov [eax + 16], dword 0
mov ecx, [eax + 20]
inc ecx
mov [eax + 20], ecx
jmp convertDate
notDecember:
;its not december, just move up the month by 1.
mov ecx, [eax + 16]
inc ecx
mov [eax + 16], ecx
convertDate:
mov [tmstruct], eax
;make a timestamp
;push tmstruct <-- Wrong
push dword [tmstruct] ; <-- Right
call mktime
add esp, 4
;move timestamp
mov [timestamp], eax
;make a new tm struct
push timestamp
call localtime
add esp, 4
;now we have the correct date, check the day of the week
mov ecx, [eax + 24]
push ecx ;<--- preserve this value or c function calls will trash it!
;do a ctime call
;push dword eax <-- Wrong
push timestamp ; <-- Right
call ctime
add esp, 4
push dword eax
push string
call printf
add esp, 8
pop ecx ;<--- pop preserved value!
push dword ecx
call dayOfWeek
popa
call exit
dayOfWeek:
cmp [esp + 4], dword 0
je pSun
cmp [esp + 4], dword 1
je pMon
cmp [esp + 4], dword 2
je pTue
cmp [esp + 4], dword 3
je pWed
cmp [esp + 4], dword 4
je pThu
cmp [esp + 4], dword 5
je pFri
cmp [esp + 4], dword 6
je pSat
push dword esp
push format_1
call printf
add esp, 8
push format_1
jmp endDow
pSun:
push sSun
jmp endDow
pMon:
push sMon
jmp endDow
pTue:
push sTue
jmp endDow
pWed:
push sWed
jmp endDow
pThu:
push sThu
jmp endDow
pFri:
push sFri
jmp endDow
pSat:
push sSat
jmp endDow
endDow:
push string
call printf
add esp, 8
ret 4
基本上,我被告知“mktime 函数忽略结构成员 tm_wday 和 tm_yday 的指定内容......”(来自 localtime tm 结构)“......并从分解的时间结构中的其他信息重新计算它们。”
看到这种情况,我的计划是为当前时间创建一个 tm 结构,并将其所有元素简单地修改为指向下个月第一天的第一秒,然后使用 mktime 。但是,您会看到程序将“劫持”结构输出为“1969 年 12 月 31 日星期三 18:00:59”,但随后我什至从 THAT 打印出星期几,我得到星期天。我做了什么让这里变得如此错误?