Silver_Smoulder

Bunch of Programs by Prof. Lichtenstein

Dec 4th, 2019
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.27 KB | None | 0 0
  1. floating point number redone
  2. functions calling functions
  3. ISBN check
  4. Leap Year
  5. load byte I-O
  6. look for vowels in word
  7. recursive fibonacci
  8. recursive hailstone reversed
  9. recursive euclid
  10. upper to lower
  11.  
  12.  
  13. ----------------------------------------------------------------------
  14. .text
  15. .globl main
  16. main:
  17. jal getInt #get integer part of number
  18. beqz $t0,exit #if 0 exit
  19. jal getSign
  20. jal getString
  21. jal printSign
  22. jal getExponent
  23. jal getSignificand
  24. jal buildWord
  25. jal print
  26. j main
  27. getInt:
  28. li $v0, 4 # Ask for an integer
  29. la $a0, ask4int
  30. syscall
  31.  
  32. li $v0, 5 # Read an Integer.
  33. syscall # Value is in $v0.
  34.  
  35. move $t0, $v0 # Move Integer into $t0
  36. jr $ra
  37. getSign:
  38. bltz $t0, negnum # if integer is negative set sign to 1
  39. posnum:
  40. li $t6,0 # set sign to 0 - positive number
  41. j negreturn # continue
  42. negnum:
  43. li $t6,1 # set sign to 1 - negative number
  44. neg $t0,$t0 # negate number
  45. negreturn:
  46. move $t3,$t0 # save number in $t3
  47. jr $ra
  48. getString:
  49. li $v0, 4 #ask for a fraction as a binary string
  50. la $a0, ask4fract
  51. syscall
  52. li $v0, 8 # Read a string
  53. la $a0,bin # get input buffer where the string is to be stored
  54. li,$a1,24 # length of input buffer
  55. syscall # Value is in $v0.
  56. jr $ra
  57. printSign:
  58. li $v0, 4 # print sign message
  59. la $a0, signequals
  60. syscall
  61. li $v0, 1 # Print sign
  62. move $a0,$t6
  63. syscall
  64. li $v0, 4 # go to next line
  65. la $a0,nl
  66. syscall
  67. jr $ra
  68. #normalize to IEEE format for floating point
  69. # create a single bit to check if it is set in input
  70. #find 1st 1 and that's where decimal place goes after
  71. getExponent:
  72. li $t1, 0x40000000 # set first bit after sign to 1 to check for 1's and 0's
  73. li $t2,31 # count of what bit up to
  74. testforbitset:
  75. and $t4,$t1,$t3 #check the bit
  76. beqz $t4,gotonextbit #if the bit isn't set go check next
  77. addi $t2,$t2,-1 #1st set bit is found
  78. #subtract 1 from counter $t2 to get exponent
  79. move $t7,$t2 #save position of bit
  80. addi $t2,$t2,127 #convert exponent to excess 127
  81. li $v0, 4 # print exponent message
  82. la $a0,expequals
  83. syscall
  84.  
  85. li $v0,1 # Print exponent
  86. move $a0,$t2
  87. syscall
  88.  
  89. li $v0, 4 # go to next line
  90. la $a0,nl
  91. syscall
  92. xor $t3,$t3,$t1 #get rid of high order bit in number (t1 has mask where the bit was found)
  93. sll $t6,$t6,31 # put sign in bit 0 (sign position)
  94. sll $t2,$t2,23 #put exponent in bits 1-8
  95. or $t6,$t6,$t2 #combine sign and exponent
  96. jr $ra
  97.  
  98. gotonextbit:
  99. srl $t1,$t1,1 # go check next bit
  100. addi $t2,$t2,-1 #change counter
  101. j testforbitset
  102.  
  103. getSignificand:
  104. li $t4,23 #only process 23-number of bits in fraction rest will be ignored
  105. sub $t4,$t4,$t7 #t4 left with number of fraction bits to process (t7 is position bit found in integer)
  106. li $t2,0 #$t2 will count how many digits found in fraction
  107. li $t0,0 # create binary string in $t0
  108. beqz $t4,shiftdone #if 23 bits used by integer ignore fraction
  109. la $a0,bin # get input buffer where the string is to be stored
  110. addi $a0,$a0,-1 # can start 1 before since will add 1
  111.  
  112. shiftbit: #look for first digit in decimal
  113. addi $a0,$a0,1 #get the next digit
  114. lb $t1,($a0) # get digit
  115. beq $t1,10,endfound # \n is decimal 10 so if found done with all bits
  116. bne $t1,46,shiftbit # look for . to start processing (46 is a .)
  117.  
  118. shiftagain2: #. found so know where first (leftmost)digit is
  119. addi $a0,$a0,1 #go to next digit
  120. lb $t1,($a0) #get digit
  121. beq $t1,10,endfound # \n is decimal 10 so if found done with all bits
  122. sll $t0,$t0,1 # shift bits left 1 so can put next digit in
  123. addi $t2,$t2,1 #counting how many digits in significand
  124. addi $t1,$t1,-48 #subtract 48 which is X'30' to change to binary
  125. or $t0,$t0,$t1 # put digit into bit string (creating number)
  126. beq $t2,$t4,calcshift #had to truncate fractional input because significand is full
  127. j shiftagain2
  128.  
  129. endfound:
  130. move $t4,$t2 #save number of bits in the fraction in $t4 for later
  131. calcshift:
  132. move $t1,$t4 #shift integer number of bits in fraction bits for later
  133.  
  134. shiftagain: #shift rest of integer into position
  135. beqz $t1,shiftdone # if all done no shift needed
  136. sll $t3,$t3,1 # shift left
  137. addi $t1,$t1,-1 #count the number of shifts
  138. b shiftagain #do another shift
  139.  
  140. shiftdone:
  141. jr $ra
  142. buildWord:
  143. or $t0,$t0,$t3 #put integer part in floating point word created
  144. add $t7,$t4,$t7 #shift fraction part left as far 23-(bits in fraction + bits in integer)
  145. li $t5,23 #t5 now has number of bits to shift significand
  146. sub $t5,$t5,$t7
  147.  
  148. checkingshiftdone: # shift significand into position
  149. blez $t5,shiftdone2 #if in correct position and will give non zero result
  150. sll $t0,$t0,1 # shift 1 more position
  151. addi $t5,$t5,-1 #1 less shift to do
  152. j checkingshiftdone #check if done shifting
  153.  
  154. shiftdone2:
  155. or $t0,$t0,$t6 # put sign bit and exponent in word building
  156. move $s0,$t0 # put it in $s0 to be printed later
  157. jr $ra
  158. print: #done -print everything
  159. addi $sp,$sp,-4
  160. sw $ra,0($sp)
  161. li $v0, 4 # print bits of floating point number message
  162. la $a0, writebits
  163. syscall
  164. li $v0, 4 # go to next line
  165. la $a0,nl
  166. syscall
  167. jal prtbin
  168. li $v0, 4 # print floating point number message
  169. la $a0, writenumber
  170. syscall
  171.  
  172. mtc1 $s0,$f12 #move to coprocessor 1 (floating point unit)
  173. li $v0,2
  174. syscall
  175.  
  176. li $v0, 4 # go to next line
  177. la $a0,nl
  178. syscall
  179.  
  180. li $v0, 4 # go to next line
  181. la $a0,nl
  182. syscall
  183. lw $ra,0($sp)
  184. addi $sp,$sp,4
  185. jr $ra # go back and start over
  186.  
  187. exit:
  188. li $v0, 10
  189. syscall # DONE-exit
  190.  
  191. #function to print numbers in binary
  192. prtbin:
  193. li $t0,0x80000000 #initial mask
  194. li $t6,4
  195. nextdigit:
  196. addi $t6,$t6,-1
  197.  
  198. and $t1,$s0,$t0 #extract bit
  199. beqz $t1,prt0 #0, so print it
  200. li $a0,1 #will print a 1
  201. j printdigit
  202. prt0:
  203. li $a0,0 #will print a 0
  204. printdigit:
  205. li $v0,1 #print the digit
  206. syscall
  207. bnez $t6,next
  208. la $a0,space
  209. li $v0,4
  210. syscall
  211. li $t6,4
  212. next:
  213. srl $t0,$t0,1 #move mask 1
  214. bnez $t0,nextdigit #while not shifted out
  215. la $a0,nl
  216. li $v0,4
  217. syscall
  218. jr $ra #done printing
  219.  
  220. .data
  221. ask4int: .asciiz "Enter signed integer part. \n"
  222. ask4fract: .asciiz "Enter fraction as a binary string.\n"
  223. signequals: .asciiz "\nsign bit = "
  224. expequals: .asciiz "Exponent in decimal = "
  225. writebits: .asciiz "All bits of the floating point number are: \n"
  226. writenumber: .asciiz "The floating point number is:\n "
  227. nl: .asciiz "\n"
  228. space: .ascii " "
  229. .align 2
  230. bin: .space 24 # Space for floating point number in ascii
  231.  
  232.  
  233. -----------------------
  234.  
  235. .text
  236. .globl main
  237. main:
  238. jal input
  239. move $t1,$v0 #t1 has A
  240. jal input #get B
  241. move $t2,$v0 #t2 has B
  242. jal add
  243.  
  244. li $v0,10
  245. syscall
  246. input:
  247. addi $sp,$sp,-4
  248. sw $ra,0($sp)
  249. la $a0,aprompt
  250. li $v0,4
  251. syscall #print the prompt
  252. li $v0,5
  253. syscall #get the number
  254. move $a1,$v0
  255. jal determine
  256. move $v0,$a1
  257. lw $ra,0($sp)
  258. addi $sp,$sp,4
  259. jr $ra
  260.  
  261. determine:
  262. addi $sp,$sp,-4
  263. sw $ra,0($sp)
  264. bltz $a1,negg
  265. bgtz $a1,pos
  266. la $a0,zzero
  267. jal printDetermination
  268. negg:
  269. la $a0,negative
  270. jal printDetermination
  271. pos:
  272. la $a0,positive
  273. jal printDetermination
  274. lw $ra,0($sp)
  275. addi $sp,$sp,4
  276. jr $ra
  277.  
  278.  
  279. printDetermination:
  280. addi $sp,$sp,-4
  281. sw $ra,0($sp)
  282. li $v0,4
  283. syscall
  284. lw $ra,0($sp)
  285. addi $sp,$sp,4
  286. jr $ra
  287.  
  288. add:
  289. addi $sp,$sp,-4
  290. sw $ra,0($sp)
  291. add $a1,$t1,$t2
  292. jal outputSum
  293. lw $ra,0($sp)
  294. addi $sp,$sp,4
  295. jr $ra
  296. outputSum:
  297. addi $sp,$sp,-4
  298. sw $ra,0($sp)
  299. la $a0,sumMess
  300. li $v0,4
  301. syscall
  302. li $v0,1
  303. move $a0,$a1
  304. syscall
  305. la $a0,nl
  306. li $v0,4
  307. syscall
  308. lw $ra,0($sp)
  309. addi $sp,$sp,4
  310. jr $ra
  311. .data
  312. aprompt: .asciiz "Enter a number: "
  313. sumMess: .asciiz "The sum is: "
  314. negative: .asciiz "number is negative\n"
  315. positive: .asciiz "number is positive\n"
  316. zzero: .asciiz "number is zero\n "
  317. nl: .asciiz "\n"
  318.  
  319.  
  320. ------------------------------------------------------
  321.  
  322. .text
  323. .globl main
  324.  
  325. main:
  326. la $a0, enter_ISBN #ask for ISBN
  327. li $v0, 4
  328. syscall
  329. la $a0, Inputstring #where to put input
  330. li $a1, 20 # Length in a1-last must be enter (this actually gets 19 digits).
  331. li $v0, 8 # Read Input string.
  332. syscall
  333. #count number of digits-last is check digit
  334. li $t8,20 #set counter to size of characters buffer
  335. getchar:
  336. addi $t8,$t8,-1 #go to next character
  337. bltz $t8,error #if counter goes negative no data was entered
  338. lb $t0, Inputstring($t8) # Load character from the buffer
  339. bnez $t0, knowCount #when non zero know how many characters were entered
  340. j getchar #otherwise keep looking
  341. knowCount: #number of characters is in t8 (that's the check value)
  342. addi $t8,$t8,-1 #found enter check digit is character before
  343. lb $t0,Inputstring($t8) #so get it
  344. beq $t0,'X',ten #when checkdigit is 10 place an X
  345. subi $t9,$t0,48 #get check value (48 = '0') for 0-9
  346. j checkISBN
  347. ten:
  348. li $t9,10 #checkdigit is X when calcuted as 10
  349. checkISBN:
  350. la $t0,Inputstring #index into ISBN t0
  351. add $t8,$t8,$t0 #t8 is address of checkdigit
  352. li $t1,10 #digit multiplier t1
  353. li $t2,0 #accumulater t2
  354. li $t4,'-' #will skip hyphens
  355. li $t5, '0' #min value
  356. li $t6, '9' #max value
  357.  
  358. getNum: lb $t3,0($t0)
  359. beq $t3,$t4,nextdigit #skip hyphens
  360. blt $t3,$t5,error #not a number <'0'
  361. bgt $t3,$t6,error #not a number >'9'
  362. sub $t3,$t3,'0' #make it a digit
  363. mult $t3,$t1 #multiply value of digit by reverse position
  364. mflo $t7 #get result
  365. add $t2,$t2,$t7 #and add to accumulator
  366. addi $t1,$t1,-1 #next multiplier
  367. nextdigit: addi $t0,$t0,1 #go to next digit
  368. beq $t0,$t8,calcDone #up to checkdigit if yes done
  369. j getNum #if not process next digit
  370. calcDone:
  371. li $t0,11 #need mod 11
  372. div $t2,$t0 #divide accumulator by 11
  373. mfhi $t2 #and get remainder
  374. sub $t2,$t0,$t2 #subtract that from 11 to get checkdigit
  375.  
  376. digitFound:
  377. beq $t2,$t9,valid #compare to checkdigit - should be equal
  378. la $a0, no #bad isbn entered
  379. li $v0, 4
  380. syscall
  381. j end
  382. valid:
  383. la $a0, yes #valid isbn entered
  384. li $v0, 4
  385. syscall
  386. j end
  387. error: la $a0, badData #bad isbn entered
  388. li $v0, 4
  389. syscall
  390.  
  391. end:
  392. li $v0,10
  393. syscall
  394. .data
  395. badData: .asciiz "must consist of 0-9,X and hyphens only\n program exiting\n"
  396. enter_ISBN: .asciiz "Enter a 10 digit ISBN: "
  397. yes: .asciiz "You entered a valid ISBN\n"
  398. no: .asciiz "You entered an invalid ISBN\n"
  399. .align 2
  400. Inputstring: .space 20 # Space for input ISBN
  401.  
  402.  
  403.  
  404.  
  405. ---------------------------------
  406.  
  407.  
  408. .text
  409. .globl main
  410.  
  411. main: # get the values into registers
  412. la $a0,getYear
  413. jal print #ask for year
  414. jal input #and get it
  415. beqz $v0,exit #sentinel(=0) exit
  416. #check for leap year
  417. move $a0,$v0 #move year to a0
  418. li $v0,1
  419. syscall #and print it
  420. #divisible by 400?
  421. li $a1,400 #check divisible by 400
  422. jal divide
  423. move $s0,$v0 #save the results
  424. #divisible by 100?
  425. li $a1,100
  426. jal divide #check if divisible by 100
  427. move $s1,$v0
  428. #divisible by 4?
  429. li $a1,4
  430. jal divide #check if divisible by 4
  431. move $a0,$s0 #move results to arguments
  432. move $a1,$s1
  433. move $a2,$v0
  434. jal check
  435. beqz $v0,notLeap #if 0 returned not a leapyear
  436. #is a leap year
  437. la $a0,is
  438. jal print #report a leap year
  439. b main #and try again
  440. #not a leap year
  441. notLeap:
  442. la $a0,isnt
  443. jal print #report not a leap year
  444. b main #and try again
  445.  
  446. #print program complete and exit
  447. exit:
  448. la $a0,complete
  449. li $v0,4
  450. syscall #print complete
  451. li $v0,10 #exit program
  452. syscall
  453.  
  454. check:
  455. #check criterion (100 and 400) or (4 and not 100)
  456. and $t0,$a0,$a1 #100 and 400
  457. not $t2,$a1 #not 100
  458. and $t1,$t2,$a2 #4 and not 100
  459. or $v0,$t0,$t1 #(100 and 400) or (4 and not 100)
  460. jr $ra
  461.  
  462.  
  463. divide:
  464. move $t0,$s0 #save s registers I am using, as defined by assignment
  465. move $t1,$s1
  466. div $a0,$a1 #divide year by indicated value
  467. mfhi $t2 #get the remainder to see if evenly divisible
  468. beqz $t2,true #evenly divisible if no remaider so return 1 for true
  469. move $v0,$zero #otherwise return false
  470. j restore
  471. true:
  472. li $v0,1 #return true
  473. restore:
  474. move $s0,$t0 #restore only s registers I am using
  475. move $s1,$t1
  476. jr $ra #and return
  477. print:
  478. li $v0,4
  479. syscall #print the message who's address is in a0
  480. jr $ra #and return
  481. input:
  482. li $v0,5 #get a number
  483. syscall
  484. bgez $v0,returnInput #valid input (not <0)
  485. la $a0,error
  486. li $v0,4
  487. syscall #print error message
  488. b input #reget input
  489.  
  490. returnInput:
  491. jr $ra #return to caller
  492.  
  493.  
  494. .data
  495. getYear: .asciiz "Enter a year(0 to exit): "
  496. error: .asciiz "invalid entry-must be >=0)\nEnter a year(0 to exit): "
  497. is: .asciiz " is a leap year\n\n"
  498. isnt: .asciiz " is not a leap year\n\n"
  499. complete: .asciiz "program complete"
  500.  
  501.  
  502.  
  503. ----------------------------
  504.  
  505.  
  506. #Input 2 digits, each as a character, convert them to numbers, add and output
  507.  
  508. .text
  509. .globl main
  510.  
  511. main:
  512. la $a0, m1 #what to output (contents of m1)
  513. li $v0, 4 # Display a message.
  514. syscall
  515.  
  516. la $a0, Instr #where to put input
  517. li $a1, 3 # Length in a1-last must be enter (this actually # gets 3 digits).
  518. li $v0, 8 # Read Input string.
  519. syscall
  520.  
  521. la $t1, Instr #get address of input
  522. li $t2,48 # amount to subtract to make ASCII decimal
  523. addi $a1,$a1,-1 #since actually got 3 digits set counter to 3
  524. getdigit:
  525. lb $t0, ($t1) # Load from the address in t1
  526. sub $t0, $t0,$t2 #make it a digit
  527.  
  528. la $a0, m6 # print a newline.
  529. li $v0 4
  530. syscall
  531.  
  532. move $a0, $t0
  533. li $v0 1 #print number
  534. syscall
  535.  
  536. addi $t1,$t1,1 #go to next digit
  537. addi $a1,$a1,-1 #is it a digit
  538. bgt $a1, $0, getdigit #if not done continue
  539. end:
  540. la $a0, m8 #Prints Goodbye.
  541. li $v0 4
  542. syscall
  543.  
  544. la $v0,10
  545. syscall # au revoir...
  546.  
  547. ###########################################################
  548. # data segment
  549. ###########################################################
  550.  
  551. .data
  552. m1: .asciiz "Enter a Number \n "
  553. m6: .asciiz "\nThe digit is: \n "
  554. m8: .asciiz "\nGoodbye!\n" #For exiting.
  555. .align 2
  556. Instr: .space 10 # Space for in string.
  557.  
  558. ## end of file.
  559.  
  560.  
  561.  
  562. -----------------------------
  563.  
  564. .text
  565. .globl main
  566.  
  567. main:
  568. li $t6,6 #there are 6 counters and vowels
  569. la $a0, enter_word #what to output
  570. li $v0, 4 # Display a message, to enter a word.
  571. syscall
  572.  
  573. la $a0, Inputstring #where to put input
  574. li $a1, 16 # Length in a1-last must be enter (this actually gets 15 digits).
  575. li $v0, 8 # Read word.
  576. syscall
  577.  
  578. li $t1,0 #set counter to go through input buffer
  579. li $t2,97 #ascii value of a
  580. li $t3,122 #ascii value of z
  581. lb $t4,nl($zero) #ascii value of \n
  582. li $a0,0 #total vowels found
  583. getchar:
  584. lb $t0, Inputstring($t1) # Load character from the buffer
  585. beq $t0,$t4,donechecking #\n so done
  586. blt $t0,$t2,nextchar #outside range go to next character
  587. bgt $t0,$t3,nextchar #outside range go to next character
  588. #do table look up
  589. jal lookup
  590. nextchar:
  591. addi $t1,$t1,1
  592. j getchar
  593. lookup:
  594. li $t7,-1 #counter through vowels
  595. lookagain:
  596. addi $t7,$t7,1 #go to next vowel
  597. beq $t7,$t6,return #check a,e,i,o,u,y?
  598. lb $t8,vowels($t7) #get vowel to check
  599. bne $t8,$t0,lookagain #letter not that vowel
  600. addi $a0,$a0,1 #a vowel found
  601. lb $t9,counters($t7) #bump that letters counter
  602. addi $t9,$t9,1
  603. sb $t9,counters($t7)
  604. return:
  605. jr $ra
  606. donechecking:
  607. la $t1,counters
  608. lb $t2,5($t1) #if > 1 vowel and not y reset y to 0
  609. beq $a0,$t2,noY #y only vowel so don't reset
  610. sb $zero,5($t1) #clear y counter since other vowels in word
  611. noY:
  612. la $a0, vowelsm #Prints The vowels are:
  613. li $v0 4
  614. syscall
  615.  
  616. li $t0,-1
  617. next:
  618. addi $t0,$t0,1
  619. beq $t0,$t6,end
  620. lb $t1, counters($t0) #get counter
  621. beqz $t1,next #don't print if 0
  622. la $a0,vowelbuffer #get the vowel
  623. lb $t2, vowels($t0)
  624. sb $t2,vowelbuffer($zero) #put in buffer to output
  625. li $v0,4
  626. syscall
  627. li $v0,1 #output vowel
  628. move $a0,$t1
  629. syscall
  630. la $a0,nl #and count
  631. li $v0,4
  632. syscall
  633. j next
  634.  
  635. end:
  636. li $v0,10
  637. syscall
  638. .data
  639. enter_word: .asciiz "Enter a word\n "
  640. vowelsm: .asciiz "the vowels and their count are\n "
  641. nl: .asciiz "\n"
  642. .align 4
  643. vowels: .byte 'a','e','i','o','u','y'
  644. counters: .byte 0,0,0,0,0,0
  645. vowelbuffer: .asciiz " - "
  646. .align 4
  647. Inputstring: .space 16 # Space for input string.
  648.  
  649.  
  650. ---------------------------------------
  651.  
  652.  
  653.  
  654. .text
  655. .globl main
  656.  
  657. main:
  658. la $a0,getNum # Ask what number
  659. li $v0, 4
  660. syscall
  661.  
  662. li $v0, 5 #get rhe number
  663. syscall
  664. move $a0,$v0
  665. jal fib
  666. move $t0,$v0
  667. la $a0,value # print to solution
  668. li $v0, 4
  669. syscall
  670.  
  671. li $v0, 1 #print the number
  672. move $a0,$t0
  673. syscall
  674. la $v0,10
  675. syscall #exit.
  676.  
  677. fib:
  678. addi $sp,$sp,-12
  679. sw $a0, 0($sp) #save n and return address
  680. sw $ra, 4($sp)
  681. #base?
  682. beqz $a0,base
  683. beq $a0,1,base
  684. addi $a0,$a0,-1
  685. jal fib #get fib(n-1)
  686. sw $v0,8($sp) #save returned value in stack for n
  687. addi $a0,$a0,-1
  688. jal fib
  689. lw $t0,8($sp)
  690. add $v0,$t0,$v0
  691. j return
  692. base:
  693. move $v0,$a0
  694.  
  695. return:
  696. lw $a0, 0($sp) #restore all from stack
  697. lw $ra, 4($sp)
  698. addi $sp,$sp,12
  699. jr $ra
  700. .data
  701. getNum: .asciiz "Enter a Number: "
  702. value: .asciiz "\nThe value is: "
  703.  
  704.  
  705. ------------------------------------------------
  706.  
  707.  
  708. .text
  709. .globl main
  710. main:
  711. la $a0,enterN #ask for starting number
  712. li $v0,4
  713. syscall
  714. li $v0,5 #get it
  715. syscall
  716. move $t1,$v0 #and save in t1
  717. jal hail # call hailstone
  718. li $v0,10 #and exit
  719. syscall
  720. # int hailstone(int n)
  721. # {cout<<n<<"\n";
  722. # if(n==1)
  723. # return n;
  724. # else if(n%2==0)
  725. # return hailstone(n/2);
  726. # else
  727. # return hailstone(n*3+1);
  728. # }
  729. hail: addi $sp,$sp,-8 #allocate room in stack for return address and parameter
  730. sw $ra,4($sp) #save return address
  731. sw $t1,0($sp) #and parameter
  732. move $a0,$t1 #output number just got
  733. # li $v0,1
  734. # syscall
  735. # la $a0,nl
  736. # li $v0,4
  737. # syscall #and go to next line
  738. li $t2,1 #ending number
  739. li $t4,3 #to multiply by 3
  740. li $t5,2 #to divide by 2
  741. beq $t1,$t2,return1 #n=1 done
  742. andi $t3,$t1,1 #get lest significant digit
  743. beqz $t3,even #if even branch
  744. odd:
  745. mult $t1,$t4 #n=n*3+1
  746. mflo $t1
  747. addi $t1,$t1,1
  748. jal hail #next number
  749. j return #and exit
  750. even:
  751. #n=n/2
  752. srl $t1,$t1,1
  753. jal hail #next number
  754. j return #and exit
  755. return1:
  756. la $a0,reversed
  757. li $v0,4
  758. syscall
  759. move $a0,$t1 #print the 1
  760. li $v0,1
  761. syscall
  762. la $a0,nl
  763. li $v0,4
  764. syscall #and go to next line
  765. return:
  766.  
  767. addi $sp,$sp,8 #when come back advance stack pointer this is bypassing number on the top
  768. #should be after the lw
  769. lw $ra,4($sp) #restore return address
  770. lw $a0,0($sp) #and value of n just used to print
  771. li $v0,1
  772. syscall
  773. la $a0,nl
  774. li $v0,4
  775. syscall #and go to next line
  776. jr $ra #return to where came from
  777.  
  778. .data
  779. enterN: .asciiz "Enter a starting value: "
  780. reversed: .asciiz "reversed\n"
  781. nl: .asciiz "\n"
  782.  
  783.  
  784.  
  785. ------------------------------------------------------------
  786.  
  787. .text
  788. .globl main
  789. main:
  790. la $a0,aprompt
  791. jal input #get A
  792. move $t1,$v0 #t1 has A
  793. la $a0,bprompt
  794. jal input #get B
  795. move $t2,$v0 #t2 has B
  796. jal getGCD
  797. la $a0,gcdMess
  798. li $v0,4
  799. syscall
  800. li $v0,1
  801. move $a0,$t1
  802. syscall
  803. li $v0,10
  804. syscall
  805. input:
  806. li $v0,4
  807. syscall #print the prompt
  808. li $v0,5
  809. syscall #get the number
  810. jr $ra #and return
  811.  
  812. getGCD:
  813. addi $sp,$sp,-4
  814. sw $ra,0($sp)
  815. beqz $t2,return
  816. move $t3,$t2
  817. div $t1,$t2
  818. mfhi $t2
  819. move $t1,$t3
  820. jal getGCD
  821.  
  822. return:
  823. lw $ra,0($sp)
  824. addi $sp,$sp,4
  825. jr $ra #and rerturn
  826.  
  827. .data
  828. aprompt: .asciiz "Enter first number: "
  829. bprompt: .asciiz "Enter a 2nd number: "
  830. gcdMess: .asciiz "The GCD is: "
  831.  
  832.  
  833. -------------------------------------------------------------------------
  834.  
  835. #Input 2 digits, each as a character, convert them to numbers, add and output
  836.  
  837. .text
  838. .globl main
  839.  
  840. main:
  841. la $a0, m1 #what to output (contents of m1)
  842. li $v0, 4 # Display a message.
  843. syscall
  844.  
  845. la $a0, Instr #where to put input
  846. li $a1, 4 # Length in a1-last must be enter (this actually # gets 3 digits).
  847. li $v0, 8 # Read Input string.
  848. syscall
  849.  
  850. li $t1, 0 #get address of input
  851. li $t2,32 # amount to add to make lower case
  852. addi $a1,$a1,-1 #since actually got 3 digits set counter to 3
  853. getdigit:
  854. lb $t0, Instr($t1) # Load from the address in t1
  855. add $t0, $t0,$t2 #make it a lower case
  856. sb $t0,Instr($t1)
  857.  
  858.  
  859. addi $t1,$t1,1 #go to next digit
  860. addi $a1,$a1,-1 #is it a digit
  861. bgt $a1, $0, getdigit #if not done continue
  862.  
  863. la $a0, m6 #Prints message.
  864. li $v0 4
  865. syscall
  866. la $a0,Instr
  867. li $v0 4
  868. syscall
  869. end:
  870.  
  871. la $a0, m8 #Prints Goodbye.
  872. li $v0 4
  873. syscall
  874.  
  875. la $v0,10
  876. syscall # au revoir...
  877.  
  878. ###########################################################
  879. # data segment
  880. ###########################################################
  881.  
  882. .data
  883. m1: .asciiz "Enter a 3 letter word in all upper case \n "
  884. m6: .asciiz "\nIn lower case is: \n "
  885. m8: .asciiz "\nGoodbye!\n" #For exiting.
  886. .align 2
  887. Instr: .space 10 # Space for in string.
  888.  
  889. ## end of file.
Advertisement
Add Comment
Please, Sign In to add comment