Use of loops(for/while/do while) in Assembly language in MIPS on Mars(Programs)
Use of loops
#Task 1 :
#Write a MIPS program to print all natural numbers from 1 to n. using while loop
.data msg1: .asciiz "Natural Numbers are \n" num1:.word 1 num2:.word 100 .text lw $t1,num1 lw $t2,num2 addi $t3,$t3,0 li $v0,4 la $a0,msg1 syscall while: blt $t3,$t2,print li $v0,10 syscall print: add $t3,$t3,$t1 li $v0,1 move $a0,$t3 syscall j while#Task 2 :
#Write a MIPS program to print all natural numbers
#in reverse (from n to 1). using while loop.
.data msg1: .asciiz "Natural Numbers are \n" num1:.word 1 num2:.word 0 .text lw $t1,num1 lw $t2,num2 addi $t3,$t3,100 li $v0,4 la $a0,msg1 syscall while: bgt $t3,$t2,print li $v0,10 syscall print: sub $t3,$t3,$t1 li $v0,1 move $a0,$t3 syscall j while#Task 3 :
#Write a MIPS program to print all odd number between 1 to 100.
#using do while loop..data msg1: .asciiz "odd Numbers are \n" msg2:.asciiz"This is even number\n" msg3:.asciiz"This is odd number.\n" num1:.word 1 num2:.word 2 .text lw $t1,num1 lw $t2,num2 addi $t3,$t3,0 addi $t4,$t4,0 addi $t5,$t5,100 li $v0,4 la $a0,msg1 syscall do: add $t3,$t3,$t1 rem $t6,$t3,$t2 beq $t3,$t5,while beq $t6,$t4,even bgt $t6,$t4,odd syscall while: beq $t3,$t5,exit syscall j do exit: li $v0,10 syscall even: li $v0,4 la $a0,msg2 syscall j do odd: li $v0,4 la $a0,msg3 syscall j do#Task 4 :
#Write a MIPS program to find sum
.data num1:.word 1 num2:.word 2 .text lw $t1,num1 lw $t2,num2 addi $t3,$t3,0 addi $t4,$t4,0 addi $t7,$t7,0 li $v0,5 syscall move $t0,$v0 while: beq $t3,$t0,exit add $t3,$t3,$t1 rem $t6,$t3,$t2 beq $t6,$t4,evenSum j while syscall evenSum: add $t7,$t7,$t3 j while syscall exit: li $v0,1 move $a0,$t7 syscall li $v0,10 syscall
#of all even numbers between 1 to n.#Task 5 :
#Write a MIPS program to
.data num1:.word 1 num2:.word 2 .text lw $t1,num1 lw $t2,num2 addi $t3,$t3,0 addi $t4,$t4,10 addi $t7,$t7,1 li $v0,5 syscall move $t0,$v0 while: beq $t3,$t4,exit add $t3,$t3,$t1 mul $t7,$t0,$t3 li $v0,1 move $a0,$t7 syscall j while syscall exit: li $v0,10 syscall
#print multiplication table of any number.
Comments
Post a Comment