当前位置:首页 > 微机原理与接口技术(第四版)课后习题答案北京大学出版社
mov ax,2020h mov esi,6 again0: mov [ebx],edx add ebx,4 mov ecx,16
again1: mov word ptr [ebx],ax add ebx,2 inc al loop again1 add ebx,2 add edx,1 dec esi jnz again0 mov eax,offset table call dispmsg exit 0 end start
〔习题4.19〕请按如下说明编写子程序: 子程序功能:把用ASCII码表示的两位十进制数转换为压缩BCD码
入口参数:DH=十位数的ASCII码,DL=个位数的ASCII码
出口参数:AL=对应BCD码 〔解答〕
asctob proc shl dh,4 mov al,dh and dl,0fh or al,dl ret asctob endp
〔习题4.20〕乘法的非压缩BCD码调整指令AAM执行的操作是:AH←AL÷10的商,AL←AL÷10的余数。利用AAM可以实现将AL中的100内数据转换为ASCII码,程序如下: xor ah,ah aam
add ax,3030h
利用这段程序,编写一个显示AL中数值(0~99)的子程序。 〔解答〕
disp99 proc xor ah,ah aam add ax,3030h push ax mov al,ah call dispc pop ax call dispc ret
17
disp99 endp
〔习题4.21〕编写一个源程序,在键盘上按一个键,
将其返回的ASCII码值显示出来,如果按下ESC键(对应ASCII码是1BH)则程序退出。请调用书中的HTOASC子程序。
〔解答〕 ; 代码段,主程序 again: call readc cmp al,1bh jz done mov bl,al mov al,':' call dispc mov al,bl rol al,4 call htoasc ; 调用子程序 call dispc ; 显示一个字符 mov al,bl call htoasc ; 调用子程序 call dispc ; 显示一个字符 call dispcrlf jmp again done:
〔习题4.22〕编写一个子程序,它以二进制形式显示EAX中32位数据,并设计一个主程序验证。 〔解答〕 ; 代码段,主程序 mov eax,8F98FF00H call dispbd ; 调用子程序 ; 代码段,子程序 dispbd proc ; 32位二进制数的输出 push ecx push edx mov ecx,32 ; 要输出的字符个数 dbd: rol eax,1 ; AL循环左移一位 push eax and al,01h ; 取AL最低位 add al,30h ; 转化成相应的ASCLL码值 call dispc ; 以二进制的形式显示 pop eax loop dbd pop edx pop ecx ret dispbd endp
〔习题4.23〕将例题4-16的32位寄存器改用16位
寄存器,仅实现输出-215~+215
-1之间的数据。 〔解答〕 ; 数据段
array word 12345,-1234,32767,-32768,0,667
writebuf byte 6 dup(0) ; 代码段,主程序 mov ecx,lengthof array mov ebx,0 again:
mov ax,array[ebx*2] call write call dispcrlf
inc ebx ;此时ebx代表array中的第几个数
dec ecx ;此时ecx代表循环的次数
jnz again ; 代码段,子程序
write proc ;子程序开始 push ebx push ecx push edx
mov ebx,offset writebuf ;ebx指向显示缓冲区
test ax,ax jnz write1
mov byte ptr [ebx],30h inc ebx jmp write5
write1: ;若不为0则首先判断是正是负
jns write2 ;若为正则跳过下面两步到write2
mov byte ptr [ebx],'-' inc ebx neg ax write2:
mov cx,10
push cx ;将cx=10压入栈,作为退出标志
write3: ;write3是让eax循环除以10并把余数的ASCII码压入栈 cmp ax,0 jz write4 xor dx,dx div cx add dx,30h push dx jmp write3
write4: ;余数的ASCII码出栈,遇到10终止并转到write5显示结果 pop dx cmp dx,cx jz write5
mov byte ptr [ebx],dl inc ebx jmp write4
write5: ;显示结果 mov byte ptr [ebx],0 mov eax,offset writebuf
18
call dispmsg pop edx pop ecx pop ebx ret write endp
〔习题4.24〕参考例题4-17,编写实现32位无符号整数输入的子程序,并设计一个主程序验证。 〔解答〕 ; 数据段 count =10
array dword count dup(0) temp dword ?
readbuf byte 30 dup(0) errmsg byte 'Input error,enter again!',13,10,0 msg1 byte 'Input ten unsigned numbers,each number ends with enter key:',13,10,0 msg2 byte 'Check the numbers your inputted:',13,10,0 ; 代码段,主程序 mov eax,offset msg1 call dispmsg mov ecx,count
mov ebx,offset array again:
call read mov eax,temp mov [ebx],eax add ebx,4 dec ecx jnz again
mov eax,offset msg2 call dispmsg
mov edx,offset array mov ecx,count next:
mov eax,[edx] call dispuid call dispcrlf add edx,4 dec ecx jnz next ; 代码段,子程序 read proc push eax push ecx push ebx push edx read0:
mov eax,offset readbuf call readmsg test eax,eax jz readerr
cmp eax,12 ja readerr
mov edx,offset readbuf xor ebx,ebx xor ecx,ecx mov al,[edx] cmp al,'+' jz read1 cmp al,'-' jnz read2 jmp readerr read1:
inc edx
mov al,[edx] test al,al jz read3 ;如果为0,则说明该字符串已结束 read2:
cmp al,'0' jb readerr cmp al,'9' ja readerr sub al,30h
imul ebx,10 ;ebx用来存储处理过的数据
jc readerr movzx eax,al add ebx,eax jnc read1 readerr:
mov eax,offset errmsg call dispmsg jmp read0 read3:
mov temp,ebx pop edx pop ebx pop ecx pop eax ret read endp
〔习题4.25〕编写一个计算字节校验和的子程序。所谓“校验和”是指不记进位的累加,常用于检查信息的正确性。主程序提供入口参数,有数据个数和数据缓冲区的首地址。子程序回送求和结果这个出口参数。
〔解答〕 ; 计算字节校验和的通用过程 ; 入口参数:DS:EBX=数组的段地址:偏移地址,ECX=元素个数 ; 出口参数:AL=校验和 ; 说明:除EAX/EBX/ECX外,不影响其他寄存器 checksum proc xor al,al ; 累加器清0
19
sum: add al,[ebx]
; 求和
inc ebx ; 指向下一个字节
loop sum ret checksum endp 〔习题4.26〕
编制3个子程序把一个32位二进制数用8位十六进制形式在屏幕上显示出来,分别运用如下3种参数传递方法,并配合3个主程序验证它。
(1)采用EAX寄存器传递这个32位二进制数 (2)采用temp变量传递这个32位二进制数 (3)采用堆栈方法传递这个32位二进制数 〔解答〕
(1) ; 数据段 wvar word 307281AFH ; 代码段,主程序 mov eax,wvar call disp mov al,'H' call dispc ; 代码段,子程序 disp proc push ebx push ecx mov ecx,8 ; 8位 dhw1: rol eax,4 mov ebx,eax and al,0fh ; 转换为ASCII码 add al,30h cmp al,'9' jbe dhw2 add al,7 dhw2: call dispc mov eax,ebx loop dhw1 pop ecx pop ebx ret disp endp
(2) ; 数据段 wvar word 307281AFH temp word ? ; 代码段,主程序 mov eax,wvar mov temp,eax call disp mov al,'H' call dispc ; 代码段,子程序 disp proc push ebx push ecx mov ecx,8 ; 8位
mov eax,temp dhw1: rol eax,4 mov ebx,eax and al,0fh ; 转换为ASCII码 add al,30h cmp al,'9' jbe dhw2 add al,7
dhw2: call dispc ; 显示一个字符 mov eax,ebx loop dhw1 pop ecx pop ebx ret disp endp
(3) ; 数据段 wvar word 307281AFH ; 代码段,主程序 push wvar call disp add esp,4 mov al,'H' call dispc ; 代码段,子程序 disp proc push ebp mov ebp,esp push ebx push ecx mov ecx,8 ; 8位 mov eax,[ebp+8] dhw1: rol eax,4 mov ebx,eax and al,0fh ; 转换为ASCII码 add al,30h cmp al,'9' jbe dhw2 add al,7 dhw2: call dispc mov eax,ebx loop dhw1 pop ecx pop ebx pop ebp ret disp endp
〔习题4.27〕配合例题4-11的简单加密解密程序,设计一个输入密码的程序,将输入的若干字符经过适当算法得到一个字节量密码。 〔解答〕 ;ex0427.asm include io32.inc .data key byte ?
20
msg0 byte 'Enter your password:',0
passw byte 50 dup(0) errormsg byte 'Password error, input again!',13,10,0 bufnum = 255
buffer byte bufnum+1 dup(0) ; 定义键盘输入需要的缓冲区 msg1 byte 'Enter messge: ',0 msg2 byte 'Encrypted message: ',0 msg3 byte 'Original messge: ',0 .code start: mov eax,offset msg0 ; 提示输入加密密码 call dispmsg mov eax,offset passw ; 设置入口参数EAX call readmsg ; 调用输入字符串子程序输入密码 mov ecx,eax dec ecx xor ebx,ebx mov al,passw[ebx] again0: inc ebx xor al,passw[ebx] ;使用简单的异或方法得到加密关键字 loop again0 mov key,al ; 保存加密关键字 mov eax,offset msg1 ; 提示输入字符串 call dispmsg mov eax,offset buffer ; 设置入口参数EAX call readmsg ; 调用输入字符串子程序 push eax ; 字符个数保存进入堆栈 mov ecx,eax ; ECX=实际输入的字符个数,作为循环的次数 xor ebx,ebx ; EBX指向输入字符 encrypt: mov al,key ; AL=加密关键字 xor buffer[ebx],al ; 异或加密 inc ebx dec ecx ; 等同于指令:loop encrypt jnz encrypt ; 处理下一个字符 mov eax,offset msg2 call dispmsg mov eax,offset buffer ; 显示加密后的密文 call dispmsg call dispcrlf ;
again: mov eax,offset msg0 ; 提示输入解密密码 call dispmsg
共分享92篇相关文档