logo
down
shadow

mips nested procedure with jr $ra


mips nested procedure with jr $ra

Content Index :

mips nested procedure with jr $ra
Tag : assembly , By : Tonix
Date : November 28 2020, 09:01 AM

wish help you to fix your issue jr $ra relies on the return address having been placed in $ra, but you're calling FUNC using jr $s5 which doesn't set $ra. Use jalr $s5 instead.
The b xintersect in main should probably be jal xintersect, because otherwise the jr $ra in xintersect won't work. Also keep in mind that xintersect must save $ra somewhere (e.g. on the stack) if it needs to both call FUNC and then be able to return back to main.

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

MIPS Is it elegant to use $s0... etc reginsters inside the procedure and restore it on end


Tag : assembly , By : Lee KW
Date : March 29 2020, 07:55 AM
This might help you
I read in MIPS tutorial, that only reginsters $s0-$s7 are preserved across procedure calls.

Mips datapath procedure for executing an AND instruction?


Tag : development , By : Richard Laksana
Date : March 29 2020, 07:55 AM
I hope this helps you . Starting from after the instruction is read from instruction memory, you need to know that AND is an r-type instruction and thus uses 3 registers. Which register is actually used is based off of the encoded instruction. (An R-Type has 3 5-bit fields, one for each reg.) rs and rt go to Read register 1 and 2, while rd goes to Write register. From there, Read data 1 and 2 (the bits of registers s and t) go to the ALU where a bitwise AND is performed on them. The result of that is written to the write register. I traced the path in your picture (omitting the PC incrementing part). I'm taking a class that uses that exact book this semester. If you look a little ahead, it goes deeper into what is going on. The explanation of the control (blue) lines helps a lot. The mux blocks are multiplexers, that is they allow alternating the output between two inputs. In this case, the ALUSrc mux will use Read data 2 because AND is an r-type. If it were i-type, it would switch to use the data coming from the sign extend, because that would be the immediate. The other mux is to allow either memory from data to be written to the write register or the result of an ALU operation. In this case, it will be the result of an ALU operation.

Translating blanket function from C to a MIPS Procedure


Tag : c , By : Mark
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Except for some missing boilerplate, you were very close. Here's a version annotated with the bugs:
main:
# NOTE/BUG: a0/a1 are _not_ set up for the call
    jal     countNegatives

# NOTE/BUG: we just fall through into countNegatives again [which is bad]

countNegatives:
    li      $t0,0                   # count = 0
    li      $t1,0                   # i = 0

loop:
    bge     $t1,$a1,endloop
    sll     $t2,$t1,2               # $t2 = 4*i
    add     $t2,$a0,$t2             # $t2 = &table[i]
    lw      $t3,0($t2)              # temp = table[i]
    bge     $t3,$zero,endif
    addi    $t0,$t0,1               # counter++

endif:
    addi    $t1,$t1,1               # i++
# NOTE/BUG: we need to loop here

endloop:
    jr      $ra
    .data
arr:    .word   10 20 -5 7 -6 0 1 -1 37

    .text
    .globl  main
main:
    la      $a0,arr                 # point to array
    li      $a1,9                   # array count
    jal     countNegatives

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

    li      $v0,10
    syscall

# countNegatives -- count number of negatives
#
# RETURNS:
#   v0 -- number of negative numbers found
#
# arguments:
#   a0 -- pointer to array
#   a1 -- array count
#
# temporaries:
#   t1 -- index variable "i"
#   t2 -- array offset / &table[i]
#   t3 -- temp (value of table[i])
countNegatives:
    li      $v0,0                   # count = 0
    li      $t1,0                   # i = 0

loop:
    bge     $t1,$a1,endloop         # i >= count? if yes, fly
    sll     $t2,$t1,2               # $t2 = 4*i
    addu    $t2,$a0,$t2             # $t2 = &table[i]
    lw      $t3,0($t2)              # temp = table[i]
    bge     $t3,$zero,endif
    addi    $v0,$v0,1               # counter++

