EEE 105 Lab Exercise

Embed Size (px)

Citation preview

  • 7/24/2019 EEE 105 Lab Exercise

    1/16

    EEE 105: Computer Organization

    Lab 3: MIPS Subroutines/Functions

    University of the PhilippinesElectrical and Electronics Engineering Institute

  • 7/24/2019 EEE 105 Lab Exercise

    2/16

    Function/Subroutine

    Function fundamental unit of programming

    gives a simple form of program abstraction

    allows reuse of codes In assembly language, a function is

    referred to as subroutine.

  • 7/24/2019 EEE 105 Lab Exercise

    3/16

    Subroutine

    A subroutine can be called anywhere inthe program.

    Once execution of subroutine is complet

    program execution should return to thelocation that called the subroutine.

  • 7/24/2019 EEE 105 Lab Exercise

    4/16

    Subroutine

    Sets up the arguments forthe subroutine

    Calls the subroutine

    Caller Subroutine

    arguments

    returnvalues

    Uses the arguments toperform somecomputations.

    Returns the results to tcaller.

  • 7/24/2019 EEE 105 Lab Exercise

    5/16

    Calling Subroutines in MIPS

    Two important instructionsjal label

    Copies the address of the next instruction inregister $ra and then jumps to address label

    jr $register

    Jumps execution to the address in register$register. Use register $ra if returning

    execution to caller.

  • 7/24/2019 EEE 105 Lab Exercise

    6/16

    Register Convention $v0-$v1

    Return values of subroutines (not preserved)

    $a0-$a3 Argument registers, used by the caller to pass data

    subroutine

    $t0-$t9 Temporary registers, can be used inside the

    subroutines

    $s0-$s7

    Saved registers, these registers should be preserveacross function calls

  • 7/24/2019 EEE 105 Lab Exercise

    7/16

    Subroutine Example 1main: lw $a0, 0x2000($zero)

    lw $a1, 0x2004($zero)

    jal mult #no negative inputs

    j end_main

    mult: add $v0, $zero, $zero #clear reg for output

    loop: beq $a1, $zero, end

    add $v0, $v0, $a0 #update output

    addi $a1, $a1, -1 #input $a1 is not pres

    j loop

    end: jr $ra

    end_main:

  • 7/24/2019 EEE 105 Lab Exercise

    8/16

    MIPS Calling Convention

    Allocate memory and save the valueof registers ($ra, $a0, $s0, etc.)

    Load the original value of registers($ra, $a0, $s0, etc) from memory

    Do some things here

    End the function and return

  • 7/24/2019 EEE 105 Lab Exercise

    9/16

    Stack

    Good for storing large and variable length d

    LIFO (last in, first out) data structure Uses push/pop operations to change the top of

    stack

    May not be followed based on the context and long as you know what you are doing

    Implementation-dependent Top-of-stack may either point to the last item or

    next free slot For our class, the TOS points to the next free s

  • 7/24/2019 EEE 105 Lab Exercise

    10/16

    Stack

    INACTIVE

    FRAME(N-3)

    DATA 0x3FFC STACK ORIGIN

    DATA 0x3FF8

    DATA 0x3FF4

    INACTIVEFRAME

    (N-2)

    RETURN ADDRESS to N-3 0x3FF0

    DATA 0x3FEC

    DATA 0x3FE8

    INACTIVEFRAME

    (N-1)

    RETURN ADDRESS to N-2 0x3FE4

    DATA 0x3FE0

    DATA 0x3FDC

    ACTIVEFRAME

    (N)

    RETURN ADDRESS to N-1 0x3FD8

    DATA

    DATA

    DATA

    AVAILABLE or

    UNUSEDSTACK SPACE

    STACK POINTER

    0x3004

    0x3000

  • 7/24/2019 EEE 105 Lab Exercise

    11/16

    Stack on MARS Stack is filled from top to bottom

    Memory address 0x00003FFF down to 0x00003000 Memory Configuration: Compact, Text at Address 0

    $sp initializes at 0x00003FFC 0x00003FFF is not a word address

    Data Address (word aligned)FF FE FD FC 0x00003FFC

    FB FA F9 F8 0x00003FF8

    F7 F6 F5 F4 0x00003FF4

    07 06 05 04 0x00003004

    03 02 01 00 0x00003000

    Data Address (word aligned)

    xxxx 0x00003FFC

    xxxx 0x00003FF8

    xxxx 0x00003FF4

    xxxx 0x00003004

    xxxx 0x00003000

    Data Address (word aligned)0x0000FFFF 0x00003FFC

    xxxx 0x00003FF8

    xxxx 0x00003FF4

    xxxx 0x00003004

    xxxx 0x00003000

  • 7/24/2019 EEE 105 Lab Exercise

    12/16

    Stack Example

    subroutine:

    add $a0, $ra, $zerojal push #save return address

    . . .

    #call other subroutines

    . . .

    jal pop #retrieve return address

    jr $v0

    push: sw $a0, 0($sp) # store data from $a0

    addi $sp, $sp, -4 # $sp is updated to point to the ne

    jr $ra # free address

    pop: lw $v0, 4($sp) # load data to $v0

    addi $sp, $sp, 4 # $sp is updatedjr $ra

    Relative Addre

    Use data storedaddress stored w/ offset

  • 7/24/2019 EEE 105 Lab Exercise

    13/16

    MIPS Example 2

    main: lw $a0, 0x2000($zero)

    lw $a1, 0x2004($zero)lw $a2, 0x2008($zero)

    jal mult3

    j end

    mult: sw $a1, 0($sp)

    addi $sp, $sp, -4

    add $v0, $zero, $zero

    m_loop: beq $a1, $zero, m_end

    add $v0, $v0, $a0

    addi $a1, $a1, -1

    j m_loop

    m_end: addi $sp, $sp, 4

    lw $a1, 0($sp)jr $ra

    mult3: sw $ra, 0($sp)

    sw $a0, -4($sp)sw $a1, -8($sp)

    addi $sp, $sp, -12

    jal mult

    add $a0, $v0, $zero

    add $a1, $a2, $zerojal mult

    addi $sp, $sp, 12

    lw $a1, -8($sp)

    lw $a0, -4($sp)

    lw $ra, 0($sp)

    jr $ra

  • 7/24/2019 EEE 105 Lab Exercise

    14/16

    ME 3 OverviewCreate an assembly program in MARS that

    gets an input (greater than 1) from the memory address0x00002000 (memory configuration 3)

    stores in the stack (starting at 0x00003FFC downwards) thprime factors of the input $sp should point to the next free address

    Examples:

    N = 90

    Data Address

    2 0x00003FFC

    3 0x00003FF8

    3 0x00003FF45 0x00003FF0

    $sp = 0x00003FEC

    N = 391

    Data Address

    17 0x00003FFC

    23 0x00003FF8

    $sp = 0x00003FF4

  • 7/24/2019 EEE 105 Lab Exercise

    15/16

    ME3 Specifications Given:

    isprime subroutine

    input $a0 (retained) output $v0 0 if not prime, 1 if prime all registers are preserved aside from $v0

    Algorithm for the prime factorization

    Tasks mod_div subroutine

    input $a0 for dividend, $a1 for divisor output $v0 for the remainder, $v1 for the quotient all registered are preserved aside from $v0 and $v1 Note: the mod_div subroutine is called by the isprime subroutine

    Implementation of the prime factorization algorithm Not strict but must use the isprime subroutine at the very least

    Multiply, divide, square root and pseudo instructions are not allowed

    INPUT OUTPUT

    N > 1(32-bit number)

    Prime facto

    0x2000Stack: 0x3F

    downward

  • 7/24/2019 EEE 105 Lab Exercise

    16/16

    Prime Factorization Algorithm