Graduate Institute of Electronics Engineering, NTU. SPIM and MIPS Asm. Presenter: 沈文中 (TA) Date: 2004/10/13

Similar documents
MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

QtSPIM and MARS : MIPS Simulators

MIPS Processor Overview

COMP2421 COMPUTER ORGANZATION. Lab 3

Assignment 1: Pipelining Implementation at SPIM Simulator

MIPS Architecture and Assembly Language Overview

Computer Systems and Architecture

MIPS Procedure Calls. Lecture 6 CS301

SPIM Instruction Set

EECS 322 The SPIM simulator

CENG3420 Lab 1-1: MIPS assembly language programing

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

MIPS function continued

SPIM & MIPS Programming

MIPS Assembly Language Programming

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

Course Administration

SPIM S20: A MIPS R2000 Simulator

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

MIPS Assembly Language. Today s Lecture

MIPS Assembly Language Programming

MIPS Assembly Language Guide

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

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

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

MIPS (SPIM) Assembler Syntax

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

ECE 250 / CS 250 Computer Architecture. Procedures

ECE 30 Introduction to Computer Engineering

USING A SIMULATOR. QUICK INTRODUCTION From various sources For cs470

MIPS Assembly (Functions)

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

CPSC 330 Computer Organization

MIPS Coding Snippets. Prof. James L. Frankel Harvard University. Version of 9:32 PM 14-Feb-2016 Copyright 2016 James L. Frankel. All rights reserved.

SPIM & MIPS Programming

MIPS Functions and the Runtime Stack

Lab 4 : MIPS Function Calls

Assembly Language Programming

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

MIPS Assembly Language

MIPS Assembly Language

Compiling Techniques

Going From C to MIPS Assembly. Getting Started

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Lecture 7: Examples, MARS, Arithmetic

MIPS Assembly Programming

ECE 15B COMPUTER ORGANIZATION

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

SPIM & MIPS. Department of Information and Management. Computer Organization and Structure 年 9 月 29 日星期 一

Course Administration

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

Procedure Call and Return Procedure call

Lecture 7: Procedures

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

CS61C : Machine Structures

Chapter 2. Instructions:

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

Assembler. Lecture 8 CS301

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

CS 61c: Great Ideas in Computer Architecture

Chapter 3. Instructions:

4.2: MIPS function calls

Kernel Registers 0 1. Global Data Pointer. Stack Pointer. Frame Pointer. Return Address.

We can emit stack-machine-style code for expressions via recursion

ECE 30 Introduction to Computer Engineering

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

MIPS Functions and Instruction Formats

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

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

UCB CS61C : Machine Structures

MIPS Assembly (FuncDons)

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

EEC 581 Computer Architecture Lecture 1 Review MIPS

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

instructions aligned is little-endian

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

MIPS Reference Guide

COMP2611: Computer Organization MIPS function and recursion

Lecture 5. Announcements: Today: Finish up functions in MIPS

Instructor: Francis G. Wolff Case Western Reserve University This presentation uses powerpoint animation: please viewshow

MIPS Assembly (FuncDons)

Instruction Set Architecture

Assembler. #13 Running a Program II

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Review of Activation Frames. FP of caller Y X Return value A B C

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

A crash course in MIPS assembly programming

CS 61c: Great Ideas in Computer Architecture

Chapter 2A Instructions: Language of the Computer

CS61C : Machine Structures

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Transcription:

SPIM and MIPS Asm Presenter: 沈文中 (TA) Date: 2004/10/13 ACCESS IC LAB

SPIM Introduction Installation Outline Graphic Interface Description Loading and Running a Program Simulator Setting MIPS ASM Assembler Syntax System Calls A Detailed Example page2

Introduction A simulator of MIPS R2000/R3000 Computers Developed by James R. Larus, Computer Sciences Department, University of Wisconsin-Madison Multiple versions for Unix, DOS and Windows page3

Get Related Files http://lion.ee.ntu.edu.tw/class/spim/spim.htm Windows Version Installation -Extract the zipped file to a new folder -Execute setup.exeand follow the installation procedure page4

Graphic Interface Description Menu bar File operations Simulator settings Windows views online Help information Register window Text Segment window Data Segment window Message window Console window page5

Registers Text Segment DataSegment Messages

Loading and Runing a Program Load: Run: Trace: Restart: File->Open (*.s) Simulator->Go Control-C Simulator->Continue/Break Simulator->Single Step/Multiple Step Simulator->Breakpoints Simulator->Clear Registers/Reinitialize Simulator->Reload page8

