Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- floating point number redone
- functions calling functions
- ISBN check
- Leap Year
- load byte I-O
- look for vowels in word
- recursive fibonacci
- recursive hailstone reversed
- recursive euclid
- upper to lower
- ----------------------------------------------------------------------
- .text
- .globl main
- main:
- jal getInt #get integer part of number
- beqz $t0,exit #if 0 exit
- jal getSign
- jal getString
- jal printSign
- jal getExponent
- jal getSignificand
- jal buildWord
- jal print
- j main
- getInt:
- li $v0, 4 # Ask for an integer
- la $a0, ask4int
- syscall
- li $v0, 5 # Read an Integer.
- syscall # Value is in $v0.
- move $t0, $v0 # Move Integer into $t0
- jr $ra
- getSign:
- bltz $t0, negnum # if integer is negative set sign to 1
- posnum:
- li $t6,0 # set sign to 0 - positive number
- j negreturn # continue
- negnum:
- li $t6,1 # set sign to 1 - negative number
- neg $t0,$t0 # negate number
- negreturn:
- move $t3,$t0 # save number in $t3
- jr $ra
- getString:
- li $v0, 4 #ask for a fraction as a binary string
- la $a0, ask4fract
- syscall
- li $v0, 8 # Read a string
- la $a0,bin # get input buffer where the string is to be stored
- li,$a1,24 # length of input buffer
- syscall # Value is in $v0.
- jr $ra
- printSign:
- li $v0, 4 # print sign message
- la $a0, signequals
- syscall
- li $v0, 1 # Print sign
- move $a0,$t6
- syscall
- li $v0, 4 # go to next line
- la $a0,nl
- syscall
- jr $ra
- #normalize to IEEE format for floating point
- # create a single bit to check if it is set in input
- #find 1st 1 and that's where decimal place goes after
- getExponent:
- li $t1, 0x40000000 # set first bit after sign to 1 to check for 1's and 0's
- li $t2,31 # count of what bit up to
- testforbitset:
- and $t4,$t1,$t3 #check the bit
- beqz $t4,gotonextbit #if the bit isn't set go check next
- addi $t2,$t2,-1 #1st set bit is found
- #subtract 1 from counter $t2 to get exponent
- move $t7,$t2 #save position of bit
- addi $t2,$t2,127 #convert exponent to excess 127
- li $v0, 4 # print exponent message
- la $a0,expequals
- syscall
- li $v0,1 # Print exponent
- move $a0,$t2
- syscall
- li $v0, 4 # go to next line
- la $a0,nl
- syscall
- xor $t3,$t3,$t1 #get rid of high order bit in number (t1 has mask where the bit was found)
- sll $t6,$t6,31 # put sign in bit 0 (sign position)
- sll $t2,$t2,23 #put exponent in bits 1-8
- or $t6,$t6,$t2 #combine sign and exponent
- jr $ra
- gotonextbit:
- srl $t1,$t1,1 # go check next bit
- addi $t2,$t2,-1 #change counter
- j testforbitset
- getSignificand:
- li $t4,23 #only process 23-number of bits in fraction rest will be ignored
- sub $t4,$t4,$t7 #t4 left with number of fraction bits to process (t7 is position bit found in integer)
- li $t2,0 #$t2 will count how many digits found in fraction
- li $t0,0 # create binary string in $t0
- beqz $t4,shiftdone #if 23 bits used by integer ignore fraction
- la $a0,bin # get input buffer where the string is to be stored
- addi $a0,$a0,-1 # can start 1 before since will add 1
- shiftbit: #look for first digit in decimal
- addi $a0,$a0,1 #get the next digit
- lb $t1,($a0) # get digit
- beq $t1,10,endfound # \n is decimal 10 so if found done with all bits
- bne $t1,46,shiftbit # look for . to start processing (46 is a .)
- shiftagain2: #. found so know where first (leftmost)digit is
- addi $a0,$a0,1 #go to next digit
- lb $t1,($a0) #get digit
- beq $t1,10,endfound # \n is decimal 10 so if found done with all bits
- sll $t0,$t0,1 # shift bits left 1 so can put next digit in
- addi $t2,$t2,1 #counting how many digits in significand
- addi $t1,$t1,-48 #subtract 48 which is X'30' to change to binary
- or $t0,$t0,$t1 # put digit into bit string (creating number)
- beq $t2,$t4,calcshift #had to truncate fractional input because significand is full
- j shiftagain2
- endfound:
- move $t4,$t2 #save number of bits in the fraction in $t4 for later
- calcshift:
- move $t1,$t4 #shift integer number of bits in fraction bits for later
- shiftagain: #shift rest of integer into position
- beqz $t1,shiftdone # if all done no shift needed
- sll $t3,$t3,1 # shift left
- addi $t1,$t1,-1 #count the number of shifts
- b shiftagain #do another shift
- shiftdone:
- jr $ra
- buildWord:
- or $t0,$t0,$t3 #put integer part in floating point word created
- add $t7,$t4,$t7 #shift fraction part left as far 23-(bits in fraction + bits in integer)
- li $t5,23 #t5 now has number of bits to shift significand
- sub $t5,$t5,$t7
- checkingshiftdone: # shift significand into position
- blez $t5,shiftdone2 #if in correct position and will give non zero result
- sll $t0,$t0,1 # shift 1 more position
- addi $t5,$t5,-1 #1 less shift to do
- j checkingshiftdone #check if done shifting
- shiftdone2:
- or $t0,$t0,$t6 # put sign bit and exponent in word building
- move $s0,$t0 # put it in $s0 to be printed later
- jr $ra
- print: #done -print everything
- addi $sp,$sp,-4
- sw $ra,0($sp)
- li $v0, 4 # print bits of floating point number message
- la $a0, writebits
- syscall
- li $v0, 4 # go to next line
- la $a0,nl
- syscall
- jal prtbin
- li $v0, 4 # print floating point number message
- la $a0, writenumber
- syscall
- mtc1 $s0,$f12 #move to coprocessor 1 (floating point unit)
- li $v0,2
- syscall
- li $v0, 4 # go to next line
- la $a0,nl
- syscall
- li $v0, 4 # go to next line
- la $a0,nl
- syscall
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra # go back and start over
- exit:
- li $v0, 10
- syscall # DONE-exit
- #function to print numbers in binary
- prtbin:
- li $t0,0x80000000 #initial mask
- li $t6,4
- nextdigit:
- addi $t6,$t6,-1
- and $t1,$s0,$t0 #extract bit
- beqz $t1,prt0 #0, so print it
- li $a0,1 #will print a 1
- j printdigit
- prt0:
- li $a0,0 #will print a 0
- printdigit:
- li $v0,1 #print the digit
- syscall
- bnez $t6,next
- la $a0,space
- li $v0,4
- syscall
- li $t6,4
- next:
- srl $t0,$t0,1 #move mask 1
- bnez $t0,nextdigit #while not shifted out
- la $a0,nl
- li $v0,4
- syscall
- jr $ra #done printing
- .data
- ask4int: .asciiz "Enter signed integer part. \n"
- ask4fract: .asciiz "Enter fraction as a binary string.\n"
- signequals: .asciiz "\nsign bit = "
- expequals: .asciiz "Exponent in decimal = "
- writebits: .asciiz "All bits of the floating point number are: \n"
- writenumber: .asciiz "The floating point number is:\n "
- nl: .asciiz "\n"
- space: .ascii " "
- .align 2
- bin: .space 24 # Space for floating point number in ascii
- -----------------------
- .text
- .globl main
- main:
- jal input
- move $t1,$v0 #t1 has A
- jal input #get B
- move $t2,$v0 #t2 has B
- jal add
- li $v0,10
- syscall
- input:
- addi $sp,$sp,-4
- sw $ra,0($sp)
- la $a0,aprompt
- li $v0,4
- syscall #print the prompt
- li $v0,5
- syscall #get the number
- move $a1,$v0
- jal determine
- move $v0,$a1
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra
- determine:
- addi $sp,$sp,-4
- sw $ra,0($sp)
- bltz $a1,negg
- bgtz $a1,pos
- la $a0,zzero
- jal printDetermination
- negg:
- la $a0,negative
- jal printDetermination
- pos:
- la $a0,positive
- jal printDetermination
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra
- printDetermination:
- addi $sp,$sp,-4
- sw $ra,0($sp)
- li $v0,4
- syscall
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra
- add:
- addi $sp,$sp,-4
- sw $ra,0($sp)
- add $a1,$t1,$t2
- jal outputSum
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra
- outputSum:
- addi $sp,$sp,-4
- sw $ra,0($sp)
- la $a0,sumMess
- li $v0,4
- syscall
- li $v0,1
- move $a0,$a1
- syscall
- la $a0,nl
- li $v0,4
- syscall
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra
- .data
- aprompt: .asciiz "Enter a number: "
- sumMess: .asciiz "The sum is: "
- negative: .asciiz "number is negative\n"
- positive: .asciiz "number is positive\n"
- zzero: .asciiz "number is zero\n "
- nl: .asciiz "\n"
- ------------------------------------------------------
- .text
- .globl main
- main:
- la $a0, enter_ISBN #ask for ISBN
- li $v0, 4
- syscall
- la $a0, Inputstring #where to put input
- li $a1, 20 # Length in a1-last must be enter (this actually gets 19 digits).
- li $v0, 8 # Read Input string.
- syscall
- #count number of digits-last is check digit
- li $t8,20 #set counter to size of characters buffer
- getchar:
- addi $t8,$t8,-1 #go to next character
- bltz $t8,error #if counter goes negative no data was entered
- lb $t0, Inputstring($t8) # Load character from the buffer
- bnez $t0, knowCount #when non zero know how many characters were entered
- j getchar #otherwise keep looking
- knowCount: #number of characters is in t8 (that's the check value)
- addi $t8,$t8,-1 #found enter check digit is character before
- lb $t0,Inputstring($t8) #so get it
- beq $t0,'X',ten #when checkdigit is 10 place an X
- subi $t9,$t0,48 #get check value (48 = '0') for 0-9
- j checkISBN
- ten:
- li $t9,10 #checkdigit is X when calcuted as 10
- checkISBN:
- la $t0,Inputstring #index into ISBN t0
- add $t8,$t8,$t0 #t8 is address of checkdigit
- li $t1,10 #digit multiplier t1
- li $t2,0 #accumulater t2
- li $t4,'-' #will skip hyphens
- li $t5, '0' #min value
- li $t6, '9' #max value
- getNum: lb $t3,0($t0)
- beq $t3,$t4,nextdigit #skip hyphens
- blt $t3,$t5,error #not a number <'0'
- bgt $t3,$t6,error #not a number >'9'
- sub $t3,$t3,'0' #make it a digit
- mult $t3,$t1 #multiply value of digit by reverse position
- mflo $t7 #get result
- add $t2,$t2,$t7 #and add to accumulator
- addi $t1,$t1,-1 #next multiplier
- nextdigit: addi $t0,$t0,1 #go to next digit
- beq $t0,$t8,calcDone #up to checkdigit if yes done
- j getNum #if not process next digit
- calcDone:
- li $t0,11 #need mod 11
- div $t2,$t0 #divide accumulator by 11
- mfhi $t2 #and get remainder
- sub $t2,$t0,$t2 #subtract that from 11 to get checkdigit
- digitFound:
- beq $t2,$t9,valid #compare to checkdigit - should be equal
- la $a0, no #bad isbn entered
- li $v0, 4
- syscall
- j end
- valid:
- la $a0, yes #valid isbn entered
- li $v0, 4
- syscall
- j end
- error: la $a0, badData #bad isbn entered
- li $v0, 4
- syscall
- end:
- li $v0,10
- syscall
- .data
- badData: .asciiz "must consist of 0-9,X and hyphens only\n program exiting\n"
- enter_ISBN: .asciiz "Enter a 10 digit ISBN: "
- yes: .asciiz "You entered a valid ISBN\n"
- no: .asciiz "You entered an invalid ISBN\n"
- .align 2
- Inputstring: .space 20 # Space for input ISBN
- ---------------------------------
- .text
- .globl main
- main: # get the values into registers
- la $a0,getYear
- jal print #ask for year
- jal input #and get it
- beqz $v0,exit #sentinel(=0) exit
- #check for leap year
- move $a0,$v0 #move year to a0
- li $v0,1
- syscall #and print it
- #divisible by 400?
- li $a1,400 #check divisible by 400
- jal divide
- move $s0,$v0 #save the results
- #divisible by 100?
- li $a1,100
- jal divide #check if divisible by 100
- move $s1,$v0
- #divisible by 4?
- li $a1,4
- jal divide #check if divisible by 4
- move $a0,$s0 #move results to arguments
- move $a1,$s1
- move $a2,$v0
- jal check
- beqz $v0,notLeap #if 0 returned not a leapyear
- #is a leap year
- la $a0,is
- jal print #report a leap year
- b main #and try again
- #not a leap year
- notLeap:
- la $a0,isnt
- jal print #report not a leap year
- b main #and try again
- #print program complete and exit
- exit:
- la $a0,complete
- li $v0,4
- syscall #print complete
- li $v0,10 #exit program
- syscall
- check:
- #check criterion (100 and 400) or (4 and not 100)
- and $t0,$a0,$a1 #100 and 400
- not $t2,$a1 #not 100
- and $t1,$t2,$a2 #4 and not 100
- or $v0,$t0,$t1 #(100 and 400) or (4 and not 100)
- jr $ra
- divide:
- move $t0,$s0 #save s registers I am using, as defined by assignment
- move $t1,$s1
- div $a0,$a1 #divide year by indicated value
- mfhi $t2 #get the remainder to see if evenly divisible
- beqz $t2,true #evenly divisible if no remaider so return 1 for true
- move $v0,$zero #otherwise return false
- j restore
- true:
- li $v0,1 #return true
- restore:
- move $s0,$t0 #restore only s registers I am using
- move $s1,$t1
- jr $ra #and return
- print:
- li $v0,4
- syscall #print the message who's address is in a0
- jr $ra #and return
- input:
- li $v0,5 #get a number
- syscall
- bgez $v0,returnInput #valid input (not <0)
- la $a0,error
- li $v0,4
- syscall #print error message
- b input #reget input
- returnInput:
- jr $ra #return to caller
- .data
- getYear: .asciiz "Enter a year(0 to exit): "
- error: .asciiz "invalid entry-must be >=0)\nEnter a year(0 to exit): "
- is: .asciiz " is a leap year\n\n"
- isnt: .asciiz " is not a leap year\n\n"
- complete: .asciiz "program complete"
- ----------------------------
- #Input 2 digits, each as a character, convert them to numbers, add and output
- .text
- .globl main
- main:
- la $a0, m1 #what to output (contents of m1)
- li $v0, 4 # Display a message.
- syscall
- la $a0, Instr #where to put input
- li $a1, 3 # Length in a1-last must be enter (this actually # gets 3 digits).
- li $v0, 8 # Read Input string.
- syscall
- la $t1, Instr #get address of input
- li $t2,48 # amount to subtract to make ASCII decimal
- addi $a1,$a1,-1 #since actually got 3 digits set counter to 3
- getdigit:
- lb $t0, ($t1) # Load from the address in t1
- sub $t0, $t0,$t2 #make it a digit
- la $a0, m6 # print a newline.
- li $v0 4
- syscall
- move $a0, $t0
- li $v0 1 #print number
- syscall
- addi $t1,$t1,1 #go to next digit
- addi $a1,$a1,-1 #is it a digit
- bgt $a1, $0, getdigit #if not done continue
- end:
- la $a0, m8 #Prints Goodbye.
- li $v0 4
- syscall
- la $v0,10
- syscall # au revoir...
- ###########################################################
- # data segment
- ###########################################################
- .data
- m1: .asciiz "Enter a Number \n "
- m6: .asciiz "\nThe digit is: \n "
- m8: .asciiz "\nGoodbye!\n" #For exiting.
- .align 2
- Instr: .space 10 # Space for in string.
- ## end of file.
- -----------------------------
- .text
- .globl main
- main:
- li $t6,6 #there are 6 counters and vowels
- la $a0, enter_word #what to output
- li $v0, 4 # Display a message, to enter a word.
- syscall
- la $a0, Inputstring #where to put input
- li $a1, 16 # Length in a1-last must be enter (this actually gets 15 digits).
- li $v0, 8 # Read word.
- syscall
- li $t1,0 #set counter to go through input buffer
- li $t2,97 #ascii value of a
- li $t3,122 #ascii value of z
- lb $t4,nl($zero) #ascii value of \n
- li $a0,0 #total vowels found
- getchar:
- lb $t0, Inputstring($t1) # Load character from the buffer
- beq $t0,$t4,donechecking #\n so done
- blt $t0,$t2,nextchar #outside range go to next character
- bgt $t0,$t3,nextchar #outside range go to next character
- #do table look up
- jal lookup
- nextchar:
- addi $t1,$t1,1
- j getchar
- lookup:
- li $t7,-1 #counter through vowels
- lookagain:
- addi $t7,$t7,1 #go to next vowel
- beq $t7,$t6,return #check a,e,i,o,u,y?
- lb $t8,vowels($t7) #get vowel to check
- bne $t8,$t0,lookagain #letter not that vowel
- addi $a0,$a0,1 #a vowel found
- lb $t9,counters($t7) #bump that letters counter
- addi $t9,$t9,1
- sb $t9,counters($t7)
- return:
- jr $ra
- donechecking:
- la $t1,counters
- lb $t2,5($t1) #if > 1 vowel and not y reset y to 0
- beq $a0,$t2,noY #y only vowel so don't reset
- sb $zero,5($t1) #clear y counter since other vowels in word
- noY:
- la $a0, vowelsm #Prints The vowels are:
- li $v0 4
- syscall
- li $t0,-1
- next:
- addi $t0,$t0,1
- beq $t0,$t6,end
- lb $t1, counters($t0) #get counter
- beqz $t1,next #don't print if 0
- la $a0,vowelbuffer #get the vowel
- lb $t2, vowels($t0)
- sb $t2,vowelbuffer($zero) #put in buffer to output
- li $v0,4
- syscall
- li $v0,1 #output vowel
- move $a0,$t1
- syscall
- la $a0,nl #and count
- li $v0,4
- syscall
- j next
- end:
- li $v0,10
- syscall
- .data
- enter_word: .asciiz "Enter a word\n "
- vowelsm: .asciiz "the vowels and their count are\n "
- nl: .asciiz "\n"
- .align 4
- vowels: .byte 'a','e','i','o','u','y'
- counters: .byte 0,0,0,0,0,0
- vowelbuffer: .asciiz " - "
- .align 4
- Inputstring: .space 16 # Space for input string.
- ---------------------------------------
- .text
- .globl main
- main:
- la $a0,getNum # Ask what number
- li $v0, 4
- syscall
- li $v0, 5 #get rhe number
- syscall
- move $a0,$v0
- jal fib
- move $t0,$v0
- la $a0,value # print to solution
- li $v0, 4
- syscall
- li $v0, 1 #print the number
- move $a0,$t0
- syscall
- la $v0,10
- syscall #exit.
- fib:
- addi $sp,$sp,-12
- sw $a0, 0($sp) #save n and return address
- sw $ra, 4($sp)
- #base?
- beqz $a0,base
- beq $a0,1,base
- addi $a0,$a0,-1
- jal fib #get fib(n-1)
- sw $v0,8($sp) #save returned value in stack for n
- addi $a0,$a0,-1
- jal fib
- lw $t0,8($sp)
- add $v0,$t0,$v0
- j return
- base:
- move $v0,$a0
- return:
- lw $a0, 0($sp) #restore all from stack
- lw $ra, 4($sp)
- addi $sp,$sp,12
- jr $ra
- .data
- getNum: .asciiz "Enter a Number: "
- value: .asciiz "\nThe value is: "
- ------------------------------------------------
- .text
- .globl main
- main:
- la $a0,enterN #ask for starting number
- li $v0,4
- syscall
- li $v0,5 #get it
- syscall
- move $t1,$v0 #and save in t1
- jal hail # call hailstone
- li $v0,10 #and exit
- syscall
- # int hailstone(int n)
- # {cout<<n<<"\n";
- # if(n==1)
- # return n;
- # else if(n%2==0)
- # return hailstone(n/2);
- # else
- # return hailstone(n*3+1);
- # }
- hail: addi $sp,$sp,-8 #allocate room in stack for return address and parameter
- sw $ra,4($sp) #save return address
- sw $t1,0($sp) #and parameter
- move $a0,$t1 #output number just got
- # li $v0,1
- # syscall
- # la $a0,nl
- # li $v0,4
- # syscall #and go to next line
- li $t2,1 #ending number
- li $t4,3 #to multiply by 3
- li $t5,2 #to divide by 2
- beq $t1,$t2,return1 #n=1 done
- andi $t3,$t1,1 #get lest significant digit
- beqz $t3,even #if even branch
- odd:
- mult $t1,$t4 #n=n*3+1
- mflo $t1
- addi $t1,$t1,1
- jal hail #next number
- j return #and exit
- even:
- #n=n/2
- srl $t1,$t1,1
- jal hail #next number
- j return #and exit
- return1:
- la $a0,reversed
- li $v0,4
- syscall
- move $a0,$t1 #print the 1
- li $v0,1
- syscall
- la $a0,nl
- li $v0,4
- syscall #and go to next line
- return:
- addi $sp,$sp,8 #when come back advance stack pointer this is bypassing number on the top
- #should be after the lw
- lw $ra,4($sp) #restore return address
- lw $a0,0($sp) #and value of n just used to print
- li $v0,1
- syscall
- la $a0,nl
- li $v0,4
- syscall #and go to next line
- jr $ra #return to where came from
- .data
- enterN: .asciiz "Enter a starting value: "
- reversed: .asciiz "reversed\n"
- nl: .asciiz "\n"
- ------------------------------------------------------------
- .text
- .globl main
- main:
- la $a0,aprompt
- jal input #get A
- move $t1,$v0 #t1 has A
- la $a0,bprompt
- jal input #get B
- move $t2,$v0 #t2 has B
- jal getGCD
- la $a0,gcdMess
- li $v0,4
- syscall
- li $v0,1
- move $a0,$t1
- syscall
- li $v0,10
- syscall
- input:
- li $v0,4
- syscall #print the prompt
- li $v0,5
- syscall #get the number
- jr $ra #and return
- getGCD:
- addi $sp,$sp,-4
- sw $ra,0($sp)
- beqz $t2,return
- move $t3,$t2
- div $t1,$t2
- mfhi $t2
- move $t1,$t3
- jal getGCD
- return:
- lw $ra,0($sp)
- addi $sp,$sp,4
- jr $ra #and rerturn
- .data
- aprompt: .asciiz "Enter first number: "
- bprompt: .asciiz "Enter a 2nd number: "
- gcdMess: .asciiz "The GCD is: "
- -------------------------------------------------------------------------
- #Input 2 digits, each as a character, convert them to numbers, add and output
- .text
- .globl main
- main:
- la $a0, m1 #what to output (contents of m1)
- li $v0, 4 # Display a message.
- syscall
- la $a0, Instr #where to put input
- li $a1, 4 # Length in a1-last must be enter (this actually # gets 3 digits).
- li $v0, 8 # Read Input string.
- syscall
- li $t1, 0 #get address of input
- li $t2,32 # amount to add to make lower case
- addi $a1,$a1,-1 #since actually got 3 digits set counter to 3
- getdigit:
- lb $t0, Instr($t1) # Load from the address in t1
- add $t0, $t0,$t2 #make it a lower case
- sb $t0,Instr($t1)
- addi $t1,$t1,1 #go to next digit
- addi $a1,$a1,-1 #is it a digit
- bgt $a1, $0, getdigit #if not done continue
- la $a0, m6 #Prints message.
- li $v0 4
- syscall
- la $a0,Instr
- li $v0 4
- syscall
- end:
- la $a0, m8 #Prints Goodbye.
- li $v0 4
- syscall
- la $v0,10
- syscall # au revoir...
- ###########################################################
- # data segment
- ###########################################################
- .data
- m1: .asciiz "Enter a 3 letter word in all upper case \n "
- m6: .asciiz "\nIn lower case is: \n "
- m8: .asciiz "\nGoodbye!\n" #For exiting.
- .align 2
- Instr: .space 10 # Space for in string.
- ## end of file.
Advertisement
Add Comment
Please, Sign In to add comment