0% found this document useful (0 votes)
11 views98 pages

Part II-Midterm - Exercises - 20241 - ICT

The document outlines a series of programming exercises focused on integer numbers, including tasks such as printing divisible numbers, generating Fibonacci sequences, checking for prime and perfect numbers, and finding the largest and smallest digits in a number. It also includes functionality for triangle validation, calculating GCD and LCM, and more, all implemented using assembly language. Each exercise requires user input and provides specific outputs based on the calculations performed.

Uploaded by

Đạt Tiến
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views98 pages

Part II-Midterm - Exercises - 20241 - ICT

The document outlines a series of programming exercises focused on integer numbers, including tasks such as printing divisible numbers, generating Fibonacci sequences, checking for prime and perfect numbers, and finding the largest and smallest digits in a number. It also includes functionality for triangle validation, calculating GCD and LCM, and more, all implemented using assembly language. Each exercise requires user input and provides specific outputs based on the calculations performed.

Uploaded by

Đạt Tiến
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 98

Part II: Midterm Exercises

Type-A. Integer Numbers


1. Enter a positive integer N from the keyboard, print all the positive integers
less than N that are divisible by 3 or 5.
2. Enter a positive integer N from the keyboard, print out the Fibonacci
numbers less than N.
3. Write a function to check if a number is a prime number. Then enter two
positive integers M and N from the keyboard, print out all the prime
numbers between M and N.
4. A perfect number is a number that equals the sum of its divisors excluding
itself. Write a function to check if a number is a perfect number. Enter a
positive integer N from the keyboard, print out all the perfect numbers less
than N.
5. A number is called a lucky number if the sum of the digits in its left half
equals the sum of the digits in its right half. Enter a positive integer N from
the keyboard, check if the number is a lucky number.
6. Enter a positive integer N from the keyboard, print out the sum of the digits
in the binary representation of N.
7. Write a function to check if a number is a perfect square. Then enter a
positive integer N from the keyboard, print out all the square numbers (or
perfect squares) less than N.
.data
prompt: .asciz "Enter a positive integer N: "
newline: .asciz "\n"
buffer: .space 12 # Buffer for reading input number
yes: .asciz "YES\n"
no: .asciz "NO\n"
.text
.globl main

1
main:
# Print the prompt message
li a7, 4 # syscall for print string
la a0, prompt
ecall

# Read the input number


li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the input number n

# Initialize loop variables


li t1, 1 # Start with i = 1

loop_check:
mul t2, t1, t1 # t2 = i * i
bgt t2, t0, print_no # If i * i > n, it's not a perfect square
beq t2, t0, print_yes # If i * i == n, it's a perfect square
# Print the perfect square
mv a0, t2 # Move the perfect square to a0
li a7, 1 # syscall for print integer
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline

2
ecall

addi t1, t1, 1 # Increment i


j loop_check # Repeat the loop
print_yes:
# Print "Yes"
li a7, 4 # syscall for print string
la a0, yes
ecall
j exit_program

print_no:
# Print "No"
li a7, 4 # syscall for print string
la a0, no
ecall
exit_program:
# Exit the program
li a7, 10 # syscall for exit
ecall
8. Enter a positive integer N (with at least 3 digits) from the keyboard. Print out
the largest digit of N.
.data
prompt: .asciz "Enter a positive integer N (at least 3 digits): "
result_msg: .asciz "The largest digit is: "
newline: .asciz "\n"
buffer: .space 12 # Buffer to store the input number (up to 10 digits + null
terminator)
3
.text
.globl main

main:
# Print prompt for the input number
li a7, 4 # syscall for print string
la a0, prompt
ecall

# Read the input number


li a7, 8 # syscall for read string
la a0, buffer # Buffer to store the input number
li a1, 11 # Max number of bytes to read (10 digits + null terminator)
ecall

# Initialize largest digit to '0'


li t0, '0'

# Pointer to the start of the buffer


la t1, buffer

find_largest_digit:
lb t2, 0(t1) # Load byte (character) from the buffer
beqz t2, print_result # If null terminator, exit loop

# Check if the current character is a digit

4
li t3, '0'
li t4, '9'
blt t2, t3, next_char # If character < '0', skip
bgt t2, t4, next_char # If character > '9', skip

# Compare with the current largest digit


bgt t2, t0, update_largest

next_char:
addi t1, t1, 1 # Move to the next character
j find_largest_digit # Repeat loop

update_largest:
mv t0, t2 # Update the largest digit
j next_char

print_result:
# Print the result message
li a7, 4 # syscall for print string
la a0, result_msg
ecall

# Print the largest digit


li a7, 11 # syscall for print character
mv a0, t0 # Move largest digit to a0
ecall

5
# Print newline for readability
li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
9. Enter a positive integer N (with at least 3 digits) from the keyboard. Print out
the smallest digit of N.
.data
prompt: .asciz "Enter a positive integer N (at least 3 digits): "
result_msg: .asciz "The smallest digit is: "
newline: .asciz "\n"
buffer: .space 12 # Buffer to store the input number (up to 10 digits +
null terminator)

.text
.globl main

main:
# Print prompt for the input number
li a7, 4 # syscall for print string
la a0, prompt
ecall

# Read the input number


li a7, 8 # syscall for read string
6
la a0, buffer # Buffer to store the input number
li a1, 11 # Max number of bytes to read (10 digits + null
terminator)
ecall

# Initialize largest digit to '0'


li t0, '9'

# Pointer to the start of the buffer


la t1, buffer

find_largest_digit:
lb t2, 0(t1) # Load byte (character) from the buffer
beqz t2, print_result # If null terminator, exit loop

# Check if the current character is a digit


li t3, '0'
li t4, '9'
blt t2, t3, next_char # If character < '0', skip
bgt t2, t4, next_char # If character > '9', skip

# Compare with the current largest digit


blt t2, t0, update_largest

next_char:
addi t1, t1, 1 # Move to the next character
j find_largest_digit # Repeat loop

7
update_largest:
mv t0, t2 # Update the largest digit
j next_char

print_result:
# Print the result message
li a7, 4 # syscall for print string
la a0, result_msg
ecall

# Print the largest digit


li a7, 11 # syscall for print character
mv a0, t0 # Move largest digit to a0
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
10.Enter a positive integer N (with at least 3 digits) from the keyboard. Print out
the number formed by reversing the digits of N.
11.Enter a positive integer N (with at least 5 digits) from the keyboard, print
out the sum of odd digits and the sum of even digits of N.

8
12.Enter a positive integer N (with at least 2 digits) from the keyboard, print
out the octal representation of N.
13.Enter three positive integers A, B, and C from the keyboard. Check if A, B,
and C are the sides of a triangle. If they form a triangle, check if it is an
isosceles triangle.
.data
prompt_a: .asciz "Enter side A: "
prompt_b: .asciz "Enter side B: "
prompt_c: .asciz "Enter side C: "
triangle_msg: .asciz "The sides form a triangle.\n"
not_triangle_msg: .asciz "The sides do NOT form a triangle.\n"
isosceles_msg: .asciz "The triangle is isosceles.\n"
not_isosceles_msg: .asciz "The triangle is NOT isosceles.\n"
newline: .asciz "\n"
buffer: .space 12 # Buffer for reading input

.text
.globl main

main:
# Prompt for side A
li a7, 4 # syscall for print string
la a0, prompt_a
ecall

# Read side A
li a7, 5 # syscall for read integer
ecall
9
mv t0, a0 # t0 holds side A

# Prompt for side B


li a7, 4 # syscall for print string
la a0, prompt_b
ecall

# Read side B
li a7, 5 # syscall for read integer
ecall
mv t1, a0 # t1 holds side B

# Prompt for side C


li a7, 4 # syscall for print string
la a0, prompt_c
ecall

# Read side C
li a7, 5 # syscall for read integer
ecall
mv t2, a0 # t2 holds side C

# Check if A, B, and C can form a triangle


add t3, t0, t1 # t3 = A + B
bge t3, t2, check_bc # if A + B >= C, continue
j not_triangle # else, not a triangle

1
0
check_bc:
add t3, t1, t2 # t3 = B + C
bge t3, t0, check_ca # if B + C >= A, continue
j not_triangle # else, not a triangle

check_ca:
add t3, t2, t0 # t3 = C + A
bge t3, t1, is_triangle # if C + A >= B, it's a triangle
j not_triangle # else, not a triangle

is_triangle:
# Print triangle message
li a7, 4 # syscall for print string
la a0, triangle_msg
ecall

# Check if the triangle is isosceles


beq t0, t1, is_isosceles
beq t1, t2, is_isosceles
beq t2, t0, is_isosceles
j not_isosceles

