您的位置:首页 > 其它

用汇编语言实现:以十进制形式输出双精度整型数

2015-06-24 12:54 531 查看
题目:以十进制形式输出双精度整型数(正数)(EDX:EAX双精度保存在两个寄存器中)

题解:

1.ans = (EDX * 2 ^ 32 + EAX)

2.每次对ans模10,除10

3.EDX = q1 * 10 + r1 , 2 ^ 32 = q2 * 10 + r2 , EAX = q3 * 10 + r3

4.ans = q1 * q2 * 100 + 10 * q1 * r2 + 10 * q2 * r1 + q3 * 10 + r1 * r2 + r3

5.ans / 10 = q1 * q2 * 10 + q1 * r2 + q2 * r1 + q3 + (r1 * r2 + r3) / 10

6.ans % 10 = (r1 * r2 + r3) % 10

7重复操作,直到EAX,EDX都为0时停止

总结:

1.考试前开始想这个题目的,但是没有想出来。突然觉得,每次我没有想出来一个编程问题,都是心思没有全都放在

想题上面的时候!比如想和谁谁谁比赛啦,争什么面子啦,对待知识不够纯粹,导致占用了大脑的一部分空间!

2.想到舍友yyz总说:相信这个题没有错,相信自己能够把题做出来,就可以攻无不克,战无不胜了!

3.然后就是写代码出了一些小bug,又是无脑的输出中间结果,其实错误很明显,直接静态查错,几下就全改出来了,

以后再碰到小程序错误,要改成先静态查错,再总观全局,最后输出中间结果。

4.对于这个想题思路上就是,类比法,对于10进制数以2进制形式输出的方法会了以后,可以类比到这个题目上来看。

类比是一种很好的模仿解题方法

.data
	var byte 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
	ind dword 0
	ans byte 65 dup(0)
	_low dword 0
	_high dword 0
	eax_quotient dword  0
	eax_remainder dword  0
	edx_quotient dword  0
	edx_remainder dword  0
	const_quotient dword 429496729
	const_remainder dword 6
	cur dword 10
.code
main PROC

	comment ##
	
	;first : read edx
	;second : read eax 
	call ReadHex
	mov edx,eax
	call ReadHex
     	call display
     	

   	
   	
    
main ENDP

display Proc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;												;		
;												;
;	function:display edx:eax in dec								;	
;	receives:EDX:EAX									;
;	return:nothing										;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;		
	mov _low, eax
	mov _high, edx    ;save eax£¬edx in memery
	
	
	
	do_64:
	
	mov edx, 0
	mov eax, _high
	div cur
	mov [edx_remainder], edx      ;save edx's remainder
	mov [edx_quotient], eax       ;save edx's quotient
	
	mov edx, 0
	mov eax, _low
	div cur
	mov [eax_remainder], edx      ;save eax's remainder
	mov [eax_quotient], eax       ;save eax's quotient
	;call DumpRegs
	
	mov eax, const_remainder
	mul [edx_remainder]
	add eax, [eax_remainder]           ;r = remainder1 * remainder2 + remainder3 
	
	cdq
	div cur 			   ;r / 10
	inc ind                              
	mov ebx, ind
	mov [ans + ebx], dl                  ;save r % 10
	
	
	mov [_low], eax                     ;save r / 10
	mov [_high], 0                      ;init _high
	
	
	
	
	
	jmp go_on
	
	continue:
	jmp do_64
	
	go_on:
	
	
	
	mov eax, 4294967290
	mul [edx_quotient]            ;10 * quotient2 * quotient1
	
	add [_low], eax                    ;add it
	adc [_high], edx 		   ;add it
	
	
	mov eax, const_quotient		   ;quotient1 * remainder2
	mul edx_remainder
	add [_low], eax                    ;add it
	adc [_high], edx 		   ;add it
	
	mov eax, const_remainder	   ;quotient2 * remainder1
	mul edx_quotient
	add [_low], eax                    ;add it
	adc [_high], edx 		   ;add it
	
	mov eax, eax_quotient              ;quotient3	
	add [_low], eax                    ;add it	
	adc [_high], 0                     ;add it
	
	
	
	
	cmp _low, 0
	jz second
	jmp continue
	second: cmp _high, 0
	jnz continue
	
	
	
	
	
	mov ecx, ind
	write_it:
	movzx eax, [ans + ecx]
	call WriteDec
	loop write_it
	
	
	
	call Crlf
		
	ret

display endp

end main
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: