微机原理与接口技术(钱晓捷版)课后习题答案 下载本文

内容发布更新时间 : 2024/5/17 22:39:41星期一 下面是文章的全部内容请认真阅读。

; 代码段,子程序 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 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 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 ; 代码段,子程序