MIPS (SPIM) Assembler Syntax

Similar documents
Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CS3350B Computer Architecture MIPS Introduction

CS3350B Computer Architecture

Course Administration

Chapter 2. Instruction Set Architecture (ISA)

Course Administration

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Computer Organization MIPS ISA

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

CS3350B Computer Architecture MIPS Instruction Representation

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Computer Architecture

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Chapter 3. Instructions:

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Instructions: Language of the Computer

Chapter 2: Instructions:

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

CENG3420 Lecture 03 Review

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

CSSE 232 Computer Architecture I. I/O and Addressing

CS222: MIPS Instruction Set

We will study the MIPS assembly language as an exemplar of the concept.

Communicating with People (2.8)

Assembler. Lecture 8 CS301

Chapter 2A Instructions: Language of the Computer

Systems Architecture I

ECE 30 Introduction to Computer Engineering

MIPS%Assembly% E155%

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

CS/COE1541: Introduction to Computer Architecture

ECE 15B COMPUTER ORGANIZATION

Lecture 4: MIPS Instruction Set

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Course Administration

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

COMPUTER ORGANIZATION AND DESIGN

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Instructions: Assembly Language

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

EEC 581 Computer Architecture Lecture 1 Review MIPS

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

EE 361 University of Hawaii Fall

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

MIPS Instruction Set Architecture (2)

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Chapter 2. Instructions:

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set


ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Computer Architecture. The Language of the Machine

MIPS Memory Access Instructions

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

CS 4200/5200 Computer Architecture I

Computer Architecture

Administrivia. Midterm Exam - You get to bring. What you don t need to bring. Conflicts? DSP accomodations? Head TA

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Chapter 2. Instructions: Language of the Computer

All instructions have 3 operands Operand order is fixed (destination first)

Lecture 7: Examples, MARS, Arithmetic

Lecture 6: Assembly Programs

Instruction Set Architecture

CS 61c: Great Ideas in Computer Architecture

Assembler. #13 Running a Program II

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Reduced Instruction Set Computer (RISC)

Instructions: Language of the Computer

Thomas Polzer Institut für Technische Informatik

From Code to Program: CALL Con'nued (Linking, and Loading)

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

MIPS Reference Guide

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

Machine Instructions - II. Hwansoo Han

Instruction encoding. MIPS Instruction encoding

CS3350B Computer Architecture MIPS Procedures and Compilation

EN164: Design of Computing Systems Lecture 09: Processor / ISA 2

ECE 486/586. Computer Architecture. Lecture # 8

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Transcription:

MIPS (SPIM) Assembler Syntax Comments begin with # Everything from # to the end of the line is ignored Identifiers are a sequence of alphanumeric characters, underbars (_), and dots () that do not begin with a number Labels are declared by putting them at the beginning of a line followed by a colon data item: word 1 text global main # Must be global main: lw $t0, item 331 Week 41 Spring 2005

SPIM supported MIPS directive align n ascii str asciiz str byte b1,, bn data <addr> double d1,,dn align the next datum on a 2 n byte boundary store the string str in mem, but do not null-terminate it store the string str in mem, but nullterminate it store the n values in successive bytes of memory subsequent items are stored in the data segment If the optional argument addr is present, subsequent items are stored starting at address addr store the n floating-point double precision numbers in successive memory locations 331 Week 42 Spring 2005

SPIM supported MIPS directive (cont d) extern sym size Declare that the datum stored at sym is size bytes large and is a global label float f1,,fn global sym half h1,, hn kdata <addr> store the n floating-point single precision numbers in successive memory locations Declare that label sym is global and can be referenced from other files store the n 16-bit quantities in successive memory halfwords subsequent items are stored in the kernel data segment If the optional argument addr is present, subsequent items are stored starting at addr ktext <addr> Subsequent items are put in the kernel text segment If the optional argument addr is present, subsequent items are stored starting at addr 331 Week 43 Spring 2005

