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

Post on 29-Dec-2015

217 views 2 download

Tags:

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

MIPS coding

SPIM

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

3

A C Sort Example

4

A C Sort Example

5

A C Sort Example

6

A C Sort Example

7

A C Sort Example

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

04/19/23 week04-3.ppt 9

String Copy Procedure

04/19/23 week04-3.ppt 10

String Copy Procedure

• Do we have to use $s0?

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

04/19/23 CDA3100 12

C Programs Using Arrays and Pointers

04/19/23 CDA3100 13

C Programs Using Arrays and Pointers

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

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

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?

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

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

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

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

04/19/23 CDA3100 21

Tree Node

• In MIPS, we need three words

04/19/23 CDA3100 22

Searching the Tree

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

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

04/19/23 week04-3.ppt 25

Number 1234 in Memory

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

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

28

Translating a C Program

29

Generated Intel Assembly Example

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?

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)

32

Linkers

33

Loaders• Note that an executable file is on the disk

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

34

Dynamically Linked Libraries