CSC 2025 Programming A Letter Frequency Table Exercise

########################################

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

######################

.data

buffer: .space 400 # Input buffer (100 characters + null terminator)

prompt: .asciiz “Enter a string (empty to exit): “

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

newLine: .asciiz “\n”

space: .asciiz ” “

string1: .asciiz “\nPlease input a string: “

string2: .asciiz “\nThe compressed string is: “

string3: .asciiz “\nLetter Frequency Table\n”

buffer2: .space 400 # Updated to 400

lfTable: .space 104 # Letter frequency table (26 integers * 4 bytes each)

.text

.globl main

main:

# Print prompt

la $a0, string1

li $v0, 4

syscall

# Read user input string

li $v0, 8

la $a0, buffer

li $a1, 401 # Updated to 401 to match the buffer size

syscall

# Save the input string to s0

move $s0, $a0

# Initialize buffer2 for storing the compressed string

la $t9, buffer2

move $t8, $t9

# Process the input string

process_string:

jal compress_string

jal calculate_letter_frequency

# Print compressed string

la $a0, string2

li $v0, 4

syscall

move $a0, $t8

li $v0, 4

syscall

# Print letter frequency table

la $a0, string3

li $v0, 4

syscall

jal print_letter_frequency_table

# Check for null input string and exit if found

lb $t1, 0($s0)

beqz $t1, exit

# Read the next string and continue

j main

exit:

li $v0, 10

syscall

# Compress the string (remove non-alphabetic characters)

compress_string:

# …

# (Use the compress_string part of the code provided earlier)

# …

jr $ra

# Calculate the letter frequency

calculate_letter_frequency:

# Initialize loop index and frequency table pointer

move $t0, $t8

la $t1, lfTable

li $t6, 26

# Zero out frequency table

clear_table:

sw $zero, 0($t1)

addi $t1, $t1, 4

addi $t6, $t6, -1

bgtz $t6, clear_table

# Count letter frequencies

count_loop:

lb $t2, 0($t0) # Load character from compressed string

beqz $t2, end_count # If end of string, exit loop

jal to_lowercase # Convert to lowercase

addi $t2, $t2, -97 # Convert to index (ASCII ‘a’ = 97)

blt $t2, 0, skip # Skip if index is negative

bge $t2, 26, skip # Skip if index is greater than 25

sll $t2, $t2, 2 # Multiply index by 4 for word addressing

add $t2, $t2, $t1 # Add base address of lfTable

lw $t3, 0($t2) # Load current frequency count

addi $t3, $t3, 1 # Increment frequency count

sw $t3, 0($t2) # Store updated frequency count

j skip # Jump to skip

skip:

addi $t0, $t0, 1 # Increment string index

j count_loop

end_count:

# Convert character to lowercase

to_lowercase:

slti $t3, $t2, 65 # Check if character is less than ‘A’ (65)

bne $t3, $zero, end_to_lowercase

slti $t3, $t2, 91 # Check if character is less than ‘Z’ (91)

beq $t3, $zero, end_to_lowercase

addi $t2, $t2, 32 # Convert to lowercase

end_to_lowercase:

jr $ra

# Print letter frequency table

print_letter_frequency_table:

la $t1, lfTable

li $t6, 0

print_loop:

lw $a0, 0($t1) # Load frequency count

li $v0, 1

syscall # Print frequency count

addi $t1, $t1, 4 # Increment table index

addi $t6, $t6, 1 # Increment counter

blt $t6, 26, print_space

# Print newline and reset counter

la $a0, newLine

li $v0, 4

syscall

li $t6, 0

j print

_loop

print_space:

la $a0, space

li $v0, 4

syscall

j print_loop

########################################

# The following is part 2 from assignment 7 which is supposed to be starting point

.data

string1: .asciiz “\nPlease input a string: ” # string to ask input

string2: .asciiz “\nThe compressed string is: ” # string to show result

buffer: .space 400 # reserve 100

buffer2: .space 400 # reserve 100

.text.globl main

main: la $a0, string1 # load address of string1 for syscall

li $v0, 4 # specify Print String service

syscall

li $v0, 8 # take in input

la $a0, buffer # load byte space into address

li $a1, 400 # allot the byte space for string

la $t9,buffer2

move $t8,$t9

move $s0, $a0 # save string to s0

syscall

la $a0, string2 # load address of stmt1 for syscall

li $v0, 4 # specify Print String servicesyscall

la $a0,buffer # Load address of buffer.

li $t2, 0 # initialize the count to zero

move $t7,$a0

j loop

loop: # loop until last char in string

lb $t1, 0($t7)

beqz $t1, print

li $t2,1

li $t3,64

li $t4,96

slti $s1,$t1,91 # check if char is between ascii value 64 and 91

slt $s2,$t3,$t1

and $s3,$s1,$s2

slti $s4,$t1,123 # check if char is between ascii value 96 and 123

slt $s5,$t4,$t1

and $s6,$s4,$s5

or $s7,$s3,$s6 # doing or if char is either between 64,91 and 96,122

beqz $s7,update

sb $t1,0($t9) # then store it in char array

addi $t9,$t9,1

j update

update: addi $t7, $t7, 1

j loop

print: lb $t1, 0($t8) # get the bytes from compressed char array buffer2

bgt $t8,$t9 exit # print the string

move $a0,$t1

li $v0,11

syscall

addi $t8,$t8,1

j print

exit: li $v0,10

syscall # exit

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER