Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory and Data Structures  Arrays  Stacks  Queues.

Similar presentations


Presentation on theme: "Memory and Data Structures  Arrays  Stacks  Queues."— Presentation transcript:

1 Memory and Data Structures  Arrays  Stacks  Queues

2 CMPE12cGabriel Hugh Elkaim 2 Memory This is the “RAM” in a system Labels and addresses point to pieces of memory holding: –Words –Bytes –Strings –Floats Memory is just a collection of bits Can be used to represent integers, characters, or some arbitrary representation.

3 CMPE12cGabriel Hugh Elkaim 3 Instead: treat memory as a giant flat array of bytes –Compiler or Progammer decides what to make of it –Element numbering starts at 0 –The element number is an address In “C” memory is allocated by: char m[size of array]; Memory

4 CMPE12cGabriel Hugh Elkaim 4 Storage of bytes MIPS architecture is “byte addressable” meaning that all addresses are “byte” addresses. This means the smallest unit of memory we can allocate is a byte. Use ‘lb’ (load byte) to access this unit.

5 CMPE12cGabriel Hugh Elkaim 5 Example.data mychar:.byte newline:.byte‘ \ n’ ….text … lb$t0, newline li$v0, 12# getc syscall sb$v0, mychar beq$t0,$v0, found _ newline … found _ newline:… Storage of bytes

6 CMPE12cGabriel Hugh Elkaim 6 Storage of bytes The data is placed in memory like this at start up (assuming.data section starts at address 4). The mychar variable will change to the value of the character entered by the user once stored. 30 70A004 98 1512 Memory newline variable mychar variable addresses

7 CMPE12cGabriel Hugh Elkaim 7 Storage of Integers Use 4 bytes beginning at an address that is multiple of 4. So if stored at address 20, uses bytes 20, 21, 22, and 23. Use ‘lw’ (load word) instead of ‘lb’ (load byte).

8 CMPE12cGabriel Hugh Elkaim 8.data n:.word1 n2:.word-1 newline:.byte ‘ \ n’ str1:.asciiz“Enter a number ”.text main:la$a0, str1 li$v0, 4# print string syscall li$v0, 5# get integer syscall lw$t1, n add$t2, $v0, $t1 sw$t2, n2 Storage of Integers

9 CMPE12cGabriel Hugh Elkaim 9 4000 0143 44FF 47 480A456E744B 4C657220614F 50206E756D53 546265722057 58005B 5C5F Memory 1 in 2SC -1 in 2SC New line “Enter a number “ addresses Storage of Integers

10 CMPE12cGabriel Hugh Elkaim 10 Memory And the program would become this eventually:.text main:li $a0, 49# expects an address for a string li $v0, 4# code for put string syscall lw $t1, (40)# expects an address add $t2, $t1, $t1# expects “values” sw $t2, (44)# expects an address Real machine code has addresses for variables, not labels. Later, we will show how to store instructions and get rid of branch labels. Then the.text segment will look similar to the.data segment.

11 CMPE12cGabriel Hugh Elkaim 11 Endian Issues Consider this code:.data n:.word0x61626364#ASCII for ‘a’, ‘b’, ’c’, ’d’.text main: lb$a0, n li$v0, 11# putc code syscall Do you think an ‘a’ or a ‘d’ is printed?

12 CMPE12cGabriel Hugh Elkaim 12 Endian Issues Answer: ‘a’ since MIPS is a big Big Endian architecture. 406162636443 addresses memory lw$s0, n $s061626364 But if we did: We would get this: 031 As we would expect.

13 CMPE12cGabriel Hugh Elkaim 13 Big Endian: smallest address is most significant (biggest) Little Endian: smallest address is least significant (littlest) So answer would be ‘d’ If we did a lw $s1, n $s1 ← 0x61626364 Endian Issues 406463626143 addresses memory Same as with big endian