is_isosceles:
# Print isosceles message
li a7, 4 # syscall for print string
la a0, isosceles_msg
ecall

1
1
j end

not_isosceles:
# Print not isosceles message
li a7, 4 # syscall for print string
la a0, not_isosceles_msg
ecall
j end

not_triangle:
# Print not triangle message
li a7, 4 # syscall for print string
la a0, not_triangle_msg
ecall

end:
# Exit the program
li a7, 10 # syscall for exit
ecall
14.Enter three positive integers A, B, and C from the keyboard. Check if A, B,
and C are the sides of a triangle. If they form a triangle, check if it is a right
triangle.
15.Enter two positive integers M and N from the keyboard. Print out the
greatest common divisor (GCD) of M and N.
16.Enter two positive integers M and N from the keyboard. Print out the least
common multiple (LCM) of M and N.
.data
prompt_m: .asciz "Enter the first positive integer M: "

1
2
prompt_n: .asciz "Enter the second positive integer N: "
gcd_msg: .asciz "The GCD is: "
lcm_msg: .asciz "\nThe LCM is: "
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for the first integer M
li a7, 4 # syscall for print string
la a0, prompt_m
ecall

# Read the first integer M


li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the first integer M

# Prompt for the second integer N


li a7, 4 # syscall for print string
la a0, prompt_n
ecall

# Read the second integer N


li a7, 5 # syscall for read integer
ecall

1
3
mv t1, a0 # t1 holds the second integer N

# Compute GCD using Euclidean algorithm


mul a4,t0,t1
gcd_loop:
beq t1, zero, gcd_done # If N == 0, GCD is M (t0)
rem t2, t0, t1 # t2 = M % N
mv t0, t1 #M=N
mv t1, t2 #N=M%N
j gcd_loop # Repeat the loop

gcd_done:
mv t3, t0 # t3 holds the GCD
# Print the GCD result message
div a5, a4, t3
li a7, 4 # syscall for print string
la a0, gcd_msg
ecall

# Print the GCD


mv a0, t3 # Move GCD to a0
li a7, 1 # syscall for print integer
ecall
#LCD
li a7, 4 # syscall for print string
la a0, lcm_msg
ecall

1
4
mv a0,a5
li a7,1
ecall
# Print newline for readability
li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
17.Enter two positive integers M and N from the keyboard (M is greater than
N). Find and print out the largest integer divisible by N and less than M.
.data
prompt_m: .asciz "Enter the first positive integer M: "
prompt_n: .asciz "Enter the second positive integer N: "
result_msg: .asciz "The largest integer divisible by N and less than M is: "
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for the first integer M
li a7, 4 # syscall for print string
la a0, prompt_m
ecall

1
5
# Read the first integer M
li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the first integer M

# Prompt for the second integer N


li a7, 4 # syscall for print string
la a0, prompt_n
ecall

# Read the second integer N


li a7, 5 # syscall for read integer
ecall
mv t1, a0 # t1 holds the second integer N

# Calculate largest integer divisible by N and less than M


addi t0, t0, -1 #t0=t0-1
div t2, t0, t1 #t2=t0/t1
mul a3, t2, t1
print_result:
# Print the result message
li a7, 4 # syscall for print string
la a0, result_msg
ecall

# Print the largest integer divisible by N and less than M


mv a0, a3 # Move the result to a0
li a7, 1 # syscall for print integer
1
6
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
18.Enter two positive integers M and N from the keyboard (M is greater than
N). Find and print out the smallest integer that is a divisor of M and greater
than N.
.data
prompt_m: .asciz "Enter the first positive integer M: "
prompt_n: .asciz "Enter the second positive integer N: "
result_msg: .asciz "The smallest integer that is a divisor of M and greater
than N is: "
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for the first integer M
li a7, 4 # syscall for print string
la a0, prompt_m
ecall

1
7
# Read the first integer M
li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the first integer M

# Prompt for the second integer N


li a7, 4 # syscall for print string
la a0, prompt_n
ecall

# Read the second integer N


li a7, 5 # syscall for read integer
ecall
mv t1, a0 # t1 holds the second integer N

addi t2, t1, 1 # Start checking from N + 1

loop:
# Check if t2 is a divisor of M
rem a3, t0, t2 # a3 = M % t2
beqz a3, print_result # If M % t2 == 0, print the result
addi t2, t2, 1 # Increment t2
j loop # Repeat the loop

print_result:
# Print the result message

1
8
li a7, 4 # syscall for print string
la a0, result_msg
ecall

# Print the smallest divisor of M greater than N


mv a0, t2 # Move the result to a0
li a7, 1 # syscall for print integer
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

exit:
# Exit the program
li a7, 10 # syscall for exit
ecall
19.Enter a positive integer N from the keyboard, print out the smallest prime
number greater than N.
.data
prompt_prime: .asciz "Enter a positive integer N: "
result_prime: .asciz "The smallest prime number greater than N is: "
newline: .asciz "\n"

.text
.globl main

1
9
main:
# Prompt for the integer N
li a7, 4 # syscall for print string
la a0, prompt_prime
ecall

# Read the input number


li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the input number N

# Find the smallest prime number greater than N


addi t0, t0, 1 # Start checking from N+1

find_prime:
li t1, 2 # Start with the first divisor t1 = 2
li t2, 1 # Assume prime is true (t2 = 1)

prime_check_loop:
mul t3, t1, t1 # t3 = t1 * t1 (i * i)
bgt t3, t0, prime_done # If t1 * t1 > t0, the number is prime
rem t4, t0, t1 # t4 = t0 % t1
beq t4, zero, not_prime # If t0 % t1 == 0, it's not prime
addi t1, t1, 1 # Increment divisor
j prime_check_loop # Repeat check

not_prime:

2
0
addi t0, t0, 1 # Increment number to check the next number
j find_prime

prime_done:
# Print the result message
li a7, 4 # syscall for print string
la a0, result_prime
ecall

# Print the smallest prime number


mv a0, t0 # Move the prime number to a0
li a7, 1 # syscall for print integer
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall

20.Enter a positive integer N from the keyboard, print out the smallest perfect
square greater than N.
.data
prompt_square: .asciz "Enter a positive integer N: "
result_square: .asciz "The smallest perfect square greater than N is: "
2
1
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for the integer N
li a7, 4 # syscall for print string
la a0, prompt_square
ecall

# Read the input number


li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the input number N

# Initialize loop variables


li t1, 0 # Start with i = 1

find_square:
mul t2, t1, t1 # t2 = i * i
bgt t2, t0, print_square # If i * i > N, print the perfect square
addi t1, t1, 1 # Increment i
j find_square # Repeat the loop

print_square:
# Print the result message
li a7, 4 # syscall for print string
2
2
la a0, result_square
ecall

# Print the smallest perfect square greater than N


mv a0, t2 # Move the perfect square to a0
li a7, 1 # syscall for print integer
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
21.Enter a positive integer N from the keyboard, print out the largest power of 2
less than N.
.data
prompt_power: .asciz "Enter a positive integer N: "
power_result: .asciz "The largest power of 2 less than N is: "
newline: .asciz "\n"
buffer: .space 12 # Buffer for reading input number

.text
.globl main

main:
2
3
# Print the prompt message
li a7, 4 # syscall for print string
la a0, prompt_power
ecall

# Read the input number


li a7, 5 # syscall for read integer
ecall
mv t0, a0 # t0 holds the input number N

# Initialize variables
li t1, 1 # Start with the smallest power of 2 (2^0)

find_power:
slli t2, t1, 1 # t2 = t1 * 2 (next power of 2)
bge t2, t0, print_power # If t2 >= N, print t1
mv t1, t2 # Update t1 to the new power of 2
j find_power # Repeat the loop

print_power:
li a7, 4 # syscall for print string
la a0, power_result
ecall

# Print the largest power of 2 less than N


mv a0, t1
li a7, 1 # syscall for print integer

2
4
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall

Type-B. Arrays
"Enter an array of integers" involves entering the number of elements and the
elements themselves.
1. Enter an array of integers from the keyboard. Count the elements of the
array that lie within the range (M, N), where M and N are two integers
entered from the keyboard.

.data
array: .space 400 # Reserve space for up to 100 integers (4 bytes each)
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter array element: "
prompt_m: .asciz "Enter the value of M: "
prompt_n: .asciz "Enter the value of N: "
result_msg: .asciz "Number of elements within range (M, N): "
newline: .asciz "\n"

.text

2
5
.globl main

main:
# Prompt for array size
li a7, 4 # Syscall number for print_string
la a0, prompt_size # Load address of prompt_size
ecall # Print the prompt
li a7, 5 # Syscall to read integer
ecall # Read array size from user
mv t0, a0 # Store array size in t0

