CS 3841 Computer Organization Recitation LAB10 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS 3841 Computer Organization Recitation LAB10

Description:

Upside down Stack of words. The stack grows toward lower address. 11. Dynamic allocation ... On the stack. 32 character buffer. Allocate. addiu $sp, $sp, -32 ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 17
Provided by: csU66
Category:

less

Transcript and Presenter's Notes

Title: CS 3841 Computer Organization Recitation LAB10


1
CS 3841 Computer OrganizationRecitation LAB10
  • - Function, Reentrant, Recursive -

LI XU
2
Functions in C
  • int Avg(int x, int y)
  • return((xy)/2)
  • main()
  • int a, b, result
  • a 123 b 456
  • result Avg(a,b)
  • printf( The Average of d and d is d, a, b,
    result)
  • a 321 b 654
  • result Avg(a,b)
  • printf( The Average of d and d is d, a, b,
    result)

3
Control flow with Jump instruction
Problem many calls and one return
4
Functions in MIPS
  • Use Jump and link instruction
  • Function call (in CALLER)
  • jal PL
  • Save the address of next instruction in ra (ra
    ? PC4)
  • Jump to the Function entry point
  • (PC ? PL)
  • Function return (in CALLEE)
  • jr ra
  • Jump back to the address in ra
  • (PC ? ra)

5
Registers Usage Conventions
  • By agreement between programmers (not by
    hardware) registers have been assigned different
    roles with function linkage
  • t0 - t9 The function is free to change these
    registers.
  • s0 - s7 The function must not change these
    registers ( if used, the callee saves and
    restores them).
  • a0 - a3 These registers contain arguments for
    the function. The subroutine can change them.
  • v0 - v1 These registers contain values returned
    from the function .

6
Function Avg
  • -------------------------------------------------
    --
  • FUNCTION name avg
  • DESCRIPTION Calculate average of two integers.
  • INPUT a0 - first integer
  • a1 - second integer
  • OUTPUT v0 - avg, i.e., (a0 a1)/2
  • -------------------------------------------------
    --
  • avg
  • add v0, a0, a1 v0 a0 a1
  • sra v0, v0, 1 v0 v0 / 2
  • jr ra return

7
Using Avg Function
  • main
  • call the avg function with 2 arguments
  • li a0, 123 a0 123
  • li a1, 456 a1 456
  • jal avg call avg function
  • Now, v0 has the result value.
  • print v0
  • move a0, v0
  • li, v0, 1
  • syscall

8
Example- ReverseCap
  • The function called Reverse that
  • Takes a string address as an argument
  • Reverse the order of the string
  • Returns the address of the new string
  • The function called Cap that
  • Takes a string address as an argument
  • Converts each letter of the string to capital
  • Returns the address of the new string
  • The program to read a string and call them
  • The source code has showed on my webpage.

9
Reentrant Functions
  • The functions which can be shared by multiple
    processes.
  • It can be safely called recursively.

10
Rules for reentrant code
  • No allocated memory variables in the global data
    segment.
  • Constants is OK.
  • Must use no static data.
  • Must not call non-reentrant function.

11
MIPS Memory Segments
  • Upside down Stack of words
  • The stack grows toward lower address.

12
Dynamic allocation
  • Dynamic memory allocation is when an executing
    program requests more memory, the operating
    system give it a block of main memory.
  • The program then uses this memory for some
    purpose. Usually the purpose is to add a node to
    a data structure. In object oriented languages,
    dynamic memory allocation is used to get the
    memory for a new object.

13
Dynamic allocation example
  • On the stack
  • 32 character buffer
  • Allocate
  • addiu sp, sp, -32
  • move a0, sp
  • li a1, 32
  • li v0, 8
  • Syscall (read a string)
  • Deallocate
  • addiu sp, sp, 32

14
Exercise Factorial function
  • Input N output N!, recursively call Fac
  • N! 1 2 3 N
  • N (N 1)! (0!1 1!1)
  • return N Fac (N-1)
  • Using recursive function.
  • N range from 0 to 13, if N is not in this range,
    ask the user input it again.

15
Factorial Recursive vs. Iterative
  • Recursive way
  • Fact_r (int N)
  • if (N lt 1)
  • return 1
  • else
  • return N Fact_r(N-1)
  • Iterative way
  • Fact_i (int N)
  • int i, result1
  • for (i1 iltN i)
  • result resulti
  • return result

16
Hint recursive call Fac
  • Go
  • addi a0, a0, -1
  • sw a0, 0(sp) Pass N-1 to
    factorial function
  • jal Fac Recursive
    call
  • lw v0, 4(sp) Get (N-1)! back
  • lw ra, 12(sp)
  • lw a0, 8(sp)
  • mult v0, a0 N (N-1)!
  • mflo v0
  • facret
  • addiu sp, sp, 16 Deallocate
  • sw v0, 4(sp)
  • jr ra

Fac lw a0, 0(sp) bltz a0,
Problem addi t1, a0, -13 bgtz
t1, Problem 13 is largest value
we can accept addiu sp, sp, -16
Allocate sw ra, 12(sp) save
return address sw a0, 8(sp)
slti t0, a0, 2 If N is 1 or 0, then
return the value 1
beqz t0, Go li v0, 1 b
facret
Write a Comment
User Comments (0)
About PowerShow.com