用汇编语言写一个四位秒表
时间: 2024-03-25 17:35:33 浏览: 121
用汇编语言编写数字秒表
4星 · 用户满意度95%
好的,以下是用汇编语言写的四位秒表程序:
```
; 4-Digit Stopwatch Program
.MODEL SMALL
.STACK 100H
.DATA
COUNT DB 0 ; Counter for seconds
DISP DB 0,0,0,0 ; Display buffer for 4 digits
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize DS register
MOV DS, AX
MOV AH, 0 ; Clear screen
INT 10H
MOV AH, 2 ; Set cursor position to row 1, column 1
MOV BH, 0 ; Display page 0
MOV DH, 0
MOV DL, 0
INT 10H
MOV AH, 1 ; Enable keyboard input
INT 16H
START: ; Main loop
CALL COUNTER ; Increment counter
CALL DISPLAY ; Update display
JMP START ; Repeat indefinitely
COUNTER PROC
MOV AL, COUNT ; Load count into AL register
INC AL ; Increment count
CMP AL, 60 ; Check if count is 60
JB SKIP ; Jump if below 60
XOR AL, AL ; Reset count to 0
SKIP:
MOV COUNT, AL ; Save count back to memory
RET
COUNTER ENDP
DISPLAY PROC
MOV BL, COUNT ; Load count into BL register
MOV CL, 10 ; Set divisor to 10
MOV SI, OFFSET DISP ; Set pointer to display buffer
; Convert count to BCD and store in display buffer
MOV AH, 0
DIV CL
MOV [SI+3], AH
MOV AH, 0
DIV CL
MOV [SI+2], AH
MOV AH, 0
DIV CL
MOV [SI+1], AH
MOV AH, 0
DIV CL
MOV [SI], AH
; Output display buffer to screen
MOV AH, 2 ; Set cursor position to row 2, column 1
MOV BH, 0 ; Display page 0
MOV DH, 1
MOV DL, 0
INT 10H
MOV CX, 4 ; Loop 4 times for each digit
MOV SI, OFFSET DISP ; Set pointer to display buffer
DISP_LOOP:
MOV DL, [SI] ; Load digit into DL register
ADD DL, 30H ; Convert digit to ASCII
MOV AH, 2 ; Output digit to screen
INT 10H
INC SI ; Move to next digit in display buffer
LOOP DISP_LOOP ; Repeat for all 4 digits
RET
DISPLAY ENDP
MAIN ENDP
END MAIN
```
这个程序使用了两个子程序:`COUNTER` 和 `DISPLAY`。`COUNTER` 用于递增计数器 `COUNT`,每秒钟递增一次,最大值为 59。`DISPLAY` 用于将 `COUNT` 的值转换成 BCD 码,并显示在屏幕上。程序会一直运行,直到用户按下任意键退出程序。
阅读全文