![]() |
#2
zimmerk2012-05-26 03:57
|
代码可以编译通过及正常运行,逻辑上应该没有错。想请大家帮忙看看编码是否标准,风格怎么样,有没有能改进的地方。
新人第一次发贴,还是在这个时候……大家给点面子……

assume cs:code_sg, ds:data_sg, ss:stack_sg
data_sg segment
; BL R G B I R G B
db 00000010b, 00100100b, 01110001b ; 显示配置
db 'Welcome to masm!', 0 ; 要显示的字符串
data_sg ends
stack_sg segment
dw 0, 0, 0, 0, 0, 0, 0, 0
dw 0, 0, 0, 0, 0, 0, 0, 0
stack_sg ends
code_sg segment
__start:
; 初始化段寄存器
mov ax, data_sg
mov ds, ax
mov ax, stack_sg
mov ss, ax
mov sp, 32
sub sp, 2 ; 栈中申请一个字单元作临时变量,存放字串长度 ss:[30]
sub sp, 2 ; 栈中申请一个字单元作临时变量,存放字符串列输出位置 ss:[28]
sub sp, 2 ; 栈中申请一个字单元作临时变量,存放字符串行输出位置 ss:[26]
; 计算字串长度
mov bx, 3
push bx ; 保存bx原来的值
xor cx, cx
__travelsal_string_char: ; 遍历字符串中的每个字符
mov cl, [bx]
jcxz __travelsal_string_char_end ; 遇到中止符结束
inc bx
jmp __travelsal_string_char
__travelsal_string_char_end:
mov ss:[30], bx ; 将结果保存
pop bx ; 恢复bx
sub ss:[30], bx ; 得到字串真正长度
; 计算输出位置
push bx
; 计算列位置
mov ax, 80
mov bx, ss:[30]
sub ax, bx
mov bx, 2
div bl
xor ah, ah
mov ss:[28], ax ; 保存结果
; 计算行位置
mov al, 3
mov bl, 11
mul bl
mov ss:[26], ax
pop bx
mov ax, 0b800h
add ax, ss:[26]
mov es, ax
; 遍历显示配置
mov cx, 3
__get_display_settings:
mov bx, cx
mov ah, [bx - 1] ; 奇(高)地址存放字符的颜色属性
; 遍历字串每个字符并输出
push cx ; 保存cx
mov cx, ss:[30] ; 取得字串长度
mov si, ss:[28] ; 取得输出位置
mov bx, 3
__display_string_next_char:
mov al, [bx]
mov es:[si], ax
add si, 2
inc bx
loop __display_string_next_char
__get_string_char_end: ; 字串中止
push ax
mov ax, es
add ax, 0ah
mov es, ax
pop ax
pop cx ; 恢复cx
loop __get_display_settings
; 延时
mov cx, 0ffffh
__sleep1:
push cx
mov cx, 0ah
__sleep2:
nop
nop
nop
nop
loop __sleep2
pop cx
loop __sleep1
mov ax, 4c00h
int 21h
code_sg ends
end __start
data_sg segment
; BL R G B I R G B
db 00000010b, 00100100b, 01110001b ; 显示配置
db 'Welcome to masm!', 0 ; 要显示的字符串
data_sg ends
stack_sg segment
dw 0, 0, 0, 0, 0, 0, 0, 0
dw 0, 0, 0, 0, 0, 0, 0, 0
stack_sg ends
code_sg segment
__start:
; 初始化段寄存器
mov ax, data_sg
mov ds, ax
mov ax, stack_sg
mov ss, ax
mov sp, 32
sub sp, 2 ; 栈中申请一个字单元作临时变量,存放字串长度 ss:[30]
sub sp, 2 ; 栈中申请一个字单元作临时变量,存放字符串列输出位置 ss:[28]
sub sp, 2 ; 栈中申请一个字单元作临时变量,存放字符串行输出位置 ss:[26]
; 计算字串长度
mov bx, 3
push bx ; 保存bx原来的值
xor cx, cx
__travelsal_string_char: ; 遍历字符串中的每个字符
mov cl, [bx]
jcxz __travelsal_string_char_end ; 遇到中止符结束
inc bx
jmp __travelsal_string_char
__travelsal_string_char_end:
mov ss:[30], bx ; 将结果保存
pop bx ; 恢复bx
sub ss:[30], bx ; 得到字串真正长度
; 计算输出位置
push bx
; 计算列位置
mov ax, 80
mov bx, ss:[30]
sub ax, bx
mov bx, 2
div bl
xor ah, ah
mov ss:[28], ax ; 保存结果
; 计算行位置
mov al, 3
mov bl, 11
mul bl
mov ss:[26], ax
pop bx
mov ax, 0b800h
add ax, ss:[26]
mov es, ax
; 遍历显示配置
mov cx, 3
__get_display_settings:
mov bx, cx
mov ah, [bx - 1] ; 奇(高)地址存放字符的颜色属性
; 遍历字串每个字符并输出
push cx ; 保存cx
mov cx, ss:[30] ; 取得字串长度
mov si, ss:[28] ; 取得输出位置
mov bx, 3
__display_string_next_char:
mov al, [bx]
mov es:[si], ax
add si, 2
inc bx
loop __display_string_next_char
__get_string_char_end: ; 字串中止
push ax
mov ax, es
add ax, 0ah
mov es, ax
pop ax
pop cx ; 恢复cx
loop __get_display_settings
; 延时
mov cx, 0ffffh
__sleep1:
push cx
mov cx, 0ah
__sleep2:
nop
nop
nop
nop
loop __sleep2
pop cx
loop __sleep1
mov ax, 4c00h
int 21h
code_sg ends
end __start