Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dereje #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions First Q
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# COMP3410 Program Template
# Author: Your Name Dereje Arega
# Assignment: PA1
# Date: 2/11/2015

# Turn in one .asm file per assignment component
# Remember to submit it as a pull request to the GitHub repo for the assignment

# fibonacci.asm
# Input: None
# Output: Print to terminal N Fibonacci numbers

##########################################################
# .data segment. Constant and variable definitions go here.
##########################################################

.data


fibs: .word 0 : 9 # create an array variable named "fibs" of 9 word-length elements (4 bytes each)
size: .word 9 # create a single integer variable named "size" that indicates the length of the array
text: .asciiz "How many Fibonacci numbers you want print?\n"
###############################################
# .text segment. Assembly instructions go here.
###############################################
.text
la $s0, fibs # load address of array into $s0
#la $s5, size # load address of size variable into $s5
# lw $s5, 0($s5) # load array size from its address in the register
li $v0,4
la $a0, text
syscall
li $v0,5
syscall
move $s5,$v0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-5 Doesn't limit the user to 9 and therefore breaks on relatively small input. This is unsafe because your loop is accessing addresses that are out of the bounds set in fibs. (For my test, output is invalid for input > 21 where if you allocate more memory you will see that it works fine)

In fact if you try to run it with 25 numbers and pay attention to $a0 after it loads the "space" memory address, you may see the value 268501076, which is where the space character is stored, but for 25 fibonacci numbers, the sequence ends at 268501084, which means that the space character " " has been replaced with another character.


li $s2, 1 # Initialize the Fibonacci numbers with value 1, stored in $s2
sw $s2, 0($s0) # Set fibs[0] to 1
sw $s2, 4($s0) # Set fibs[1] to 1
addi $s1, $s5, -2 # Counter for loop, will execute (size-2) times

# Loop to compute each Fibonacci number using the previous two Fib. numbers.
# On line 37, "loop" is a label
loop: lw $s3, 0($s0) # Get value from array fibs[i-2]
lw $s4, 4($s0) # Get value from array fibs[i-1]
add $s2, $s3, $s4 # fibs[i] = fibs[i-1] + fibs[i-2]
sw $s2, 8($s0) # Store newly computed fibs[i] in array
addi $s0, $s0, 4 # increment address for next fibonacci entry
addi $s1, $s1, -1 # decrement loop counter
bgtz $s1, loop # repeat while not finished

# Now the Fibonacci numbers are computed and stored in array. Print them.
la $a0, fibs # first argument for print (array)
add $a1, $zero, $s5 # second argument for print (size)
jal print # call print routine (note the 'print' label on line 68)

# The program is finished. Exit.
li $v0, 10 # system call for exit
syscall

###############################################################
# Subroutine to print the numbers on one line. Another .data segment.
###########################################################
.data

space: .asciiz " " # Print a space between each pair of numbers
head: .asciiz "The Fibonacci numbers are:\n" # Print a little helpful intro

###########################################################
# Another .text segment, for printing
###########################################################

.text

print: add $t0, $zero, $a0 # starting address of array of data to be printed
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of the print heading string
li $v0, 4 # specify that you're printing a string
syscall # print the heading string

out: lw $a0, 0($t0) # load the integer to be printed (the current Fib. number)
li $v0, 1 # specify that you're printing an integer
syscall # print fibonacci number

la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # print the spacer string

addi $t0, $t0, 4 # increment address of data to be printed
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat while not finished

jr $ra # return from subroutine
# End of subroutine to print the numbers on one line
###############################################################
65 changes: 65 additions & 0 deletions Second Q
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#Name: Dereje Arega
.data
head: .asciiz "Name: Dereje Arega \n MIPS Assembly Programs\n Writ a MIPS program that prform three things:\n 1 Prompt the user for two numbers and save them in two different registers.\n 2. Perform 3 calculations (sum, difference, product) using these two numbers, and save each result in data memory.\n 3. For each computed result, retrieve it from memory and print it to the terminal.\nPrint a concluding message and exit \n"

.word

prompt0:.asciiz "\nEnter the first number\n"
prompt1:.asciiz "\nEnter the second number\n"
prompt2:.asciiz "\nSum = N1 + N2\n"
prompt3:.asciiz "\nDif = N1 - N2 \n"
prompt4:.asciiz "\nmult = N1*N2\n"
prompt5:.asciiz "\nDiv = N1/N2\n"
.text
li $v0,4
la $a0,head
syscall
li $v0,4
la $a0,prompt0
syscall
li $v0,5
syscall
move $s1, $v0 #enter the first umber

li $v0,4
la $a0,prompt1
syscall
li $v0,5
syscall
move $s2,$v0 #enter the second number

add $t0,$s1,$s2 #n and the first number and the second number
sub $t1,$s1,$s2 # Substract the second number from the first one
div $t2, $s1,$s2 # divade the first number by the second
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user enters 0 for N2, division by 0 occurs and the program breaks. Good idea to protect against that. No points lost since division was not required.

mul $t3,$s1,$s2 #
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-8 Part of the exercise is to store arithmetic results in memory (instead of just using registers), then to retrieve them from memory for printing.


li $v0,4
la $a0,prompt3 # prompt the diference
syscall
li $v0,1
move $a0,$t1
syscall

li $v0,4
la $a0,prompt2 # prompt the sum
syscall
li $v0,1
move $a0,$t0
syscall

li $v0,4
la $a0,prompt4 # prompt the multiplication
syscall
li $v0,1
move $a0,$t3
syscall

li $v0,4
la $a0,prompt5 # prompt the multiplication
syscall
li $v0,1
move $a0,$t2
syscall



43 changes: 43 additions & 0 deletions Third Q
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#Name: Dereje Arega
# PA1
# Date: 2/11/2015
.data
FV:.word 1 # the first value
SV:.word 1# the second value
Tn:.word
space:.asciiz "\n" #creat a line break
odlNum:.word
.text
li $k0,1
li $s1,8 # the number of old numbers from 1- 15 enclusive
loop: bge $k0,$s1,labl

la $t0,SV
lw $t0,0($t0)


la $t1,FV
lw $t1,0($t1)

addi $t2,$t1,2

add $s4,$t0,$t2

sw $s4 SV
sw $t2,FV

addi $k0,$k0,1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-2 $k0 and $k1 are intended for OS use (k for kernel) and should NOT be used as general-purpose registers even though it works.


la $a0,SV
lw $a0,0($a0)
li $v0,1
syscall

la $a0,space
li $v0, 4
syscall
j loop
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-5 Virtually NO comments

labl:li $v0,10
syscall