当前位置:文档之家› 浮点数的表示和计算

浮点数的表示和计算

《计算机组成原理》实验报告sw $aO, O($fp) #calculate the first nu mberandi $s2, $s0, 0x80000000 # s2 is the sig nsrl $s2, $s2, 31andi $s3, $s0, 0x7f800000 # s3 is the exp onentsrl $s3, $s3, 23andi $s4, $s0, 0x007fffff # s4 is the fractio naddi $s4, $s4, 0x00800000 #calculate the seco nd numberandi $s5, $s1, 0x80000000 # s5 is the sig nsrl $s5, $s5, 31andi $s6, $s1, 0x7f800000 # s6 is the exp onentsrl $s6, $s6, 23andi $s7, $s1, 0x007fffff # s7 is the fractio naddi $s7, $s7, 0x00800000sub $t0, $s3, $s6bit $t0, 0, sumL1 # add subbgt $t0, 0, sumL2 # sub addbeq $t0, 0, sumL32.减法指令如下:mysub: subu $sp, $sp, 32sw $ra, 20($sp)sw $fp, 16($sp)addiu $fp, $sp, 28sw $a0, 0($fp) #calculate the first nu mberandi $s2, $s0, 0x80000000 # s2 is the sig nsrl $s2, $s2, 31andi $s3, $s0, 0x7f800000 # s3 is the exp onentsrl $s3, $s3, 23andi $s4, $s0, 0x007fffff # s4 is the fractio naddi $s4, $s4, 0x00800000 #calculate the seco nd numberxori $s5, $s1, 0x80000000 # s5 is the sig nsrl $s5, $s5, 31andi $s6, $s1, 0x7f800000 # s6 is the exp onentsrl $s6, $s6, 23andi $s7, $s1, 0x007fffff # s7 is the fractio naddi $s7, $s7, 0x00800000sub $t0, $s3, $s6blt $t0, 0, subL1 # +,-bgt $t0, 0, subL2 # -,+beq $t0, 0, subL3 # +,+ or -,-3.乘法指令如下:mutilStart: srl $t2, $s0, 31srl $t3, $s1, 31sll $t4, $s0, 1srl $t4, $t4, 24 # expsll $t5, $s1, 1srl $t5, $t5, 24 # exp # 0...1fracsll $t6, $s0, 9srl $t6, $t6, 9ori $t6, $t6, 0x00800000sll $t6, $t6, 8addi $t4, $t4, 1 # 0...1fracsll $t7, $s1, 9srl $t7, $t7, 9ori $t7, $t7, 0x00800000sll $t7, $t7, 8addi $t5, $t5, 1sub $t4, $t4, 127add $t4, $t4, $t5 # final expsub $t5, $t5, $t5mutilCompareSig n:add $t2, $t2, $t3sll $t2, $t2, 31 # final sig nmultu $t6, $t7mfhi $t5#regularandi $t8, $t5,0x80000000beq$t8, 0x80000000, muti In extsll $t5, $t5, 1sub $t4, $t4, 1mutil next: sll $t5, $t5, 1 # IEEE754sub $t4, $t4, 1srl $t5, $t5, 9 # final fracti onmutilFi nal: sll $t4, $t4, 24srl $t4, $t4, 1addu $t2, $t2, $t4addu $t2, $t2, $t5 # resultadd $s2, $t2, $zero # save resultli $v0, 4la $a0, msg3syscallli $v0, 2mtc1 $t2, $f12syscallla $a0, nsg0 # new lineli $v0, 4syscallIw Iw $ ad dii jr $ra,fp, u$ra20($sp)16($sp)$sp, $sp, 324.除法指令如下:divideStart: srl $t2, $s0, 31srl $t3. $s1, 31sll $t4. $s0, 1srl $t4, $t4, 24 # expsll $t5, $s1, 1srl $t5, $t5, 24 # expsll $t6, $s0, 9srl $t6, $t6, 9ori $t6, $t6, 0x00800000sll $t7, $s1, 9srl $t7, $t7, 9ori $t7, $t7, 0x00800000sub $t4, $t4, $t5 # final expaddi $t4, $t4, 127sub $t5, $t5, $t5divideCompareSig n: add $t2, $t2, $t3sll $t2, $t2, 31 # final sig nsub $t8, $t8, $t8 # use to record dividsub $t3, $t3, $t3 # use to record rema in dersub $t5, $t5, $t5 # use as coun terdividecompare bge $t5, 24, dividebreakout #compare the two nu mber blt $t6, $t7, dividelowerdividegreater: sub $t6, $t6, $t7 #divide is lowersll $t6, $t6, 1sll $t8, $t8, 1addi $t8, $t8, 1addi $t5, $t5, 1j dividecomparedividelower: sll $t6, $t6, 1sll $t8, $t8, 1addi $t5, $t5, 1j dividecomparedividebreakout: blt $t8, 0x00800000, dividedoSllbge $t8, 0x01000000, dividedoSrlj divideresultdividedoSII: sll $t8, $t8, 1subi $t4, $t4, 1blt $t8, 0x00800000, dividedoSII j divideresult dividedoSrl: srl $t8, $t8, 1addi $t4, $t4, 1bge $t8, 0x01000000, dividedoSrl j divideresult divideresult:bgt $t4, 255 ,divideoverFlowsll $t8, $t8, 9 srl $t8, $t8, 9sll $t4, $t4, 24srl $t4, $t4, 1add $t2, $t2, $t4add $t2, $t2, $t8add $s2, $t2, $zero# save resultli $v O 4la $ a 0, msg3 #output messagesyscallli $v0,2mtc1$t2, $f12syscall j diviede ndsw $ra, 20($sp)sw $fp, 16($sp)addiu $fp, $sp, 28sw $a0, 0($fp)add $a0, $s2, $zeroadd $t9, $zero, $a0sub $t6, $t6, $t6 # set $t6=0sub $t7, $t7, $t7 # set $t7=0 addi $t5, $zero, 32 # set $t5=32 as a coun ter addi$t8, $zero,0x80000000 # set $t8=1000 00000000 0000 00005.十进制转换为二进制指令如下:dex2:subu $sp, $sp, 32 0000 0000 0000begi nLoop: addi $t5, $t5, -1and$t7, $t8, $t9srl $t8, $t8, 1# coun ter --srlv add$aO, li$v0, syscallbeq $t5, jbegi$t7, $t7, $t5$t7, $zero 1$t6, ExistnLoop#output one bit#if equals jump exist6.十进制转换为十八进制指令如下:dex16:subu $sp, $sp, 32sw $ra, 20($sp)sw $fp, 16($sp)addiu $fp, $sp, 28sw $a0, 0($fp)sub $tO, $t0, $t0# use as coun tersrloop: bge $t0, & dex16end#16addi $t0, $t0, 1 srl $t1, $s2, 28sll $s2, $s2, 4bgt $t1, 9, outcharli $v0, 1add $a0, $t1, $zerosyscallj srloopoutchar: addi $t1, $t1, 55 # cha nge to charli $v0,11add $a0, $t1, $zerosyscallj srloopdex16e nd:la $a0, msg0 # new lineli $v0, 4syscalllw $ra, 20($sp)lw $fp, 16($sp)addiu $sp, $sp, 32jr $ra四、实验结果及分析和(或)源程序调试过程1. 实验结果:本次实验我们以十进制实数形式的输入两个浮点数,在内存中以 IEEE 754单精度方式表示,执行运算操作后,以十进制形式把结果输出,并且可以以二进制和十六 进制的方式显示输出,下图是执行加法运算,并输出二进制形式:a QP : 1, add 2.3. miol4. Aiv Cl. qui t1I tie F Lirs t Float mg value i s ' 1.25Ihe Second floating value i s :1. 1 匮Ih«! rgASMit i s. 2.375Inp ut B. the Tarm at t o outjiut: 5_ biaary & Hex : 5Diooooaocooi woooooooooaocoooGooZnput a OP: 1. add 2. sul 3. miol 4. diw 0. quitF图是执行乘法操作,并输出十六进制形式:'Input a 心P: 1. add 2. snl 3. nnol 4. Aiw Cl. quitTTke TI JTS t Floating Valu.电JL s . 2. SThe Second Floating is:0. 5The re&sult i s. 1.25^tnput st the Form at to outjut:5. binary 6 Hex : 63F/UDOOOO'Input a CP_ L. add 2 gtfb 3. niul 4. div C. quit减法运算如下图:工itput a OP: 1 . ad丄 2. sub 3. rwul 4. div qui 12Ih_E First FJ.Da.txik百va_lue 1 stl&Iks Vtlut ii;20Tk.fi reasult GIitpui * tht Format tc output ;吕.binury B ;日C1200000Ikput t OP' 1. add 2. sub 3 mu! 4 div 0,礬it除法运算如下图:Iiupiit 旦OP :1. add 2. sub 3. mul 4- da v 0. qni tAIhke First Floating value 1 s 112. 24The Second floating value is2Th色reasult is! 6.12Inpiit B th.^ Ear mat t g* ^tput ;5> binary & Ilex ;日plOOOOOOl LOOOOl 111010111DOOO1010 Input & OP: 1. ^.dd 2. sub 3.. mul 4・div* ® q-uit当输入错误,如除数为0时,会显示错误信息并退出,如下图:Input a OP 1 t_ add 2. sub 3. mil 4. div 0. KJUI t4I he f urst FL D all ng val 口总is:1Iht Second FLoafinc valu# is;0Error-一prograim is finishe! running:一一2. 实验分析:本次实验我们使用MIPS汇编指令,利用整数运算指令来编写了一个浮点数运算程序,通过在MARS 4.4软件上进行调试运行,得到了正确结果,本次实验实现了以下功能:(1) 程序提供了人机交互方式(字符界面)供用户选择相应的功能;(2) 接受十进制实数形式的输入,在内存中以IEEE 754单精度方式表示,支持以二进制和十六进制的方式显示输出;(3) 实现了浮点数的加减乘除运算;(4) 没有使用浮点指令,只利用了整数运算指令来完成软件的编写。

相关主题