mips1

Friday, July 15, 2016

Saving register to the stack Mips code

.data
    newLine:  .asciiz "\n"

.text
    main:
             addi $s0, $0, 10
       jal increaseMyregister
   #prints a new line
  li $v0, 4
  la$a0, newLine
syscall

  #printing the value
         li $v0, 1            #reading the integer
        move $a0, $s0   #moving the value from $s0 to $a0
      syscall

li $v0, 10              #system call finish the program
syscall


#funtion
increaseMyregister:
         addi $sp, $sp, -4     #making space for stack and string 4 byte or allocation place
        sw $s0, 0($sp)         #save the value of $s0 in the first place into the stack pointer
   

       addi $s0, $s0, 50       #now 30 will add with 10 which is on the top

#print new value in function
li $v0, 1
move $a0, $s0
syscall

lw $s0, 0($sp)
addi $sp, $sp, 4




jr $ra

Mips Function Argument and Return Values

Hi there this tutorial site will teach how to make an argument and return the value. This is kine of very interesting program that allow you to make an argument and then allow you to return it. 


.data 
.text
        main:
                     addi $a1, $0, 1500         #add 1500 + 0 = store in $a1
                     addi $a2, $0, 200           #add 200 + 0 = store in $a2
jal totalAdd
     li $v0, 1
    addi $a0, $v1, 0
   syscall 

   #The following code tell the system that program is done
    li $v0, 10
    syscall 

totalAdd:
   add $v1, $a1, $a2             # $a1 + $a2 = $v1
    jr $ra 




Introduction to Mips Function - killer tutorial site

Let me tell you something before we can start. The function also called the procedure so please don't confuse about it.

. data
      message: .asciiz "Hi there, My name is krish.\n"
.text
    main:
      jal displayMessage     # this is the target

# Tell system to finish program.
li $vo, 10
syscall            




displayMessage:         #this is name of function

li $v0, 4          # This line
la $a0, message   #This line will print the message
syscall

jr $ra        #jump register means go back to main