endif:
    addi    $t1,$t1,1               # i++
    j       loop

endloop:
    jr      $ra
    .data
arr:    .word   10 20 -5 7 -6 0 1 -1 37

    .text
    .globl  main
main:
    la      $a0,arr                 # point to array
    li      $a1,9                   # array count
    jal     countNegatives

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

    li      $v0,10
    syscall

# countNegatives -- count number of negatives
#
# RETURNS:
#   v0 -- number of negative numbers found
#
# arguments:
#   a0 -- pointer to array
#   a1 -- array count
#
# temporaries:
#   t1 -- index variable "i"
#   t2 -- array offset / &table[i]
#   t3 -- temp (value of table[i])
countNegatives:
    li      $v0,0                   # count = 0
    li      $t1,0                   # i = 0
    j       loop_start              # start the loop

loop:
    sll     $t2,$t1,2               # $t2 = 4*i
    addu    $t2,$a0,$t2             # $t2 = &table[i]
    lw      $t3,0($t2)              # temp = table[i]
    slt     $t3,$t3,$zero           # temp = (temp < 0)
    add     $v0,$v0,$t3             # counter += temp

    addi    $t1,$t1,1               # i++

loop_start:
    blt     $t1,$a1,loop            # i < count? if yes, fly

    jr      $ra
    .data
arr:    .word   10 20 -5 7 -6 0 1 -1 37

    .text
    .globl  main
main:
    la      $a0,arr                 # point to array
    li      $a1,9                   # array count
    jal     countNegatives

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

    li      $v0,10
    syscall

# countNegatives -- count number of negatives
#
# RETURNS:
#   v0 -- number of negative numbers found
#
# arguments:
#   a0 -- pointer to array (ptr)
#   a1 -- array count
#
# temporaries:
#   a1 -- array limit (endp)
#   t3 -- temp (value of table[i])
countNegatives:
    li      $v0,0                   # count = 0
    sll     $a1,$a1,2               # get byte offset
    addu    $a1,$a0,$a1             # endp = &arr[count]
    j       loop_start              # start the loop

loop:
    lw      $t3,0($a0)              # temp = *ptr
    slt     $t3,$t3,$zero           # temp = (temp < 0)
    add     $v0,$v0,$t3             # counter += temp

    addiu   $a0,$a0,4               # ptr += 4

loop_start:
    bne     $a0,$a1,loop            # ptr != endp? if yes, fly

    jr      $ra
int
countNegatives(int *table, int n)
{
    int *endp;
    int count = 0;

    endp = &table[n];

    for (;  table != endp;  ++table)
        count += (*table < 0);

    return count;
}

How can I return an array from a MIPS procedure?


Tag : arrays , By : Paulh
Date : March 29 2020, 07:55 AM
With these it helps The standard ways are the same as what you can do in C:
have the caller pass an output pointer (to an already-allocated array, e.g. on the stack) have the function malloc an array and return a pointer. (Or mmap or whatever, but the caller has to know how the memory was allocated, because the caller then becomes responsible for freeing it.)

What is mistake of this MIPS procedure call?


Tag : development , By : afds
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The most serious violation is that func expects $a1 to survive the function call to foo — which is incorrect.  By the calling convention, $a1 is an argument register, and is not preserved by a function call.  $a1 is also not a return value, so it should be considered uninitialized after a function call, i.e. it contains uselss garbage.
func: addi $sp,$sp,-4    
      sw $ra, 0($sp)    
      addi $a0,$a0,1    
      addi $a1,$a1,2    
      jal foo           # this call effectively wipes out argument registers (ok)
      addi $a0,$v0,4    # here the function re-initializes $a0 from return value $v0 (ok)
      add $v0,$a0,$a1   # but here it uses uninitliazed $a1 (not ok)
      lw $ra,0($sp)    
      addi $sp,$sp,4    
      jr $ra
