(Display the given value on the screen in decimal form) Wang Shuang's third edition of assembly language experiment 10.2

Features

Given six values, the
general idea is to display them on the screen in decimal: first of all, we need to know that the display on the screen is in the form of characters (ascii code), we first need to convert the given values ​​into characters one by one. a place and then show it on the screen

code section

1. Clean the screen
clear_screen:
		mov dx,0700h	;相当于黑底空字符
		mov cx,2000	;循环次数
		mov bx,0b800h
		mov es,bx
		mov di,0
		
clearscreen:
		mov es:[di],dx
		add di,2
		loop clearscreen

		ret	
2. Initialize the register

Set the string segment, convert each value into a character, and store it in it

init_reg:
		mov bx,data
		mov ds,bx

		mov bx,string
		mov es,bx
		ret
3. Process values ​​(outer loop)

The function of this function is to iterate over each given value

show_number:
		mov bx,0
		mov si,9	;es:[si]
		mov di,160*10+30*2

		mov cx,6

;相当于外循环每次处理一个值
shownumber:			
		call show_word
		add di,160	;代表屏幕中显示的位置每次加一行
		add bx,2	;代表data段中的数据每次换成下一个数据
		loop shownumber

		ret
4. Divide the remainder

Store the given value in the ax register, then first assign ax to cx, then determine whether cx is 0, and store the remainder dx in the string segment each time

short_div:
		mov cx,10	;除数
		div cx
		add dl,30h	;dl是余数
		mov es:[si],dl	;放到我们设定好的字段
		mov cx,ax	;判断是否被除到0
		jcxz short_div_ret
		dec si
		mov dx,0	;重置dx(做除法时dx会被改变)
		jmp short_div

short_div_ret:

		ret
5. Initialize display characters

Point the ds register to the initial string segment, and point the es register to the location of the screen

init_reg_show_string:
		mov bx,string
		mov ds,bx

		mov bx,0b800h
		mov es,bx

		ret
6. Display String

At this time, our si points to the highest bit of the string, which is directly output to the position of the value 0

show_string:
		push cx
		push ds
		push es
		push si
		push di


		mov cx,0
showstring:
		mov cl,ds:[si]

		jcxz show_string_ret
		mov es:[di],cl
		inc si
		add di,2
		jmp showstring

show_string_ret:

		pop di
		pop si
		pop es
		pop ds
		pop cx

		ret

full code

assume cs:code,ds:data,ss:stack

data segment
	dw 1230,12666,1,8,3,38
data ends

string segment		;先将数字放入这个位置
	db 10 dup('0'),0
string ends

stack segment stack
	db 128 dup(0)
stack ends

code segment
	start:
		mov ax,stack
		mov ss,ax
		mov sp,128

		call clear_screen

		call init_reg	;初始化寄存器
	
		call show_number


		mov ax,4c00h
		int 21h
;=============================================
clear_screen:
		mov dx,0700h	;相当于黑底空字符
		mov cx,2000	;循环次数
		mov bx,0b800h
		mov es,bx
		mov di,0
		
clearscreen:
		mov es:[di],dx
		add di,2
		loop clearscreen

		ret		

;=============================================	
show_number:
		mov bx,0
		mov si,9	;es:[si]
		mov di,160*10+30*2

		mov cx,6

;相当于外循环每次处理一个值
shownumber:			
		call show_word
		add di,160	;代表屏幕中显示的位置每次加一行
		add bx,2	;代表data段中的数据每次换成下一个数据
		loop shownumber

		ret
;=============================================
show_word:
		push ax
		push bx
		push cx
		push dx
		push ds
		push es
		push si
		push di		



		mov ax,ds:[bx]	;ax寄存器接收当前值(方便除法)
		mov dx,0

		call short_div	;对当前值做除法取余转换成字符放到指定字段
	
		call init_reg_show_string	;初始化

		call show_string

		pop di
		pop si
		pop es
		pop ds
		pop dx
		pop cx
		pop bx
		pop ax

		ret

;=============================================
show_string:
		push cx
		push ds
		push es
		push si
		push di


		mov cx,0
showstring:
		mov cl,ds:[si]

		jcxz show_string_ret
		mov es:[di],cl
		inc si
		add di,2
		jmp showstring

show_string_ret:

		pop di
		pop si
		pop es
		pop ds
		pop cx

		ret
			


;=============================================
init_reg_show_string:
		mov bx,string
		mov ds,bx

		mov bx,0b800h
		mov es,bx

		ret


;=============================================
short_div:
		mov cx,10	;除数
		div cx
		add dl,30h	;dl是余数
		mov es:[si],dl	;放到我们设定好的字段
		mov cx,ax	;判断是否被除到0
		jcxz short_div_ret
		dec si
		mov dx,0	;重置dx(做除法时dx会被改变)
		jmp short_div

short_div_ret:

		ret
	
;=============================================
init_reg:
		mov bx,data
		mov ds,bx

		mov bx,string
		mov es,bx
		ret




code ends
end start

running result

insert image description here

Guess you like

Origin blog.csdn.net/weixin_46035615/article/details/124158344
Recommended