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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:42:29+00:00 2026-06-10T17:42:29+00:00

I’ve got this program in MIPS assembly which comes from a C code that

  • 0

I’ve got this program in MIPS assembly which comes from a C code that does the simple average of the eigth arguments of the function.

average8:
    addu $4,$4,$5
    addu $4,$4,$6
    addu $4,$4,$7
    lw $2,16($sp)
    #nop
    addu $4,$4,$2
    lw $2,20($sp)
    #nop
    addu $4,$4,$2
    lw $2,24($sp)
    #nop
    addu $4,$4,$2
    lw $2,28($sp)
    #nop
    addu $2,$4,$2
    bgez $2,$L2
    addu $2,$2,7
$L2:
    sra $2,$2,3
    j $31

When the number is positve, we directly divided by 8 (shift by 3 bits), but when the number is negative, we first addu 7 then do the division.

My question is why do we add 7 to $2 when $2 is not >= 0 ?


EDIT : Here is the C code :

int average8(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
{
    return (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8) / 8;
}

note : the possible loss in the division since we are using ints instead of floats or doubles is not important in this case.

  • 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-10T17:42:31+00:00Added an answer on June 10, 2026 at 5:42 pm

    The difference appears to be accounting for the different behaviors of / 8 and >> 3 when negative numbers are involved:

    int main() {
        printf("%d\n", (-50) / 8);
        printf("%d\n", (-50) >> 3);
        printf("%d\n", (-50 + 7) >> 3);
    }
    

    gives

    -6
    -7
    -6
    

    So, the compiler wants to use the >> 3 optimization, but it’s not exactly the same as / 8, so it adds some code to correct for it.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.