MIPS编程实例集锦(1).docx_第1页
MIPS编程实例集锦(1).docx_第2页
MIPS编程实例集锦(1).docx_第3页
MIPS编程实例集锦(1).docx_第4页
MIPS编程实例集锦(1).docx_第5页
已阅读5页,还剩58页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PROBLEMS IN MIPS PROGRAMMING How to write a program to add all positive numbers once & all negative numbers once? Write a program to display the prime numbers between 1to 1000? Program to write Fibonacci using recursion? Write a program to swap the 2 strings? Write a program to reverse of given string? Write a program to multiply 2 numbers ( eg 128*256) ? Write a program to convert given number into other base ? ( eg: given number in decimal converting to base like 2 it is binary so it converts decimal to binary ) Write a program to convert “COMPUTER ORGANISATION” to lower case? Write program to bubble sort of the given numbers? Write a program to comparing strings whether or not ? Write a program convert lowercase letters to uppercase letters? Write a program for multiplication table? Write a program to find given number is even or odd? Write a program for palindrome number? Writing program for raising power? Write a program for sum of 3 integers?1) How to write a program to add all positive numbers once & all negative numbers once? Code :.dataarray: .word -4, 5, 8, -1msg1: .asciiz n The sum of the positive values = msg2: .asciiz n The sum of the negative values = .globl main.textmain:li $v0, 4 # system call code for print_strla $a0, msg1 # load address of msg1. into $a0syscall # print the stringla $a0, array # Initialize address Parameterli $a1, 4 # Initialize length Parameterjal sum # Call summove $a0, $v0 # move value to be printed to $a0li $v0, 1 # system call code for print_intsyscall # print sum of Pos:li $v0, 4 # system call code for print_strla $a0, msg2 # load address of msg2. into $a0syscall # print the stringli $v0, 1 # system call code for print_intmove $a0, $v1 # move value to be printed to $a0syscall # print sum of negli $v0, 10 # terminate program run andsyscall # return control to systemsum:li $v0, 0li $v1, 0 # Initialize v0 and v1 to zeroloop:blez $a1, retzz # If (a1 0 GOTO recurs (THE ARGUMENT n IS LOCATED AT 4($fp)lw$a0, 4($fp)bgt$a0, 1, recurs # OTHERWISE RETURN 1li$v0, 1breturnrecurs:# PUSH n-1 AS THE ARGUMENT TO factlw$a0, 4($fp)subu$a0, $a0, 1sw$a0, ($sp)subu$sp, $sp, 4 # LOAD STATIC LINKlw$v0, -8($fp)jalfact # CALL fact# MULTIPLY THE RESULT OF fact(n-1) BY nlw$a0, 4($fp)mul$v0, $v0, $a0return:# RETURN FROM fact (RESTORE REGISTERS AND POP FRAME)lw$ra, -4($fp)move$sp, $fplw$fp, ($sp)# RESTORE OLD FRAME POINTERjr$ra.text# main MUST BE GLOBAL.globlmainmain:# ALLOCATE A FRAME FOR THE DYNAMIC LINK, RETURN ADDRESS, STATIC LINK, AND FOR THE INTEGERS i AND res (TOTAL: 5*4 = 20 BYTES)sw$fp, ($sp)# PUSH OLD FRAME POINTER (DYNAMIC LINK)move$fp, $sp# FRAMEPOINTER NOW POINTS TO THE TOP OF STACK subu$sp, $sp, 20# ALLOCATE TWENTY BYTES IN THE STACK# SAVE RETURN ADDRESS IN FRAME sw$ra, -4($fp) # SAVE STATIC LINK IN FRAME sw$v0, -8($fp) loop: # PRINT prompt.dataprompt:.asciizEnter a Number? .text li$v0, 4la$a0, promptsyscallli$v0, 5syscallsw$v0, -12($fp)# IF i=j break - $t1 0) : msg2: .asciiz Please insert the number system B you want to convert to (2=B=10): #Above sting must be in one linemsg3: .asciiz nResult : .text.globl mainmain:addi $s0,$zero,2addi $s1,$zero,10getA:li $v0,4la $a0,msg1syscallli $v0,5syscallblt $v0,$zero,getAmove $t0,$v0getB:li $v0,4la $a0,msg2syscallli $v0,5syscallblt $v0,$s0,getBbgt $v0,$s1,getBadd $t1,$zero,$v0li $v0,4la $a0,msg3syscalladd $a0,$zero,$t0add $a1,$zero,$t1jal convertli $v0,10syscallconvert:#a0=A#a1=Baddi $sp,$sp,-16sw $s3,12($sp) #counter,used to know#how many times we will pop from stacksw $s0,8($sp) #Asw $s1,4($sp) #Bsw $ra,0($sp)add $s0,$zero,$a0add $s1,$zero,$a1beqz $s0,enddiv $t4,$s0,$s1 #t4=A/Brem $t3,$s0,$s1 #t3=A%Badd $sp,$sp,-4sw $t3,0($sp) #save t3add $a0,$zero,$t4 #pass A/Badd $a1,$zero,$s1 #pass Baddi $s3,$s3,1jal convert #call convertend:lw $ra,0($sp)lw $s1,4($sp)lw $s0,8($sp)lw $s3,12($sp)beqz $s3,donelw $a0,16($sp)li $v0,1syscalldone: addi $sp,$sp,20jr $ra #returnOutput: 9) Write a program to convert “COMPUTER ORGANISATION” to lower case? Code :.text.globl _startmain: #execution starts herela $t1, strnextCh: lb $t2, ($t1)beqz $t2, strEndaddi $t4, $zero, 0x61sub $t3, $t2, $t4bgez $t3, l1add $t2, $t2, 32sb $t2, ($t1)l1: add $t1, $t1, 1j nextChstrEnd: la $a0, ansli $v0, 4syscallla $a0, strli $v0,4syscallla $a0, endlli $v0, 4syscallli $v0, 10syscall.datastr: .asciiz COMPUTER ORGANISATION ans: .asciiz lowercase string =endl: .asciiz nOutput :10) Write program to bubble sort the given numbers? .data.align 4Table: .space 24msg1: .asciiz Please insert an integer: msg2: .asciiz msg3: .asciiz nVector contents: .text.globl mainmain:addi $s0,$zero,5addi $t0,$zero,0in:li $v0,4la $a0,msg1syscallli $v0,5syscalladd $t1,$t0,$zerosll $t1,$t0,2add $t3,$v0,$zerosw $t3,Table ( $t1 )addi $t0,$t0,1slt $t1,$s0,$t0beq $t1,$zero,inla $a0,Tableaddi $a1,$s0,1 #a1=6#call buble_sortjal buble_sort#print tableli $v0,4la $a0,msg3syscallla $t0,Table#s0=5add $t1,$zero,$zeroprinttable:lw $a0,0($t0)li $v0,1syscallli $v0,4la $a0,msg2syscalladdi $t0,$t0,4addi $t1,$t1,1slt $t2,$s0,$t1beq $t2,$zero,printtableli $v0,10syscallbuble_sort:#a0=address of table#a1=sizeof tableadd $t0,$zero,$zero #counter1( i )=0loop1:addi $t0,$t0,1 #i+bgt $t0,$a1,endloop1 #if t0 a1 break;add $t1,$a1,$zero #counter2=size=6loop2:bge $t0,$t1,loop1 #j = i#slt $t3,$t1,$t0#bne $t3,$zero,loop1addi $t1,$t1,-1 #j-mul $t4,$t1,4 #t4+a0=tablejaddi $t3,$t4,-4 #t3+a0=tablej-1add $t7,$t4,$a0 #t7=tablejadd $t8,$t3,$a0 #t8=tablej-1lw $t5,0($t7)lw $t6,0($t8)bgt $t5,$t6,loop2#switch t5,t6sw $t5,0($t8)sw $t6,0($t7)j loop2endloop1:jr $raOutput:11) Write a program to comparing strings whether or not ?.datamsg1:.asciiz Please insert text (max 20 characters): msg2:.asciiz nNOT SAMEmsg3:.asciiz nSAMEstr1: .space 20str2: .space 20.text.globl mainmain:addi $v0,4la $a0,msg1syscallli $v0,8la $a0,str1addi $a1,$zero,20syscall #got string 1li $v0,4la $a0,msg1syscallli $v0,8la $a0,str2addi $a1,$zero,20syscall #got string 2la $a0,str1 #pass address of str1la $a1,str2 #pass address of str2jal strcmp #call strcmpbeq $v0,$zero,ok #check resultli $v0,4la $a0,msg2syscallj exitok:li $v0,4la $a0,msg3syscallexit:li $v0,10syscallstrcmp:add $t0,$zero,$zeroadd $t1,$zero,$a0add $t2,$zero,$a1loop:lb $t3($t1) #load a byte from each stringlb $t4($t2)beqz $t3,checkt2 #str1 endbeqz $t4,missmatchslt $t5,$t3,$t4 #compare two bytesbnez $t5,missmatchaddi $t1,$t1,1 #t1 points to the next byte of str1addi $t2,$t2,1j loopmissmatch: addi $v0,$zero,1j endfunctioncheckt2:bnez $t4,missmatchadd $v0,$zero,$zeroendfunction:jr $raOutput :12) Write a program convert lowercase letters to uppercase letters?.dataname_prompt:.asciizPlease type your name: out_msg:.asciizYour name in capitals is: in_name:.space 31# space for input string.text.globl mainmain: la $a0,name_prompt# print prompt string li $v0,4 syscall la $a0,in_name# read the input string li $a1,31# at most 30 chars + 1 null char li $v0,8 syscall la $a0,out_msg # write output message li $v0,4 syscall la $t0,in_nameloop: lb $t1,($t0) beqz $t1,exit_loop # if NULL, we are done blt $t1,a,no_change bgt $t1,z,no_change addiu $t1,$t1,-32 # convert to uppercase: A-a=-32 sb $t1,($t0)no_change: addiu $t0,$t0,1 # increment pointer j loopexit_loop: la $a0,in_name # output converted string li $v0,4 syscall li $v0,10 # exit syscallOutput:13) Write a program for multiplication table? .text main: # read A into $t0, B into $t1. li $v0, 5 # syscall 5 = read_int syscall move $t0, $v0 # A = integer just read li $v0, 5 # syscall 5 = read_int syscall move $t1, $v0 # B = integer just read blez $t1, exit # if B = B, its a palindrome. lb $t3, ($t1) # load the byte at addr A into $t3, lb $t4, ($t2) # load the byte at addr B into $t4. bne $t3, $t4, not_palin # if $t3 != $t4, not a palindrome. # Otherwise, addu $t1, $t1, 1 # increment A, subu $t2, $t2,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论