CS 3841 Computer Organization II Recitation LAB8 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS 3841 Computer Organization II Recitation LAB8

Description:

CS 3841 Computer Organization II Recitation LAB8 – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: CS 3841 Computer Organization II Recitation LAB8


1
CS 3841 Computer Organization IIRecitation LAB8
  • - Array,Dynamic Mem. Alloc.,Stack -

LI XU
2
Memory segment
  • Data segment
  • Global constants and variables
  • Text segment
  • Machine language code
  • Stack segment
  • Parameters, local variables and return address

3
Declare array
4
Example - Array
  • .data
  • array1 .word -2,-1,0,1,2
  • array2 .space 5
  • .text
  • la t0, array1 load address of array1 into t0
  • lw t1, 0(t0) lw t2, 4(t0)
  • or addi t0, t0, 4
  • la t2, array2
  • lb t3, 3(t2) t3 array23

5
Dynamic Memory Allocation
  • Here is how a SPIM program requests a block
    of memory from SPIM's heap
  • li a0,xxx a0 contains the number of
    bytes you need.
  • This must be a multiple of
    four.
  • li v0,9 code 9 allocate memory
  • syscall call the service.
  • v0 lt-- the address of the
    first byte
  • of the dynamically
    allocated block
  • You don't know in advance what range of
    addresses you will get back for the allocate
    memory request. (This is similar to a call to
    malloc() in "C".)

6
MIPS Stack
  • The stack is often used to hold temporary values
    when most registers are already in use.
  • By convention, sp(29) always points to the top
    of the stack. Also by convention, the stack grows
    downward (in terms of memory addresses).
  • On a computer with a full operating system, the
    stack pointer is initialized by the operating
    system before control is passed to a user
    program. In MIPS architecture, Initially sp
    0x7FFFFFFC
  • The data elements in our stacks are 32-bit words.
    In general, stacks can be used for all types of
    data. But in this course, stacks contain only
    32-bit full words.

7
MIPS Stack
  • LIFO
  • Last In First Out
  • sp (29)
  • TOS (top of stack)
  • Initially sp 0x7FFFEFFC
  • Upside down Stack of words
  • The stack grows toward lower address.

8
Stack - Push Operation
  • To push an item onto the stack, first subtract 4
    from the stack pointer, then store the item at
    the address in the stack pointer.
  • PUSH the item in t0
  • subu sp,sp,4
  • point to the new place
  • sw t0,(sp)
  • store t0 at the new top

9
Stack - Pop Operation
  • To pop the top item from a stack, copy the item
    pointed at by the stack pointer, then add 4 to
    the stack pointer.
  • POP the item into t0
  • lw t0,(sp)
  • copy the top item into t0
  • addu sp,sp,4
  • point to the new place

10
Example Reverse String
  • Program Outline
  • Input the string into a buffer -------------------
    ----(1)
  • Push each character onto the stack
    ---------------(2)
  • Pop chars from stack back into the buffer
    --------(3)
  • Print the reversed string ----------------------
    -------(4)

11
First section Input
  • input the string
  • li v0,8 service code
  • la a0,str address of buffer
  • li a1,128 buffer length
  • syscall
  • li t0,0 push a null
  • subu sp,sp,4 onto the stack
  • sw t0,(sp) to signal its bottom

12
Second section Push
  • push each character onto the stack
  • pushl
  • lbu t0,str(t1) get current char into
  • a full word
  • beqz t0,stend null byte end of
    string
  • subu sp,sp,4 push the full word
  • sw t0,(sp) holding the char
  • stend

13
Third section Pop
  • pop chars from stack back into the buffer
  • popl
  • lw t0,(sp) pop a char off the stack
  • addiu sp,sp,4
  • beqz t0,done null means empty stack
  • done

14
Fourth section Output
  • print the reversed string
  • done
  • li v0,4 service code
  • la a0,str address of string
  • syscall
  • li v0,10 exit
  • syscall

15
Exercise
  • Reserve and output a user-supplied string
  • t0 stores character pushed or popped.
  • t1 Index into string buffer
  • v0 a0 a1 can be used in system call

16
Assignment 2 (100pts)
  • Due day 8th April, Wednesday. By 11 P.M.
  • Submission
  • Three source code files.
  • Submit the above files by email to the TA
    (lxu_at_cs.utsa.edu)
Write a Comment
User Comments (0)
About PowerShow.com