# Initialize pointer to array storage in memory


la s0, array # Base address for array

# Read array elements


li t1, 0 # Loop counter (i = 0)
read_elements:
bge t1, t0, read_range # If i >= size, go to read range

# Prompt for array element


li a7, 4
la a0, prompt_element
ecall

# Read array element


li a7, 5
ecall
nop # Add a small delay to process input correctly

2
6
sw a0, 0(s0) # Store element in array
addi s0, s0, 4 # Move to next array position
addi t1, t1, 1 # Increment loop counter
j read_elements # Repeat loop

read_range:
# Prompt for M
li a7, 4
la a0, prompt_m
ecall
li a7, 5
ecall
mv t2, a0 # Store M in t2

# Prompt for N
li a7, 4
la a0, prompt_n
ecall
li a7, 5
ecall
mv t3, a0 # Store N in t3

# Initialize variables for counting


li t4, 0 # Initialize count of elements in range
la s0, array # Reset pointer to start of array
li t1, 0 # Reset loop counter

count_elements:

2
7
bge t1, t0, print_result # If i >= size, go to print result

lw t5, 0(s0) # Load array element into t5


addi s0, s0, 4 # Move to next array element

# Check if element is within range (M, N)


bge t5, t3, next_element # If element >= N, skip to next
ble t5, t2, next_element # If element <= M, skip to next
addi t4, t4, 1 # Increment count if within range

next_element:
addi t1, t1, 1 # Increment loop counter
j count_elements # Repeat loop

print_result:
# Print result message
li a7, 4
la a0, result_msg
ecall

# Print the count of elements within range


mv a0, t4
li a7, 1
ecall

# Print a newline
li a7, 4
la a0, newline
ecall
2
8
# Exit the program
li a7, 10
ecall

# Array storage

2
9
2. Enter an array of integers from the keyboard. Count the elements of the
array that lie outside the range (M, N), where M and N are two integers
entered from the keyboard.
.data
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter array element: "
prompt_m: .asciz "Enter the value of M: "
prompt_n: .asciz "Enter the value of N: "
result_msg: .asciz "Number of elements outside range (M, N): "
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for array size
li a7, 4 # Syscall number for print_string
la a0, prompt_size # Load address of prompt_size
ecall # Print the prompt
li a7, 5 # Syscall to read integer
ecall # Read array size from user
mv t0, a0 # Store array size in t0

# Initialize pointer to array storage in memory


la s0, array # Base address for array

# Read array elements


li t1, 0 # Loop counter (i = 0)
read_elements:

3
0
bge t1, t0, read_range # If i >= size, go to read range

# Prompt for array element


li a7, 4
la a0, prompt_element
ecall

# Read array element


li a7, 5
ecall
nop # Add a small delay to process input correctly
sw a0, 0(s0) # Store element in array
addi s0, s0, 4 # Move to next array position
addi t1, t1, 1 # Increment loop counter
j read_elements # Repeat loop

read_range:
# Prompt for M
li a7, 4
la a0, prompt_m
ecall
li a7, 5
ecall
mv t2, a0 # Store M in t2

# Prompt for N
li a7, 4
la a0, prompt_n

3
1
ecall
li a7, 5
ecall
mv t3, a0 # Store N in t3

# Initialize variables for counting


li t4, 0 # Initialize count of elements outside range
la s0, array # Reset pointer to start of array
li t1, 0 # Reset loop counter

count_elements:
bge t1, t0, print_result # If i >= size, go to print result

lw t5, 0(s0) # Load array element into t5


addi s0, s0, 4 # Move to next array element

# Check if element is outside range (M, N)


ble t5, t2, outside_range # If element <= M, count as outside
bge t5, t3, outside_range # If element >= N, count as outside
j next_element # If within range, skip to next

outside_range:
addi t4, t4, 1 # Increment count if outside range

next_element:
addi t1, t1, 1 # Increment loop counter
j count_elements # Repeat loop

print_result:
3
2
# Print result message
li a7, 4
la a0, result_msg
ecall

# Print the count of elements outside range


mv a0, t4
li a7, 1
ecall

# Print a newline
li a7, 4
la a0, newline
ecall

# Exit the program


li a7, 10
ecall

# Array storage
.data
array: .space 400 # Reserve space for up to 100 integers (4 bytes each)

3. Enter an array of integers from the keyboard. Print out the pair of adjacent
elements with the largest product. For instance, enter the array [3, 6, -2, -5,
7, 3], the adjacent pair with the largest product is 7 and 3.
.data
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter array element: "

3
3
result_msg: .asciz "The adjacent pair with the largest product is: "
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for array size
li a7, 4 # Syscall number for print_string
la a0, prompt_size # Load address of prompt_size
ecall # Print the prompt
li a7, 5 # Syscall to read integer
ecall # Read array size from user
mv t0, a0 # Store array size in t0

# Check if there are at least 2 elements


li t6, 2
blt t0, t6, exit_program # If size < 2, exit program

# Initialize pointer to array storage in memory


la s0, array # Base address for array

# Read array elements


li t1, 0 # Loop counter (i = 0)
read_elements:
bge t1, t0, find_max_pair # If i >= size, go to find max pair

# Prompt for array element


li a7, 4
3
4
la a0, prompt_element
ecall

# Read array element


li a7, 5
ecall
nop # Add a small delay to process input correctly
sw a0, 0(s0) # Store element in array
addi s0, s0, 4 # Move to next array position
addi t1, t1, 1 # Increment loop counter
j read_elements # Repeat loop

find_max_pair:
# Initialize pointers and variables for maximum product
la s0, array # Reset pointer to start of array
lw t2, 0(s0) # Load first element of array into t2
lw t3, 4(s0) # Load second element of array into t3
mul t4, t2, t3 # Initial max product = array[0] * array[1]
mv t5, t2 # Store first element of max pair
mv t6, t3 # Store second element of max pair

# Loop through the array to find max product of adjacent pairs


li t1, 1 # Start at index 1
loop_find:
addi t1, t1, 1 # Move to the next pair (index i and i+1)
addi s0, s0, 4 # Increment array pointer by 4 bytes

bge t1, t0, print_result # If i >= size - 1, go to print result

3
5
lw t2, 0(s0) # Load current element into t2
lw t3, 4(s0) # Load next element into t3
mul a4, t2, t3 # Calculate product of current pair

# Check if this product is greater than the max product


bgt a4, t4, update_max # If a4 > max product, update max product
j loop_find # Otherwise, continue to next pair

update_max:
mv t4, a4 # Update max product
mv t5, t2 # Update first element of max pair
mv t6, t3 # Update second element of max pair
j loop_find # Continue to next pair

print_result:
# Print result message
li a7, 4
la a0, result_msg
ecall

# Print first element of max pair


mv a0, t5
li a7, 1
ecall

# Print a space
li a7, 4
la a0, newline
3
6
ecall

# Print second element of max pair


mv a0, t6
li a7, 1
ecall

# Print a newline
li a7, 4
la a0, newline
ecall

exit_program:
# Exit the program
li a7, 10
ecall

# Array storage
.data
array: .space 400 # Reserve space for up to 100 integers (4 bytes each)

4. Enter an array of integers from the keyboard. Print out the pair of adjacent
elements with the smallest sum. For instance, enter the array [3, 6, -2, -5, 7,
3], the adjacent pair with the smallest sum is -5 and 7.
.data
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter array element: "
result_msg: .asciz "The adjacent pair with the smallest sum is: "
newline: .asciz "\n"

3
7
space: .word 400

.text
# Prompt for array size
li a7, 4 # Syscall number for print_string
la a0, prompt_size # Load address of prompt_size
ecall # Print the prompt
li a7, 5 # Syscall to read integer
ecall # Read array size from user
mv t0, a0 # Store array size in t0

# Check if there are at least 2 elements


li t6, 2
blt t0, t6, exit_program # If size < 2, exit program

# Initialize pointer to array storage in memory


la s0, array # Base address for array

# Read array elements


li t1, 0 # Loop counter (i = 0)
read_elements:
bge t1, t0, find_min_pair # If i >= size, go to find min pair

# Prompt for array element


li a7, 4
la a0, prompt_element
ecall

# Read array element


3
8
li a7, 5
ecall
mv s11, a0 # Add a small delay to process input correctly
sw s11, 0(s0) # Store element in array
addi s0, s0, 4 # Move to next array position
addi t1, t1, 1 # Increment loop counter
j read_elements # Repeat loop

find_min_pair:
# Initialize pointers and variables for minimum sum
la s0, array # Reset pointer to start of array
lw t2, 0(s0) # Load first element of array into t2
lw t3, 4(s0) # Load second element of array into t3
add t4, t2, t3 # Initial min sum = array[0] + array[1]
mv t5, t2 # Store first element of min pair
mv t6, t3 # Store second element of min pair

