-write assembly language programs to: -define a recursive procedure/function and
ID: 3751507 • Letter: #
Question
-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) = (3*n)-5 if n <= 3
= (n-1)*function1(n-1) + function1(n-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 following is a sample output (user input is in bold):
Enter an integer:
8
The solution is: 7842
--------------------------------------------------
Explanation / Answer
Here the function1 is generate the answer based on the nature of input provide by conditional statements
.file 1 ""
.section .mdebug.abi32
.previous
.nan legacy
.module fp=32
.module nooddspreg
.abicalls
.text
.align 2
.globl function1
.set nomips16
.set nomicromips
.ent function1
.type function1, @function
function1:
.frame $fp,48,$31 # vars= 8, regs= 3/0, args= 16, gp= 8
.mask 0xc0010000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-48
sw $31,44($sp)
sw $fp,40($sp)
sw $16,36($sp)
move $fp,$sp
movz $31,$31,$0
.cprestore 16
sw $4,48($fp)
lw $2,48($fp)
nop
slt $2,$2,4
beq $2,$0,$L2
nop
lw $3,48($fp)
nop
move $2,$3
sll $2,$2,1
addu $2,$2,$3
addiu $2,$2,-5
sw $2,24($fp)
lw $2,24($fp)
b $L3
nop
$L2:
lw $2,48($fp)
nop
addiu $16,$2,-1
lw $2,48($fp)
nop
addiu $2,$2,-1
move $4,$2
lw $2,%got(function1)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,function1
1: jalr $25
nop
lw $28,16($fp)
mult $16,$2
mflo $16
lw $2,48($fp)
nop
addiu $2,$2,-2
move $4,$2
lw $2,%got(function1)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,function1
1: jalr $25
nop
lw $28,16($fp)
addu $3,$16,$2
lw $2,48($fp)
nop
subu $2,$3,$2
sw $2,28($fp)
lw $2,28($fp)
$L3:
move $sp,$fp
lw $31,44($sp)
lw $fp,40($sp)
lw $16,36($sp)
addiu $sp,$sp,48
j $31
nop
.set macro
.set reorder
.end function1
.size function1, .-function1
.rdata
.align 2
$LC0:
.ascii "Enter an integer:"
.align 2
$LC1:
.ascii "%d"
.align 2
$LC2:
.ascii "The solution is: %d "
.text
.align 2
.globl main
.set nomips16
.set nomicromips
.ent main
.type main, @function
main:
.frame $fp,40,$31 # vars= 8, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-40
sw $31,36($sp)
sw $fp,32($sp)
move $fp,$sp
.cprestore 16
movz $31,$31,$0
lw $2,%got($LC0)($28)
nop
addiu $4,$2,%lo($LC0)
lw $2,%call16(puts)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
nop
lw $28,16($fp)
addiu $2,$fp,28
move $5,$2
lw $2,%got($LC1)($28)
nop
addiu $4,$2,%lo($LC1)
lw $2,%call16(__isoc99_scanf)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,__isoc99_scanf
1: jalr $25
nop
lw $28,16($fp)
lw $2,28($fp)
nop
move $4,$2
lw $2,%got(function1)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,function1
1: jalr $25
nop
lw $28,16($fp)
sw $2,24($fp)
lw $5,24($fp)
lw $2,%got($LC2)($28)
nop
addiu $4,$2,%lo($LC2)
lw $2,%call16(printf)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,printf
1: jalr $25
nop
lw $28,16($fp)
move $2,$0
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
addiu $sp,$sp,40
j $31
nop
.set macro
.set reorder
.end main
.size main, .-main
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.