Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:33:06+00:00 2026-05-26T08:33:06+00:00

Ok, so my task was to modify this code to count both upper and

  • 0

Ok, so my task was to modify this code to count both upper and lower case vowels. The point of the program is to demonstrate the use of stack to preserve data across function calls:

##
## vowel.a - prints out number of vowels in  
##         - the string str
##
##  a0 - points to the string
##

#################################################
#                                               #
#                 text segment                  #
#                                               #
#################################################

            .text           
            .globl __start 
   __start:         # execution starts here


            la $a0,str
            jal vcount      # call vcount

            move $a0,$v0
            li $v0,1
            syscall         # print answer


            la $a0,endl
            li $v0,4
            syscall         # print newline

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

           #------------------------------------------------
           # vowelp - takes a single character as a
           # parameter and returns 1 if the character 
           # is a (lower case) vowel otherwise return 0.
           #        a0 - holds character
           #        v0 - returns 0 or 1
           #------------------------------------------------

           vowelp:  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


           #------------------------------------------------
           # vcount - use vowelp to count the vowels in a
           # string.
           #        a0 - holds string address
           #        s0 - holds number of vowels
           #        v0 - returns number of vowels
           #------------------------------------------------

           vcount:  
           sub $sp,$sp,16   # save registers on stack
           sw $a0,0($sp)
           sw $s0,4($sp)
           sw $s1,8($sp)
           sw $ra,12($sp)

           li $s0,0 # count of vowels
           move $s1,$a0     # address of string

           nextc:   lb $a0,($s1)    # get each character
           beqz $a0,done    # zero marks end
           jal vowelp       # call vowelp 
           add $s0,$s0,$v0  # add 0 or 1 to count
           add $s1,$s1,1    # move along string
           b nextc
           done:    move $v0,$s0    # use $v0 for result

           lw $a0,0($sp)    # restore registers
           lw $s0,4($sp)
           lw $s1,8($sp)
           lw $ra,12($sp)
           add $sp,$sp,16
           jr $ra


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


           .data
     str:   .asciiz "long time ago in a galaxy far away"
     endl:  .asciiz "\n"

     ##
     ## end of file vowel.a

my modified code that works:

    ##
    ## vowel.a - prints out number of vowels in  
    ##         - the string str
    ##
    ##      a0 - points to the string
    ##

    #################################################
    #                                               #
    #               text segment                    #
    #                                               #
    #################################################

            .text           
            .globl __start 
    __start:                # execution starts here

            la $a0,str
            jal vcount      # call vcount

            move $a0,$v0
            li $v0,1
            syscall         # print answer

            la $a0,endl
            li $v0,4
            syscall         # print newline

            move $a0,$t0
            li $v0,1
            syscall

            la $a0,endl
            li $v0,4
            syscall

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

            vowell: 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
            vowelu:
            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

            vcount:
            sub $sp,$sp,20
            sw $a0,0($sp)
            sw $s0,4($sp)
            sw $s1,8($sp)
            sw $ra,12($sp)
            sw $s2,16($sp)

            li $s0,0
            li $s2,0
            move $s1,$a0

            nextc:
            lb $a0,($s1)
            beqz $a0,done
            jal vowell
            add $s0,$s0,$v0
            jal vowelu
            add $s2,$s2,$v0
            add $s1,$s1,1
            b nextc
            done:
            move $v0,$s0
            move $t0,$s2

            lw $a0,0($sp)
            lw $s0,4($sp)
            lw $s1,8($sp)
            lw $ra,12($sp)
            lw $s2,16($sp)
            add $sp,$sp,20
            jr $ra

            .data
            str:    .asciiz "Long Time Ago in a Galaxy Far Far Away"
            endl:   .asciiz "\n"

I don’t understand what the lw block at the end is for. The program stores the count in s0 and t0 respectively, so whats the point? It looks as though its just restoring the original values at the end. Whoop de do was that there just to demonstrate that its possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:33:06+00:00Added an answer on May 26, 2026 at 8:33 am

    I’m not familiar with MIPS assembly, but in general each platform has conventions as to how subroutines are supposed to behave. One of those conventions is usually around which CPU registers a subroutine must preserve. The conventions, taken together, form the ABI.

    Think of it this way: When you have a program with only a few subroutines, its easy enough to keep track of “yeah, this routine destroys register X” each time you call it. But as your program grows, that becomes very difficult. Imagine the difficulty of changing a function to use a new register—you’d have to then check each subroutine that calls this routine, to make sure it doesn’t rely on the register across the call. And every routine that calls those routines, etc. Change a commonly-used utility function, and you wind up having to verify the entire program; in this way lies madness.

    There are two maintainable solutions to this: either the caller saves all registers it is using, or the callee saves all registers it changes. Normally, you’d expect code to get less complex (and use less registers) the further in the call chain you get, so the callee probably has a smaller set to save. Further, the number of function calls normally exceeds the number of functions, so callee saving also produces less code. It looks like MIPS follows this logic, and requires the callee to save the registers. Sometimes, on architectures with a lot of registers (e.g., PowerPC) there are some which are considered “temporary” and thus the callee doesn’t have to save them; this is a combination of the two approaches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create and modify tasks in Windows Task Scheduler on Windows Server
I need to modify a (xml-)file from Apache Ant. loadfile task allows to load
Task at hand — I have three versions of some code, developed by different
My task is to decompress a packet(received) using zlib and then use an algoritm
Here's a link to my previous question on this same block of code with
I have taken the task of trying to get an older program to run
I have HTML code like this: <tr> <td align=left valign=bottom class=leftfooter><a href=#>Customer Support</a> <a
I have the following code which inserts a task into a table with a
System Specs and task I am using Code::Blocks on Ubuntu 10.10 and playing around
Our company use SVN for VCS, and we want this to be done: pre-commit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.