# Loop through the array to find min sum of adjacent pairs


li t1, 1 # Start at index 1
loop_find:
addi t1, t1, 1 # Move to the next pair (index i and i+1)
addi s0, s0, 4 # Increment array pointer by 4 bytes

bge t1, t0, print_result # If i >= size - 1, go to print result

lw t2, 0(s0) # Load current element into t2


lw t3, 4(s0) # Load next element into t3
add a4, t2, t3 # Calculate sum of current pair

3
9
# Check if this sum is less than the min sum
blt a4, t4, update_min # If a4 < min sum, update min sum
j loop_find # Otherwise, continue to next pair

update_min:
mv t4, a4 # Update min sum
mv t5, t2 # Update first element of min pair
mv t6, t3 # Update second element of min pair
j loop_find # Continue to next pair

print_result:
# Print result message
li a7, 4
la a0, result_msg
ecall

# Print first element of min pair


mv a0, t5
li a7, 1
ecall

# Print a space
li a7, 4
la a0, newline
ecall

# Print second element of min pair


mv a0, t6
4
0
li a7, 1
ecall

# Print a newline
li a7, 4
la a0, newline
ecall

exit_program:
# Exit the program
li a7, 10
ecall

# Array storage
.data
array: .space 400 # Reserve space for up to 100 integers (4 bytes each)

5. Enter an array from the keyboard. Sort the positive elements in ascending
order while keeping the rest in their original positions. For instance, enter
the array [-1, 150, 190, 170, -2, -3, 160, 180], the result after sorting is [-1,
150,
160, 170, -2, -3, 180, 190].

6. Enter an array from the keyboard. Sort the negative elements in descending
order while keeping the rest in their original positions. For instance, enter
the array [-1, 4, -3, -2, 2, 5, 6, -4], the result after sorting is [-1, 4, -2, -3, 2, 5,
6, 4].
7. Enter an array of integers from the keyboard. Print out the sum of the
negative elements and the sum of the positive elements in the array.
.data

4
1
prompt: .asciz "Enter the number of elements in the array: "
input_prompt: .asciz "Enter an integer: "
positive_msg: .asciz "Sum of positive elements: "
negative_msg: .asciz " Sum of negative elements: "

.text
.globl main
main:
# Print prompt for array size
la a0, prompt
li a7, 4 # Syscall for print_string
ecall

# Read array size from user


li a7, 5 # Syscall for read_int
ecall
mv t0, a0 # Store array size in t0

# Initialize sums for positive and negative elements


li t1, 0 # positive_sum = 0
li t2, 0 # negative_sum = 0

# Loop to read each element and determine if it's positive or negative


li t3, 0 # Counter i = 0
read_loop:
# Check if we've read all elements
4
2
bge t3, t0, print_result

# Prompt for each element


la a0, input_prompt
li a7, 4 # Syscall for print_string
ecall

# Read integer
li a7, 5 # Syscall for read_int
ecall
mv t4, a0 # Store input integer in t4

# Check if number is positive or negative


blt t4, zero, negative # If t4 < 0, number is negative

positive:
add t1, t1, t4 # positive_sum += t4
j continue # Jump to continue

negative:
add t2, t2, t4 # negative_sum += t4

continue:
addi t3, t3, 1 # i++
j read_loop # Repeat loop

4
3
print_result:
# Print sum of positive elements
la a0, positive_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t1 # Load positive_sum


li a7, 1 # Syscall for print_int
ecall

# Print sum of negative elements


la a0, negative_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t2 # Load negative_sum


li a7, 1 # Syscall for print_int
ecall

# Exit program
li a7, 10 # Syscall for exit
ecall

8. Enter an array of integers from the keyboard. Print out the sum of the even
elements and the sum of the odd elements in the array.
.data

4
4
prompt: .asciz "Enter number of elements in the array: "
input_prompt: .asciz "Enter an integer: "
even_msg: .asciz "Sum of even elements: "
odd_msg: .asciz " Sum of odd elements: "

.text
.globl main
main:
# Print prompt for array size
la a0, prompt
li a7, 4 # Syscall for print_string
ecall

# Read array size from user


li a7, 5 # Syscall for read_int
ecall
mv t0, a0 # Store array size in t0

# Initialize sums for even and odd elements


li t1, 0 # even_sum = 0
li t2, 0 # odd_sum = 0

# Loop to read each element and determine if it's even or odd


li t3, 0 # Counter i = 0
read_loop:
# Check if we've read all elements
bge t3, t0, print_result

4
5
# Prompt for each element
la a0, input_prompt
li a7, 4 # Syscall for print_string
ecall

# Read integer
li a7, 5 # Syscall for read_int
ecall
mv t4, a0 # Store input integer in t4

# Check if number is even or odd


li t6, 2
rem t5, t4, t6 # t5 = t4 % 2
beq t5, zero, even # If t5 == 0, number is even

odd:
add t2, t2, t4 # odd_sum += t4
j continue # Jump to continue

even:
add t1, t1, t4 # even_sum += t4

continue:
addi t3, t3, 1 # i++
j read_loop # Repeat loop

print_result:
# Print sum of even elements
la a0, even_msg
4
6
li a7, 4 # Syscall for print_string
ecall

mv a0, t1 # Load even_sum


li a7, 1 # Syscall for print_int
ecall

# Print sum of odd elements


la a0, odd_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t2 # Load odd_sum


li a7, 1 # Syscall for print_int
ecall

# Exit program
li a7, 10 # Syscall for exit
ecall

9. Enter an array of integers from the keyboard. Print out the position and value
of the two smallest positive elements in the array.
.data
prompt_size: .asciz "Enter number of elements in the array: "
input_prompt: .asciz "Enter an integer: "
smallest_msg: .asciz "Smallest positive element: "
second_smallest_msg: .asciz "Second smallest positive element: "

4
7
no_positive_msg: .asciz "No positive elements found."
newline: .asciz "\n" # Newline string

.text
.globl main
main:
# Print prompt for array size
la a0, prompt_size
li a7, 4 # Syscall for print_string
ecall

# Read array size from user


li a7, 5 # Syscall for read_int
ecall
mv t0, a0 # Store array size in t0

# Initialize variables for smallest and second smallest positive elements


li t1, -1 # smallest_index = -1 (no positive elements found yet)
li t2, -1 # second_smallest_index = -1 (no positive elements found yet)
li t3, 2147483647 # smallest_value = max int value
li t4, 2147483647 # second_smallest_value = max int value

# Loop to read each element and find the smallest and second smallest
positives
li t5, 0 # Counter i = 0
read_loop:
# Check if we've read all elements
bge t5, t0, print_result

4
8
# Prompt for each element
la a0, input_prompt
li a7, 4 # Syscall for print_string
ecall

# Read integer
li a7, 5 # Syscall for read_int
ecall
mv t6, a0 # Store input integer in t6

# Check if the number is positive


ble t6, zero, continue # Skip if t6 <= 0

# Update smallest and second smallest positive elements


blt t6, t3, update_smallest
blt t6, t4, update_second_smallest
j continue

update_smallest:
mv t4, t3 # second_smallest_value = smallest_value
mv t2, t1 # second_smallest_index = smallest_index
mv t3, t6 # smallest_value = t6
mv t1, t5 # smallest_index = i
j continue

update_second_smallest:
mv t4, t6 # second_smallest_value = t6

4
9
mv t2, t5 # second_smallest_index = i

continue:
addi t5, t5, 1 # i++
j read_loop # Repeat loop

print_result:
# Check if at least one positive element was found
bge t1, zero, print_smallest
# If no positive elements found
la a0, no_positive_msg
li a7, 4 # Syscall for print_string
ecall
j exit

print_smallest:
# Print smallest positive element
la a0, smallest_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t3 # Load smallest_value


li a7, 1 # Syscall for print_int
ecall

# Print a newline after smallest positive element


la a0, newline
li a7, 4 # Syscall for print_string

5
0
ecall

# Print its position


mv a0, t1 # Load smallest_index
li a7, 1 # Syscall for print_int
ecall

# Print a newline after position


la a0, newline
li a7, 4 # Syscall for print_string
ecall

# Check if second smallest element exists


bge t2, zero, print_second_smallest
j exit

print_second_smallest:
# Print second smallest positive element
la a0, second_smallest_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t4 # Load second_smallest_value


li a7, 1 # Syscall for print_int
ecall

# Print a newline after second smallest positive element


la a0, newline

5
1
li a7, 4 # Syscall for print_string
ecall

# Print its position


