C code to MIPS code
These days I am taking a Computer architecture course. The textbook and the lecture use MIPS to explain computer architecture. In this article, I will rewrite some C codes to MIPS codes. I am going to explain the MIPS code line by line. This will not only help to understand MIPS instructions themselves but also understand the computer architecture and procedure call or function call.
- Factorial example
Let’s say the argument n is in $a0 and the result in $v0. The MIPS instruction of this C code would be following:
2. String copy example
Let’s say addresses of x and y is in $a0 and $a1. Plus, the local variable i will be stored in $s0. The MIPS instruction of this C code would be following:
3. Sort procedure example
There are many sorting algorithms. In this example, we are using a bubble sort algorithm.
Let’s say v is in $a0, n is in $a1, i is in $s0, j is in $s1, the MIPS instruction of ‘sort’ would be following: