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 7723311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:24:39+00:00 2026-06-01T04:24:39+00:00

I can’t quite figure out what I’m doing wrong in my assembly code. I’m

  • 0

I can’t quite figure out what I’m doing wrong in my assembly code. I’m trying to write a program that compares two null terminated strings that are inputted as $a0, and $a1 sometime during the “main:” section and then calls

jal hamming

to start the program.

Basically for this section I want two strings to be compared char by char until one string hits the null terminating char. Then the program stops and returns how many chars were different until termination.

I think it has to do with the jumps that I am using, but I’m not quite sure. The program is sort of long so I took the main piece that I think is the problem (hence ignore variables like $a3, that has been initialized and defined already):

diffchar:
        li $t4, 0
        li $t5, 1

        beq $a0, $a1, samechars
        move $v0, $t5
        j diffcharend

samechars:
        move $v0, $t4

diffcharend:
        jr $ra

hamming:

absvaluedone:
        li $a2, 0
        #li $v0, 0

        move $t0, $a0
        move $t1, $a1

hammingloopbegin:
        lb $t2, 0($t0)
        lb $t3, 0($t1)
        beq $t2, $0, hammingdone
        beq $t3, $0, hammingdone

        la $a0, 0($t0)
        la $a1, 0($t1)
        jal diffchar **#this is the line that causes me problems, if I take this out it is fine**
        beq $v0, $0, next
        addiu $a2, $a2, 1

next:
        addiu $t0, $t0, 1
        addiu $t1, $t1, 1
        j hammingloopbegin

hammingdone:
        add $v0, $a2, $a3
        jr $ra

When I run my program my output looks like an infinite loop that keeps saying:

Exception occurred at PC=0x00400144
  Bad address in data/stack read: 0x10021226
  Exception 7  [Bad address in data/stack read]  occurred and ignored
Exception occurred at PC=0x00400140
  Bad address in data/stack read: 0x1002121b
  Exception 7  [Bad address in data/stack read]  occurred and ignored

I think there’s something wrong with diffchar or the process I use to jump around to diffchar. This is my first time writing assembly code so I think theres something really basic I am missing thats making this mess up. Any pointers would be great.

Thanks for the help

  • 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-06-01T04:24:41+00:00Added an answer on June 1, 2026 at 4:24 am

    You do not save the return address before jal diffchar. It returns and $ra still holds the new value. When you jr $ra again in hammingdone, you jump back to right after the diffchar call. You need to store $ra somewhere before calling diffchar and restore it after the call returns.

    Here is a good explanation of nested procedures in MIPS, which describes the problem you are experiencing and a solution using the runtime stack:

    When the jal B instruction is executed, the return address in register
    $ra for procedure A will be overwritten with the return address for
    procedure B. Procedure B will return correctly to A, but when
    procedure A executes the jr instruction, it will return again to the
    return address for B, which is the next instruction after jal B in
    procedure A. This puts procedure A in an infinite loop.

    …

    The system stack is commonly used to save return addresses. They can
    be pushed on the stack when a procedure is called and popped off to
    execute a return instruction.

    A return address in register $ra can be pushed onto the system stack
    with the following MAL code:

        sw      $ra, ($sp)
        add     $sp, -4
    

    The following code pops a return address from the top of the stack and
    returns it in register $ra:

        add     $sp, 4
        lw      $ra, ($sp)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I figure out if a function has already been assigned to an event?
Can't figure out how to do this in a pretty way : I have
Can anyone help me trying to find out why this doesn't work. The brushes
can anyone help me in trying to check whether JavaScript is enabled in client
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can somebody point me to a resource that explains how to go about having
Can a LINQ enabled app run on a machine that only has the .NET
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.