mv a0, t2 # Load second_smallest_index
li a7, 1 # Syscall for print_int
ecall

exit:
# Exit program
li a7, 10 # Syscall for exit
ecall

10.Enter an array of integers from the keyboard. Print out the position and value
of the two largest negative elements in the array.
.data
prompt_size: .asciz "Enter number of elements in the array: "
input_prompt: .asciz "Enter an integer: "
largest_msg: .asciz "Largest negative element: "
second_largest_msg: .asciz "Second largest negative element: "
no_negative_msg: .asciz "No negative elements found."
newline: .asciz "\n" # Newline string

.text
.globl main
main:
# Print prompt for array size
la a0, prompt_size

5
2
li a7, 4 # Syscall for print_string
ecall

# Read array size from user


li a7, 5 # Syscall for read_int
ecall
mv t0, a0 # Store array size in t0

# Initialize variables for largest and second largest negative elements


li t1, -1 # largest_index = -1 (no negative elements found yet)
li t2, -1 # second_largest_index = -1 (no negative elements found yet)
li t3, -2147483648 # largest_value = min int value
li t4, -2147483648 # second_largest_value = min int value

# Loop to read each element and find the largest and second largest negatives
li t5, 0 # Counter i = 0
read_loop:
# Check if we've read all elements
bge t5, t0, print_result

# Prompt for each element


la a0, input_prompt
li a7, 4 # Syscall for print_string
ecall

# Read integer
li a7, 5 # Syscall for read_int
ecall

5
3
mv t6, a0 # Store input integer in t6

# Check if the number is negative


bge t6, zero, continue # Skip if t6 >= 0

# Update largest and second largest negative elements


bgt t6, t3, update_largest
bgt t6, t4, update_second_largest
j continue

update_largest:
mv t4, t3 # second_largest_value = largest_value
mv t2, t1 # second_largest_index = largest_index
mv t3, t6 # largest_value = t6
mv t1, t5 # largest_index = i
j continue

update_second_largest:
mv t4, t6 # second_largest_value = t6
mv t2, t5 # second_largest_index = i

continue:
addi t5, t5, 1 # i++
j read_loop # Repeat loop

print_result:
# Check if at least one negative element was found
bge t1, zero, print_largest

5
4
# If no negative elements found
la a0, no_negative_msg
li a7, 4 # Syscall for print_string
ecall
j exit

print_largest:
# Print largest negative element
la a0, largest_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t3 # Load largest_value


li a7, 1 # Syscall for print_int
ecall

# Print a newline after largest negative element


la a0, newline
li a7, 4 # Syscall for print_string
ecall

# Print its position


mv a0, t1 # Load largest_index
li a7, 1 # Syscall for print_int
ecall

# Print a newline after position


la a0, newline

5
5
li a7, 4 # Syscall for print_string
ecall

# Check if second largest element exists


bge t2, zero, print_second_largest
j exit

print_second_largest:
# Print second largest negative element
la a0, second_largest_msg
li a7, 4 # Syscall for print_string
ecall

mv a0, t4 # Load second_largest_value


li a7, 1 # Syscall for print_int
ecall

# Print a newline after second largest negative element


la a0, newline
li a7, 4 # Syscall for print_string
ecall

# Print its position


mv a0, t2 # Load second_largest_index
li a7, 1 # Syscall for print_int
ecall

exit:

5
6
# Exit program
li a7, 10 # Syscall for exit
ecall

11.Enter an array of integers and a number K from the keyboard. Delete the
element at position K from the array.
.data
array: .space 400 # Reserve space for up to 100 integers (4 bytes
each)
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter array element: "
prompt_k: .asciz "Enter the position K to delete (0-indexed): "
result_msg: .asciz "Modified array: "
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for array size
li a7, 4 # Syscall number for print_string
la a0, prompt_size # Load address of prompt_size
ecall # Print the prompt
li a7, 5 # Syscall to read integer
ecall # Read array size from user
mv t0, a0 # Store array size in t0

# Initialize pointer to array storage in memory

5
7
la s0, array # Base address for array

# Read array elements


li t1, 0 # Loop counter (i = 0)
read_elements:
bge t1, t0, read_k # If i >= size, go to read position K

# Prompt for array element


li a7, 4
la a0, prompt_element
ecall

# Read array element


li a7, 5
ecall
nop # Add a small delay to process input correctly
sw a0, 0(s0) # Store element in array
addi s0, s0, 4 # Move to next array position
addi t1, t1, 1 # Increment loop counter
j read_elements # Repeat loop

read_k:
# Prompt for position K
li a7, 4
la a0, prompt_k
ecall
li a7, 5
ecall

5
8
mv t2, a0 # Store position K in t2

# Check if K is valid
blt t2, zero, invalid_k # If K < 0, go to invalid_k
bge t2, t0, invalid_k # If K >= size, go to invalid_k

# Delete the element at position K


la s0, array # Reset pointer to start of array
slli t3, t2, 2 # Calculate byte offset for position K (K * 4)
add t4, s0, t3 # Address of element to delete (t4)

# Shift elements to the left


addi t1, t2, 1 # Start from the next element (K + 1)
shift_elements:
bge t1, t0, update_size # If i >= size, go to update size
slli t5, t1, 2 # Calculate byte offset for position i (i * 4)
add t6, s0, t5 # Address of element to shift (t6)
lw a1, 0(t6) # Load the next element
sw a1, 0(t4) # Store it in the position of the deleted element
addi t4, t4, 4 # Move to the next position to delete
addi t1, t1, 1 # Increment loop counter
j shift_elements # Repeat shift

update_size:
# Decrease the size of the array by 1
addi t0, t0, -1 # New size is old size - 1

print_array:

5
9
# Print the modified array
li t1, 0 # Reset loop counter
la s0, array # Reset pointer to start of array

li a7, 4 # Syscall for print_string


la a0, result_msg # Load address of result message
ecall # Print the result message

print_elements:
bge t1, t0, end_program # If i >= new size, end program

lw a0, 0(s0) # Load element into a0 for printing


li a7, 1 # Syscall to print integer
ecall

# Print a newline after each element (optional)


li a7, 4
la a0, newline
ecall

addi s0, s0, 4 # Move to next element


addi t1, t1, 1 # Increment loop counter
j print_elements # Repeat print

invalid_k:
# Print invalid position message
li a7, 4
la a0, newline

6
0
ecall

# Exit the program


end_program:
li a7, 10
ecall # Exit the program

12.Enter an array of integers and a number M from the keyboard. Assume the
array is sorted in ascending order. Insert the integer M into the array while
maintaining the ascending order.
13.Enter an array of integers from the keyboard. Print out the number of distinct
elements in the array.
# dong dau tien nhap so phan tu, cac dong tiep theo nhap cac phan tu cua mang
.data
array: .space 400 # Reserve space for the array (up to 100 integers)
array_len: .word 0 # Space to store the size of the array
result: .word 0 # Space to store the number of distinct elements

.text
.global _start
_start:
# Input the size of the array
li a7, 5 # syscall: read integer
ecall
la a1, array_len
sw a0, 0(a1) # store input array size in array_len

# Load the array size into t1


lw t1, array_len

6
1
# Check if the array size is valid (greater than 0)
blez t1, end_program # if array size <= 0, end the program

# Input each element of the array


li t2, 0 # t2 will be our index for input
input_loop:
beq t2, t1, end_input_loop # if index == length, end input loop

li a7, 5 # syscall: read integer


ecall
la t3, array
slli t4, t2, 2 # calculate offset for array[t2] (t2 * 4)
add t4, t3, t4
sw a0, 0(t4) # store input element in array[t2]

addi t2, t2, 1 # increment index


j input_loop # repeat input loop

end_input_loop:

# Initialize variables for distinct count


li t2, 0 # t2 will hold the count of distinct elements

# Outer loop: Traverse each element in the array


li s0, 0 # s0 = outer loop index (i)
outer_loop:
beq s0, t1, end_outer_loop # if i == length, end the outer loop

6
2
# Load the current element
la t3, array
slli t4, s0, 2 # calculate the offset for array[i] (i * 4)
add t4, t3, t4
lw t5, 0(t4) # t5 = array[i]

# Check if the current element is distinct


li s1, 0 # s1 = inner loop index (j)
li s2, 1 # assume the element is distinct
inner_loop:
beq s1, s0, end_inner_loop # if j == i, end the inner loop
slli t6, s1, 2 # calculate offset for array[j]
add t6, t3, t6
lw t0, 0(t6) # load array[j] into t0
beq t5, t0, duplicate_found # if array[i] == array[j], not distinct
addi s1, s1, 1 # increment inner loop index (j)
j inner_loop # repeat inner loop

duplicate_found:
li s2, 0 # set flag to 0 (not distinct)

