Download presentation
Presentation is loading. Please wait.
Published byDebra Bradley Modified over 9 years ago
1
MIPS coding
2
SPIM Some links can be found such as: http://www.cs.gmu.edu/~dnord/cs265/spim_intro.html
3
3 A C Sort Example
4
4
5
5
6
6
7
7
8
9/23/2015week04-3.ppt8 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
9
9/23/2015week04-3.ppt9 String Copy Procedure
10
9/23/2015week04-3.ppt10 String Copy Procedure Do we have to use $s0?
11
9/23/2015CDA310011 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
12
9/23/2015CDA310012 C Programs Using Arrays and Pointers
13
9/23/2015CDA310013 C Programs Using Arrays and Pointers
14
9/23/2015CDA310014 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
15
9/23/2015CDA310015 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
16
9/23/2015CDA310016 Comparing Two Versions Suppose the size of array is n, how many instructions for the array version? How many instructions for the pointer version?
17
9/23/2015CDA310017 Comparing Two Versions Suppose the size of array is n, how many instructions for the array version? How many instructions for the pointer version?
18
9/23/2015CDA310018 Comparing Two Versions Suppose the size of array is n, how many instructions for the array version? How many instructions for the pointer version?
19
9/23/2015CDA310019 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
20
9/23/2015CDA310020 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
21
9/23/2015CDA310021 Tree Node In MIPS, we need three words
22
9/23/2015CDA310022 Searching the Tree
23
9/23/2015CDA310023 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
24
9/23/2015week04-3.ppt24 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
25
9/23/2015week04-3.ppt25 Number 1234 in Memory
26
9/23/2015week04-3.ppt26 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
27
9/23/2015week04-3.ppt27 MIPS Addressing Modes 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
28 Translating a C Program
29
29 Generated Intel Assembly Example
30
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
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
32 Linkers
33
33 Loaders Note that an executable file is on the disk – To run a program, it needs to be loaded into memory
34
34 Dynamically Linked Libraries
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.