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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:43:47+00:00 2026-05-13T18:43:47+00:00

I understand that whenever I have a function that has more than four arguments

  • 0

I understand that whenever I have a function that has more than four arguments in MIPS I should utilize the stack. However in my code below after saving the fifth argument at sw $t0, 4($sp) and do a jal sad, then right at the beginning of the sad function I adjust the stack pointer again to save the $sx registers that is used by the caller. Am I doing something wrong here?

vbsme:  subu    $sp, $sp, 8     # create space on the stack pointer
    sw  $ra, 0($sp)     # save return address   

    li  $v0, 0          # reset $v0 
    li  $v1, 0          # reset $v1
    li  $s0, 1          # i(row) = 1 
    li  $s1, 1          # j(col) = 1
    lw  $s2, 0($a0)     # row size
    lw  $s3, 4($a0)     # col size
    mul     $s4, $s2, $s3       # row * col
    li  $s5, 0          # element = 0
loop:   bgeq    $s5, $s4, exit      # if element >= row * col then exit

    subi    $a3, $s0, 1     # 4th parameter: i-1
    subi    $t0, $s1, 1     
    sw  $t0, 4($sp)     # 5th parameter: j-1

    jal     sad         # calculate the sum of absolute difference using the frame starting from row a3 and col 4($sp)

    add $s6, $s0, $s1
    andi $s7, $s6, 1
if: bneq $s7, $zero, else
inif:   bge $s1, $s2, inelse
    addi $s1, $s1, 1
    j inif1
inelse: addi $s0, $s0, 2
inif1:  subi $s7, $s0, 1  
    beq $s7, $zero, loop_back
    subi $s0, $s0, 1
    j loop_back
else:   bge $s0, $s2, inelse1
    addi $s0, $s0, 1
    j inif2
inelse1:addi $s1, $s1, 2
inif2:  subi $s7, $s1, 1  
    beq $s7, $zero, loop_back
    subi $s1, $s1, 1
    j loop_back
loop_back: addi $s5, $s5, 1
       j loop
exit:   lw  $ra, 0($sp)     # restore return address
    addi    $sp, $sp, 8     # restore stack pointer   
    jr $ra              # return    

.globl  sad
sad:    subu $sp, $sp, 32       # allocate stack space for largest function
    sw $s7, 28($sp)         # save $s7 value    
    sw $s6, 24($sp)         # save $s6 value
    sw $s5, 20($sp)         # save $s5 value
    sw $s4, 16($sp)         # save $s4 value
    sw $s3, 12($sp)         # save $s3 value
    sw $s2, 8($sp)          # save $s2 value
    sw $s1, 4($sp)          # save $s1 value
    sw $s0, 0($sp)          # save $s0 value


    #some code to be filled later



    lw $s7, 28($sp)         # restore original value of $s7 for caller
    lw $s6, 24($sp)         # restore original value of $s6 for caller
    lw $s5, 20($sp)         # restore original value of $s5 for caller
    lw $s4, 16($sp)         # restore original value of $s4 for caller
    lw $s3, 12($sp)         # restore original value of $s3 for caller
    lw $s2, 8($sp)          # restore original value of $s2 for caller
    lw $s1, 4($sp)          # restore original value of $s1 for caller
    lw $s0, 0($sp)          # restore original value of $s0 for caller
    addiu $sp, $sp, 32      # restore the caller's stack pointer
    jr $ra              # return to caller's code
  • 1 1 Answer
  • 1 View
  • 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-13T18:43:47+00:00Added an answer on May 13, 2026 at 6:43 pm

    This is how its done by gcc. For more information, you (could) should read
    the Mips ABI. Some things may differ.

    http://math-atlas.sourceforge.net/devel/assembly/mipsabi32.pdf

    By convention, the fifth argument should go on the fifth word of the stack.

    So you should

    sad:
        sub $sp,$sp,24 #24 byte stack frame
        ... some code ...
        #Convention indicates to store $a0..$a3 in A..D (see below)
        sw $a0,0(sp)
        sw $a1,4(sp)
        sw $a2,8(sp)
        sw $a3,12(sp)
    
        #Get the 5th argument
        lw $t0,40($sp) #40 : 24 + 16
    

    To store the 5th argument in the stack, you should know this:

    If vbsme is going to call another function, then the bottom 4 words of the stack should be saved for the callee to store argument values there. If more than 4 arguments are passed, then an additional word should be saved for each argument.

    vbsme's stack frame bottom part (Argument building area)
    
        |    ...       |
        ---------------
        |   5th arg    |  <---- sw      $t5,16($sp)     
        ---------------
        |     D        |
        ---------------
        |     C        |
        ---------------
        |     B        |
        ---------------
        |     A        |
        ---------------  <--sp (of vbsme stack frame)
    

    Also, the $ra register should be saved at the top of the stack, since its register 31.

    vbsme:  
         subu    $sp, $sp, 20+N # 20: space for 5 arguments, 
                                #N space for other stuff (ra,$tx, etc)
    
    
         #Set arguments (assumes 5th parameter value is in register $t5)
         subi    $a3, $s0, 1     # 4th parameter: i-1
         sw      $t5,16($sp)     #
    
        ...
     .end
    

    In response to

    Why is it that you do: 
    lw $t0,40($sp)
    to get the 5th argument, why did you add 24 to 16? when you do
    sub $sp,$sp,24 
    don't you already move
    the sp 24 place?
    

    Yes, $sp + 24 points to the base of the caller´s stack. However, thats not where I placed the fifth argument. The fifth argument is placed on the fifth word of the callers stack, thats why I add 16.

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

Sidebar

Related Questions

I understand that JVM and CLR were designed as stack-based virtual machines. When JIT
I understand that in CUDA's memory hierachy, we have things like shared memory, texture
I have a CDialog ( myDialogBox ) that has a CComboBox member ( myComboBox
We have an custom error class that is used whenever we throw an exception:
From what I understand about inheritance in C++ is that whenever the constructor of
I understand that: '\n' // literally the backslash character followed by the character for
I understand that the em measurement is a relative unit for font-size, relative to
I understand that this question may be subjective, this is why I need an
I understand that applications under the same domain name can talk to each other
I understand that storage history is something that is better to keep for vcs

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.