BUAA group p2_Mips_ Review tips


Assembler review

Important: before running, ensure that the Mars settings memory configuration is compact and data at address 0

Because there are no variable names, function names, various operation symbols and statement blocks in the main code of assembly language. We can only realize the functions that can be realized by the C language we learned before through a limited number of 32 registers and many assembly instructions and tags.

  1. Pseudo instruction

    • . data defines the initial address of the program data segment

    • . text defines the code snippet of the program

    • .space

      • name: .space n
      • name's address is by The initial address of the data segment is calculated by adding the previously applied data size
      • When applying for space, try to make n a multiple of 4
      • Apply for space in bytes, 1 byte, and an int takes up 4 bytes
    • .word

      • name: word 0:n
      • In the memory data segment, data is continuously stored in words, 32bit
    • .asciiz

      • name: .asciiz:"string"
      • Stores the string in bytes, ending with NULL
      • Since it is stored by bytes, try to put it after the allocated space of bytes
  2. Use of macros

    • . macro implements code reuse, and nested macro instructions are not allowed
    .macro macro_name(%parameter1, %parameter2, ...)
    //Code snippet
    • . eqv enhance code readability
    .eqv EQV_NAME string
  3. MIPS assembler design

  • Conditional statement

     slt $t3, $t1, $t2      # if(t1<t2) t3=1;
     beq $t3, $0, if_1_else
     # do something
     j if_1_end
     # do something else
  • Circular statement

     slt $t3, $t2, $t1;
     beq $t3, $0, for_end1
     # do something
     addi $t2, $t2, 1
     j for_begin1



  1. Matrix read in
    • Use the stack to read the matrix, reserve the starting positions of the three matrices, and calculate the offset of the elements in the matrix
  2. Matrix multiplication
    • Calculate the offset, calculate the result, and store it in the target address
  3. Output results
    • for loop + calculated offset + output result

Common summary

  1. When. word requests storage space, it is word 0:n, n is the actual number of bytes

    matrix: .word 0:100       # 10\*10\*1word is to apply for a 10 * 10 two-dimensional matrix of int data
  2. Give the fixed output in the title to label and write it in Under data

    space: .asciiz " "
  3. sw, lw operation on word

    • sw is store word
    sw $t3, array($t4)		# store contents of $t3 into array[index]
    • lw is load word
    lw $t3, symbol($t4)     # load contents of $t3 into symbol[i]
    • Similarly, lb and sb are operations on byte
  4. li, la

    • la is load address
    • li is load immediate
  5. Common syscall commands

    Read in integer
    li $v0, 5     # read an integer into $v0
    Output string
    la $a0, space   # $a0 = address of null-terminated string to print
    li $v0, 4       
    Output integer
    move $a0, out    # $a0 = integer to print
    li $v0, 1
    Read in character
    li $v0, 12      # $v0 contains character read
    End program
    li $v0, 10
  6. Division: quotient exists in lo and remainder exists in hi; Multiplication: the lower four bits exist in lo and the higher four bits exist in hi


First of all, we need to understand the nature or operation process of recursion, and write mips on this basis
mips code example is a question of full arrangement

  • Recursion process: determine whether to terminate recursion, enter the termination output and trace back, or enter the core part containing the recursion module

    • Recursive module focuses on: recursive termination conditions, recursive entry into variable storage, recursive backtracking
    1. Recursive termination condition
    • That is, the case of recursive termination
    1. Recursive entry into variable storage
    • Storage of parameters in recursion
      • Parameters before entering a new round of recursion
    • Storage of the address of the next instruction of each recursive jump instruction
      • $ra stores the next instruction of jal recursion instruction, that is, the address of the next instruction at the end of this recursion
        sw $ra, 0($sp)		# store address
        addi $sp, $sp, -4
        sw index, 0($sp)
        addi $sp, $sp, -4   # store index 
        sw i, 0($sp)
        addi $sp, $sp, -4	#store i
    1. recursion tracing
      • Take back the parameters of this recursion in the reverse order of storage order and continue to complete this recursion function
        sw $ra, 0($sp)		# store address
        addi $sp, $sp, -4
        sw index, 0($sp)
        addi $sp, $sp, -4   # store index 
        sw i, 0($sp)
        addi $sp, $sp, -4	#store i
      • Operation after recursive backtracking:
            bne $t4, 8, nxt
            li $s1, 0
            add $s2, $s1, $s0
            sw $s2, fib_array($t4) 
            move $s1, $s0		#Previous previous
            move $s0, $s2		# $s0 previous
            jr $ra


  1. Due to the number of registers, the same register is often used repeatedly. In order to avoid reusing the registers that should not be changed eqv starts from $s7 or $t7 and is used from large to small
  2. Register names often cause confusion. It is recommended to use them frequently eqv, intuitive naming is the most important. If you lack registers, you must add more comments to help you clarify your ideas!
  3. Question logic
    3.1 first clarify the module order of the topic, such as the topic of Joseph Ring: first read in n, recycle the structure to get the array of required results, and finally output the module
    3.2 write out the specific process of the core module, either in the form of pseudo code or directly write C, and then correspond the register to the core variable one by one
    3.3 translate layer by layer, or write according to the idea of assembly. Pay attention to initialization, end jump and register usage
    3.4 it's best to write it right again. After writing, if it's a simple topic, add simple comments. If it's a little complex, be sure to add detailed comments, and then debug

In class test

  1. After debugging the environment on the computer, don't press the key on the keyboard at will. It takes a long time to restart the computer in the computer room.

  2. In case of Bug, local modification is preferred rather than overall rewriting. First, there is not necessarily or do not necessarily want to get a better algorithm, but rewriting takes a lot of time.

    • When there is a requirement to limit the number of steps for such compilation topics, if TLE is, priority should be given to deleting the unnecessary steps in most cases (rather than foolishly unable to think of other methods).
  3. When translating C's mips assembly title, you can slightly adjust the order of judgment sentences to be more suitable for assembly writing.

  4. If you are worried about too much data, press the stack directly, but remember to store the top address of the stack.

  5. The instruction to end the program (li $v0, 10 \n syscall) must be added at the end of the program. Otherwise, if you are used to writing the jump execution module label at the end, the program may have an endless loop / metaphysical bug. The problem can be found through debugging. If it is output while operating, the output should be correct.

Posted by FirePhoenix on Sat, 07 May 2022 16:00:33 +0300