How to use if/else or branches statement in Assembly language in MIPS on Mars(Program)
Use of Branch Statement in MIPS
Task
1:Write a program to assign grades (A, B, C) based on marks
obtained by a student.
I.
if the percentage is above 90, assign
grade A
II.
if the percentage is above 75, assign
grade B
III.
if the percentage is above 65, assign
grade C
.data
total:.asciiz "Enter the total Marks:"
obtain:.asciiz "Enter the obtained marks:"
grade:.asciiz "Your marks are:"
A:.asciiz "\nA grade\n"
B:.asciiz "\nB grade\n"
C:.asciiz "\nC grade\n"
D:.asciiz "\nLess than 65%\n"
num1:.word 90
num2:.word 75
num3:.word 65
num4:.word 100
.text
lw $t5,num1
lw $t6,num2
lw $t7,num3
lw $t8,num4
main:
li $v0,4
la $a0,total
syscall
li $v0,5
syscall
move $t1,$v0
li $v0,4
la $a0,obtain
syscall
li $v0,5
syscall
move $t2,$v0
li $v0,4
la $a0,grade
syscall
mul $t3,$t2,$t8
div $t4,$t3,$t1
li $v0,1
move $a0,$t4
syscall
bge $t4,$t5,gradeA
bge $t4,$t6,gradeB
bge $t4,$t7,gradeC
blt $t4,$t7,gradeD
syscall
li $v0,10
syscall
gradeA:
li $v0,4
la $a0,A
syscall
li $v0,10
syscall
gradeB:
li $v0,4
la $a0,B
syscall
li $v0,10
syscall
gradeC:
li $v0,4
la $a0,C
syscall
li $v0,10
syscall
gradeD:
li $v0,4
la $a0,D
syscall
li $v0,10
syscall
Task 2: Write a program to take an input integer from user and check whether an integer is positive or negative
.data
input:.asciiz "Enter an integer:"
A:.asciiz "\ninteger is positive\n"
B:.asciiz "\ninteger is negative\n"
.text
addi $t0,$zero,0
main:
li $v0,4
la $a0,input
syscall
li $v0,5
syscall
move $t1,$v0
blt $t1,$t0,negative
bge $t1,$t0,positive
syscall
li $v0,10
syscall
positive:
li $v0,4
la $a0,A
syscall
li $v0,10
syscall
negative:
li $v0,4
la $a0,B
syscall
li $v0,10
syscall
Task 3: Write a
program to take an input integer from user and check if an integer is even or
odd or neither
.data
input:.asciiz "Enter an integer:"
A:.asciiz "\ninteger is odd\n"
B:.asciiz "\ninteger is even\n"
num1:.word 2
.text
addi $t0,$zero,0
lw $t3,num1
main:
li $v0,4
la $a0,input
syscall
li $v0,5
syscall
move $t1,$v0
rem $t2,$t1,$t3
beq $t2,$t0,even
bne $t2,$t0,odd
syscall
li $v0,10
syscall
odd:
li $v0,4
la $a0,A
syscall
li $v0,10
syscall
even:
li $v0,4
la $a0,B
syscall
li $v0,10
syscall
Task 4: Write a program for a simple MIPS calculator which have different functions for different operations. Make a menu base selection screen using Branches statement. Ever operation should have its own function.
.data
State:.asciiz "Enter 1 to add\nEnter 2 to subtract\nEnter 3 to Multiply\nEnter 4 to divide:"
input1:.asciiz "Enter num1:"
input2:.asciiz "Enter num2:"
num1:.word 1
num2:.word 2
num3:.word 3
num4:.word 4
.text
lw $t5,num1
lw $t6,num2
lw $t7,num3
lw $t4,num4
main:
li $v0,4
la $a0,State
syscall
li $v0,5
syscall
move $t1,$v0
beq $t1,$t5,addition
beq $t1,$t6,subtraction
beq $t1,$t7,multiplication
beq $t1,$t8,division
syscall
li $v0,10
syscall
addition:
li $v0,4
la $a0,input1
syscall
li $v0,5
syscall
move $t2,$v0
li $v0,4
la $a0,input2
syscall
li $v0,5
syscall
move $t3,$v0
add $t4,$t2,$t3
li $v0,1
move $a0,$t4
syscall
li $v0,10
syscall
subtraction:
li $v0,4
la $a0,input1
syscall
li $v0,5
syscall
move $t2,$v0
li $v0,4
la $a0,input2
syscall
li $v0,5
syscall
move $t3,$v0
sub $t4,$t2,$t3
li $v0,1
move $a0,$t4
syscall
li $v0,10
syscall
multiplication:
li $v0,4
la $a0,input1
syscall
li $v0,5
syscall
move $t2,$v0
li $v0,4
la $a0,input2
syscall
li $v0,5
syscall
move $t3,$v0
mul $t4,$t2,$t3
li $v0,1
move $a0,$t4
syscall
li $v0,10
syscall
division:
li $v0,4
la $a0,input1
syscall
li $v0,5
syscall
move $t2,$v0
li $v0,4
la $a0,input2
syscall
li $v0,5
syscall
move $t3,$v0
div $t4,$t2,$t3
li $v0,1
move $a0,$t4
syscall
li $v0,10
syscall
Comments
Post a Comment