1.在屏幕的8行3列,用绿色显示data段中的字符串
assume cs:code
data segment
db 'Welcome to masm!',0
data ends
code segment
start: mov dh,8
mov dl,3
mov cl,2
mov ax,data
mov ds,ax
mov si,0
call show_str
mov ax,4c00h
int 21h
show_str:
push ax
push cx
mov ax,0b800h ;设置在指定dh行dl列显示
mov es,ax
mov al,160
mul dh
push ax
mov al,2
mul dl
pop di
add di,ax
print: mov cx,ds:[si] ;
jcxz print_end
mov es:[di],cx
inc si
inc di
mov byte ptr es:[di],36 ;设置颜色
inc di
jmp short print
print_end: pop cx
pop ax
ret
code ends
end start
2.解决除法溢出的问题
进行不会产生溢出的除法运算,被除数dword型,除数为word型,结果dword型。
assume cs:code
code segment
start:
mov ax,4240H
mov dx,000FH
mov cx,0AH
call divdw
mov ax,4c00h
int 21h
divdw: push bx ;保存bx内容
push ax ;保存低16位
mov ax,dx
mov dx,0
div cx
pop bx ;取出低16位
push ax ;保存高16位结果
mov ax,bx
div cx
mov cx,dx ;cx保存余数
pop dx ;取出高16位结果
divdw_end:
pop bx
ret
code ends
end start
3.编程,将data段中的数据以十进制的形式显示出来。
assume cs:code
data segment
dw 123,12666,1,8,3,38
data ends
stack segment
db 10 dup(0)
stack ends
code segment
start:
mov ax,data
mov es,ax
mov bp,0
mov cx,6
mov dh,8
mov dl,3
s:
push cx
mov ax,es:[bp]
mov bx,stack
mov ds,bx
mov si,0
call dtoc
call show_str
add bp,2
add dh,2
pop cx
loop s
mov ax,4c00h
int 21h
dtoc: push ax
push bx
push cx
push dx
push si
mov bx,0 ;用于判断入栈元素中所需元素的结束标记
push bx
dtoc_start: mov dx,0
mov cx,10
div cx
add dx,30H
push dx
mov cx,ax
jcxz dtoc_end
jmp short dtoc_start
dtoc_end: pop cx ;将压入栈中的数据保存在data段中
jcxz dtoc_over
mov ds:[si],cx
inc si
jmp short dtoc_end
dtoc_over: mov byte ptr ds:[si+1],0
pop si
pop dx
pop cx
pop bx
pop ax
ret
show_str: push ax
push cx
push es
push si
push di
mov ax,0b800h ;设置在指定dh行dl列显示
mov es,ax
mov al,160
mul dh
push ax
mov al,2
mul dl
pop di
add di,ax
print: mov cx,ds:[si] ;
jcxz print_end
mov es:[di],cx
inc si
inc di
mov byte ptr es:[di],36 ;设置颜色
inc di
jmp short print
print_end: pop di
pop si
pop es
pop cx
pop ax
ret
code ends
end start