end_inner_loop:
# If the element is distinct, increment distinct count
beqz s2, skip_increment # if s2 == 0, skip increment
addi t2, t2, 1 # increment distinct count

skip_increment:
addi s0, s0, 1 # increment outer loop index (i)

6
3
j outer_loop # repeat outer loop

end_outer_loop:
# Store the result in result
la t0, result
sw t2, 0(t0) # store distinct count in result

# Output the result


lw a0, result # load result into a0 for printing
li a7, 1 # syscall: print integer
ecall

end_program:
# Exit
li a7, 10 # syscall: exit
ecall

6
4
14.Enter an array of integers from the keyboard. Print out the element that
appears the most in the array.
# dong dau tien nhap so phan tu, cac dong tiep theo nhap cac phan tu cua mang
.data
array: .space 400 # Reserve space for the array (up to 100
integers)
array_len: .word 0 # Space to store the size of the array
max_elem: .word 0 # Store the most frequent element
max_count: .word 0 # Store the count of the most frequent element

.text
.global _start
_start:
# Input the size of the array
li a7, 5 # syscall: read integer
ecall
la a1, array_len
sw a0, 0(a1) # store input array size in array_len

# Load the array size into t1


lw t1, array_len

# Check if the array size is valid (greater than 0)


blez t1, end_program # if array size <= 0, end the program

# Input each element of the array


li t2, 0 # t2 will be our index for input
input_loop:
beq t2, t1, end_input_loop # if index == length, end input loop

li a7, 5 # syscall: read integer


ecall

6
5
la t3, array
slli t4, t2, 2 # calculate offset for array[t2] (t2 * 4)
add t4, t3, t4
sw a0, 0(t4) # store input element in array[t2]

addi t2, t2, 1 # increment index


j input_loop # repeat input loop

end_input_loop:

# Initialize variables for finding most frequent element


li t2, 0 # t2 will hold the maximum frequency count

# Outer loop: Traverse each element in the array


li s0, 0 # s0 = outer loop index (i)
outer_loop:
beq s0, t1, end_outer_loop # if i == length, end the outer loop

# Load the current element


la t3, array
slli t4, s0, 2 # calculate the offset for array[i] (i * 4)
add t4, t3, t4
lw t5, 0(t4) # t5 = array[i]

# Count occurrences of array[i] in the array


li s1, 0 # s1 = inner loop index (j)
li s2, 0 # s2 = count of occurrences of array[i]
count_loop:
beq s1, t1, end_count_loop # if j == length, end count loop

slli t6, s1, 2 # calculate offset for array[j]


add s3, t3, t6
lw t0, 0(s3) # load array[j] into t0
6
6
beq t5, t0, increment_count # if array[i] == array[j], increment count
j skip_increment_count

increment_count:
addi s2, s2, 1 # increment count

skip_increment_count:
addi s1, s1, 1 # increment inner loop index (j)
j count_loop # repeat count loop

end_count_loop:
# Check if the current element's count is the highest
lw t0, max_count # load max_count into t0
blt t0, s2, update_max # if max_count < count, update max element

j skip_update_max

update_max:
la a2, max_count
la a3, max_elem
sw s2, 0(a2) # update max_count
sw t5, 0(a3) # update max_elem with the most frequent element

skip_update_max:
addi s0, s0, 1 # increment outer loop index (i)
j outer_loop # repeat outer loop

end_outer_loop:
# Output the most frequent element
lw a0, 0(a3) # load max_elem into a0 for printing
li a7, 1 # syscall: print integer
ecall

6
7
end_program:
# Exit
li a7, 10 # syscall: exit
ecall

15.Enter an array of integers from the keyboard. Print out the element that
appears the least in the array.
.data
array: .space 400 # Reserve space for the array (up to 100
integers)
array_len: .word 0 # Space to store the size of the array
min_elem: .word 0 # Store the least frequent element
min_count: .word 0 # Store the count of the least frequent element

.text
.global _start
_start:
# Input the size of the array
li a7, 5 # syscall: read integer
ecall
la a1, array_len
sw a0, 0(a1) # store input array size in array_len

# Load the array size into t1


lw t1, array_len

# Check if the array size is valid (greater than 0)


blez t1, end_program # if array size <= 0, end the program

6
8
# Input each element of the array
li t2, 0 # t2 will be our index for input
input_loop:
beq t2, t1, end_input_loop # if index == length, end input loop

li a7, 5 # syscall: read integer


ecall
la t3, array
slli t4, t2, 2 # calculate offset for array[t2] (t2 * 4)
add t4, t3, t4
sw a0, 0(t4) # store input element in array[t2]

addi t2, t2, 1 # increment index


j input_loop # repeat input loop

end_input_loop:

# Initialize variables for finding least frequent element


li t2, 0 # t2 will hold the minimum frequency count

# Outer loop: Traverse each element in the array


li s0, 0 # s0 = outer loop index (i)
outer_loop:
beq s0, t1, end_outer_loop # if i == length, end the outer loop

# Load the current element


la t3, array
slli t4, s0, 2 # calculate the offset for array[i] (i * 4)

6
9
add t4, t3, t4
lw t5, 0(t4) # t5 = array[i]

# Count occurrences of array[i] in the array


li s1, 0 # s1 = inner loop index (j)
li s2, 0 # s2 = count of occurrences of array[i]
count_loop:
beq s1, t1, end_count_loop # if j == length, end count loop

slli t6, s1, 2 # calculate offset for array[j]


add s3, t3, t6
lw t0, 0(s3) # load array[j] into t0
beq t5, t0, increment_count # if array[i] == array[j], increment count
j skip_increment_count

increment_count:
addi s2, s2, 1 # increment count

skip_increment_count:
addi s1, s1, 1 # increment inner loop index (j)
j count_loop # repeat count loop

end_count_loop:
# Check if the current element's count is the lowest or first valid count
lw t0, min_count # load min_count into t0
# Initialize min_count with a high value (0 is not a valid count)
li t0, -1
blt t0, s2, update_min # if min_count < count, update min element

7
0
j skip_update_min

update_min:
la a2, min_count
la a3, min_elem
sw s2, 0(a2) # update min_count
sw t5, 0(a3) # update min_elem with the least frequent element

skip_update_min:
addi s0, s0, 1 # increment outer loop index (i)
j outer_loop # repeat outer loop

end_outer_loop:
# Output the least frequent element
lw a0, 0(a3) # load min_elem into a0 for printing
li a7, 1 # syscall: print integer
ecall

end_program:
# Exit
li a7, 10 # syscall: exit
ecall

16.Enter an array of integers from the keyboard. Sort the array so that positive
numbers come first in ascending order, and negative numbers come last in
descending order.
17.Enter an array of integers from the keyboard. Print out the largest even
number that is smaller than all the odd numbers in the array.
7
1
18.Enter an array of integers from the keyboard. Print out the smallest odd
number that is greater than all the even numbers in the array.
.data
array: .space 400 # Reserve space for up to 100 integers (4 bytes each)
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter array element: "
result_msg: .asciz "Smallest odd number greater than all even numbers: "
not_found_msg: .asciz "No odd number found."
newline: .asciz "\n"

.text
.globl main

main:
# Prompt for array size
li a7, 4 # Syscall for print_string
la a0, prompt_size # Load address of prompt_size
ecall # Print the prompt
li a7, 5 # Syscall to read integer
ecall # Read array size from user
mv t0, a0 # Store array size in t0

# Initialize pointer to array storage in memory


la s0, array # Base address for array
li t1, 0 # Loop counter (i = 0)

# Read array elements


read_elements:

7
2
bge t1, t0, find_max_even # If i >= size, go to find max even

# Prompt for array element


li a7, 4
la a0, prompt_element
ecall

# Read array element


li a7, 5
ecall
sw a0, 0(s0) # Store element in array
addi s0, s0, 4 # Move to next array position
addi t1, t1, 1 # Increment loop counter
j read_elements # Repeat loop

find_max_even:
la s0, array # Reset pointer to start of the array
li t1, 0 # Reset loop counter
li t2, -1 # Initialize max_even to -1 (no even found)

find_even_loop:
bge t1, t0, find_smallest_odd # If i >= size, go to find smallest odd

lw t3, 0(s0) # Load array element


addi s0, s0, 4 # Move to next array element

# Check if t3 is even
andi t4, t3, 1 # Check if the least significant bit is 0 (even)

7
3
beq t4, zero, update_max_even # If even, update max_even

j next_even

update_max_even:
bgt t3, t2, save_max_even # If t3 > max_even, save it
j next_even

save_max_even:
mv t2, t3 # Update max_even with t3

next_even:
addi t1, t1, 1 # Increment loop counter
j find_even_loop # Repeat loop