SPIM supported MIPS directive (cont d) set no at set no at space n text <addr> word w1,,wn It prevents SPIM from complaining about subsequent instructions that use register $at It prevents SPIM from complaining about subsequent instructions that use register $at Allocate n bytes of space in the current segment (which must be data segment in SPIM) Subsequent items are put in the text segment If the optional argument addr is present, subsequent items are stored starting at addr store the n 32-bit quantities in successive memory words 331 Week 44 Spring 2005

Branching Far Away What if the branch destination is further away than can be captured in 16 bits? beq $s0, $s1, L1 331 Week 45 Spring 2005

Dealing with Constants Small constants are used quite frequently (often 50% of operands) eg, A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? put typical constants in memory and load them create hard-wired registers (like $zero) for constants Allow for MIPS instructions like addi $sp, $sp, 4 slti $t0, $t1, 10 andi $t0, $t0, 6 ori $t0, $t0, 4 How do we make this work? 331 Week 46 Spring 2005

Immediate Operands MIPS immediate instructions: addi $sp, $sp, 4 #$sp = $sp + 4 slti $t0, $s2, 15 #$t0 = 1 if $s2<15 Machine format: op rs rt 16 bit immediate I format 8 29 29 4 10 18 8 15 The constant is kept inside the instruction itself! I format Immediate format Limits immediate values to the range +2 15 1 to -2 15 331 Week 47 Spring 2005

How About Larger Constants? We'd also like to be able to load a 32 bit constant into a register Must use two instructions, new "load upper immediate" instruction lui $t0, 1010101010101010 16 0 8 1010101010101010 Then must get the lower order bits right, ie, ori $t0, $t0, 1010101010101010 1010101010101010 0000000000000000 0000000000000000 1010101010101010 331 Week 48 Spring 2005

MIPS Addressing Modes Register addressing operand is in a register Base (displacement) addressing operand is at the memory location whose address is the sum of a register and a 16-bit constant contained within the instruction Immediate addressing operand is a 16-bit constant contained within the instruction PC-relative addressing instruction address is the sum of the PC and a 16-bit constant contained within the instruction Pseudo-direct addressing instruction address is the 26-bit constant contained within the instruction concatenated with the upper 4 bits of the PC 331 Week 49 Spring 2005

Addressing Modes Illustrated 1 Register addressing op rs rt rd funct 2 Base addressing op rs rt offset base register 3 Immediate addressing op rs rt operand 4 PC-relative addressing op rs rt offset Program Counter (PC) 5 Pseudo-direct addressing op jump address Program Counter (PC) Register word operand Memory word or byte operand Memory branch destination instruction Memory jump destination instruction 331 Week 410 Spring 2005

Design Principles Simplicity favors regularity fixed size instructions 32-bits small number of instruction formats Smaller is faster limited instruction set limited number of registers in register file limited number of addressing modes Good design demands good compromises three instruction formats Make the common case fast arithmetic operands from the register file (load-store machine) allow instructions to contain immediate operands 331 Week 411 Spring 2005

Review: MIPS ISA, so far Category Instr Op Code Example Meaning Arithmetic (R & I format) add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3 add immediate 8 addi $s1, $s2, 6 $s1 = $s2 + 6 Data Transfer (I format) or immediate 13 ori $s1, $s2, 6 $s1 = $s2 v 6 load word 35 lw $s1, 24($s2) $s1 = Memory($s2+24) store word 43 sw $s1, 24($s2) Memory($s2+24) = $s1 load byte 32 lb $s1, 25($s2) $s1 = Memory($s2+25) store byte 40 sb $s1, 25($s2) Memory($s2+25) = $s1 load upper imm 15 lui $s1, 6 $s1 = 6 * 2 16 Cond Branch (I & R format) Uncond Jump (J & R format) br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L br on not equal 5 bne $s1, $s2, L if ($s1!=$s2) go to L set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0 set on less than immediate 10 slti $s1, $s2, 6 if ($s2<6) $s1=1 else $s1=0 jump 2 j 2500 go to 10000 jump register 0 and 8 jr $t1 go to $t1 jump and link 3 jal 2500 go to 10000; $ra=pc+4 331 Week 412 Spring 2005

