MIPS Assembly Language Programming - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

MIPS Assembly Language Programming

Description:

CDA 3101 Discussion Section 06 MIPS Assembly Language Programming Function Call and Recursive * * Problem1 Write a function MinMax(&X, N) to find the minimum and ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 10
Provided by: ying112
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: MIPS Assembly Language Programming


1
MIPS Assembly Language Programming
CDA 3101 Discussion Section 06
  • Function Call and Recursive

2
Problem1
  • Write a function MinMax(X, N) to find the
    minimum and
  • maximum of an array X of N integers. The address
    of the
  • array is passed in a0, and the number of words
    in the array
  • is passed in a1. The minimum and maximum are
    returned in
  • registers v0 and v1 respectively.
  • Also, write a short main program that
  • Prompts user to enter 10 integers one by one to
    fill a global integer array X of size 10
  • Calls the MinMax function to find and return the
    minimum and maximum of the array X.
  • Prints the minimum and the maximum value.

3
System Calls
4
Problem 2
  • Implement a recursive function that determines if
    a string is palindrome. The function should
    return 1 or 0 to the calling function to indicate
    if the given string is palindrome or not. You can
    assume that the string has only characters from
    the set a-z, A-Z.Note that
  • palindrome(X) true, if X lt 2
  • palindrome(aXa) palindrome(X), if X gt 2

5
Conventions for Registers
  • Following registers should be spilled to the
    stack
  • ra (31)
  • a0-a3 (4-7)
  • t0-t7 (8-15)
  • s0-s7 (16-23)
  • fp (30)

Saved by caller on stack before jal and restored
after returning from jal done only for registers
used after jal
Saved by called procedure before rewriting and
then restored back before returning
6
Conventions for Function Call
  • Before calling a function, the caller must
  • Save on stack
  • ra
  • any t registers that will be used after the jal
    instruction
  • Any a registers that will be used after the jal
    instruction
  • Move arguments to a0-a3
  • Execute jal instruction to jump to the procedure

7
Conventions for Function Call
  • On entry, the called function must
  • Save on stack
  • any s registers that it is going to overwrite
  • Before returning, the called function must
  • Save results in v0-v1
  • Restore all s registers values from the stack if
    necessary
  • Adjust sp to point to the address it was
    pointing to before this function was called
  • Return to calling function by using jr ra

8
Conventions for Function Call
  • After the function returns, the caller must
  • Read the results returned by the function from
    v0-v1
  • Restore ra register values from stack
  • Restore t and a register values from stack, if
    their values were saved on stack before function
    call

9
Key Points
  • Dynamically allocate memory
  • li v0, 9 Memory allocation service
  • li a0, ltintgt Allocate ltintgt bytes of
    mem.
  • syscall
  • move t0, v0 Move address of array to
    safety.
  • Recursion
  • self
  • addi sp, sp, -4 Allocate stack space
  • sw ra, 0(sp) Save old return
    address
  • jal self Jump to self
  • lw ra, 0(sp) Load old return address
  • addi sp, sp, 4 Restore stack to old state
Write a Comment
User Comments (0)
About PowerShow.com