find_smallest_odd:
la s0, array # Reset pointer to start of the array
li t1, 0 # Reset loop counter
li t3, 2147483647 # Initialize smallest_odd to max int (for comparison)

find_odd_loop:
bge t1, t0, check_result # If i >= size, go to check result

lw t4, 0(s0) # Load array element


addi s0, s0, 4 # Move to next array element

# Check if t4 is odd and greater than max_even


andi t5, t4, 1 # Check if the least significant bit is 1 (odd)
ble t4, t2, next_odd # If odd <= max_even, skip
7
4
# Update smallest_odd if t4 < smallest_odd
blt t4, t3, update_smallest_odd

next_odd:
addi t1, t1, 1 # Increment loop counter
j find_odd_loop # Repeat loop

update_smallest_odd:
mv t3, t4 # Update smallest_odd with t4
j next_odd

check_result:
# Print the result message
li a7, 4
la a0, result_msg
ecall

# Check if smallest_odd was updated


li t4, 2147483647 # Compare with initial value
beq t3, t4, print_not_found # If smallest_odd is unchanged, print not found

# Print smallest_odd
mv a0, t3
li a7, 1 # Syscall for print_integer
ecall

# Print a newline
j print_newline
7
5
print_not_found:
li a7, 4
la a0, not_found_msg
ecall

print_newline:
# Print a newline
li a7, 4
la a0, newline
ecall

# Exit the program


li a7, 10
ecall

Type-C. Strings
1. Enter a string, check if it is a palindrome. For instance, “abc121cba” is a
palindrome.
2. Enter a string, print out the distinct characters in the string.
3. Enter a string, print out the shortest word in the string. Assume there is only
one space between words and no space at the beginning or end of the string.
4. Enter a string, print out the longest word in the string. Assume there is only
one space between words and no space at the beginning or end of the string.
5. Enter a string, print out the most frequent uppercase letter ('A' to 'Z') in the
string and its positions.
6. Enter a string, print out the least frequent lowercase letter ('a' to 'z') in the
string and its positions.

7
6
7. Enter two strings S1 and S2, check if S2 is a substring of S1.
8. Enter a string and a character C from the keyboard. Print the occurrence of
character C in the string (case-insensitive).
.data
prompt_string: .asciz "Enter a string: "
prompt_char: .asciz "Enter a character: "
buffer: .space 100 # Space for the input string
char_buffer: .space 2 # Space for the input character (1 char + null
terminator)
result_msg: .asciz "\nOccurrences of character: "

.text
.globl main

main:
# Prompt for the input string
li a7, 4 # syscall for print string
la a0, prompt_string
ecall

# Read the input string


li a7, 8 # syscall for read string
la a0, buffer # Buffer to store the input string
li a1, 100 # Max number of bytes to read
ecall

7
7
# Prompt for the character to search for
li a7, 4 # syscall for print string
la a0, prompt_char
ecall

# Read the character


li a7, 8 # syscall for read string
la a0, char_buffer # Buffer to store the input character
li a1, 2 # Max number of bytes to read (1 char + null terminator)
ecall

# Load the character to search for


la a0, char_buffer # Load the address of char_buffer into a0
lb t1, 0(a0) # Load the character into t1
li t2, 'A' # ASCII for 'A'
li t3, 'Z' # ASCII for 'Z'

# Convert to lowercase if it's uppercase


blt t1, t2, char_done # If < 'A', skip
bgt t1, t3, char_done # If > 'Z', skip
addi t1, t1, 32 # Convert uppercase to lowercase

char_done:
# Initialize count
li t0, 0 # t0 will hold the count

7
8
la a0, buffer # Load address of the string for processing

count_loop:
lb t4, 0(a0) # Load byte (character) from the string
beqz t4, print_result # If null terminator, exit loop

# Convert string character to lowercase for comparison


li t5, 'A' # ASCII for 'A'
li t6, 'Z' # ASCII for 'Z'
blt t4, t5, check_next # If < 'A', skip
bgt t4, t6, check_next # If > 'Z', skip
addi t4, t4, 32 # Convert uppercase to lowercase

check_next:
# Compare the characters
beq t4, t1, increment_count # If they match, increment count

next_char:
addi a0, a0, 1 # Move to the next character
j count_loop # Repeat the loop

increment_count:
addi t0, t0, 1 # Increment the count
j next_char # Continue to the next character

7
9
print_result:
# Print the result message
li a7, 4 # syscall for print string
la a0, result_msg
ecall

# Print the count (character count)


mv a0, t0 # Move count to a0
li a7, 1 # syscall for print integer
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
9. Enter a string, convert the first letter of each word to uppercase and the
remaining letters to lowercase. For instance, enter the string “xIn chAO cac
bAn”, then the result is “Xin Chao Cac Ban”.

8
0
10.Enter two strings S1 and S2, check if they are the same (case-insensitive).
For instance, S1 = “xin Chao 2023”, s2 = “XIN chao 2023”, then the two
strings are the same.
.data
prompt1: .asciz "Enter the first string: "
prompt2: .asciz "Enter the second string: "
output1: .asciz "First string in lowercase: "
output2: .asciz "Second string in lowercase: "
equal_msg: .asciz "The strings are the same.\n"
not_equal_msg: .asciz "The strings are different.\n"
buffer1: .space 100 # Space for the first string
buffer2: .space 100 # Space for the second string

.text
.globl main

main:
# Prompt for the first string
li a7, 4 # ecall for print string
la a0, prompt1
ecall

# Read the first string


li a7, 8 # ecall for read string
la a0, buffer1 # Buffer to store the input
li a1, 100 # Max number of bytes to read
ecall

# Convert first string to lowercase


la a0, buffer1 # Load address of the first string
jal convert_string # Jump to convert function

8
1
# Print the first string in lowercase
li a7, 4 # ecall for print string
la a0, output1
ecall
li a7, 4 # ecall for print string
la a0, buffer1
ecall

# Prompt for the second string


li a7, 4 # ecall for print string
la a0, prompt2
ecall

# Read the second string


li a7, 8 # ecall for read string
la a0, buffer2 # Buffer to store the input
li a1, 100 # Max number of bytes to read
ecall

# Convert second string to lowercase


la a0, buffer2 # Load address of the second string
jal convert_string # Jump to convert function

# Print the second string in lowercase


li a7, 4 # ecall for print string
la a0, output2
ecall
li a7, 4 # ecall for print string
la a0, buffer2
ecall

8
2
# Compare the two strings
la a0, buffer1 # Load address of the first string
la a1, buffer2 # Load address of the second string
jal compare_strings # Jump to compare function

# Print result of comparison


beqz a0, strings_equal # If a0 is zero, strings are equal
li a7, 4 # ecall for print string
la a0, not_equal_msg
ecall
j end_program # Jump to exit

strings_equal:
li a7, 4 # ecall for print string
la a0, equal_msg
ecall

end_program:
# Exit the program
li a7, 10 # ecall for exit
ecall

# Function to convert a string to lowercase


convert_string:
lb t0, 0(a0) # Load byte (character) from the string
beqz t0, return_from_conversion # If null, exit loop

# Check if the character is uppercase (A-Z)


li t1, 'A' # Load ASCII value of 'A'
li t2, 'Z' # Load ASCII value of 'Z'

8
3
blt t0, t1, next_char # If < 'A', skip
bgt t0, t2, next_char # If > 'Z', skip

# Convert to lowercase
addi t0, t0, 32 # Convert to lowercase (A-Z to a-z)

next_char:
sb t0, 0(a0) # Store the character back to the string
addi a0, a0, 1 # Move to the next character
j convert_string # Repeat the loop

return_from_conversion:
ret # Return from the function

# Function to compare two strings


compare_strings:
lb t0, 0(a0) # Load byte from first string
lb t1, 0(a1) # Load byte from second string
beqz t0, check_end # If first string is null, check if second is also
beqz t1, strings_not_equal # If second string is null, they are not equal

# Compare characters
beq t0, t1, next_compare # If equal, continue
j strings_not_equal # If not equal, jump to not equal

next_compare:
addi a0, a0, 1 # Move to the next character of the first string
addi a1, a1, 1 # Move to the next character of the second string
j compare_strings # Repeat comparison

check_end:

8
4
# If we reached the end of the first string, check if second string is also
null
lb t1, 0(a1) # Load byte from the second string
beqz t1, strings_equal # If second string is null, they are equal
j strings_not_equal # If not, they are not equal

strings_not_equal:
li a0, 1 # Set a0 to 1 to indicate not equal
ret # Return from the function
11.Enter a string, reverse the words in the string. For instance, enter the string
“Hello World”, then the result is “olleH dlroW”.
.data
prompt: .asciz "Enter a string: "
result: .asciz "Reversed string: "
newline: .asciz "\n"
buffer: .space 256 # Buffer to store the input string

