Description and Requirements Write and test a friendly MIPS assembly program tha
ID: 3601620 • Letter: D
Question
Description and Requirements
Write and test a friendly MIPS assembly program that calculates and prints Fibonacci numbers. Specifically, your program should be divided into the following four sections, which must perform several specific tasks.
Section 1: Introduction
• Print an introduction that includes: your name, a title, and (optionally) a description of the program.
• Prompt the user to enter their name and read in the string input. (You should reserve 64 bytes in data memory for this string.)
• Print a greeting that includes the user’s name.
Section 2: Get and validate n
• Prompt the user to enter a number between 1 and 47 (inclusive); read in the input (we’ll call this n).
• Check that n is within the range. If not, print an error message and prompt the user again.
• Repeat until the user enters a valid number.
Section 3: Calculate and print the first n Fibonacci numbers
• Calculate and print each of the Fibonacci numbers up to and including the nth.
The first several numbers should be: 0, 1, 1, 2, 3, 5, 8, 13, 21, . . . (Note that you should start at 0!)
• Numbers should be printed exactly five per line, with at least four spaces between numbers on a line.
• Note that you do not need to store the Fibonacci numbers in memory. You may just print them as they are computed.
Section 4: Conclusion
• Print a farewell message that again refers to the user’s name.
• Use the exit system call to end your program.
Extra credit: (Small amount. Standard requirements must be fulfilled first!)
1. In Section 3, print the Fibonacci numbers in aligned columns. Note that you must still have at least four spaces between each number in a line.
Example execution Below is an example execution that illustrates what your program should do:
Fibonacci Numbers by, John Smith
What is your name?: Jonathan Bisnett
Hi, Jonathan Bisnett
How many Fibonacci numbers should I display?
Enter an integer in the range [1..47]: -7
That number was out of range, try again.
How many Fibonacci numbers should I display?
Enter an integer in the range [1..47]: 50
That number was out of range, try again.
How many Fibonacci numbers should I display?
Enter an integer in the range [1..47]: 22
0 1 1 2 3
5 8 13 21 34
55 89 144 233 377
610 987 1597 2584 4181
6765 10946
Explanation / Answer
Please find the below assembly code
.file 1 ""
.section .mdebug.abi32
.previous
.nan legacy
.module fp=32
.module nooddspreg
.abicalls
.rdata
.align 2
$LC0:
.ascii "First 10 fibonacci numbers:"
.align 2
$LC1:
.ascii "%d, "
.align 2
$LC2:
.ascii ""
.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
sw $4,40($fp)
sw $5,44($fp)
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)
sw $0,24($fp)
b $L2
nop
$L3:
lw $4,24($fp)
lw $2,%got(fibonacci)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,fibonacci
1: jalr $25
nop
lw $28,16($fp)
sw $2,28($fp)
lw $5,28($fp)
lw $2,%got($LC1)($28)
nop
addiu $4,$2,%lo($LC1)
lw $2,%call16(printf)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,printf
1: jalr $25
nop
lw $28,16($fp)
lw $2,24($fp)
nop
addiu $2,$2,1
sw $2,24($fp)
$L2:
lw $2,24($fp)
nop
slt $2,$2,10
bne $2,$0,$L3
nop
lw $2,%got($LC2)($28)
nop
addiu $4,$2,%lo($LC2)
lw $2,%call16(puts)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,puts
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
.align 2
.globl fibonacci
.set nomips16
.set nomicromips
.ent fibonacci
.type fibonacci, @function
fibonacci:
.frame $fp,40,$31 # vars= 0, regs= 3/0, args= 16, gp= 8
.mask 0xc0010000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-40
sw $31,36($sp)
sw $fp,32($sp)
sw $16,28($sp)
move $fp,$sp
movz $31,$31,$0
.cprestore 16
sw $4,40($fp)
lw $2,40($fp)
nop
slt $2,$2,2
beq $2,$0,$L6
nop
lw $2,40($fp)
b $L7
nop
$L6:
lw $2,40($fp)
nop
addiu $2,$2,-1
move $4,$2
lw $2,%got(fibonacci)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,fibonacci
1: jalr $25
nop
lw $28,16($fp)
move $16,$2
lw $2,40($fp)
nop
addiu $2,$2,-2
move $4,$2
lw $2,%got(fibonacci)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,fibonacci
1: jalr $25
nop
lw $28,16($fp)
addu $2,$16,$2
$L7:
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
lw $16,28($sp)
addiu $sp,$sp,40
j $31
nop
.set macro
.set reorder
.end fibonacci
.size fibonacci, .-fibonacci
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609"