.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 basic code
mips1
Friday, July 15, 2016
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
. 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
Tuesday, June 21, 2016
Mips Factorial calculation code
.data
msg1: .asciiz "Enter the integer number you wish\n"
msg2: .asciiz "The factorial is: "
.text
.globl main
main:
# Printing Message1:
la $a0, msg1 #load address of msg1
li $v0, 4 # print message1
syscall
# READING INTEGER FROM USER FOR MESSAGE -1
li $v0, 5 # read the integer from the user
syscall
move $s0, $v0, # move from $v0 t the $t0
addi $sp, $sp -12 #make room for $ra and $fp on the stack
sw $ra, 8($sp) #push $ra
sw $s0, 0($sp) #push $s0
# NUMBER TO FUNCTION CALL
jal factorial # do multiply, and result in $v0
lw $t0, 4($sp) #restore (pop) $to
lw $ra, 8($sp) #restore (pop) $ra
addi $sp, $sp, 12 # adjust $sp
# Printing Message2:
la $a0, msg2 #load address of msg2 into $a0
li $v0, 4 # printing the string from the message 2
syscall
move $a0, $t0 #moving the value from $t0 to the $a0
li $v0, 1 # this means that it is printing the integer number value
syscall
li $v0, 10 # to print out the integer
syscall
factorial:
lw $s0, 0($sp)
beq $s0, 0, finishTheProgram
addi $s0, $s0, -1
# calling factorial funtion
addi $sp, $sp, -12 # making the space
sw $s0, 0($sp) # pushing into $s0
sw $ra, 8($sp) #pushing into $ra
jal factorial
lw $s1, 4($sp) #loading word into the $s1
lw $ra, 8($sp) # loading word into $ra
addiu $sp, $sp, 12
lw $s0, 0($sp) #pushing inside the $s0
mul $s2, $s1, $s0 #multiplying $s1 and $s0 and puting inside the $s2
sw $s2, 4($sp) #pushing into the $s2
jr $ra
finishTheProgram:
li $s0, 1
sw $s0, 4($sp)
jr $ra
msg1: .asciiz "Enter the integer number you wish\n"
msg2: .asciiz "The factorial is: "
.text
.globl main
main:
# Printing Message1:
la $a0, msg1 #load address of msg1
li $v0, 4 # print message1
syscall
# READING INTEGER FROM USER FOR MESSAGE -1
li $v0, 5 # read the integer from the user
syscall
move $s0, $v0, # move from $v0 t the $t0
addi $sp, $sp -12 #make room for $ra and $fp on the stack
sw $ra, 8($sp) #push $ra
sw $s0, 0($sp) #push $s0
# NUMBER TO FUNCTION CALL
jal factorial # do multiply, and result in $v0
lw $t0, 4($sp) #restore (pop) $to
lw $ra, 8($sp) #restore (pop) $ra
addi $sp, $sp, 12 # adjust $sp
# Printing Message2:
la $a0, msg2 #load address of msg2 into $a0
li $v0, 4 # printing the string from the message 2
syscall
move $a0, $t0 #moving the value from $t0 to the $a0
li $v0, 1 # this means that it is printing the integer number value
syscall
li $v0, 10 # to print out the integer
syscall
factorial:
lw $s0, 0($sp)
beq $s0, 0, finishTheProgram
addi $s0, $s0, -1
# calling factorial funtion
addi $sp, $sp, -12 # making the space
sw $s0, 0($sp) # pushing into $s0
sw $ra, 8($sp) #pushing into $ra
jal factorial
lw $s1, 4($sp) #loading word into the $s1
lw $ra, 8($sp) # loading word into $ra
addiu $sp, $sp, 12
lw $s0, 0($sp) #pushing inside the $s0
mul $s2, $s1, $s0 #multiplying $s1 and $s0 and puting inside the $s2
sw $s2, 4($sp) #pushing into the $s2
jr $ra
finishTheProgram:
li $s0, 1
sw $s0, 4($sp)
jr $ra
Subscribe to:
Posts (Atom)