Member Avatar for Jeph_1

Hello, I am trying to count the number of characters in a string $a0 that return a 1 when passed to the function, otherwise return 0. Heres what I did: I dont have the correct answer __ output format must be 3, I have 11

  .text
        .globl __start
__start:                # execution starts here  */


        la $a0,ans
        li $v0,4        # print out string
        syscall

        la $a0,str
        jal strchk      # call strchk function

        move $a0,$v0
        li $v0,1        # print out returned value
        syscall

        la $a0,endl     # system call to print
        li $v0,4        # out a newline
        syscall

        li $v0,10
        syscall         # au revoir...

#
# checkch is a function which takes a character
# in a0,  and returns either 0 or 1 in v0,
# depending on the result of some test.
# Do not rely of the particular operation performed
# by checkch or the registers used.
# These will be different in other mipsmark cases.
#

checkch:
        li $v0,0
        beq  $a0,'a',yes
        beq  $a0,'e',yes
        beq  $a0,'i',yes
        beq  $a0,'o',yes
        beq  $a0,'u',yes
        jr $ra
yes:    li $v0,1
        jr $ra

#/* Any changes above this line will be discarded by
# mipsmark. Put your answer between dashed lines. */
#/*-------------- start cut ----------------------- */

strchk:

        sub $sp, $sp, 16                # adjusting a stack pointer to store an address of string and return an address
        sw $a0, 0($sp)
        sw $s0, 4($sp)
        sw $s1, 8($sp)
        sw $ra, 12($sp)

        li $s0, 0                       # for total vowel count
        move $s1, $a0                   # move address in $a0


loop:

        lb $a0, 0($s1)                  # load char in $a0
        beqz $a0, done                  # if null char, branch to done
        jal checkch                     # jump to checkch
#       add  $s0, $s0, $v0              # increment an address offset
        addi $s1, $s1, 1                # add vowel counter
#       j loop
        bne $a0, $v0, jump

jump:

        add $s0, $s0, 1
        j loop

done:

        move $v0, $s0                   # use $v0 for result
        lw  $a0, 0($sp)
        lw $s0, 4($sp)
        lw $s0, 8($sp)

        add $a0, $0, $s1

        lw $ra, 12($sp)                 # retrieve a return address
        lw $a0, 0($sp)                  # retrieve value in $a0
        add $sp, $sp, 16                # adjust stack pointer
        jr $ra                          # jump to calling




#/*
        j __start       #nasty loop if mips program not exited */
#/*--------------  end cut  -----------------------
# Any changes below this line will be discarded by
# mipsmark. Put your answer between dashed lines.

#################################################
#                                               #
#               data segment                    #
#                                               #
#################################################

        .data                   #*/
str:    .asciiz "aei0956xyz\n"
ans:    .asciiz "number of characters that pass test = "
endl:   .asciiz "\n"
#

This one is simple as checkch tells us you only consider aeiou to be characters. If you want to include all characters you don't need to call checkch at all.

You'll need to tell me what you tried.

As an example make any character a match I could break checkch() like this:

# checkch is a function which just says yes.

checkch:
yes:    li $v0,1
        jr $ra

Did you figure it out? I see your comments in the emails that DANIWEB sends out.

commented: I was able to fix it myself. Thanks +0
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.