MIPS编程实例集锦2.docx
- 文档编号:6295460
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:45
- 大小:1.38MB
MIPS编程实例集锦2.docx
《MIPS编程实例集锦2.docx》由会员分享,可在线阅读,更多相关《MIPS编程实例集锦2.docx(45页珍藏版)》请在冰豆网上搜索。
MIPS编程实例集锦2
PROBLEMSINMIPSPROGRAMMING
∙Writeaprogramtoprintnextintegeranditsupposetotakeinputfromkeyboardandechoesback?
∙Writeaprogramtodisplaygivenintegertobereverse?
∙WriteaprogramtoconverttemperatureinCelsiustoFahrenheit?
∙WriteaprogramforSTRCPYINMIPS?
∙Writeaprogramtofindproductof2numbersthataretakefromkeyboard?
∙HowtowriteaprogramFORloopinmips?
∙Writeamipsprogramforgivenccode(passingparameters)?
∙Implementfollowingg=h+A[8],A[12]=h+A[8],g=h+A[i]inMips?
∙Writeprogramforifelseconditionif(i==j)
of=g+h;
oelsef=g-h;InMips?
∙Writeprogramforleafexample
intleaf_example(intg,inth,inti,intj)
{intf;
f=(g+h)-(i+j);
returnf;}
∙Writeprogramforswitchcase:
switch(k){
case0:
f=i+j;break;
case1:
f=g+h;break;
case2:
f=g-h;break;
case3:
f=i-j;break;
default:
break;}
∙Writeprogramswapping2numbersusingcallbyreference?
∙Programforwhileloopinmips?
∙Writeaprogramtopassparametersinstackconvertfollowingccodetomips:
intA=109;
voidmain()
{intf;
f=parm5leaf(5,-20,13,3,A);
printf("/nf=%d",f);
return;}
intparm5leaf(intg,inth,inti,intj,intk)
{intf1,f2;
f1=(g+h);
f2=(i+j)*k;returnf1+f2;}
∙15.2.5[15]<§2.5>Considerthefollowingcodeusedtoimplementtheinstruction:
sllv$s0,$s1,$s2
∙Writeaprogramforsquareofanumberingivenlimit?
1.Writeaprogramtoprintnextintegeranditsupposetotakeinputfromkeyboardandechoesback?
Code:
.data
#Constantstringstobeoutputtotheterminal
promptInt:
.asciiz"Pleaseinputaninteger:
"
resultInt:
.asciiz"Nextintegeris:
"
linefeed:
.asciiz"\n"
enterkey:
.asciiz"Pressanykeytoendprogram."
.text
main:
#promptforaninteger
li$v0,4#codeforprint_string
la$a0,promptInt#point$a0topromptstring
syscall#printtheprompt
#getanintegerfromtheuser
li$v0,5#codeforread_int
syscall#getintfromuser-->returnedin$v0
move$t0,$v0#movetheresultingintto$t0
#computethenextinteger
addi$t0,$t0,1#t0<--t0+1
#printouttextfortheresult
li$v0,4#codeforprint_string
la$a0,resultInt#point$a0toresultstring
syscall#printtheresultstring
#printouttheresult
li$v0,1#codeforprint_int
move$a0,$t0#putresultin$a0
syscall#printouttheresult
#printoutalinefeed
li$v0,4#codeforprint_string
la$a0,linefeed#point$a0tolinefeedstring
syscall#printlinefeed
#waitfortheenterkeytobepressedtoendprogram
li$v0,4#codeforprint_string
la$a0,enterkey#point$a0toenterkeystring
syscall#printenterkey
#waitforinputbygettinganintegerfromtheuser(integerisignored)
li$v0,5#codeforread_int
syscall#getintfromuser-->returnedin$v0
#Alldone,thankyou!
li$v0,10#codeforexit
syscall#exitprogram
Output:
2.Writeaprogramtodisplaygivenintegertobereverse?
Code:
.data
mess1:
.asciiz"Enterstring:
"
mess2:
.asciiz"Reversedstring:
"
newln:
.asciiz"\n\n"
input:
.space128
output:
.space2
.text
.align2
main:
li$v0,4
la$a0,mess1
syscall#promptforinput
li$v0,8#systemcallcodeforread_string
la$a0,input
la$a1,127
syscall#readthestringwithmax127chars
li$v0,4
la$a0,newln
syscall#newline
li$v0,4
la$a0,mess2
syscall#print"Reversedstring"
la$a0,output#outputwillbeasinglechar
addi$a0,$a0,1
add$a1,$zero,$zero
sb$a1,0($a0)#setnullattheendofoutput
la$a0,input#prepareforproc.call
li$a1,0#$a1containsindex
jalrev
li$v0,4
la$a0,newln
syscall
rev:
add$t1,$a1,$a0
lbu$t0,0($t1)#loadcurchar
bne$t0,0xa,notend#ifnotend,rev
#bne$t0,$zero,notend
jr$ra#elsereturn
notend:
addi$sp,$sp,-8#alloc2wordsforcallee-saveddata
sw$ra,4($sp)#savereturnaddress
sw$t0,0($sp)#savecurchar
addi$a1,$a1,1#increaseindex
jalrev
la$a0,output#stuffthecurcharintothe'output'
lw$a1,0($sp)
sb$a1,0($a0)
li$v0,4#printcurrentchar
la$a0,output
syscall
lw$ra,4($sp)#restorecallerstate
addi$sp,$sp,8#freeupthestackspaceused
jr$ra
Output:
3.WriteaprogramtoconverttemperatureinCelsiustoFahrenheit?
Code:
.text
main:
la$a0,prompt#printpromptonterminal
li$v0,4
syscall
li$v0,5#syscall5readsaninteger
syscall
mul$t0,$v0,9#toconvert,multiplyby9
div$t0,$t0,5#thendivideby5,and
add$t0,$t0,32#add32tocompletetheconversion
la$a0,ans1#printstringbeforeresult
li$v0,4
syscall
move$a0,$t0#printresult
li$v0,1
syscall
la$a0,endl#systemcalltoprint
li$v0,4#outanewline
syscall
li$v0,10
syscall#endprogram
.data
prompt:
.asciiz"EnterTemperature(Celsius):
"
ans1:
.asciiz"ThetemperatureinFahrenheitis"
endl:
.asciiz"\n"
Output:
4.WriteaprogramforSTRCPYINMIPS?
Code:
#Mainprogram:
#charA[]="ComputerOrganization\n";
#charB[100];
#voidmain()
#{
#strcpy(B,A);
#printf("\nStringA:
%s",A);
#printf("\nStringB:
%s",B);
#return;
#}
#voidstrcpy(charx[],chary[])
#{
#inti;
#i=0;
#while((x[i]=y[i])!
=0)/*copyandtestbyte*/
#i=i+1;
#}
#
#assumesthattheaddressofxisin$a0,theaddressofyisin
#$a1andiisin$s0.
#Allocatethestaticvariablesfortheprogram
.data
A:
.asciiz"ComputerOrganization\n"
B:
.space100
.globloutputAmsg
outputAmsg:
.asciiz"\nStringA:
"
.globloutputBmsg
outputBmsg:
.asciiz"\nStringB:
"
#Startofthemainprogram
.text
.globlmain
main:
#mainhastobeagloballabel
addu$s7,$0,$ra#savethereturnaddressinaglobalregister
la$a0,B#Bisthefirstparameter(destination)
la$a1,A#Aisthesecoundparameter(source)
jalstrcpy#strcpy(B,A);
#OutputstringA:
li$v0,4#print_str(systemcall4)
la$a0,outputAmsg#output"\nStringA:
"
syscall
li$v0,4#print_str
la$a0,A#outputthestringA
syscall
#OutputstringB:
li$v0,4#print_str(systemcall4)
la$a0,outputBmsg#output"\nStringB:
"
syscall
li$v0,4#print_str
la$a0,B#outputthestringB
syscall
#Usualstuffattheendofthemain
addu$ra,$0,$s7#restorethereturnaddress
jr$ra#returntothemainprogram
add$0,$0,$0#nop
.globlstrcpy
#iisin$s0,
strcpy:
sub$sp,$sp,4#activationrecordisonlyoneword
sw$s0,4($sp)#save$s0
add$s0,$0,$0#i=0;
L1:
add$t1,$a1,$s0#addressofy[i]isin$t1
lb$t2,0($t1)#$t2=y[i]
add$t3,$a0,$s0#addressofx[i]isin$t3
sb$t2,0($t3)#x[i]=y[i]
add$s0,$s0,1#i=i+1
bne$t2,$0,L1#ify[i]!
=0,gotoL1
lw$s0,4($sp)#restore$s0
add$sp,$sp,4#restorethestackpointer
jr$ra#return
Output:
5.Writeaprogramtofindproductof2numbersthataretakefromkeyboard?
Code:
.data
msg1:
.asciiz"Pleaseenteranumber:
"
#CreatingASCIIstringforoutput
msg2:
.asciiz"Theproductis:
"
.globlmain
.text
main:
#Printingtoscreenstring“Pleaseenteranumber:
“
li$v0,4
la$a0,msg1
syscall
#Inputanintegerfromkeyboardintoregister$v0
li$v0,5
syscall
#Movecontentofregister$v0intoregister$t0
move$t0,$v0
#Printingtoscreenstring“Pleaseenteranumber:
“
li$v0,4
la$a0,msg1
syscall
#Inputanintegerfromkeyboardintoregister$v0
li$v0,5
syscall
#Movecontentofregister$v0intoregister$t1
move$t1,$v0
#Multiplying$t0by$t1.Productwillbestoredinregister$LO
mult$t0,$t1
#Movingcontentof$LO(theproduct)intoregister$t2
mflo$t2
#Printingtoscreenstring“Theproductis:
“
li$v0,4
la$a0,msg2
syscall
#Movingcontentof$t2(theproduct)intoregister$a0
move$a0,$t2
#Printingtoscreencontentof$a0
li$v0,1syscall
Output:
6.HowtowriteaprogramFORloopinmips?
Code:
.data
x:
.word42
y:
.word8
sum:
.word0
answer:
.asciiz"Thesumis"
newline:
.asciiz"\n"
.text
main:
#Functionprologue--evenmainhasone
subu$sp,$sp,24#allocatestackspace--defaultof24here
sw$fp,0($sp)#savecaller'sframepointer
sw$ra,4($sp)#savereturnaddress
addiu$fp,$sp,24#setupmain'sframepointer
#Putxinto$s0
la$t0,x
lw$s0,0($t0)
#Putyinto$s1
la$t0,y
lw$s1,0($t0)
#Puttheconstant1into$t1
li$t1,1
add$s2,$zero,$zero#sum=0
add$t0,$zero,$zero#i=0
LoopBegin:
slt$t2,$t0,$s1#isi ? beq$t2,$zero,LoopEnd#branchbelowendofloopifdone add$s2,$s2,$s0#sum=sum+x add$t0,$t0,$t1#i++ jLoopBegin LoopEnd: #Printmessage la$a0,answer li$v0,4 syscall#Printthesum add$a0,$s2,$zero li$v0,1 syscall#Printnewline la$a0,newline li$v0,4 syscall done: #Epilogueformain--restorestack&framepointersandreturn lw$ra,4($sp)#getreturnaddressfromstack lw$fp,0($sp)#restorethecaller'sframepointer addiu$sp,$sp,24#restorethecaller'sstackpointer jr$ra#returntocaller'scode Output: 7.Writeamipsprogramforgivenccode? Code: #implementsacalltoafunctionwith5parameters #intA=109; #voidmain() #{ #intf; #f=parm5leaf(5,-20,13,3,A); #printf("/nf=%d",f); #return; #} #intparm5leaf(intg,inth,inti,intj,intk) #{ #intf1,f2; #f1=(g+h); #f2=(i+j)*k; #returnf1+f2; #} #assumes: #fisin$s0inthemainprogram.Allocatef1andf2as #automaticvariablesonthestack. #allocatethestaticvariablesandmessages .data A: .word10
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MIPS 编程 实例 集锦
![提示](https://static.bdocx.com/images/bang_tan.gif)