.text
.globl main

main:
# Print the prompt message
li a7, 4 # syscall for print string
la a0, prompt
ecall

# Read the input string from the console


li a7, 8 # syscall for read string
la a0, buffer
8
5
li a1, 255 # Maximum length
ecall

# Calculate the length of the input string


la a0, buffer # a0 = address of buffer
li t0, 0 # t0 = length = 0

strlen_loop:
lb t1, 0(a0) # Load byte from buffer
beq t1, zero, strlen_done # If null terminator, end loop
addi a0, a0, 1 # Move to next character
addi t0, t0, 1 # Increment length
j strlen_loop

strlen_done:
la a0, buffer # a0 = address of buffer
addi t0, t0, -1 # Exclude the null terminator

# Initialize pointers for reversing


la t1, buffer # t1 points to the start of the string
add t2, t1, t0 # t2 points to the end of the string

reverse_loop:
blt t1, t2, swap_chars # Continue if start < end
j done # Exit if start >= end

swap_chars:

8
6
lb t3, 0(t1) # Load char from start
lb t4, 0(t2) # Load char from end
sb t4, 0(t1) # Store end char at start
sb t3, 0(t2) # Store start char at end
addi t1, t1, 1 # Move start pointer right
addi t2, t2, -1 # Move end pointer left
j reverse_loop # Repeat loop

done:
# Print the result message
li a7, 4 # syscall for print string
la a0, result
ecall

# Print the reversed string


li a7, 4 # syscall for print string
la a0, buffer
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall

8
7
12.Enter a string, convert the uppercase letters to lowercase and the lowercase
letters to uppercase. All other characters remain unchanged. For instance,
enter the string “xIn chAO 2023”, then the result is “XiN Chao 2023”.
.data
prompt: .asciz "Enter a string: "
result: .asciz "Converted string: "
newline: .asciz "\n"
buffer: .space 256 # Buffer to store the input string

.text
.globl main

main:
# Print the prompt message
li a7, 4 # syscall for print string
la a0, prompt
ecall

# Read the input string from the console


li a7, 8 # syscall for read string
la a0, buffer
li a1, 255 # Maximum length
ecall

# Initialize pointers for processing


la t1, buffer # t1 points to the start of the string

convert_loop:
8
8
lb t2, 0(t1) # Load byte from buffer
beq t2, zero, done # If null terminator, end loop

# Check if uppercase (A-Z)


li t3, 'A'
li t4, 'Z'
blt t2, t3, check_lowercase
bgt t2, t4, check_lowercase
# Convert to lowercase (A-Z to a-z)
addi t2, t2, 32
sb t2, 0(t1)
j next_char

check_lowercase:
# Check if lowercase (a-z)
li t3, 'a'
li t4, 'z'
blt t2, t3, next_char
bgt t2, t4, next_char
# Convert to uppercase (a-z to A-Z)
addi t2, t2, -32
sb t2, 0(t1)

next_char:
addi t1, t1, 1 # Move to next character
j convert_loop

8
9
done:
# Print the result message
li a7, 4 # syscall for print string
la a0, result
ecall

# Print the converted string


li a7, 4 # syscall for print string
la a0, buffer
ecall

# Print newline for readability


li a7, 4 # syscall for print string
la a0, newline
ecall

# Exit the program


li a7, 10 # syscall for exit
ecall
13.Enter two strings A and B, print out the lowercase letters that appear in A
but do not in B.
.data
prompt_a: .asciz "Enter string A: "
prompt_b: .asciz "Enter string B: "
output_msg: .asciz "Lowercase letters in A but not in B: "
buffer_a: .space 100 # Space for string A
buffer_b: .space 100 # Space for string B
output_buffer: .space 100 # Buffer for output characters
9
0
.text
.globl main

main:
# Prompt for string A
li a7, 4 # ecall for print string
la a0, prompt_a
ecall

# Read string A
li a7, 8 # ecall for read string
la a0, buffer_a
li a1, 100 # Max number of bytes
ecall

# Prompt for string B


li a7, 4 # ecall for print string
la a0, prompt_b
ecall

# Read string B
li a7, 8 # ecall for read string
la a0, buffer_b
li a1, 100 # Max number of bytes
ecall

9
1
# Initialize output buffer and pointers
la a0, buffer_a # Pointer to string A
la a1, buffer_b # Pointer to string B
la a2, output_buffer # Pointer to output buffer
li t0, 0 # Counter for output buffer

# Loop through string A


loop_a:
lb t1, 0(a0) # Load character from string A
beqz t1, print_output # If null terminator, go to print output

# Check if character is lowercase


li t2, 'a'
li t3, 'z'
blt t1, t2, next_a # If < 'a', skip
bgt t1, t3, next_a # If > 'z', skip

# Check if t1 exists in string B


la t4, buffer_b # Reset pointer to string B
li t5, 0 # Flag to indicate if found

check_in_b:
lb t6, 0(t4) # Load character from string B
beqz t6, add_to_output # If null terminator, not found

# Compare characters
beq t1, t6, found_in_b # If equal, found in B

9
2
addi t4, t4, 1 # Move to next character in B
j check_in_b # Repeat check

found_in_b:
li t5, 1 # Set flag found

add_to_output:
# If not found in B, add to output
beqz t5, store_char # If not found (flag is 0), store the char

next_a:
addi a0, a0, 1 # Move to the next character in A
j loop_a # Repeat loop for A

store_char:
sb t1, 0(a2) # Store character in output buffer
addi a2, a2, 1 # Move output buffer pointer
addi t0, t0, 1 # Increment output count

j next_a # Continue to next character in A

print_output:
sb zero, 0(a2) # Null-terminate output string

# Print output message


li a7, 4 # ecall for print string
la a0, output_msg

9
3
ecall

# Print the output characters


li a7, 4 # ecall for print string
la a0, output_buffer
ecall

# Exit the program


li a7, 10 # ecall for exit
ecall
14.Enter two strings A and B, print out the uppercase letters that appear in both
A and B.
.data
prompt_a: .asciz "Enter string A: "
prompt_b: .asciz "Enter string B: "
output_msg: .asciz "Uppercase letters in both A and B: "
buffer_a: .space 100 # Space for string A
buffer_b: .space 100 # Space for string B
output_buffer: .space 100 # Buffer for output characters

.text
.globl main

main:
# Prompt for string A
li a7, 4 # ecall for print string
la a0, prompt_a
ecall
9
4
# Read string A
li a7, 8 # ecall for read string
la a0, buffer_a
li a1, 100 # Max number of bytes
ecall

# Prompt for string B


li a7, 4 # ecall for print string
la a0, prompt_b
ecall

# Read string B
li a7, 8 # ecall for read string
la a0, buffer_b
li a1, 100 # Max number of bytes
ecall

# Initialize output buffer and pointers


la a0, buffer_a # Pointer to string A
la a1, buffer_b # Pointer to string B
la a2, output_buffer # Pointer to output buffer
li t0, 0 # Counter for output buffer

# Loop through string A


loop_a:
lb t1, 0(a0) # Load character from string A

9
5
beqz t1, print_output # If null terminator, go to print output

# Check if character is uppercase


li t2, 'A'
li t3, 'Z'
blt t1, t2, next_a # If < 'A', skip
bgt t1, t3, next_a # If > 'Z', skip

# Check if t1 exists in string B


la t4, buffer_b # Reset pointer to string B
li t5, 0 # Flag to indicate if found

check_in_b:
lb t6, 0(t4) # Load character from string B
beqz t6, add_to_output # If null terminator, not found

# Compare characters
beq t1, t6, found_in_b # If equal, found in B
addi t4, t4, 1 # Move to next character in B
j check_in_b # Repeat check

found_in_b:
li t5, 1 # Set flag found

add_to_output:
# If found in B, add to output
beqz t5, next_a # If not found (flag is 0), skip storing

9
6
sb t1, 0(a2) # Store character in output buffer
addi a2, a2, 1 # Move output buffer pointer
addi t0, t0, 1 # Increment output count

next_a:
addi a0, a0, 1 # Move to the next character in A
j loop_a # Repeat loop for A

print_output:
sb zero, 0(a2) # Null-terminate output string

# Print output message


li a7, 4 # ecall for print string
la a0, output_msg
ecall

# Print the output characters


li a7, 4 # ecall for print string
la a0, output_buffer
ecall

# Exit the program


li a7, 10 # ecall for exit
ecall
15.Enter two strings A and B, print out the digits that do not appear in either A
or B.

9
7
9
8

You might also like