本实验要将十进制数字,以ASCII码的形式显示到屏幕上
assume cs:code
data segment
db 10 dup (0) //存放要写到屏幕上的数字的内存段
data ends
code segment
start:
mov ax , 17897 //将十进制数字17897显示到屏幕上
mov bx , data
mov ds , bx
mov si , 0
call dtoc //调用函数dtoc ,将数字写入内存,注意是写入数字对于的字符的值,比如数字1,则写入31H,对于字符'1',最终构成字符串"17897",以0结尾
mov dh , 8
mov dl , 3
mov cl , 2
call show_str //调用show_str函数,将已经写入内存中的字符串"17897”显示到屏幕的第8行,第3列,绿色字体
mov ax , 4c00h
int 21h
dtoc: //该函数能完成32位数字转化为字符串
push si
push bx
mov bx , 0
push bx //先push一个0到栈中,作为出栈时候的标志位
cal:
mov cx , 0AH //将除数10放入cx,把数字转为十进制字符串的算法就是不断除10,将余数入栈,知道商为0
call divdw //调用divdw函数,不用div指令是因为要确保能显示32位的数字都能成功转化
add cx , 30H //这里加上30H,就是将数字1转化为ASCII字符 '1'
push cx //将余数入栈
jmp short testax //判断此时商是否为0,注意现在商的高16位在dx,低16位在ax,只有ax和dx都为0,商才真正为0,所以下面有两个检测,分别检测ax和dx
testax:
mov cx , ax
jcxz testdx
jmp short cal
testdx:
mov cx , dx
jcxz ook
jmp short cal
ook:
pop cx //开是出栈,将字符写入data段内存中
jcxz over
mov [si] , cl
inc si
jmp short ook
over:
mov byte ptr [si] , 0 //给字符串的结尾增加一个0作为字符串结束的标志位
pop bx
pop si
ret
show_str: //调用函数显示字符串
push cx
push bx
push ax
push es
mov ax, 0B800h
mov bx, 0
mov es, ax
sub ax, ax
mov al, 0A0h
mul dh
add bx, ax
sub ax, ax
mov al, 2
mul dl
add bx, ax
mov al, cl
s:
mov ch, 0
mov cl, [si]
jcxz ok
mov es:[bx] , cl
inc bx
mov es:[bx] , al
inc bx
inc si
jmp short s
ok:
pop es
pop ax
pop bx
pop cx
ret
divdw:
push bx
push si
mov bx , ax
mov ax , dx
sub dx , dx
div cx
mov si , ax
mov ax , bx
div cx
mov cx , dx
mov dx , si
pop si
pop bx
ret
code ends
end start