使用x86汇编语言编写输入年、月、日,判断当日的星期数,并进行输出的代码。该代码使用Intel语法,需要在DOSBox或类似的环境中运行。
时间: 2024-02-17 15:01:15 浏览: 94
汇编版记事本源代码.txt
4星 · 用户满意度95%
好的,以下是代码:
```assembly
.model small
.stack 100h
.data
; 定义一些常量
year db ?
month db ?
day db ?
dayOfWeek db ?
prompt1 db 'Please enter year (yyyy): $'
prompt2 db 'Please enter month (mm): $'
prompt3 db 'Please enter day (dd): $'
output db 'The day of the week is: $'
sunday db 'Sunday', 13, 10, '$'
monday db 'Monday', 13, 10, '$'
tuesday db 'Tuesday', 13, 10, '$'
wednesday db 'Wednesday', 13, 10, '$'
thursday db 'Thursday', 13, 10, '$'
friday db 'Friday', 13, 10, '$'
saturday db 'Saturday', 13, 10, '$'
.code
main proc
mov ax, @data
mov ds, ax
; 依次获取用户输入的年、月、日
lea dx, prompt1
mov ah, 9
int 21h
mov ah, 1
int 21h
sub al, 48 ; 将字符转换成数字
mov year, al
lea dx, prompt2
mov ah, 9
int 21h
mov ah, 1
int 21h
sub al, 48
mov month, al
lea dx, prompt3
mov ah, 9
int 21h
mov ah, 1
int 21h
sub al, 48
mov day, al
; 根据公式计算当日的星期数
mov ah, 0
mov al, year
mov bl, 4
div bl ; year / 4
add ax, year ; year / 4 + year
add ax, day ; year / 4 + year + day
mov bl, month
sub bl, 1
mov cl, 2
mul cl ; (year / 4 + year + day) * 2
add ax, bl ; (year / 4 + year + day) * 2 + month - 1
cmp bl, 2
jne notFeb
dec ax ; 二月份要减一
notFeb:
mov bl, 12
mul bl ; (year / 4 + year + day) * 2 + month - 1 + 13 * (month + 1) / 5
mov cl, 13
add bl, cl
mov cl, 5
div cl
add ax, bl
add ax, 6 ; (year / 4 + year + day) * 2 + month - 1 + 13 * (month + 1) / 5 + 6
and ax, 7 ; 取余数
mov dayOfWeek, al
; 输出结果
lea dx, output
mov ah, 9
int 21h
cmp dayOfWeek, 0
je sundayLabel
cmp dayOfWeek, 1
je mondayLabel
cmp dayOfWeek, 2
je tuesdayLabel
cmp dayOfWeek, 3
je wednesdayLabel
cmp dayOfWeek, 4
je thursdayLabel
cmp dayOfWeek, 5
je fridayLabel
cmp dayOfWeek, 6
je saturdayLabel
sundayLabel:
lea dx, sunday
jmp printLabel
mondayLabel:
lea dx, monday
jmp printLabel
tuesdayLabel:
lea dx, tuesday
jmp printLabel
wednesdayLabel:
lea dx, wednesday
jmp printLabel
thursdayLabel:
lea dx, thursday
jmp printLabel
fridayLabel:
lea dx, friday
jmp printLabel
saturdayLabel:
lea dx, saturday
printLabel:
mov ah, 9
int 21h
mov ah, 4ch
int 21h
main endp
end main
```
代码中的注释已经说明了每个步骤的含义。请在类 DOSBox 的环境中运行该代码。
阅读全文