14 CMPE12cGabriel Hugh Elkaim 14 Endian Issues Little Endian – address LSB (VAX, Alpha, x86, PDP-11) Word Address Bytes MSB  LSB 0 3 2 1 0 4 7 6 5 4 Word Address Bytes MSB  LSB 0 0 1 2 3 4 4 5 6 7 Bi-Endian: Selectable data format Big Endian – address MSB (IBM 360  390, Motorola 680x0, Sparc, MIPS)

15 CMPE12cGabriel Hugh Elkaim 15 Programmable or bi-endian: on data only (why not instructions?) i860 (Little-endian instructions) PowerPC (Big-endian instructions) In general: When accessing values smaller than a word must also consider alignment, sign extension, and effect on high bytes of register. Endian Issues

16 CMPE12cGabriel Hugh Elkaim 16 Arrays Array implementation is very important Most assembly languages have no concept of arrays From an array, any other data structure we might want can be built

17 CMPE12cGabriel Hugh Elkaim 17 Properties of arrays: –Each element is the same size –Char = 1 byte –Integer = 1 word –Elements are stored contiguously –First element at the smallest memory address In assembly language we must –Allocate correct amount of space for an array –Map array addresses to memory addresses Arrays

18 CMPE12cGabriel Hugh Elkaim 18 Arrays MAL declarations of arrays within memory To allocate a portion of memory (more than a single variable’s worth) variablename:typeinitvalue:numelements type is as before -.byte,.word, or.float numelements is just that, numbering starts at 0 (as in C) initvalue is a starting value given to each element of the array

19 CMPE12cGabriel Hugh Elkaim 19 MAL declarations of arrays within memory To allocate a portion of memory (more than a single variable’s worth) variablenametypeinit _ value : num _ elements Type as before: –.byte.word or.float Num _ element: numbering starts at 0 (just like in “C”) Init _ value: the value of each element of the array Arrays

20 CMPE12cGabriel Hugh Elkaim 20 Arrays New directive: name:.spacenumber _ of _ bytes.space allocates space (in bytes) within memory without giving an initial value. The type of the data within this space cannot be inferred.

21 CMPE12cGabriel Hugh Elkaim 21 Examples: 8 character elements, number 0 – 7, initialized to 0 arrayname:.byte0:8 18 bytes of memory name:.space18 5 integers initialized to 3 numbers:.word3:5 Array Examples

22 CMPE12cGabriel Hugh Elkaim 22 If base address of myarray is 25 Byte address of myarray[4] = 25 + 4 = 29 Base address + distance from the first element Array of Bytes Calculating the address of an array element char myarray[7]/* C */ myarray:.byte0:7# MAL 0 25 12345 262728292A 6 2B Element index address

23 CMPE12cGabriel Hugh Elkaim 23 Addressing Byte Arrays If we want the 3 rd element It is at index 2 since indexing starts at zero. Thus myarray[2] is the at the address of array + 2. If the 1 st element is [0] and is at address 25, then the address of the 3 rd element is myarray[2] = 25 + 2 0 25 12345 2627282930 6 31 address index 3 rd element

24 CMPE12cGabriel Hugh Elkaim 24 How do you get the address of myarray? Use the “load address” instruction, “la” Keep clear the difference between an address and the contents of an address. Addressing Byte Arrays

25 CMPE12cGabriel Hugh Elkaim 25 To get address of myarray[4] in MAL, write the code… la$t0, myarray add$t1, $t0, 4 If we wanted to decrement element number 5 by 1… lb$t4, ($t1)# ALT: lb $t4, 4($t1) sub$t4, $t4, 1 sb$t4, ($t1) Addressing Byte Arrays

26 CMPE12cGabriel Hugh Elkaim 26 Array of Integers An integer is 32-bits (word) and contains 4 bytes C++: intmyarray[6]; MAL: myarray:.word0:6 Ormyarray:.space24

27 CMPE12cGabriel Hugh Elkaim 27 So to find the address of myarray[x] we must find: Where the array starts (base address) Size of an element in bytes Thus byte address of myarray[x] = base + size(x) So the byte address of myarray[3] = 80+4(3) = 8C 0 80 12345 84888C9094 index address Array of Integers

