微机原理与接口技术-课后习题答案

发布时间 : 星期六 文章微机原理与接口技术-课后习题答案更新完毕开始阅读

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位无符号整数输入的子程序,并设计一个主程序验证。 〔解答〕

; 数据段

- -33

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

34

- - 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

- -35

〔习题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 dhw1: rol eax,4 mov ebx,eax and al,0fh 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

位 转换为ASCII码 -36

- ; 8;

联系合同范文客服:xxxxx#qq.com(#替换为@)