用SPIM模拟器实现MIPS冒泡排序.docx

上传人:b****2 文档编号:24115684 上传时间:2023-05-24 格式:DOCX 页数:8 大小:71.38KB
下载 相关 举报
用SPIM模拟器实现MIPS冒泡排序.docx_第1页
第1页 / 共8页
用SPIM模拟器实现MIPS冒泡排序.docx_第2页
第2页 / 共8页
用SPIM模拟器实现MIPS冒泡排序.docx_第3页
第3页 / 共8页
用SPIM模拟器实现MIPS冒泡排序.docx_第4页
第4页 / 共8页
用SPIM模拟器实现MIPS冒泡排序.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

用SPIM模拟器实现MIPS冒泡排序.docx

《用SPIM模拟器实现MIPS冒泡排序.docx》由会员分享,可在线阅读,更多相关《用SPIM模拟器实现MIPS冒泡排序.docx(8页珍藏版)》请在冰豆网上搜索。

用SPIM模拟器实现MIPS冒泡排序.docx

用SPIM模拟器实现MIPS冒泡排序

#计算机

#冒泡排序.s排序算法

#太原科技大学计算机学院的学生伤不起呀

.text

main:

la$s0,arrayBubblela$a0,zuozhe#printmessageaboutthistext

li$v0,4

syscall

la$a0,countmsg#addressofstringtoprint

li$v0,4#syscall4=print_string

syscall#makethesyscall

##thenextblockrequestsusertointerthecountofnumberstosort,

##ifinputNumbers<=0,needtoreinter

err:

li$v0,5#syscall5=read_int

syscall

move$s1,$v0#moveread_int(count)to$s1

slt$t0,$zero,$s1#checkcount,if$s1>0$zero=1

bne$t0,$zero,inputfor1#gotoinputfor1if$s1>0

la$a0,errormsg#addressoferrorwarningerrormsg

li$v0,4#syscall4=print_string

syscall

jerr#gotoerrtorepeat-inputcount

##getnumbersonebyone

inputfor1:

la$a0,msg1#addressofstringtoprint

li$v0,4

syscall

move$a0,$s1#outputcounttoconsole

li$v0,1#syscall1=print_int

syscall

la$a0,msg2#addressofstringtoprint

li$v0,4

syscall

move$t0,$zero#set$t0to0,$t0willusedtocontrolinputcount

inputdata:

##Registerused:

#$t0-usedtoholdcurrentCountandsortedNumbersofinputnumbers

#$v0-syscallparameterandreturnvalue

li$v0,5#inputnumberto$v0

syscall

sll$t1,$t0,2#reg$t1=currentCount*4

add$t2,$s0,$t1#reg$t2=$s0+(currentCount*4)

#reg$t2hastheaddressofarrayBubble[currentCount]

sw$v0,0($t2)#arrayBubble[currentCount]=reg$v0

addi$t0,$t0,1#userhasinputcurrentCount+1numbers

slt$t1,$t0,$s1#$t1=1ifcurrentCount+1

bne$t1,$zero,inputdata#gotoinputdataifcurrentCount+1

move$a0,$s0#$a0=$s0,$a1=$s1asargumenttransferedtobubbleSort

move$a1,$s1#ifcurrentCount+1=count

jalsort#gotoLabelsortforbubbleSort(gettheaddressof

#nextinstruction

la$a0,bounder#outputbounder(agroupof#s)

li$v0,4

syscall

la$a0,sortedNumbers#outputstring(int)ofsortednumbers

li$v0,4

syscall

move$t0,$zero#set$t0to0,$t0willusedtocontroloutputcount

##outputsortedNumbersbytwospaceinterval

outputdata:

sll$t1,$t0,2#reg$t1=currentCount*4

add$t2,$s0,$t1#reg$t2=$s0+(currentCount*4)

lw$a0,0($t2)#outputthecontentinaddressspecifiedby$a0

li$v0,1

syscall

la$a0,space#outputtwospace

li$v0,4