Simulator Setting page9

Assembler Syntax Comments: begin with a sharp-sign () Identifiers: a sequence of alphanumeric characters, underbars(_), and dots(.) that do not begin with a number Reserved words: opcodes for instructions Labels: at the beginning of a line followed by a colon (:) String are enclosed in double-quotes (") Special characters in strings follow the C convention (\n, \t, \") page10

Assembler Directives.text <addr>: the next items are put in the user text segment.data <addr>: data segment.align n: align the next datum on a 2 n byte boundary.byte b1,, bn: store the n values in successive bytes.half h1,, hn: 16-bit quantities (2 bytes).word w1,, wn: 32-bit quantities (4 bytes).float f1,.., fn: single precision numbers.double d1,, dn: double precision numbers page11

Assembler Directives.ascii str: store the string in memory.asciiz str: with null.space n: allocate n bytes of space.globl sym: declare that symbol sym is global page12

SPIM for Windows Examples page13

add.s addition of an array.text.align 2.globlmain main: lw $a0, Size read the size of array li $a1, 0 index i li $a2, 0 a2 contains the sum li $t2, 4 t2 contains constant 4 loop: mul $t1, $a1, $t2 t1 gets i*4 lw $a3, Nstart($t1) a3 = N[i] add $a2, $a2, $a3 sum = sum + N[i] add $a1, $a1, 1 i = i + 1 beq $a1, $a0, STOR go to STOR if finished j loop STOR: sw $a2, Result store sum at Result.data.align 2 Nstart:.word 8, 25, -5, 55, 33, 12, -78 Size:.word 7 Result:.word 0

Text Segment ========================= [0x00400000] 0x3c011001 lui $1, 4097 [Size] ; 11: lw $a0, Size read the size of array [0x00400004] 0x8c24001c lw $4, 28($1) [Size] [0x00400008] 0x34050000 ori $5, $0, 0 ; 12: li $a1, 0 index i [0x0040000c] 0x34060000 ori $6, $0, 0 ; 13: li $a2, 0 a2 contains the sum [0x00400010] 0x340a0004 ori $10, $0, 4 ; 14: li $t2, 4 t2 contains constant 4 [0x00400014] 0x00aa0018 mult $5, $10 ; 16: mul $t1, $a1, $t2 [0x00400018] 0x00004812 mflo $9 [0x0040001c] 0x3c011001 lui $1, 4097 [Nstart] ; 17: lw $a3, Nstart($t1) [0x00400020] 0x00290821 addu $1, $1, $9 [0x00400024] 0x8c270000 lw $7, 0($1) [Nstart] [0x00400028] 0x00c73020 add $6, $6, $7 ; 18: add $a2, $a2, $a3 [0x0040002c] 0x20a50001 addi $5, $5, 1 ; 19: add $a1, $a1, 1 i = i + 1 [0x00400030] 0x10a40002 beq $5, $4, 8 [STOR-0x00400030] ; 20: beq $a1, $a0, STOR [0x00400034] 0x08100005 j 0x00400014 [loop] ; 21: j loop [0x00400038] 0x3c011001 lui $1, 4097 [Result] ; 23: sw $a2, Result store sum at Result [0x0040003c] 0xac260020 sw $6, 32($1) [Result]

Data Segment ========================= DATA [0x10000000]...[0x1000fffc] [0x1000fffc] [0x10010000] [0x10010010] [0x10010020]...[0x10040000] STACK [0x7fffeffc] KERNEL DATA [0x90000000]...[0x90010000] 0x00000000 0x00000000 0x00000008 0x00000019 0xfffffffb 0x00000037 0x00000021 0x0000000c 0xffffffb2 0x00000007 0x00000000 0x00000000 0x00000000

System Calls Service Code Arguments Result print_int 1 $a0=integer print_float 2 $f12=float print_double 3 $f12=double print_string 4 $a0=string read_int 5 integer (in $v0) read_float 6 float (in $f0) read_double 7 double (in $f0) read_string 8 $a0=buffer, $a1=length sbrk 9 $a0=amount address (in $v0) exit 10 System calls:loads the system call code into register $v0 exit: stops a program page18

How to print and read in a string.data str1: str2:.asciiz"please type a string with lenght19\n".asciiz"this string is that you input\n".text.align 2.globlmain 本文區段 main 為全域變數 main: li $v0, 4 print_string 的系統呼叫程式碼 la $a0, str1 要列印字串的位置在 str1 syscall 印出字串

li $v0, 8 read_string 的系統呼叫程式碼 li $a1, 20 可讀入的字串長度 19 syscall string 的起始位置為暫存器 $a0 addi$t0, $a0, 0 把暫存器 $a0 拷貝至暫存器 $t0 li $v0, 4 print_string 的系統呼叫程式碼 la $a0, str2 要列印字串的位置在 str2 syscall 印出字串 li $v0, 4 addi$a0, $t0, 0 把暫存器 $t0 拷貝至暫存器 $a0 syscall

Segment Data segment ASM Architecture Stack, int, string, float, Code segment Label: called by jump, loop Macro (function ): begin with.macro; end with.endmacro page21

========================================================== Static data allocation and initialization ==========================================================.data PLACE OVERALL COMMENTS FOR DATA DESCLARATIONS HERE REPLACE MYDATA1 and MYDATA2 with your data declarations MYDATA1 : PLACE COMMENTS SPECIFIC TO MYDATA1 HERE MYDATA2 : PLACE COMMENTS SPECIFIC TO MYDATA2 HERE ========================================================== Macro definition area ========================================================== ========================================================== Macro : print_int Inplicit argument : an integer in $a0 Side-effect : modifies $v0.macro print_int li $v0,1 syscall.endmacro Example program page22

========================================================== Macro : print_string Implicit argument : Address of a null terminated string in $a0 Side-effect : modifies $v0.macro print_string li $v0, 4 syscall.endmacro ADDITIONAL MACROS GO HERE ========================================================== Constants ==========================================================.define fp_off -4.define ra_off 0 ADDITIONAL CONSTANTS GO HERE ========================================================== Program text ==========================================================.text.global main page23

main : Main program entry move $t0, $fp keep track of old fp move $fp, $sp old sp in base of stack frame sw $ra, ra_off($fp) save return add. 1st thing in the frame sw $t0, fp_off($fp) save old fp on frame subu $sp, $sp, 8 move sp to new position in frame Main body PLACE INSTRUCTIONS FOR MAIN BODY HERE main exit lw $t0, fp_off($fp) restore fp lw $ra, ra_off($fp) and ra move $sp, $fp restore stack pointer move $fp, $t0 and frame pointer j $ra return ========================================================== page24

PLACE DESCRIPTIVE PROLOGUE FOR PROCEDURE IN COMMENTS BELOW Procedure : Description : Parameters : Locals : Returns : LABEL1 : procedure entry PLACE INSTRUCTIONS FOR PROCEDURE ENTRY HERE procedure body PLACE INSTRUCTIONS FOR PROCEDURE BODY HERE procedure exit PLACE INSTRUCTIONS FOR PROCEDURE EXIT HERE ADDITIONAL PROCEDURES GO HERE.end main page25

========================================================== Static data allocation and initialization ==========================================================.data String for output output :.ascliz factorial of five is newline :.ascliz \n ========================================================== Macro definition area ========================================================== ========================================================== Macro : print_int Inplicit argument : an integer in $a0 Side-effect : modifies $v0.macro print_int li $v0,1 syscall.endmacro page26

========================================================== Macro : print_string Implicit argument : Address of a null terminated string in $a0 Side-effect : modifies $v0.macro print_string li $v0, 4 syscall.endmacro ========================================================== Macro : push on stack Argument : what to put on stack Side-effect : modifies $sp.macro push(operand) sw operand, 0($sp) addu $sp, $sp, -4.endmacro ========================================================== Macro : pop from stack Argument : where to pop Side-effect : modifies $sp page27

.macro push(operand) addu $sp, $sp, 4 lw operand, 0($sp).endmacro ========================================================== Constants ==========================================================.define fp_off -4.define ra_off 0 ========================================================== Program text ==========================================================.text.global main main : Main program entry move $t0, $fp keep track of old fp move $fp, $sp old sp in base of stack frame sw $ra, ra_off($fp) save return add. 1st thing in the frame sw $t0, fp_off($fp) save old fp on frame subu $sp, $sp, 8 move sp to new position in frame page28

Main body li $a0, 5 will compute factorial of 5 jal fact move $s0, $v0 save result la $a0, output prepare to print print_string header la $a0, newline print_string move $a0, $s0 prepare to print print_int print factorial main exit lw $t0, fp_off($fp) restore fp lw $ra, ra_off($fp) and ra move $sp, $fp restore stack pointer move $fp, $t0 and frame pointer j $ra return ========================================================== page29

PROCEDURE : Fact DESCRIPTION : an integer in a0 LOCALS : RETURN : fact (a0) in v0 DESCRIPTION : Computes recursively factorial fact : Procedure entry move $t0, $fp keep track of old fp move $fp, $sp old sp in base of stack frame sw $ra, ra_off($fp) save return add. 1st thing in the frame sw $t0, fp_off($fp) save old fp on frame subu $sp, $sp, 8 move sp to new position in frame Procedure body test argument for 0; if so return with value 1 beq $a0, $0,nore test a0; if zero goto out otherwise save a0 on the stack push ($a0) a0 saved addi $a0, $a0, -1 decrement argument page30

recursive entry jal fact call back from recursive level v0 = v0 * a0 pop ($t0) pop previous argument mul $v0, $v0, $t0 get result at this level b out exit that level end of recursion nore: li $v0,1 result in v0 and exit regs, perform alu Procedure exit out: lw $t0, fp_off($fp) restore fp lw $ra, ra_off($fp) and ra move $sp, $fp restore stack pointer move $fp, $t0 and frame pointer j $ra return page31

1. A template for MIPS assembly programs.data Static data declared below, with labels for each data structure (MYDATA1,MYDATA2) MYDATA1: data storage directive such as.word,.space, etc. MYDATA2: another data storage directive.text Make main visible to other files.globl main Assembly instructions go here, with labels for procedures and branch targets (main, Sum, LABEL1): page32

1. A template for MIPS assembly programs main : NOP NOP LABEL1: NOP NOP first instruction of main a branch target j $ra last instruction of main Instructions for additional procedures go here. A descriptive prologue is a good idea. For example: PROCEDURE: Sum PARAMETERS: int * numbers (a0) Address of an array of integers int count (a1) Length of the array page33

1. A template for MIPS assembly programs LOCALS: i (t0) Loop index RETURNS: sum (v0) DESCRIPTION: Compute the sum of numbers[0].. Numbers[count], and return the result. Sum: NOP first instruction of procedure Sum j $ra last instruction of procedure Sum page34

2. Assembler directives Assembler directives do not generate instructions, but instead provide additional information to the assembler on how to arrange code and data in memory, how to interpret macros, and so on. Directives such as.text and.data are present in even the simplest programs. Other more interesting directives are described below: 2.1 Data allocation and initialization Most of the directives are to instruct the program on how to allocate space for and initialize static variables. These always appear in the data segment after the.data directive but before the.text directive. You ll probably use the following frequently: Allocate space for and initialize a null-terminated ascii string:.asciiz This is an ascii string.\n Allocate and initialize an array of 5 bytes:.byte 0x00 0x01 0x02 0x03 0x04 Allocate and initialize an array of 2 32-bit words:.word 0x00000000 0x00400000 Allocate an unitialized area of 100 byts (25 32-bit words).space 100 Additional directives are described in pages A-51~A-53 of the text. page35

2. Assembler directives 2.2 Macros Suppose you found yourself frequently using the pop and push operations described in class. Rather than having to type in two instructions every time you wanted to push a register onto the stack or pop a value from the stack into a register, it would be nice if the assembler provided primitives to do it for you. The assembler doesn t, but it allows you to create your own, with macros. For example, at the beginning of the assembler program, you could add the lines.macro push ($operand) sw $operand, 0($sp) addu $sp, $sp, -4.end_macro page36

2. Assembler directives.macro pop ($operand) addu $sp, $sp, 4 lw $operand, 0($sp).end_macro Now in your program you can use the newly defined macro to push, for example, register $a0 onto the stack: push($a0) The assembler uses your macro definition to expand this to sw $a0, 0($sp) addu $sp, $sp, -4 In general macros are useful when you find yourself in repetitive sequences of instructions. They are not a substitute for procedures, though. (Why not?) page37

3. Printing to the console To test your program, you ll need some way of displaying your output in a humanreadable format. SPIM provides several system calls for printing to the console. syscall : is effectively a special procedure call with an unusual calling convention. Before using the syscall instruction, register $2 must contain the syscall number; for now we re only interested in syscall 1 (print_int) and syscall 4 (print_string). (More syscalls can be found on page A-49 of the text.) For these two syscalls, register $4 should contains the integer value or the address of the string to be printed. So in this example it takes three syscalls to print out the result string. For your convenience, we have defined macros for these two printing syscalls within the template..data str:.asciiz The sum from 0.. 2 is newline:.asciiz \n page38

3. Printing to the console li $2, 4 Use syscall 4 (print_string) la $4, str to display string str syscall li $2, 1 Use syscall 1 (print_int) lw $4, 4($sp) to print the sum syscall li $2, 4 Use syscall 4 (print_string) la $4, newline to print a newline syscall page39