Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*MUST BE IN MIPS LANGUAGE* Write an assembly program to print out a series of nu

ID: 3662612 • Letter: #

Question

*MUST BE IN MIPS LANGUAGE* Write an assembly program to print out a series of numbers. First input a number N for the size of the list. This should not be more than 20. Next, you need to calculate F(i) for each number 0..N. This means you should print out F(0), F(1), …, F(N-1) for a total of N numbers including the zeroth number. Your series is defined as follows: F(0) = 1

F(1) = 3

F(i) = F(i-1) + F(i-2) + 1

the program should exit if the value entered for N is 0, otherwise the program should run again. So the following would be a sample program output:

Please enter a value for N: 7 1 3 5 9 15 25 41

Please enter a value for N: 6 1 3 5 9 15 25

Please enter a value for N: 0 Done!

NOTE: You must use the syscall convention. For instance to print a string: la $a0,stringaddr syscall $print_string

Explanation / Answer

The C++ code will bw:

#include<iostream>

int main()

{

int n,i;

int f[100];

for(i=0;i<n;i++)

{

if(i==0)

{

f[i]=1;

}

else if(i==1)

{

f[i]==3;

}

else

{

f[i]=f[i-1]+f[i-2]+1;

}

cout<<" "<<f[i];

}

return 0;

}

The MIPS code will be as follows:

.text

.global main

main:

la $s0,n

#<init>

ori $s5,$s0,0 #$s5=i;

la $s0,n

la $s6,f

L1: bne $s5,0,ELSE #branch if!(i==0)

lw $s7, 0($s6)

li $s7,1

bne $s5,1,ELSE

lw $s7, 0($s6)

li $s7,3

NEXT

ELSE:

add ($s7-1),($s7-2),1

UPDATE:

addi $s5,$s5,1

add $s6,$s6,1

j L1

DONE:

#intialise data

.data

n : word 10

The other assembly language code will be

main:
push {r7}
sub sp, sp, #412
add r7, sp, #0
mov r3, #0
str r3, [r7, #404]
b .L2
.L6:
ldr r3, [r7, #404]
cmp r3, #0
bne .L3
mov r3, r7
ldr r2, [r7, #404]
mov r1, #1
str r1, [r3, r2, lsl #2]
b .L4
.L3:
ldr r3, [r7, #404]
cmp r3, #1
bne .L5
mov r3, r7
ldr r2, [r7, #404]
mov r1, #3
str r1, [r3, r2, lsl #2]
b .L4
.L5:
ldr r3, [r7, #404]
add r2, r3, #-1
mov r3, r7
ldr r2, [r3, r2, lsl #2]
ldr r3, [r7, #404]
sub r1, r3, #2
mov r3, r7
ldr r3, [r3, r1, lsl #2]
adds r3, r2, r3
add r1, r3, #1
mov r3, r7
ldr r2, [r7, #404]
str r1, [r3, r2, lsl #2]
.L4:
ldr r3, [r7, #404]
add r3, r3, #1
str r3, [r7, #404]
.L2:
ldr r2, [r7, #404]
ldr r3, [r7, #400]
cmp r2, r3
ite ge
movge r3, #0
movlt r3, #1
uxtb r3, r3
cmp r3, #0
bne .L6
mov r3, #0
mov r0, r3
add r7, r7, #412
mov sp, r7
pop {r7}
bx lr
__static_initialization_and_destruction_0(int, int):
push {r7, lr}
sub sp, sp, #8
add r7, sp, #0
str r0, [r7, #4]
str r1, [r7, #0]
ldr r3, [r7, #4]
cmp r3, #1
bne .L7
ldr r2, [r7, #0]
movw r3, #65535
cmp r2, r3
bne .L7
movw r0, #:lower16:std::__ioinit
movt r0, #:upper16:std::__ioinit
bl std::ios_base::Init::Init()
movw r3, #:lower16:std::ios_base::Init::~Init()
movt r3, #:upper16:std::ios_base::Init::~Init()
movw r0, #:lower16:std::__ioinit
movt r0, #:upper16:std::__ioinit
mov r1, r3
movw r2, #:lower16:__dso_handle
movt r2, #:upper16:__dso_handle
bl __aeabi_atexit
.L7:
add r7, r7, #8
mov sp, r7
pop {r7, pc}
global constructors keyed to main:
push {r7, lr}
add r7, sp, #0
mov r0, #1
movw r1, #65535
bl __static_initialization_and_destruction_0(int, int)
pop {r7, pc}