28 CMPE12cGabriel Hugh Elkaim 28 lw vs. lb The type of lb is ‘byte’ and of lw is ‘word’ Use lb to manipulate bytes Use lw to manipulate words

29 CMPE12cGabriel Hugh Elkaim 29 The index of both lb and lw is a byte address. lb a #address byte in memory location M[a] #The following indexes base[$t1] base:.byte0:n.text la$t0, base add$t2, $t0, $t1 lb$t3, 0($t2) lw $t1, a # addresses the 4 bytes starting at M[a] Both lw and lb should always be used with a base address and allocated space. lw vs. lb

30 CMPE12cGabriel Hugh Elkaim 30 2-Dimensional Arrays 2-Dimensional arrays are more complicated in assembly Memory is a 1-D array Must map 2-D array to 1-D array Arrays have rows and columns r x c array r=rows c=columns

31 CMPE12cGabriel Hugh Elkaim 31 2-Dimensional Arrays Two sensible ways to map 2-D to 1-D Row major form: (rows are all together) Column major form: (columns are all together) 0,0 1,0 2,0 3,0 0,1 1,1 2,1 3,1 0,0 0,1 1,0 1,1 2,0 2,1 3,0 3,1 4x2 example

32 CMPE12cGabriel Hugh Elkaim 32 More Array Examples: 3x5

