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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:10:01+00:00 2026-05-13T20:10:01+00:00

any ideas? Why I am getting: Runtime exception at 0x00400020: fetch address not aligned

  • 0

any ideas? Why I am getting:
Runtime exception at 0x00400020: fetch address not aligned on word boundary 0x00000007
Problem line is: lw $s1,0($a1) #copy arg2 = size of array

.data
    .align 4 #added this, didnt work
    size:   .word   7
    .align 4 #added this, didnt work
    search: .word   30
    .align 4 #added this,didnt work
    array:  .word 10,20,30,40,50,60,70
    .align 4

.text

main:

            la  $a0,array   #$a0 = address of array
            lw  $a1,size    #a1 = size of array
            lw  $a2,search  #$a2 = search key


COUNT:
            lw $s0,0($a0)   #copy arg1 = address array
            addi $s1,$zero,7
            lw $s1,0($a1)   #copy arg2 = size of array
            lw $s2,0($a2)   #copy arg3 = search key (n)
            addi $s2,$zero,30
            COUNTLOOP:
            add $v0,$zero,$zero #v0 = res
            add $t0,$zero,$zero #$t0 = init i to 0
            slt $t1,$t0,$s1     #check if i > size of array
            beq $t1,$zero,DONECOUNT #i is n so end
            sll $t2,$s0,2       #$t2 = get off set for a[i]
            lw  $t3,0($t2)      #$t3 = get value of a[i]
            bne $t3,$s2,CLOOPBTM #check if a[i] == seach key
            addi $v0,$v0,1      #if above then increment res
            CLOOPBTM:
            addi $t0,$t0,1
            j COUNTLOOP
            DONECOUNT:
  • 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-13T20:10:02+00:00Added an answer on May 13, 2026 at 8:10 pm

    The problem with the code is, that you’re not using the address where the size is stored but the size itself:

    Here you load the address into A0 and the size (7) into A1:

            la  $a0,array   
            lw  $a1,size    #a1 = size of array
    

    Here you load the first word stored at your array (that will load a 10). This is not what you’ve intended.

            lw $s0,0($a0)   #copy arg1 = address array
            addi $s1,$zero,7
    

    Here you load the first word stored at location 0x000007. (your size). This
    is probably also not intended and will cause an exception because the address is not aligned:

            lw $s1,0($a1)   #copy arg2 = size of array
    

    and so on.

    It seems to me, that you have a misunderstanding what the LW instruction does. It reads a memory location into a register. What you want in the prolog of your loop is to make copies of a register.

    To do so you can use the move pseudo instruction if your assembler supports it. Otherwise use the OR instruction to copy registers like this:

    COUNT:
                or    $s0, $a0, $a0   #copy arg1 = address array
                addi  $s1, $zero,7
                or    $s1, $a1, $a1   #copy arg2 = size of array
                or    $s2, $a2, $a2   #copy arg3 = search key (n)
                addi  $s2, $zero,30
                COUNTLOOP:
    
                ...
    

    for a complete example of a linear search loop try this (untested and expects that the assembler cares about the delay slots)

    main:
    
                la  $a0,array            # $a0 = address of array
                lw  $a1,size             # $a1  = size of array
                lw  $a2,search           # $a2 = search key
    
    
                beq $a1, $zero, NOTFOUND # handle the size==0 case..
                or  $v0, $zero, $zero    # init counter to zero
    
    LOOP:
                lw  $s0, 0($a0)          # load element
                beq $s0, $a2, FOUND      # branch if key found:
    
                addiu $a0, $a0, 4        # increment array pointer
                addiu $v0, $v0, 1        # increment loop counter
                bne   $v0, $a1, LOOP     # repeat until we've processed the array.
    
    NOTFOUND:
                # --------------------------------------
                # if you reach this, key does not exist:
                # --------------------------------------
                li  $v0, -1              # load a -1 to signal key not found.
                jr  $lr                  # return to caller
    
    FOUND:
                # -----------------------------------------
                # v0 now contains the position of the key.
                # -----------------------------------------
                jr  $lr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any ideas on how to disable, but not uninstall Resharper 4.x or above?
Any ideas why I am getting the below error in my MVC2 project, even
Any ideas why I'm getting .... Fatal error: Allowed memory size of 33554432 bytes
Any ideas on how to implement tab completion for a .NET (C#) Console Application?
Any ideas what the average user's download speed is? I'm working on a site
Any ideas how to determine the number of active threads currently running in an
Any ideas how to display a PDF file in a WPF Windows Application? I
Any ideas why this won't validate here: http://validator.w3.org/#validate_by_input It seems the form input tags
Any ideas why the built in asp.net webserver insists on serving /default.aspx whenever you
Any ideas on how to get the functionality of an attributed string on an

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.