Features
Use the specified color at the specified position, display a string terminated with 0, and
display the content of the data section in green in 8 rows and 3 columns of the screen
code section
1. Initialization
First, the specified string is given in the data segment and the
stack segment is initially empty at the end of 0.
assume cs:code,ds:data,ss:stack
data segment
db 'Welcome to masm!',0
data ends
stack segment stack
db 128 dup(0)
stack ends
Initialize ss, sp represents stack
mov ax,stack
mov ss,ax
mov sp,128
Call the initialization function to initialize ds to represent the data segment, and es to represent the position in the screen
init_reg:
mov bx,0b800h
mov es,bx
mov bx,data
mov ds,bx
ret
The title requires us to display in the eighth row and the third column, we write functions to set the row and column respectively
Line function, each line occupies 160 bytes, so we only need to set the register to 160*the number of lines, and then use the es+ register
get_row:
mov al,160
mul dh
ret
The function of the column, each position occupies two bytes, the first represents the content, and the second represents the color, so here we have to give the number of columns * 2, and then use the es+ register.
get_col:
mov al,2
mul dl
ret
2. Clean the screen
There is no screen cleaning in the title, but here we write a function to clean up the screen for the sake of beauty,
directly set dx to 0700h 07 means empty 00 means no color, and then set the number of cycles to 2000 times, and assign dx to es:[di] each time That's it, remember to push the register used by the pop
clear_screen:
push bx
push cx
push es
push si
mov bx,0b800h
mov es,bx
mov si,0
mov dx,0700h
mov cx,2000
clearscreen:
mov es:[si],dx
add si,2
loop clearscreen
pop si
pop es
pop cx
pop bx
ret
3. Display string
The general idea: assign the content in ds to cl, and then first judge that if cl is 0, jump out of the loop directly, assign the display content to es:[si] in the loop, and assign the color to es:[si+1]
show_string:
push ax
push cx
push es
push ds
push di
push si
mov cx,0
showstring:
mov cl,ds:[si]
jcxz show_string_ret ;字符串以0结尾(循环出口)
mov es:[di],cl
mov es:[di+1],dl
add di,2
inc si
jmp showstring
show_string_ret:
pop si
pop di
pop ds
pop es
pop cx
pop ax
ret
full code
assume cs:code,ds:data,ss:stack
data segment
db 'Welcome to masm!',0
data ends
stack segment stack
db 128 dup(0)
stack ends
code segment
start:
mov ax,stack
mov ss,ax
mov sp,128
call init_reg
mov di,0
mov dh,8
call get_row ;行
add di,ax
mov dl,3
call get_col ;列
add di,ax
call clear_screen ;清理屏幕
mov cl,2
mov dl,cl ;颜色
call show_string ;显示字符串
mov ax,4c00h
int 21h
;================================================
clear_screen:
push bx
push cx
push es
push si
mov bx,0b800h
mov es,bx
mov si,0
mov dx,0700h
mov cx,2000
clearscreen:
mov es:[si],dx
add si,2
loop clearscreen
pop si
pop es
pop cx
pop bx
ret
;================================================
show_string:
push ax
push cx
push es
push ds
push di
push si
mov cx,0
showstring:
mov cl,ds:[si]
jcxz show_string_ret ;字符串以0结尾(循环出口)
mov es:[di],cl
mov es:[di+1],dl
add di,2
inc si
jmp showstring
show_string_ret:
pop si
pop di
pop ds
pop es
pop cx
pop ax
ret
;================================================
get_col:
mov al,2
mul dl
ret
;================================================
get_row:
mov al,160
mul dh
ret
;================================================
init_reg:
mov bx,0b800h
mov es,bx
mov bx,data
mov ds,bx
ret
Effect
Compilation is fun