Part II-Midterm - Exercises - 20241 - ICT
Part II-Midterm - Exercises - 20241 - ICT
1
main:
# Print the prompt message
li a7, 4 # syscall for print string
la a0, prompt
ecall
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
2
ecall
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
find_largest_digit:
lb t2, 0(t1) # Load byte (character) from the buffer
beqz t2, print_result # If null terminator, exit loop
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
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
5
# Print newline for readability
li a7, 4 # syscall for print string
la a0, newline
ecall
.text
.globl main
main:
# Print prompt for the input number
li a7, 4 # syscall for print string
la a0, prompt
ecall
find_largest_digit:
lb t2, 0(t1) # Load byte (character) from the buffer
beqz t2, print_result # If null terminator, exit loop
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
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
# Read side B
li a7, 5 # syscall for read integer
ecall
mv t1, a0 # t1 holds side B
# Read side C
li a7, 5 # syscall for read integer
ecall
mv t2, a0 # t2 holds side C
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
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
1
3
mv t1, a0 # t1 holds the second integer N
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
1
4
mv a0,a5
li a7,1
ecall
# Print newline for readability
li a7, 4 # syscall for print string
la a0, newline
ecall
.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
.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
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
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
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
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
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
.text
.globl main
main:
2
3
# Print the prompt message
li a7, 4 # syscall for print string
la a0, prompt_power
ecall
# 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
2
4
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
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
count_elements:
2
7
bge t1, t0, print_result # If i >= size, go to print result
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 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
3
0
bge t1, t0, read_range # If i >= size, go to read range
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
count_elements:
bge t1, t0, print_result # If i >= size, go to print result
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 a newline
li a7, 4
la a0, newline
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
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
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
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 a space
li a7, 4
la a0, newline
3
6
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
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
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 a space
li a7, 4
la a0, newline
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 integer
li a7, 5 # Syscall for read_int
ecall
mv t4, a0 # Store input integer in t4
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
# 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
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
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
# 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
# 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
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
5
0
ecall
print_second_smallest:
# Print second smallest positive element
la a0, second_smallest_msg
li a7, 4 # Syscall for print_string
ecall
5
1
li a7, 4 # Syscall for print_string
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
# 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
# Read integer
li a7, 5 # Syscall for read_int
ecall
5
3
mv t6, a0 # Store input integer in t6
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
5
5
li a7, 4 # Syscall for print_string
ecall
print_second_largest:
# Print second largest negative element
la a0, second_largest_msg
li a7, 4 # Syscall for print_string
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
5
7
la s0, array # Base address for array
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
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
print_elements:
bge t1, t0, end_program # If i >= new size, end program
invalid_k:
# Print invalid position message
li a7, 4
la a0, newline
6
0
ecall
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
6
1
# Check if the array size is valid (greater than 0)
blez t1, end_program # if array size <= 0, end the program
end_input_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]
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
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
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]
end_input_loop:
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
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
end_input_loop:
6
9
add t4, t3, t4
lw t5, 0(t4) # t5 = array[i]
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
7
2
bge t1, t0, find_max_even # If i >= size, go to find max even
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
# 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
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
# 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
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
7
7
# Prompt for the character to search for
li a7, 4 # syscall for print string
la a0, prompt_char
ecall
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
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
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
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
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
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
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
# 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
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
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
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
convert_loop:
8
8
lb t2, 0(t1) # Load byte from buffer
beq t2, zero, done # If null terminator, end loop
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
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
# 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
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
print_output:
sb zero, 0(a2) # Null-terminate output string
9
3
ecall
.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
# Read string B
li a7, 8 # ecall for read string
la a0, buffer_b
li a1, 100 # Max number of bytes
ecall
9
5
beqz t1, print_output # If null terminator, go to print output
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
9
7
9
8