Hi Guys,
So I started writing this program where i want the user to input a string
of numbers and letters and then i want to print out just the letters from
the sting ignoring all the letters and other characters such at , ; : * &...
I got it to take the string no problem and then i did the bgt and blt 's so
that it skips all the characters i don't want it to use but when it prints out
the resulting string I'm still getting the original string ;( I have no idea
where I made the mistake :( Please give me some ideas ;) thx

.data 
ask:     .asciiz "Type a character string (<31 chars long): " 
count: 	.asciiz "number: "
maxlen:  .word 31 
buffer:  .space 31            # allocates 31 bytes of memory in the data segment 
str:     .space 31 
	

.text 
        .globl main

main: 	
	 li $v0, 4 
         la $a0, ask          	# Print "Type a character string: " 
         syscall 

         la $a0, buffer       	# $a0 holds the address of the read buffer 
         lw $a1, maxlen       	# Max number of chars (including the end of string char) 
         li $v0, 8            	# Load the service number 
         syscall              	# Call the operating system 

# Copy the buffer into str and replace lower case letters with upper case letters 

         li $t2, 58           	# the ASCII code of ":" 
	 li $t3, 47		# the ASCII code of "/" 
         li $t0, 0            	# $t0 is the index
	 li $t4, 0

	
loop:	 
	 lb $t1, buffer($t0)  	# fetch a char from the buffer 
	 blt $t1, $t2, skip   	# the char is before ":", skip it Branch on Less Than
         bgt $t1, $t3, skip   	# the char is after "/", skip it  Branch on Greater Than
	 

skip:    
	 addi $t4, $t4, 1           
	 sb $t1, str($t0)     	# and store it into str 
	 addi $t0, $t0, 1     	# increment index 
	 bne $t1, $0, loop    	# if $t1 <> "end of string" then goto loop 

	 

	 li $v0, 4 
         la $a0, str          	# Print str 
         syscall 

	 sub $t4, $t4, 2
	 li $v0, 4
	 la $a0, count
	 syscall
	 move $a0, $t4
	 li $v0, 1
	 syscall

Dani AI

Generated

Good catch, — this is a very common MIPS pitfall and easy to miss when you first start. Two things typically make the output identical to the input: (1) the branch targets sit immediately after the branch instructions so the “skip” never actually skips anything, and (2) the code reuses the same index for reading and writing so filtered characters don’t get packed into the output buffer. Fix those two and also remember to null‑terminate the output.

Recommended approach (summary):

  • Use one register as the source index (walk the input) and a separate register as the destination index (pack the output).
  • Test for letters explicitly (ASCII ranges 'A'..'Z' and 'a'..'z') and only store when the test passes.
  • Place your “skip”/“next” label so it jumps past the store, or invert the logic so you only jump to a store label when the character is a letter.
  • After the loop write a zero byte into the output buffer so printing works correctly. If you want to exclude the input newline, skip ASCII 10 (newline).

Minimal corrected example (keeps only letters and prints the count):

    la $a0, buffer
    lw $a1, maxlen
    li $v0, 8
    syscall

    li $t0, 0      # read index
    li $t2, 0      # write index

loop:
    lb $t1, buffer($t0)
    beq $t1, $zero, finish

    # if 'A' <= c <= 'Z'
    li $t3, 65
    li $t4, 90
    blt $t1, $t3, check_lower
    bgt $t1, $t4, check_lower
    j keep

check_lower:
    li $t3, 97
    li $t4, 122
    blt $t1, $t3, next_char
    bgt $t1, $t4, next_char

keep:
    sb $t1, out($t2)
    addi $t2, $t2, 1

next_char:
    addi $t0, $t0, 1
    j loop

finish:
    sb $zero, out($t2)
    # print out and print $t2 as count...

Quick debugging tips: step the loop in SPIM/PCSpim, print one character at a time to confirm the branch behavior, and display both indices while running a test input. These checks make it obvious whether the branch is skipping the store or not.

AHHH i got it ;O) :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.