CSC 311: COMPUTER ORGANIZATION AND ARCHITECTURE
Lab 9 and 10 – Due 05/04/2022, 11:59 PM
A happy number is a number defined by the following process: Starting with any positive integer,
eplace the number by the sum of the squares of its digits, and repeat the process until the number equals
1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which
this process ends in 1 are happy numbers.
Write a program to accept a number and determine if the number is "happy".
Examples:
Input: 82
82 + 22 = 64 + 4 = 68
62 + 82 = XXXXXXXXXX = 100
XXXXXXXXXX = XXXXXXXXXX = 1
Since this process ends in 1, 82 is a happy number.
Input: 4
42 = 16
12 + 62 = 1 + 36 = 37
32 + 72 = 9 + 49 = 58
52 + 82 = XXXXXXXXXX = 89
82 + 92 = XXXXXXXXXX = 145
XXXXXXXXXX = XXXXXXXXXX = 42
42 + 22 = 16 + 4 = 20
22 + 02 = 4 (Loops back to the original number and keeps repeating)
Since this process keeps repeating and never reaches 1, 4 is not a happy number.
Display the intermediate sums and whether a number is happy.
You can do that as follows: Store the original number and all the intermediate sums (i.e. sum of squares
of digits) at each step in an a
ay. Before storing it in the a
ay, check if the number already exists. If it
does, that means the numbers will keep repeating in a cycle and will never reach the sum 1. Print out the
a
ay and that the number is not a happy number. But if the sum does reach 1, print out the a
ay and that
the number is happy. To store the numbers, you can either dynamically allocate memory from heap or
use the .space directive.
Test your program on the following values:
1) Enter a number: 49
Output:
49, 97, 130, 10, 1
It is a happy number.
2) Enter a number: 50
Output:
50, 25, 29, 85, 89, 145, 42, 20, 4 ,16, 37, 58, 89
It is not a happy number.
Submission guidelines:
You need to submit your Lab9_10.asm file and a screenshot of the Run I/O display with input values:
Enter a number: 19
Enter a number: 12
Submit a single .zip file with name Lab9_10_firstname_lastname.zip (Your .zip file should contain
Lab9_10.asm and Screenshot).
Microsoft PowerPoint - Chapter_12_space
.space directive
Allot space in the data segment (static data)
Example:
.data
a
ay: .space 40
1
.space directive
.data
uffer: .space 80
.text
main:
li $v0, 5 #Read intege
syscall #Reads value into $v0
move $t0,$v0
li $s2, 0 #A
ay index
sw $t0, buffer($s2)
addi $s2, $s2, 4
li $s5, 0
lw $t3, buffer($s5)
2