mips coding. spim some links can be found such as: dnord/cs265/spim_intro.html

34
MIPS coding

Upload: debra-bradley

Post on 29-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

MIPS coding

Page 2: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

SPIM

• Some links can be found such as:http://www.cs.gmu.edu/~dnord/cs265/spim_intro.html

Page 3: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

3

A C Sort Example

Page 4: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

4

A C Sort Example

Page 5: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

5

A C Sort Example

Page 6: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

6

A C Sort Example

Page 7: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

7

A C Sort Example

Page 8: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 8

Character and String Operations• Characters are encoded as 0’s and 1’s using ASCII

most commonly– American Standard Code for Information Interchange– Each character is represented using 8 bits (or a byte)

• MIPS provides instructions to move bytes– Load byte (lb) loads a byte to the rightmost 8 bits of a

register– Store byte (sb) write the rightmost 8 bits of a register to

memory

Page 9: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 9

String Copy Procedure

Page 10: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 10

String Copy Procedure

• Do we have to use $s0?

Page 11: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 11

Arrays vs. Pointers• Pointers sometime seem difficult to

understand in C/C++– Here by comparing arrays and pointers in MIPS

assembly we can have a much better understanding of the differences

Page 12: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 12

C Programs Using Arrays and Pointers

Page 13: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 13

C Programs Using Arrays and Pointers

Page 14: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 14

MIPS Assembly Using Arrays

• Register allocations: Two parameters are in $a0 (for array) and $a1 (for size), and local variable i is allocated to register $t0

Page 15: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 15

MIPS Assembly Using Pointers

• Register allocations: Two parameters are in $a0 (for array) and $a1 (for size), and local variable p is allocated to register $t0

Page 16: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 16

Comparing Two Versions

• Suppose the size of array is n, how many instructions for the array version?

• How many instructions for the pointer version?

Page 17: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 17

Comparing Two Versions

• Suppose the size of array is n, how many instructions for the array version?

• How many instructions for the pointer version?

n61

Page 18: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 18

Comparing Two Versions

• Suppose the size of array is n, how many instructions for the array version?

• How many instructions for the pointer version?

n61

n43

Page 19: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 19

Tree Sort

• This example shows to support data structures such as nodes in a binary tree

• Binary search tree– One way to create a sorted list is to insert values

sequentially into a binary tree, where the values in the left tree are no larger than the value in the node and the values in the right tree are no smaller than the value in the node

Page 20: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 20

Tree Node• In C, each tree node will be defined as a recursive data

structure

• How can we do that in MIPS?– In MIPS we do not have types explicitly– The types are defined implicitly based on the instructions

we use

Page 21: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 21

Tree Node

• In MIPS, we need three words

Page 22: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 22

Searching the Tree

Page 23: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 CDA3100 23

Treesort.s• It first creates an empty tree by creating a root

node with empty left and right pointers• Loop

– Read a new value– If it is the sentinel value, break out the loop– Otherwise, insert the value into the tree and repeat

• Traverse the tree recursively

• http://www.cs.fsu.edu/~zzhang/CDA3100_Spring_2008_files/tree_sort.asm

Page 24: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 24

Note

• Note that a number can be written and saved in different ways– For example, number 1234 can be represented as

“1234” (as a string)– number 1234 can be represented as a two’s

complement integer

Page 25: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 25

Number 1234 in Memory

Page 26: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 26

Addressing Modes

• An addressing mode is a form for specifying one or more operands

• Typically one instruction set architecture will use multiple forms of addressing modes– In MIPS, we have five different addressing modes

Page 27: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

04/19/23 week04-3.ppt 27

MIPS Addressing Modes

Byte Halfword Word

Registers

Memory

Memory

Word

Memory

Word

Register

Register

1. Immediate addressing

2. Register addressing

3. Base addressing

4. PC-relative addressing

5. Pseudodirect addressing

op rs rt

op rs rt

op rs rt

op

op

rs rt

Address

Address

Address

rd . . . funct

Immediate

PC

PC

+

+

Operand is constant

Operand is in register

lb $t0, 48($s0)

bne $4, $5, Label(label will be assembled into a distance)

j Label

Page 28: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

28

Translating a C Program

Page 29: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

29

Generated Intel Assembly Example

Page 30: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

30

Assembler• The primary task of an assembler is to translate

assembly into machine code– It produces an object file, which consists of machine

language instructions, data, and information needed to place instructions properly in memory

• A UNIX object file consists of the object file header, text segment, static data segment, relocation information, symbol table, and debugging information

• Which instructions may change depending on the absolute locations of instructions?

Page 31: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

31

Linkers• Object files need to be combined together in order

to produce an executable program– The tool is called the linker– Searches the libraries to find library routines used by the

program– Determines the memory locations for each module and

relocates its instructions by adjusting absolute references

– Resolve any unresolved references among files (including libraries)

Page 32: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

32

Linkers

Page 33: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

33

Loaders• Note that an executable file is on the disk

– To run a program, it needs to be loaded into memory

Page 34: MIPS coding. SPIM Some links can be found such as: dnord/cs265/spim_intro.html

34

Dynamically Linked Libraries