func: addiu $sp,$sp,-8    
      sw $ra, 4($sp)    
      addi $a0,$a0,1    
      addi $a1,$a1,2 
      sw $a1, 0($sp)    # save a1 in stack before call
      jal foo           # this call effectively wipes out argument registers
      lw $a1, 0($sp)    # restore a1 from stack after call
      addi $a0,$v0,4    # here the function re-initializes $a0 from return value $v0
      add $v0,$a0,$a1   # now using re-initialized $a1 (ok)
      lw $ra,4($sp)    
      addiu $sp,$sp,8    
      jr $ra
Related Posts Related QUESTIONS :
  • ARM assembly syntax in VST/VLD commands
  • Do assembly instructions map 1-1 to machine language?
  • What are the technical mechanics and operation of declaring variables in 32-bit MASM?
  • How are the SCAS and MOVS instructions affected by the value of the direction EFLAG?
  • Pointer to string in stand-alone binary code without .data section
  • How to enable MMU in QEMU (virt machine a57 cpu)
  • How to count character of a string in assembly not use 0Ah/int21h
  • NASM-64bits-segmentation fault calling procedure
  • Is it possible to map a process into memory without mapping the kernel?
  • Index an array with constants with Turbo C++ inline asm
  • SIMD vectorize atan2 using ARM NEON assembly
  • Inline Assembly Procedure Crash When Accessing Arguments
  • Assembly TASM: Multiply 4 digit numbers by 4 digit numbers
  • How do assembly instruction differentiate between register, memory address, immediate value or offset parameter?
  • How can segmentation be used to expand memory?
  • Print spaces between every numbers that's printed in 8086 Assembly
  • the why of MOV command restrictions
  • Why a Word is 2 bytes and not 4 bytes in x86 Assembly?
  • Assembly memory operands clarification
  • Syntax error in assembly when using letter "C"
  • 6502 XASM defini data
  • How do I identify the instruction stored LC-3
  • Int 21h/ah=39h returns with AX=3 upon directory creation
  • Filling up Delayed Branch slots
  • Syntax to read IA32 register with offset in GDB?
  • What is the purpose of positive EBP referencing?
  • What does the following byte specifier for adding to a memory reference does in NASM assembler?
  • Can I read label value in assembly?
  • How to create a void function in Assembly?
  • FAT32 Finding Stage 2 Bootloader
  • Passing a 1 byte argument to a function?
  • fail to read sectors on boot code
  • Bootloader - Loading assembly kernel, problems with memory addresses and copying data
  • Moving AX into CL - Invalid combination of opcode and operands
  • Number representation in Assembler
  • lwzx PowerPC Assembly on Dolphin Wii Emulator
  • nasm assembly for loop
  • Division produces wrong result
  • Illegal Number Error - lowercase to uppercase conversion (0xdf)
  • Counting occurances of a negative number from a list
  • The difference between signed and unsigned, what means a negative byte?
  • Beeping Bootloader (Intel x86)
  • using atof function in x86 NASM
  • Displaying input wrong in a 2-digit Calculator program using int 10h BIOS mouse + keyboard
  • Convert from decimal to binary using arrays
  • Assembly, NASM; how is the information in 32 bit registers divided among sub registers?
  • INT 21h 09h issue
  • Floating point not outputting right answer
  • Multiply float in Assembly
  • College Fee Calculator Assembly(HLA) program
  • Can I encode a 64-bit negative immediate in x86?
  • Motorola 68000 assembly comparing numbers
  • how many whole numbers in IEEE 754
  • NASM Assembly; jumps not working in this code
  • Looking for a way to sleep ( delay ) MIPS using QT SPIM
  • Is xmm8 register value preserved across calls?
  • Shortest Intel x86-64 opcode for rax=1?
  • Is this x86 assembly addressing method valid?
  • How to copy between registers with a bitshift in ARM assembly?
  • "cqo", "cdq" and "cwd" x86_64 instructions. Why not use just cqo?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com