The Code Translation Hierarchy C program compiler assembly code assembler object code library routines linker machine code executable loader memory 331 Week 413 Spring 2005

Compiler Transforms the C program into an assembly language program Advantages of high-level languages many fewer lines of code easier to understand and debug Today s optimizing compilers can produce assembly code nearly as good as an assembly language programming expert and often better for large programs good smaller code size, faster execution 331 Week 414 Spring 2005

Assembler Transforms symbolic assembler code into object (machine) code Advantages of assembly language Programmer has more control compared to higher level language much easier than remembering instruction binary codes can use labels for addresses and let the assembler do the arithmetic can use pseudo-instructions - eg, move $t0, $t1 exists only in assembler (would be implemented using add $t0,$t1,$zero ) However, must remember that machine language is the underlying reality eg, destination is no longer specified first And, when considering performance, you should count real instructions executed, not code size 331 Week 415 Spring 2005

Other Tasks of the Assembler Determines binary addresses corresponding to all labels keeps track of labels used in branches and data transfer instructions in a symbol table - pairs of symbols and addresses Converts pseudo-instructions to legal assembly code register $at is reserved for the assembler to do this Converts branches to far away locations into a branch followed by a jump Converts instructions with large immediates into a load upper immediate followed by an or immediate Converts numbers specified in decimal and hexidecimal into their binary equivalents Converts characters into their ASCII equivalents 331 Week 416 Spring 2005

Typical Object File Pieces Object file header: size and position of following pieces Text module: assembled object (machine) code Data module: data accompanying the code static data - allocated throughout the program dynamic data - grows and shrinks as needed by the program Relocation information: identifies instructions (data) that use (are located at) absolute addresses those that are not relative to a register (eg, jump destination addr) when the code and data is loaded into memory Symbol table: remaining undefined labels (eg, external references) Debugging information Object file header text segment data segment relocation information symbol table debugging information 331 Week 417 Spring 2005

MIPS (spim) Memory Allocation Memory Mem Map I/O f f f f f f f c Kernel Code $sp & Data 8000 0080 7f f e f f fc Stack 2 30 words $gp PC Dynamic data Static data Your Code Reserved 1000 8000 ( 1004 0000) 1000 0000 0040 0000 0000 0000 331 Week 418 Spring 2005

Process that produces an executable file Source file compiler + assembler object file Source file compiler + assembler object file linker Executable file Source file compiler + assembler object file program library 331 Week 419 Spring 2005

Linker Takes all of the independently assembled code segments and stitches (links) them together Much faster to patch code and recompile and reassemble that patched routine, than it is to recompile and reassemble the entire program Decides on memory allocation pattern for the code and data modules of each segment remember, segments were assembled in isolation so each assumes its code s starting location is 0x0040 0000 and its static data starting location is 0x1000 0000 Absolute addresses must be relocated to reflect the new starting location of each code and data module Uses the symbol table information to resolve all remaining undefined labels branches, jumps, and data addresses to external segments 331 Week 420 Spring 2005

Loader object file instructions Relocation records object file main: jal??? jal??? call, sub call, printf C library printf: sub: linker executable file main: printf: sub: jal sub jal printf 331 Week 421 Spring 2005

Loader Loads (copies) the executable code now stored on disk into memory at the starting address specified by the operating system Initializes the machine registers and sets the stack pointer to the first free location (0x7ffe fffc) Copies the parameters (if any) to the main routine onto the stack Jumps to a start-up routine (at PC addr 0x0040 0000 on xspim) that copies the parameters into the argument registers and then calls the main routine of the program with a jal main 331 Week 422 Spring 2005