PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING
Objectives:
-write assembly language programs to:
-define a recursive procedure/function and call it.
-use syscall operations to display integers and
strings on the console window
-use syscall operations to read integers from the
keyboard.
Assignment Description:
Implement a MIPS assembly language program that defines "main", and "function1" procedures.
The function1 is recursive and should be defined as:
function1(n) = (2*n)+9 if n <= 5
= function1(n-2) + n*function1(n-3) - 2*n otherwise.
The main asks a user to enter an integer for n and calls the function1 by passing the n value, then prints the result. If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it. Name your source code file assignment7.s.
C program that will ask a user to enter an integer, calls the fuction1, and prints the returned value from the function1.
// The function1 is a recursive procedure/function defined by: // function1(n) = (2*n)+9 if n <= 5 // = function1(n-2) + n*function1(n-3) - 2*n otherwise. int function1(int n) { if (n <= 5) { int ans1 = (2*n)+9; return ans1; } else { int ans1 = function1(n-2) + n*function1(n-3) - 2*n; return ans1; } } // The main calls function1 by entering an integer given by a user. void main() { int ans, n; printf("Enter an integer:\n"); // read an integer from user and store it in "n" scanf("%d", &n); ans = function1(n); // print out the solution computed by function 1 printf("The solution is: %d\n", ans); return; }
The following is a sample output (user input is in bold):
Enter an integer:
8
The solution is: 231
ANSWER:
.data
#declare the strings
prompt1: .asciiz "Enter an integer: "
message1: .asciiz "The solution is: "
next_line: .asciiz " "
.text
#############################################
# Procedure main
# Description: The main calls function1 by entering an integer given by a user.
# parameters:
# return value: $v0
# registers to be used: $a0, $v0, and $v1
#############################################
main:
#Print "Enter an integer: "
la $a0, prompt1
li $v0, 4
syscall
#read the integer input from the user
#$v0 = user input
li $v0, 5
syscall
#move the input value to $a0
move $a0, $v0
#Adjust sp/ store ra
addi $sp, $sp, -4
sw $ra, 0($sp)
#call the function
#jump and link function1 and $a0 is passed as parameter
jal function1
#Adjust sp/ load ra
lw $ra, 0($sp)
addi $sp, $sp, 4
#save return value from function1
move $v1, $v0
#Print "The solution is:"
la $a0, message1
li $v0, 4
syscall
#print the results
move $a0, $v1
li $v0, 1
syscall
#print new line
la $a0, next_line
li $v0, 4
syscall
#end program
li $v0, 10
syscall
################################################
# Procedure function1
# Description: The function1 is a recursive procedure defined by:
# function1(n) = (3*n)-5 if n <= 3
# = (n-1)*function1(n-1) + function1(n-2) - n otherwise.
# parameters: $a0
# return value: $v0
# registers to be used:$v1,$s1,$t0,$t4,$v1,$zero
################################################
function1:
#Save $ra, $a0, $s1, and $v1 on the stack
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $v1, 8($sp)
sw $s1, 12($sp)
#check if $a0 <4.If yes, then return 3*n-5
#otherwise go to else
slti $t0, $a0, 4
beq $t0, $zero, else
mul $a0,$a0,3#find 3*n
sub $a0,$a0,5#find 3*n-5
move $v0, $a0 #return 3*n-5
#jump to return label
j return
else:
#store the input value(n) in $v1
move $v1, $a0
#find ($a0 - 1)
sub $a0, $a0, 1
jal function1 #int function1($a0 - 1)
sub $s2, $v1,1 #$s2= n-1
mul $s1,$s2,$v0#$s1=(n-1) * function1 ($a0 - 1)
#find ($a0 - 2)
sub $a0, $a0, 1
#int function1 ($a0 - 2)
jal function1
#$t4 = function1 ($a0 - 2)-n
sub $t4,$v0,$v1
#$v0=(n-1) * function1 ($a0 - 1)+function1 ($a0 - 2)-n
add $v0, $s1,$t4
return:
#load the $ra, $a0, $s1, and $v1 from the stack
lw $s1, 12 ($sp)
lw $v1, 8 ($sp)
lw $a0, 4 ($sp)
lw $ra, 0 ($sp)
addi $sp, $sp, 16
#return to main function
jr $ra
Sample output:
PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs...
INTEL 80x86 ASSEMBLY LANGUAGE CODE Write a windows32 assembly language program that utilizes a recursive procedure. The main (_MainProc) procedure should: accept, from the user, a positive integer. Guard against non-positive integers being entered using a loop. call the sumseries sub-procedure using the cdecl protocol, receive the results of the sub-procedure, and display the results. The sumseries sub-procedure should: recursively find the sum of the series: 1*2 + 2*3 + 3*4 + ... + i*(i+1) (This is an iterative definition....
Translate the following C program to Pep/9 assembly language. It multiplies two integers using a recursive shift-and-add algorithm. mpr stands for multiplier and mcand stands for multiplicand. A recursive integer multiplication algorithm #include <stdio.h> int times(int mpr, int mcand) { if (mpr == 0) { return 0; } else if (mpr % 2 == 1) { return times(mpr / 2, mcand * 2) + mcand; } else { return times(mpr / 2, mcand * 2); } } int main() { ...
the coding language is just the basic c language and here is my first attempt at this problem my problem is that if the user inputs a number that is equal to a number that has been entered previously the code will never end until the user enters a number that is bigger than any other number previously entered any help will be helpful Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program...
Write an assembly language program that corresponds to the following C program: int width; int length; int perim; int main () { scanf ("%d%d", &width, &length); perim = (width + length) * 2; printf ("width = %d\n", width); printf ("length = %d\n\n", width); printf ("perim = %d\n", perim); return 0; }
Write MARIE assembly language programs that do the following: I. Write a program that inputs three integers, a, b, and c, in that order. It computes the following ia-bi-fc+ c The result should be written to output 2. Write a program that inputs integers, s. y, and z. It outputs the difference of the langest and first element entered. You may assume x. y, and z all have different values. So if 8, 12, and 9 are input, the output...
use c++ language, keep it simple i am using code block Exercise #2: Digitise a number Write the function digitiselint, int[]) of type int, which takes an integer N and finds all the digits of that integer and save them in an array. The function then returns the number of digits in N. Write the main() program that reads an integer, calls the function digitisel ), and prints the digits in reverse order. Sample input/output: Enter an integer: 2309456 The...
in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){ sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() { int score, highest; //missing portion of the program return 0; } 1(c). Find the missing portion of the following code, so the...
Assembly Language////Write a program that read in 10 integers from the user. Save the numbers into an array; reverse the array and display the reversed array. .data arrayInt DWORD 10 DUP(?) Your program consists of 4 procedures: 1. main procedure: call procedures getInput, reverseArray, displayArray 2. getInput procedure: prompt user to enter 10 integer numbers, save the numbers into the memory for the arrayInt 3. reverseArray: reverse arrayInt 4. displayArray: display the reversed array
Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() { int number; scanf("%d", &number); if (number % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } return 0; }
How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...