1 一个双字数据存放在DX和AX寄存器中,编程实现把这个双字右移四位。
程序代码:
DATAS SEGMENT
;此处输入数据段代码
DATAS ENDS
STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
MOV CL, 04 ;设置循环次数为4
SHR AX, CL ;实现将AX逻辑右移
MOV BL, DL ;将DL的低4位暂存在BL中
SHR DX, CL ;实现将DX逻辑右移
SHL BL, CL ;将BL逻辑左移
OR BL,AH ;将BL或上AH ;也就是使DL的低4位或上AH高4位,从而实现了DX、AX中的双字右移四位。
;此处输入代码段代码
MOV AH,4CH
INT 21H
CODES ENDS
END START
保存运行调试:
分析:
实际上就是AX右移4位,将最高4位空下,接收DX的低4位. 那DX的低4位如何传到AX的高4位中呢?借用bl(当然也可以是其它8位或16位寄存器),它先复制DX的低8位,即DL,然后把低4位移到高4位去。
这时,AH的低4位是结果不能破坏,其高4位是0,而BL的高4位是DX的低4位,正是AH中高4位希望得到的数,BL的低4位为0,所以采用OR或AND都可将BL的高4位与AH的低4位拼起来。
最后把DX右移4位,就达到目的了。
2 从键盘输入小写字母,用大写字母显示出来。
程序代码:
data segment
message db 'please enter the small letter:',0ah,0dh,'$'
message2 db 'the capital letter is:',0ah,0dh,'$'
error db 'enter must be small!please enter again:',0ah,0dh,'$'
message3 db 'the capital letter is:','$'
letter db 8
data ends
stack segment
db 100h dup(0)
stack ends
code segment
assume cs:code,ds:data,ss:stack
start: mov ax,data
mov ds,ax
jmp l1
xor ax,ax ;ax清零
error1:lea dx,error
mov ah,09h ;显示字符串
int 21h
l1:lea dx,message
mov ah,09h
int 21h
lea dx,letter
mov ah,01h ;键盘输入并回显
int 21h
push ax
mov dl,0ah
mov ah,02h
int 21h
mov dl,0dh
int 21h
lea dx,message3
mov ah,09h
int 21h
pop ax
cmp al,'z' ;比较,判断是否小写字母
ja error1 ;大于'z',不是小写字母
cmp al,'a'
jb error1 ;小于‘a’,大写字母重新输入
sub al,20H ;减 20H转换为大写字母
mov dl,al
mov ah,02h;显示字符
int 21h
mov ax,4c00h ;程序正常结束
int 21h
code ends
end start
3 将一个二进制数转换为该数的16进制数
程序代码:
ding segment
main proc far
assume cs:ding
start:
push ds
sub ax,ax
push ax
mov ch,4
mov bx,110011
rotate:
mov cl,4
rol bx,cl
mov al,bl
and al,0fh
add al,30h
cmp al,3ah
jl printit
add al,7h
printit:
mov dl,al
mov ah,2
int 21h
dec ch
jnz rotate
ret
main endp
ding ends
end