33 CMPE12cGabriel Hugh Elkaim 33 2-Dimensional Arrays How do you calculate addresses in a 2-D array? Row Major: Column Major: Base Address + element size(#columns x R i + C i ) Base Address + element size(#rows x C i + R i )

34 CMPE12cGabriel Hugh Elkaim 34 2-Dimensional Arrays Summary of 2D arrays Row/Column major (storage order) Base address Size of elements Dimensions of the array How about 3-D arrays?

35 CMPE12cGabriel Hugh Elkaim 35 Bounds Checking Many HLL’s have bounds checking (not C!!!) Assembly languages have no implied bounds checking Your program is in total control of memory With a 5 x 3 array, what does the following address? array:.word0:100.text la$t1, array add $t1, $t1, 15 lw$t0, ($t1) Bounds checking is often a good idea!! Most C development environments include optional bounds checking.

36 CMPE12cGabriel Hugh Elkaim 36 Stacks A data structure that stores data in the reverse order that it is used. The data is not known until run time. The classic analogy is a stack of dishes You can put single plates on the stack You can take plates off the stack Last in, First out (LIFO)

37 CMPE12cGabriel Hugh Elkaim 37 1st on 2nd on 3rd on Data is added or “pushed” onto the stack. Data is removed or “popped” off the stack. Stacks

38 CMPE12cGabriel Hugh Elkaim 38 Stacks Printing out a positive integer, character by character Push LSB to MSB Pop MSB to LSB (LIFO) integer = 1024 if integer == 0 then push ‘0’ else while integer != 0 digit  integer mod base char  digit + 48 push char onto stack integer  integer div base while stack is not empty pop char print char

39 CMPE12cGabriel Hugh Elkaim 39 Implementation of a Stack One implementation of a stack out of an array. Index the top of stack (tos), with a stack pointer (sp). SP is a variable that contains the address of the empty location at the top of the stack (when empty). initial state, sp points at top of stack

40 CMPE12cGabriel Hugh Elkaim 40 Stack Implementation In MAL: stack:.word0:50 stackend:.wordstack+4x50 Or stack:.space200 Labels can be used as initial values The address (label) of the stack gets put into the variable

41 CMPE12cGabriel Hugh Elkaim 41 Pushing and popping code la$s0, stack#initialization of stack ptr PUSH sw$s1, 0($s0)# $s1 has data to push add$s0, $s0, 4 POP sub$s0, $s0, 4 lw $s1, 0($s0) Stack Implementation

42 CMPE12cGabriel Hugh Elkaim 42 Stack Implementation A stack could instead be implemented such that the stack pointer points to a FULL location at the top of the stack. sp (initial state) sp (1 item on stack)

43 CMPE12cGabriel Hugh Elkaim 43 PUSH operation: add$s0, $s0, 4 sw$s1, 0($s0) POP operation: lw$s1, 0($s0) sub$s0, $s0, 4 The stack would “grow” from the end of the array towards the beginning. Stack Implementation

44 CMPE12cGabriel Hugh Elkaim 44 System Stack Gives the programmer an “infinite” dynamic memory. OS and HW work together to provide this. MIPS uses the $sp ($29) to access. Starts at the high address and moves towards the smaller addresses. Why?

45 CMPE12cGabriel Hugh Elkaim 45 System Stack Examples

46 CMPE12cGabriel Hugh Elkaim 46 More System Stack Examples

47 CMPE12cGabriel Hugh Elkaim 47 Queues A Queue is a FIFO (First In, First Out) –classic analogy is a line Get in (at the TAIL) wait, move up in line Get out at front (at the HEAD) Putting thing into queue is called ENQUEUE Getting thing out of the queue is called DEQUEUE It takes two pointers to keep track of the head and tail of the queue –Let’s use $s5 for the HEAD –Let’s use $s7 for the TAIL

48 CMPE12cGabriel Hugh Elkaim 48 Queues Initial state: Head (s5), and Tail (s7) After 1 enqueue operation: X Head (s5)Tail (s7) After another enqueue operation: XY Head (s5) Tail (s7)

49 CMPE12cGabriel Hugh Elkaim 49 Queues After a dequeue operation: XY Head (s5)Tail (s7) Like stacks, when an item is removed from the data structure, it is physically still present, but correct use of the structure cannot access it.

50 CMPE12cGabriel Hugh Elkaim 50 Queues Implementation of a queue Storage: queue:.word0:infinity# assume infinite for now.text la$s5, queue# head la$s7, queue# tail Enqueue (item): sw$t0, 0($s7)# $t0 has data to store add$s7, $s7, 4 Dequeue (item): beq$s5, $s7, queue _ empty lw$t1, 0($s5) add$s5, $s5, 4 How to add overflow, underflow detection?

51 CMPE12cGabriel Hugh Elkaim 51 Circular Queues To avoid infinite array, wrap around from end to beginning. Head == Tail means empty Head points to first item (for next dequeue) Tail point to empty location (for next enqueue) Q[7]Q[0] Q[1] Q[2] Q[3]Q[4] Q[5] Q[6] Example of an 8 element circular queue Head & Tail pointers

52 CMPE12cGabriel Hugh Elkaim 52 X Tail Head Circular Queues After pushing one element After pushing another element X Tail Head Y

53 CMPE12cGabriel Hugh Elkaim 53 Circular Queues After popping an element X Tail Head Y

54 CMPE12cGabriel Hugh Elkaim 54 Circular Queues Storage and initialization: queue:.word0:queue _ size queue _ end:.wordqueue+4*queue _ size la $s5, queue# head la$s7, queue# tail Enqueue (item) sw$t0, 0($s7)# data to enqueue is in $t0 add$s7, $s7, 4 la$t1, queue _ end blt$s7, $t1, continue1 la$s7, queue# wrap around continue1:

55 CMPE12cGabriel Hugh Elkaim 55 Circular Queues Dequeue (item): beq$s5, $s7, queue _ empty lw$t0, 0($s5) add$s5, $s5, 4 la$t1, queue_end blt$s5, $t1, continue2 la$s5, queue# wrap around continue2: How to add overflow, underflow detection?

56 CMPE12cGabriel Hugh Elkaim 56 Modulo for Queues

57 CMPE12cGabriel Hugh Elkaim 57 Queue Examples

58 CMPE12cGabriel Hugh Elkaim 58 Summary of data structures All data structures are based on the simple array. 2D Arrays, Stacks, Queues. It is all about the implementation. Bounds checking is important. If not documented can become confusing.

59 CMPE12cGabriel Hugh Elkaim 59

60 CMPE12cGabriel Hugh Elkaim 60


Download ppt "Memory and Data Structures  Arrays  Stacks  Queues."

Similar presentations


Ads by Google