用汇编语言写 体育比赛计时器并在dosbox 运行,有记忆的功能,程序运行开始计时,屏幕显示00:00:00,有记忆的功能,程序运行开始计时,屏幕显示00:00:00
时间: 2023-08-22 10:06:42 浏览: 95
基于汇编的计时器源程序
好的,以下是汇编语言实现的体育比赛计时器示例代码:
```assembly
.model small
.stack 100h
.data
hours db 0
minutes db 0
seconds db 0
last_hours db 0
last_minutes db 0
last_seconds db 0
memory_hours db 0
memory_minutes db 0
memory_seconds db 0
running db 1
.code
main proc
mov ax, @data
mov ds, ax
; Set up the timer interrupt
mov ax, 3512h
mov dx, offset newtimer
mov ds:[int08h+2], dx
mov dx, cs
mov ds:[int08h], dx
; Display initial message
mov ah, 2
mov dl, 'P'
int 21h
mov dl, 'r'
int 21h
mov dl, 'e'
int 21h
mov dl, 's'
int 21h
mov dl, 's'
int 21h
mov dl, ' '
int 21h
mov dl, 'a'
int 21h
mov dl, 'n'
int 21h
mov dl, 'y'
int 21h
mov dl, ' '
int 21h
mov dl, 'k'
int 21h
mov dl, 'e'
int 21h
mov dl, 'y'
int 21h
mov dl, ' '
int 21h
mov dl, 't'
int 21h
mov dl, 'o'
int 21h
mov dl, ' '
int 21h
mov dl, 's'
int 21h
mov dl, 't'
int 21h
mov dl, 'a'
int 21h
mov dl, 'r'
int 21h
mov dl, 't'
int 21h
mov dl, ' '
int 21h
mov dl, 't'
int 21h
mov dl, 'h'
int 21h
mov dl, 'e'
int 21h
mov dl, ' '
int 21h
mov dl, 't'
int 21h
mov dl, 'i'
int 21h
mov dl, 'm'
int 21h
mov dl, 'e'
int 21h
mov dl, 'r'
int 21h
; Wait for the user to start the timer
mov ah, 0
int 16h
; Main loop
main_loop:
; Check for user input
mov ah, 1
int 16h
jnz stop_timer
; Wait for a short while
mov cx, 1000
mov dx, 8000h
mov ah, 86h
int 15h
; Increment the timer
inc seconds
cmp seconds, 60
jne update_display
mov seconds, 0
inc minutes
cmp minutes, 60
jne update_display
mov minutes, 0
inc hours
update_display:
; Update the display if the time has changed
cmp last_seconds, seconds
je main_loop
mov ah, 2
mov dl, hours
add dl, 30h
int 21h
mov dl, ':'
int 21h
mov dl, minutes
add dl, 30h
int 21h
mov dl, ':'
int 21h
mov dl, seconds
add dl, 30h
int 21h
mov last_hours, hours
mov last_minutes, minutes
mov last_seconds, seconds
jmp main_loop
stop_timer:
; Save the current time to memory
mov memory_hours, hours
mov memory_minutes, minutes
mov memory_seconds, seconds
; Restore the old timer interrupt
mov ax, 3512h
mov dx, ds:[int08h+2]
mov ds:[int08h+2], dx
mov dx, ds:[int08h]
mov ds:[int08h], dx
; Display the saved time
mov ah, 2
mov dl, 13
int 21h
mov dl, 10
int 21h
mov dl, memory_hours
add dl, 30h
int 21h
mov dl, ':'
int 21h
mov dl, memory_minutes
add dl, 30h
int 21h
mov dl, ':'
int 21h
mov dl, memory_seconds
add dl, 30h
int 21h
; Wait for user input
mov ah, 0
int 16h
; Exit the program
mov ah, 4ch
int 21h
newtimer proc
push ax
push bx
push cx
push dx
; Increment the timer
inc seconds
cmp seconds, 60
jne done
mov seconds, 0
inc minutes
cmp minutes, 60
jne done
mov minutes, 0
inc hours
done:
; Call the old timer interrupt
mov ax, int08h
mov bx, word ptr [ax+2]
mov cx, word ptr [ax]
push ds
push bx
push cx
mov ds, word ptr [ax+4]
call dword ptr [bx]
pop cx
pop bx
pop ds
pop dx
pop cx
pop bx
pop ax
iret
newtimer endp
int08h proc
push ax
push bx
push cx
push dx
call dword ptr ds:[int08h+2]
pop dx
pop cx
pop bx
pop ax
iret
int08h endp
end main
```
这段代码与前面的 C++ 代码相似,使用了一个中断来实现计时器。在主循环中,程序检查用户是否按下任何键来停止计时器。每次中断时,程序将更新时间并检查是否需要更新屏幕上的显示。
当用户停止计时器时,程序将保存当前时间并显示在屏幕上。程序将恢复旧的计时器中断,并等待用户按下任何键后退出程序。
请注意,这只是一个简单的示例程序,您可能需要根据实际需要进行更改和优化。
阅读全文