stringassemblyreplacemipsqtspim

deleting a char from a "string" in mips


i want to completely remove certain characters(numbers) from a string in MIPS assembly language

i found a way to identify which ones need to be removed and tried replacing them but I haven't found an ascii value that is simply a blank (not a space as in ' ' but rather just '') and using '' results in an error. my code is below with detailed comments. it works but i havent found the right char to use. my question is is there a way to delete a char with a replacement? or do i need to do some address manipulation to lose the chars i dont want?

        .data
    
str:    .asciiz "i wa32n099na r9epl87ac2156e al6l52 15numb5e1rs in here"
    .text
    
main:   nop                     #main starts here
    
    la $a0, str             #load address of str into a0
    la $t5, str
    
    li $t1, '*'             #load '*' into t1       this is the replacing bit so i wanna load a blank here
    li $t2, '0'             #this will be used as the min val t2
    li $t3, '9'             #this is max val t3
    
loop:
    lb $t0, ($t5)           #load the first byte of str into t0
    
    beq $t0, $zero,done     #stop condition if equals \0
    
    bgt $t0,$t3,continue    #if first byte(asciival) is greater than '9' than it isnt a number, so continue. on false it will check if num is below 0
    blt $t0,$t2,continue    #if first byte(asciival) is smaller than '0' and than it isnt a number, so continue
    
                            #if we got here than byte is a num
    sb $t1, ($t5)           #store the byte in first position (of moved addr)
    
continue:   


    
    addi $t5,$t5,1          #increment the address
    j loop                  #loop up!
    
done:   


    li $v0, 4               #load string read into v0
    
    syscall
    
    jr $ra

Solution

  • If one wanted to remove an element from an array of integers/words, we would not look for a special value so as to mark that element as invalid; one would do as Peter says and close up the gap by repositioning succeeding elements, effectively shortening the array in place.

    The same applies for strings, which are also arrays: a string is an array of bytes/characters.

    To understand why it is better to remove the element rather than overwriting it, consider other things beside printing that we can do with strings: for example, we can compare them, we can ask for their length, we can extract individual bytes, search for substrings, save them to a file for later use by another program, etc..

    With funny characters in random locations all these operations would either not work or require special casing.  It is better to completely remove unwanted characters shortening the string, rather than overwrite them with characters of special meaning.


    Further, similar in that there are no zero-width ASCII characters, with an integer/word array, there are no special element values to choose — all the possible values of already have meaning as numbers.