如何显示屏幕,和睡眠与DOS x86汇编一秒钟多少? [英] How to display a number on the screen and and sleep for one second with DOS x86 assembly?
问题描述
我使用NASM 16位。我试图做一个简单的组装code,打印的数字从0到每个数字之间255间隔1秒。这是我到目前为止有:
I'm using NASM 16 BITS. I'm trying to do a simple assembly code that prints the numbers from 0 to 255 with 1 second interval between each number. This is what I have so far:
[bits 16]
mov ax,cs
mov ds,ax
mov cx,255
mov ax,0
myloop:
;print in screen ax value
;wait 1 second
inc ax
loop myloop
我不知道如何打印斧头的价值在屏幕,以及如何将等待1秒(将它们放置在code评论)
I'm not sure how to print the value of ax in the screen, and how to wait 1 second(placed them in a comment in the code).
推荐答案
有是在段0一个4字节计数器偏移46Ch(或者在SEG 40H,取舍6CH)维护和由PC的BIOS更新。它增加每秒18.2次。在这个柜台的最低字节或字计数18的变化可能是大约等待了最简单的方法第二:
There's a 4-byte counter at segment 0 offset 46Ch (or alternatively at seg 40h, offs 6Ch) maintained and updated by the PC BIOS. It's incremented 18.2 times per second. Counting 18 changes in the lowest byte or word of this counter is probably the simplest way of waiting out approximately a second:
mov ax, 0
mov ds, ax
mov cx, 18
mov bx, [46Ch]
WaitForAnotherChange:
NoChange:
mov ax, [46Ch]
cmp ax, bx
je NoChange
mov bx, ax
loop WaitForAnotherChange
要打印您需要二进制数转换成十进制数,取单个数字,并打印出来十进制数。您除以10的数量和收集剩余物。例如:
To print decimal numbers you need to convert binary numbers into decimal, get individual digits and print them. You divide the number by 10 and collect remainders. e.g.:
123:结果
一十分之一百二十三:商12,其余3结果
12月10日:1的商,余数2结果
1/10:0商,余数1
123:
123 / 10: quotient 12, remainder 3
12 / 10: quotient 1, remainder 2
1 / 10: quotient 0, remainder 1
通过10多次除以你得到的个别数字在相反的顺序余数:3,2,1。然后你使用DOS INT 21H功能2(负载2成 AH
打印出来,装入字符的ASCII code到 DL
,执行 INT 21H
)。
By repeatedly dividing by 10 you get the individual digits in the remainders in the reverse order: 3,2,1. Then you print them using DOS int 21h function 2 (load 2 into AH
, load the character's ASCII code into DL
, execute int 21h
).
这是另一种变型,很适合做你的问题,将是使用 DAA
指令直接在十进制增加数量而不进行任何转换。
An alternative variant, quite suited to your problem, would be to use the DAA
instruction to increment the number directly in decimal without any conversion.
下面是这一切是如何可以做到的:
Here's how it all can be done:
; file: counter.asm
; assemble: nasm.exe counter.asm -f bin -o counter.com
bits 16
org 0x100
mov ax, 0 ; initial number
mov cx, 256 ; how many numbers
NextNumber:
%if 1 ; change to 0 to use the DAA-based method
push ax
mov dx, 0
div word [ten]
push dx
mov dx, 0
div word [ten]
push dx
mov dx, 0
div word [ten]
push dx
pop dx
call PrintDigit
pop dx
call PrintDigit
pop dx
call PrintDigit
pop ax
call PrintNewLine
call Wait1s
inc ax
%else
mov dl, ah
call PrintDigit
mov dl, al
shr dl, 4
call PrintDigit
mov dl, al
and dl, 0Fh
call PrintDigit
call PrintNewLine
call Wait1s
add al, 1
daa
adc ah, 0
%endif
loop NextNumber
ret
PrintDigit:
pusha
mov ah, 2
add dl, '0'
int 21h
popa
ret
PrintNewLine:
pusha
mov dx, CRLF
mov ah, 9
int 21h
popa
ret
Wait1s:
pusha
push ds
mov ax, 0
mov ds, ax
mov cx, 18
mov bx, [46Ch]
WaitForAnotherChange:
NoChange:
mov ax, [46Ch]
cmp ax, bx
je NoChange
mov bx, ax
loop WaitForAnotherChange
pop ds
popa
ret
ten dw 10
CRLF db 13,10,"$"
如果你不喜欢的前导零或最后1秒钟的延迟,你可以有条件地跳过他们。
If you don't like the leading zeroes or the last 1-second delay, you can conditionally skip them.
下载英特尔和/或AMD x86 CPU手册描述每个指令的工作原理。阅读。此外,下载拉尔夫·布朗的中断列表
,它描述了每个BIOS和DOS功能。你需要知道他们中的一些I / O操作。也有 HelpPC
和 TechHelp
,它可以方便描述许多BIOS而像 BIOS数据DOS的事情区
,其中上述计数器的生活。
Download Intel and/or AMD x86 CPU manuals that describe how each instruction works. Read them. Also, download the Ralf Brown's Interrupt List
, which describes every BIOS and DOS function. You need to know some of them to do I/O. There are also HelpPC
and TechHelp
that conveniently describe many BIOS and DOS things like the BIOS Data Area
where the aforementioned counter lives.
这篇关于如何显示屏幕,和睡眠与DOS x86汇编一秒钟多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!