{"id":182340,"date":"2025-01-13T16:49:56","date_gmt":"2025-01-13T16:49:56","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=182340"},"modified":"2025-01-13T16:50:02","modified_gmt":"2025-01-13T16:50:02","slug":"write-a-mips-assembly-program-to-sort-an-array-of-numbers","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/01\/13\/write-a-mips-assembly-program-to-sort-an-array-of-numbers\/","title":{"rendered":"Write a MIPS Assembly program to sort an array of numbers"},"content":{"rendered":"\n<p>Write a MIPS Assembly program to sort an array of numbers. The array should have minimum of 10 numbers and can be initialized as shown in the demo program.<\/p>\n\n\n\n<p>Demo Program:<\/p>\n\n\n\n<p>.data<br>list: .word 0, 2, 6, 9, 13, 21, 23, 24, 50, 9<br>str1: .asciiz &#8220;\\nBefore swap\\n&#8221;<br>str2: .asciiz &#8220;\\nAfter swap:\\n&#8221;<br>space: .asciiz &#8221; &#8220;<\/p>\n\n\n\n<p>.text<br>la $a0, str1<br>li $v0,4<br>syscall<\/p>\n\n\n\n<p>la $t0, list #pointer to list<br>lw $s0, 0($t0)#read list[0] into $s0<br>lw $s1, 4($t0)#read list[1] into $s1<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">print list<\/h1>\n\n\n\n<p>li $v0,1<br>move $a0,$s0<br>syscall<\/p>\n\n\n\n<p>la $a0, space<br>li $v0, 4<br>syscall<\/p>\n\n\n\n<p>li $v0,1<br>move $a0,$s1<br>syscall<\/p>\n\n\n\n<p>bgt $s0, $s1, label #if list[0] &gt; list [1] dont swap exit<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">else swap $s5 is a temporary variable<\/h1>\n\n\n\n<p>move $s5, $s0<br>move $s0, $s1<br>move $s1, $s5<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">update the list after swapping<\/h1>\n\n\n\n<p>sw $s0, 0($t0)<br>sw $s1, 4($t0)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">print the updated list now<\/h1>\n\n\n\n<p>la $a0, str2<br>li $v0,4<br>syscall<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">print list[0] and list[1]<\/h1>\n\n\n\n<p>la $t0, list #pointer to list<br>lw $s0, 0($t0)#read list[0] into $s0<br>lw $s1, 4($t0)#read list[1] into $s1<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">print list<\/h1>\n\n\n\n<p>li $v0,1<br>move $a0,$s0<br>syscall<\/p>\n\n\n\n<p>la $a0, space<br>li $v0, 4<br>syscall<\/p>\n\n\n\n<p>li $v0,1<br>move $a0,$s1<br>syscall<\/p>\n\n\n\n<p>label:<br>li $v0, 10<br>syscall<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>To implement a sorting algorithm in MIPS Assembly, we will modify your demo program and use <strong>Bubble Sort<\/strong>, which is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MIPS Assembly Code for Bubble Sort:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>.data\nlist: .word 0, 2, 6, 9, 13, 21, 23, 24, 50, 9\nstr1: .asciiz \"\\nBefore swap\\n\"\nstr2: .asciiz \"\\nAfter swap:\\n\"\nspace: .asciiz \" \"\n\n.text\n.globl main\nmain:\n    # Print \"Before swap\"\n    la $a0, str1\n    li $v0, 4\n    syscall\n\n    # Load the address of the list into $t0\n    la $t0, list\n\n    # Print the list before sorting\n    li $t1, 0         # Index i = 0\nprint_list_before:\n    lw $a0, 0($t0)    # Load list&#91;i] into $a0\n    li $v0, 1         # Print integer syscall\n    syscall\n\n    la $a0, space\n    li $v0, 4         # Print space\n    syscall\n\n    addi $t1, $t1, 1  # Increment index\n    addi $t0, $t0, 4  # Move to next word in the list\n    blt $t1, 10, print_list_before  # Repeat for 10 elements\n\n    # Bubble Sort Algorithm\n    la $t0, list      # Reset pointer to start of list\n    li $t2, 10        # Set the number of elements to sort (10)\n    li $t3, 0         # Set a flag to track if swap happened\n\nouter_loop:\n    li $t1, 0         # Reset index for inner loop\n    li $t3, 0         # Reset swap flag to 0\n\ninner_loop:\n    lw $s0, 0($t0)    # Load list&#91;i] into $s0\n    lw $s1, 4($t0)    # Load list&#91;i+1] into $s1\n\n    # Compare list&#91;i] and list&#91;i+1]\n    bgt $s0, $s1, swap # If list&#91;i] &gt; list&#91;i+1], swap\n\n    # No swap, move to next pair\n    addi $t0, $t0, 4  # Increment pointer to next pair\n    addi $t1, $t1, 1  # Increment index\n    blt $t1, $t2, inner_loop  # Continue inner loop\n\n    # If no swaps occurred, sorting is done\n    beqz $t3, end_sort\n\n    # Reset pointer to start of list for next iteration\n    la $t0, list\n    sub $t2, $t2, 1   # Decrease number of elements to sort\n    j outer_loop\n\nswap:\n    # Swap the elements\n    move $s5, $s0     # Save list&#91;i] in $s5\n    move $s0, $s1     # list&#91;i] = list&#91;i+1]\n    move $s1, $s5     # list&#91;i+1] = $s5\n\n    # Store swapped values back in list\n    sw $s0, 0($t0)\n    sw $s1, 4($t0)\n\n    # Set swap flag to 1\n    li $t3, 1\n\n    # Move to next pair\n    addi $t0, $t0, 4\n    addi $t1, $t1, 1\n    blt $t1, $t2, inner_loop\n\nend_sort:\n    # Print \"After swap\"\n    la $a0, str2\n    li $v0, 4\n    syscall\n\n    # Print the sorted list\n    li $t1, 0         # Reset index for printing\n    la $t0, list      # Reset pointer to start of list\nprint_list_after:\n    lw $a0, 0($t0)    # Load list&#91;i] into $a0\n    li $v0, 1         # Print integer syscall\n    syscall\n\n    la $a0, space\n    li $v0, 4         # Print space\n    syscall\n\n    addi $t1, $t1, 1  # Increment index\n    addi $t0, $t0, 4  # Move to next word in the list\n    blt $t1, 10, print_list_after  # Repeat for 10 elements\n\n    # Exit the program\n    li $v0, 10\n    syscall<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization and Setup:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The list of numbers is initialized in the <code>.data<\/code> section, along with the strings for the before and after messages.<\/li>\n\n\n\n<li>Registers are initialized: <code>$t0<\/code> holds the address of the list, <code>$t1<\/code> is used for the index, and <code>$s0<\/code>, <code>$s1<\/code> for comparing adjacent elements.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Printing the List Before Sorting:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program prints the &#8220;Before swap&#8221; message and then iterates through the list to print each number separated by spaces.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Bubble Sort Algorithm:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The outer loop runs through the entire list, decreasing the range of the list to be sorted after each iteration (as the largest elements &#8220;bubble&#8221; to the end of the list).<\/li>\n\n\n\n<li>The inner loop compares adjacent elements and swaps them if they are out of order.<\/li>\n\n\n\n<li>If a swap is made, the <code>swap<\/code> label is executed, which uses a temporary register <code>$s5<\/code> to store one of the elements during the swap. The swap flag (<code>$t3<\/code>) is set to 1 if any swap occurs.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Printing the List After Sorting:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>After sorting, the program prints the &#8220;After swap&#8221; message and then iterates through the sorted list to print the numbers.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Program Exit:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program finishes by calling the exit system call (<code>li $v0, 10<\/code>).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n\n<p>The program will print the list of numbers before and after sorting. The list will be sorted in ascending order using the bubble sort algorithm.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a MIPS Assembly program to sort an array of numbers. The array should have minimum of 10 numbers and can be initialized as shown in the demo program. Demo Program: .datalist: .word 0, 2, 6, 9, 13, 21, 23, 24, 50, 9str1: .asciiz &#8220;\\nBefore swap\\n&#8221;str2: .asciiz &#8220;\\nAfter swap:\\n&#8221;space: .asciiz &#8221; &#8220; .textla $a0, str1li [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-182340","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/182340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=182340"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/182340\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=182340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=182340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=182340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}