syscall

addi$t0,$t0,1#reg$t0=$t0+1preparedforoutputtingnextnumber

slt$t1,$t0,$s1#check:

allcontentshavebeenoutputted

bne$t1,$zero,outputdata#gotooutputifnumbersaren'talloutput

la$a0,bounder#outputbounder(agroupof#s)

li$v0,4

syscall

jexit#endofbubbleSort

####################Savingregister##########################

sort:

addi$sp,$sp,-20#makeroomonstackfor5register

sw$ra,16($sp)#save$raonstack

sw$s3,12($sp)#save$s3onthestack

sw$s2,8($sp)#save$s2onthestack

sw$s1,4($sp)#save$s1onthestack

sw$s0,0($sp)#save$s0onthestack

move$s2,$a0#copyparameter$a0into$s2(save$a0)

move$s3,$a1#copyparameter$a1into$s3(save$a1)

##Fuctionname:

sort;fuction,invokedbymain,invokestheswapfuction

#voidsort(intv[],intn)

#{

#inti,j;

#for(i=0;i

#for(j=i-1;j>=0&&v[j]>v[j+1];j-=1){

#swap(v,j);

#}

#}

#}

###########Outerloop(thesortfuctioncontainsoutloopandinnerloop)########

move$s0,$zero#i=0

for1tst:

slt$t0,$s0,$s3#reg$t0=0if$s0>=$s3(i>=n)

beq$t0,$zero,exit1#gotoexit1if$s0>=$a1(i

###########Innerloop#############################################

addi$s1,$s0,-1#j=i-1

for2tst:

slti$t0,$s1,0#reg$to=1if$s1<0(j<0)

bne$t0,$zero,exit2#gotoexit2if$s1<0(j<0)

sll$t1,$s1,2#reg$t1=j*4

add$t2,$s2,$t1#reg$t2=v+(j*4)

lw$t3,0($t2)#reg$t3=v[j]

lw$t4,4($t2)#reg$t4=v[j+1]

slt$t0,$t4,$t3#reg$t0=0if$t4>=$t3

beq$t0,$zero,exit2#gotoexit2if$t4>=$t3

##########Passparametersandcall#############################

move$a0,$s2#firstparameterofswapisv(old$a0)

move$a1,$s1#secondparameterofswapisj

#############swap(v[],k)#########################################

sw$t4,0($t2)#v[j]=reg$t4

sw$t3,4($t2)#v[j+1]=reg$t3

##############Innerloop########################################

addi$s1,$s1,-1#j-=1

jfor2tst#jumptotestofinnerloop

###############Outerloop#########################################

exit2:

addi$s0,$s0,1#i+=1

jfor1tst#jumptotestofouterloop

exit1:

#thesortfunctiondoesn'tworkafter

##restoringregisters

lw$s0,0($sp)#restore$s0fromstack

lw$s1,4($sp)#restore$s1fromstack

lw$s2,8($sp)#restore$s2fromstack

lw$s3,12($sp)#restore$s3fromstack

lw$ra,16($sp)#restore$rafromstack

addi$sp,$sp,20#restorestackpointer

##procedurereturn

jr$ra#returntocallingroutine

###########Endofprocedure#########################################

exit:

li$v0,10#syscall10=exit

syscall#makesyscall

.data

arrayBubble:

countmsg:

.asciiz"输入你要排序的个数:

"

errormsg:

.asciiz"你要输入的数应该大于0:

"

zuozhe:

.asciiz"#计算机092002\n#王佳宾2011/12/12\n#学号:

200920010220\n#冒泡排序.s排序算法\n"

msg1:

.asciiz"你要输入的正数的总数是:

"

msg2:

.asciiz"\n请依次输入数字按enter键确定:

\n"

space:

.asciiz""

bounder:

.asciiz"\n========================================================================================================\n"

sortedNumbers:

.asciiz"由小到大的顺序依次是:

"

#endof冒泡排序.s

运行结果如下:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 动态背景

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1