SlideShare a Scribd company logo
SPIM: a MIPS simulator

                                           Michele Chinosi
                                   michele.chinosi@uninsubria.it
                                       University of Insubria - Varese (IT)


                                                 13.04.2007




Michele Chinosi (Univ. Insubria)               SPIM: a MIPS simulator         13.04.2007   1 / 26
Outline



1   Assemply Programming
      The Assembly Language


2   Program Structure
      Some introductory notions
      Memory Usage
      CPU Registers
      SPIM Directives
      MIPS Instructions Set
      Assembly Program Elements
      SPIM System Calls




    Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   2 / 26
The Assembly Language




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   3 / 26
High-level Language Program example




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   4 / 26
Assembly Language Example




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   5 / 26
Why to Use Assembly Language



Why to use assembly language
     Speed
     Size
     Embedded computers (car’s brakes computer)
     Predictable execution time
     Time-critical systems
     Ability to exploit specialized instructions
     No high-level languages availability on particular computers




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator         13.04.2007   6 / 26
Why NOT to Use Assembly Language


Why NOT to use assembly language
     Assembly programs are machine-specific
             An assembly language program remains tightly bound to its original
             architecture, even after the computer is eclipsed by new, faster, and more
             cost-effective machines
     Assembly programs are longer
             Assembly language programs are longer than the equivalent programs written
             in a high-level language
     Assembly programs are more difficult to read
             Longer programs are more difficult to read and understand and they contain
             more bugs. Assembly language exacerbates the problem because of its
             complete lack of structure. The resulting programs are hard to read because
             the reader must reconstruct every higher-level construct from its pieces and
             each instance of a statement may be slightly different.



  Michele Chinosi (Univ. Insubria)     SPIM: a MIPS simulator                  13.04.2007   7 / 26
Some introductory notions


Comments
Comments in assembler files begins with a sharp-sign (#). Everything from the
sharp-sign to the end of the line is ignored.

Identifiers
Identifiers are a sequence of alphanumeric characters, underbars ( ), and dots (.)
that do not begin with a number. Opcode for insctructions are reserved words
that are not valid identifiers.

Labels
Labels are declared by putting them at the very beginning of a line followed by a
colon (:).




   Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator           13.04.2007   8 / 26
Some introductory notions



Strings
Strings are enclosed in double-quotes (quot;).

Special characters
Special characters in strings follow the C convention:
newline → n
tab → t
quote → quot;




   Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   9 / 26
MIPS Memory Usage



Memory Usage
Systems based on MIPS processors typically divide memory into three parts.
     The first part, near the bottom of the address space (starting at 4000000hex )
     is the text segment – program’s instructions
     The second part is the data segment, which is divided in two parts:
             Static data, starting from 10000000hex -- objects whose size is known
             and lifetime same as program execution
             Dynamic Data, allocated by the program as it executes
     The third part is the stack segment and resides at the top of the virtual
     address space (starting at address 7fffffffhex ). Like dynamic data, the
     maximum size of a program’s stack is not known in advance.




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator             13.04.2007   10 / 26
Memory Usage




 Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   11 / 26
CPU Registers



SPIM Registers
     the MIPS/SPIM CPU contains 32 general purpose 32-bit registers (0-31)
     register n is designed with $n
     register $0 always contains the value 0

Registers Conventions
     MIPS has estabilished a set of conventions as to how registers should be used
     these suggestons are guidelines
     a program that violated them will not work properly with other sw




  Michele Chinosi (Univ. Insubria)    SPIM: a MIPS simulator          13.04.2007   12 / 26
CPU Registers in details


     $at (1), $k0 (26), $k1 (27): reserved for assembler and OS
     $a0...$a3 (4-7): to pass the first 4 arguments to routines
     $v0,$v1 (2,3): to return values from functions
     $t0...$t9 (8-15,24,25): caller-saved registers used for temporary quantities
     (not preserved across calls)
     $s0...$s7 (16-23): callee-saved registers that hold long-lived values
     (preserved across calls)
     $sp (29): stack pointer (last location in use)
     $fp (30): frame pointer
     $ra (31): return address for a call (written by a jal)
     $gp (28): global pointer, points to the middle of a 64K block of memory in
     the heap



  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator          13.04.2007   13 / 26
CPU Registers Summary




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   14 / 26
SPIM Directives
SPIM supports a subset of the assembler directives provided by MIPS assembler.
All the directive must be written preceding them with a single dot (.).
     .align n
       align the next datum on a 2n byte boudary.
     .ascii str
       store the string in memory, not null-terminated
     .asciiz str
       store the string in memory, null-terminated
     .byte b1, ..., bn
       store the n values in successive bytes of memory
     .data <addr>
       the following data items should be stored in the data segment
     .double d1, ..., dn
       store the n FP double precision numbers in successive memory locations
     .extern sym size
       the datum stored at sym is size byte large and is a global symbol

  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator            13.04.2007   15 / 26
SPIM Directives

     .float f1, ..., fn
       store the n FP single precision numbers in successive memory locations
     .globl sym
       declare that symbol sym is global and can be referenced from other files
     .half h1, ..., hn
       store the n 16-bit quantities in successive memory halfwords
     .kdata <addr>
       the following data items should be stored in the kernel data segment
     .ktext <addr>
       the next items (instructions / words) are put in the kernel text segment
     .space n
       allocate n bytes of space in the data segment
     .text <addr>
       the next items (instructions / words) are put in the user text segment
     .word w1, ..., wn
       store the n 32-bit quantities in successive memory words

  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator           13.04.2007   16 / 26
MIPS Instructions Set (1)


Three different instructions types: R, I, J




Some examples:
  R : add, addu, and, jr, slt, mfhi, mult
  I : addi, beq, bne, lw, ori, sb, sw
  J : j, jal




   Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   17 / 26
MIPS Instructions Set (2)


Instructions Families
All intructions can be grouped in different families depending on their application
domain:
      Arithmetic and Logical Instructions
              abs, add, and, div, mult, neg, nor, not, or, sll, xor, sub, ...
      Constant-Manipulating Instructions
              lui, li
      Comparison Instructions
              slt, seq, sge, sgt, sle, sne, ...
      Branch Instructions
              b, beq, bgez, bgtz, bltz, bne, beqz, bge, bgt, ble, blt, ...
      Jump Instructions
              j, jal, jr, ...



   Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator          13.04.2007   18 / 26
MIPS Instructions Set (3)



Instructions Families
     Load Instructions
             la, lb, lbu, lh, lw, ld, ll, ...
     Store Instructions
             sb, sh, sw, swl, sd, sc, ...
     Data Movement Instructions
             move, mfhi, mflo, mthi, mtlo, ...
     Floating-point Instructions
     Exception and Interrupt Instructions
             syscall, break, nop, ...




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   19 / 26
MIPS Instructions Set (4)

Pseudoinstructions
The whole instructions set can be divided into two different subsets:
     Core instructions set
     Pseudoinstructions set
Pseudoinstructions are composed by multiple instructions identified with a name.
They are interpreted by MIPS and substituted by the original instructions
sequence. Some examples are:
     Branch Less Then: blt  if(R[rs]R[rt]) PC = Label
     Branch Greater Then: bgt  if(R[rs]R[rt]) PC = Label
     Branch Less Then or Equal: ble  if(R[rs] ≤ R[rt]) PC = Label
     Branch Greater Then or Equal: bge  if(R[rs] ≥ R[rt]) PC = Label
     Load Immediate: li  R[rd] = Immediate
     Move: move  R[rd] = R[rs]


  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator            13.04.2007   20 / 26
Assembly Program Elements

            .data                       #   directive
foo:        .asciiz quot;Hello!nquot;          #   string null-terminated
n:          .word 1,4,0                 #   array
res:        .word 0                     #   .word 0 = memory allocation

            .text                       # directive
            .globl main                 # main becomes global

main:       lw $a0, foo                 # $a0,$a1,$t2,... = registers
loop:       li $a1, 1                   # main, loop, store = labels
            lw $a2, n($t2)              #
            add $a2, $a2, $a1           #
            beq $a2, $t3, store         #
            j loop                      # jump to label ‘loop’

store: sw $a2, res                      # res = identifier
       ...

  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator          13.04.2007   21 / 26
SPIM System Calls

SPIM provides a small set of operating-system-like services through the system
call (syscall) instruction. To request a service, a program loads the system call
code into register $v0 and the arguments into registers $a0...$a3/$f12. System
calls that return values put their result in register $v0/$f0.

      .data
str:  .asciiz quot;the answer is quot;
      .text
      .globl main
main: li $v0, 4                # system call code for print_str
      la $a0, str              # address of string to print
      syscall                  # print the string

         li $v0, 1                          # system call code for print_int
         li $a0, 5                          # integer to print
         syscall                            # print it

This code print: the answer is 5

  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator          13.04.2007   22 / 26
SPIM System Call Table


     Service                Syscall Code                     Args         Result

                                           $a0 = int
                                           $f12 = float
  print int                           1

                                           $f12 = double
  print float                          2

                                           $a0 = string
  print double                        3

                                                                       $v0 = int
  print string                        4

                                                                       $f0 = float
  read int                            5

                                                                       $f0 = double
  read float                           6

                                           $a0 = buffer, $a1 = length
  read double                         7

                                           $a0 = amount                $v0 = address
  read string                         8
  sbrk                                9
  exit                               10




  Michele Chinosi (Univ. Insubria)         SPIM: a MIPS simulator          13.04.2007   23 / 26
Hello, World!


This is a very common simple test program...
      .data
str:  .asciiz quot;Hello, World!quot;
      .text
      .globl main
main: li $v0, 4                             # system call code for print_str
      la $a0, str                           # address of string to print
      syscall                               # print the string


Exercise: write an interactive version of “Hello, World!” program




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator          13.04.2007   24 / 26
Hello, name!
One interactive version of “Hello, World!” program
       .data
str:   .asciiz quot;Hello, quot;
iname: .asciiz quot;Name: quot;
name: .space 256
       .text
       .globl main
main:
       la $a0, iname                  # print the prompt
       li $v0, 4
       syscall

        la $a0, name                  # read the string
        li $a1, 256
        li $v0, 8
        syscall

        li $v0, 4                     # print quot;Hello, quot;
        la $a0, str
        syscall

        li $v0, 4                     # print the value inserted
        la $a0, name
        syscall

        li $v0, 10                    # exit
        syscall

   Michele Chinosi (Univ. Insubria)            SPIM: a MIPS simulator   13.04.2007   25 / 26
Conclusions




Exercises
Examples and Exercises




  Michele Chinosi (Univ. Insubria)   SPIM: a MIPS simulator   13.04.2007   26 / 26

More Related Content

What's hot

бв рак лілія михайлівнам.бв
бв рак лілія михайлівнам.бвбв рак лілія михайлівнам.бв
бв рак лілія михайлівнам.бв
agusya
 

What's hot (20)

Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily JobLuca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
 
Pipelining and ILP (Instruction Level Parallelism)
Pipelining and ILP (Instruction Level Parallelism) Pipelining and ILP (Instruction Level Parallelism)
Pipelining and ILP (Instruction Level Parallelism)
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Modified booth
Modified boothModified booth
Modified booth
 
What is Bootloader???
What is Bootloader???What is Bootloader???
What is Bootloader???
 
Trace kernel code tips
Trace kernel code tipsTrace kernel code tips
Trace kernel code tips
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
бв рак лілія михайлівнам.бв
бв рак лілія михайлівнам.бвбв рак лілія михайлівнам.бв
бв рак лілія михайлівнам.бв
 
망고100 보드로 놀아보자 8
망고100 보드로 놀아보자 8망고100 보드로 놀아보자 8
망고100 보드로 놀아보자 8
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
 
Assembly language progarmming
Assembly language progarmmingAssembly language progarmming
Assembly language progarmming
 
Build your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectBuild your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto project
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
presentation on SCB,DEBUG,RESET of Arm Cortex processor
presentation on SCB,DEBUG,RESET of Arm Cortex processorpresentation on SCB,DEBUG,RESET of Arm Cortex processor
presentation on SCB,DEBUG,RESET of Arm Cortex processor
 
Co notes3 sem
Co notes3 semCo notes3 sem
Co notes3 sem
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Yocto project and open embedded training
Yocto project and open embedded trainingYocto project and open embedded training
Yocto project and open embedded training
 
Future Of Transportation Poll Summary (032910)
Future Of Transportation Poll Summary (032910)Future Of Transportation Poll Summary (032910)
Future Of Transportation Poll Summary (032910)
 

Viewers also liked (6)

Spim Mips Simulator
Spim Mips SimulatorSpim Mips Simulator
Spim Mips Simulator
 
The March to MIPS
The March to MIPSThe March to MIPS
The March to MIPS
 
Mips Assembly
Mips AssemblyMips Assembly
Mips Assembly
 
2014 MIPS Progrmming for NTUIM
2014 MIPS Progrmming for NTUIM 2014 MIPS Progrmming for NTUIM
2014 MIPS Progrmming for NTUIM
 
MIPS-SPIM Taiwan
MIPS-SPIM TaiwanMIPS-SPIM Taiwan
MIPS-SPIM Taiwan
 
Mips
MipsMips
Mips
 

Similar to Spim Mips Simulator 08 02

Hennch2nts 160526114335-160526184116-160527121537
Hennch2nts 160526114335-160526184116-160527121537Hennch2nts 160526114335-160526184116-160527121537
Hennch2nts 160526114335-160526184116-160527121537
marangburu42
 
VI) Instruction set Architecture-      Instruction set is a colle.pdf
VI) Instruction set Architecture-      Instruction set is a colle.pdfVI) Instruction set Architecture-      Instruction set is a colle.pdf
VI) Instruction set Architecture-      Instruction set is a colle.pdf
Ankitchhabra28
 
2014 valat-phd-defense-slides
2014 valat-phd-defense-slides2014 valat-phd-defense-slides
2014 valat-phd-defense-slides
Sébastien Valat
 
Chapter 2 Part1
Chapter 2 Part1Chapter 2 Part1
Chapter 2 Part1
ececourse
 
Penn  State  University          School  of.docx
Penn  State  University            School  of.docxPenn  State  University            School  of.docx
Penn  State  University          School  of.docx
danhaley45372
 
number system understand
number system  understandnumber system  understand
number system understand
rickypatel151
 
Chapter 02 instructions language of the computer
Chapter 02   instructions language of the computerChapter 02   instructions language of the computer
Chapter 02 instructions language of the computer
Bảo Hoang
 

Similar to Spim Mips Simulator 08 02 (20)

CODch3Slides.ppt
CODch3Slides.pptCODch3Slides.ppt
CODch3Slides.ppt
 
CS6303 Computer Architecture.pdf
CS6303 Computer Architecture.pdfCS6303 Computer Architecture.pdf
CS6303 Computer Architecture.pdf
 
Hennch2nts 160526114335-160526184116-160527121537
Hennch2nts 160526114335-160526184116-160527121537Hennch2nts 160526114335-160526184116-160527121537
Hennch2nts 160526114335-160526184116-160527121537
 
UNIT 2_ESD.pdf
UNIT 2_ESD.pdfUNIT 2_ESD.pdf
UNIT 2_ESD.pdf
 
VI) Instruction set Architecture-      Instruction set is a colle.pdf
VI) Instruction set Architecture-      Instruction set is a colle.pdfVI) Instruction set Architecture-      Instruction set is a colle.pdf
VI) Instruction set Architecture-      Instruction set is a colle.pdf
 
Introduction to Assembly Language
Introduction to Assembly Language Introduction to Assembly Language
Introduction to Assembly Language
 
2014 valat-phd-defense-slides
2014 valat-phd-defense-slides2014 valat-phd-defense-slides
2014 valat-phd-defense-slides
 
Chapter 2 Part1
Chapter 2 Part1Chapter 2 Part1
Chapter 2 Part1
 
An introduction to digital signal processors 1
An introduction to digital signal processors 1An introduction to digital signal processors 1
An introduction to digital signal processors 1
 
Co question 2006
Co question 2006Co question 2006
Co question 2006
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
Unix lab manual
Unix lab manualUnix lab manual
Unix lab manual
 
Penn  State  University          School  of.docx
Penn  State  University            School  of.docxPenn  State  University            School  of.docx
Penn  State  University          School  of.docx
 
number system understand
number system  understandnumber system  understand
number system understand
 
ASSEMBLY LANGUAGE.pptx
ASSEMBLY LANGUAGE.pptxASSEMBLY LANGUAGE.pptx
ASSEMBLY LANGUAGE.pptx
 
"Hints" talk at Walchand College Sangli, March 2017
"Hints" talk at Walchand College Sangli, March 2017"Hints" talk at Walchand College Sangli, March 2017
"Hints" talk at Walchand College Sangli, March 2017
 
Co question 2008
Co question 2008Co question 2008
Co question 2008
 
Chapter 02 instructions language of the computer
Chapter 02   instructions language of the computerChapter 02   instructions language of the computer
Chapter 02 instructions language of the computer
 
CISC & RISC Architecture
CISC & RISC Architecture CISC & RISC Architecture
CISC & RISC Architecture
 
Nachos
NachosNachos
Nachos
 

More from Michele Chinosi

More from Michele Chinosi (12)

Babbo Natale va in vacanza?
Babbo Natale va in vacanza?Babbo Natale va in vacanza?
Babbo Natale va in vacanza?
 
BPMN Usage Survey: Tables
BPMN Usage Survey: TablesBPMN Usage Survey: Tables
BPMN Usage Survey: Tables
 
BPMN Usage Survey: Results
BPMN Usage Survey: ResultsBPMN Usage Survey: Results
BPMN Usage Survey: Results
 
Why To Consider BPMN 2.0
Why To Consider BPMN 2.0Why To Consider BPMN 2.0
Why To Consider BPMN 2.0
 
Workflow Modeling in EU GENESIS Project
Workflow Modeling in EU GENESIS ProjectWorkflow Modeling in EU GENESIS Project
Workflow Modeling in EU GENESIS Project
 
Modeling and Validating BPMN Diagrams
Modeling and Validating BPMN DiagramsModeling and Validating BPMN Diagrams
Modeling and Validating BPMN Diagrams
 
Modeling Requirements for the Management of Electronic Records
Modeling Requirements for the Management of Electronic RecordsModeling Requirements for the Management of Electronic Records
Modeling Requirements for the Management of Electronic Records
 
Representing Business Processes: Conceptual Model and Design Methodology
Representing Business Processes: Conceptual Model and Design MethodologyRepresenting Business Processes: Conceptual Model and Design Methodology
Representing Business Processes: Conceptual Model and Design Methodology
 
BPeX: A New Approach to BPMN Model Portability - Updated Version
BPeX: A New Approach to BPMN Model Portability - Updated VersionBPeX: A New Approach to BPMN Model Portability - Updated Version
BPeX: A New Approach to BPMN Model Portability - Updated Version
 
Integrating Privacy Policies into Business Processes
Integrating Privacy Policies into Business ProcessesIntegrating Privacy Policies into Business Processes
Integrating Privacy Policies into Business Processes
 
Novelle: A collaborative open source writing tool software
Novelle: A collaborative open source writing tool softwareNovelle: A collaborative open source writing tool software
Novelle: A collaborative open source writing tool software
 
BPeX: A New Approach to BPMN Model Portability
BPeX: A New Approach to BPMN Model PortabilityBPeX: A New Approach to BPMN Model Portability
BPeX: A New Approach to BPMN Model Portability
 

Recently uploaded

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 

Spim Mips Simulator 08 02

  • 1. SPIM: a MIPS simulator Michele Chinosi michele.chinosi@uninsubria.it University of Insubria - Varese (IT) 13.04.2007 Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 1 / 26
  • 2. Outline 1 Assemply Programming The Assembly Language 2 Program Structure Some introductory notions Memory Usage CPU Registers SPIM Directives MIPS Instructions Set Assembly Program Elements SPIM System Calls Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 2 / 26
  • 3. The Assembly Language Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 3 / 26
  • 4. High-level Language Program example Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 4 / 26
  • 5. Assembly Language Example Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 5 / 26
  • 6. Why to Use Assembly Language Why to use assembly language Speed Size Embedded computers (car’s brakes computer) Predictable execution time Time-critical systems Ability to exploit specialized instructions No high-level languages availability on particular computers Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 6 / 26
  • 7. Why NOT to Use Assembly Language Why NOT to use assembly language Assembly programs are machine-specific An assembly language program remains tightly bound to its original architecture, even after the computer is eclipsed by new, faster, and more cost-effective machines Assembly programs are longer Assembly language programs are longer than the equivalent programs written in a high-level language Assembly programs are more difficult to read Longer programs are more difficult to read and understand and they contain more bugs. Assembly language exacerbates the problem because of its complete lack of structure. The resulting programs are hard to read because the reader must reconstruct every higher-level construct from its pieces and each instance of a statement may be slightly different. Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 7 / 26
  • 8. Some introductory notions Comments Comments in assembler files begins with a sharp-sign (#). Everything from the sharp-sign to the end of the line is ignored. Identifiers Identifiers are a sequence of alphanumeric characters, underbars ( ), and dots (.) that do not begin with a number. Opcode for insctructions are reserved words that are not valid identifiers. Labels Labels are declared by putting them at the very beginning of a line followed by a colon (:). Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 8 / 26
  • 9. Some introductory notions Strings Strings are enclosed in double-quotes (quot;). Special characters Special characters in strings follow the C convention: newline → n tab → t quote → quot; Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 9 / 26
  • 10. MIPS Memory Usage Memory Usage Systems based on MIPS processors typically divide memory into three parts. The first part, near the bottom of the address space (starting at 4000000hex ) is the text segment – program’s instructions The second part is the data segment, which is divided in two parts: Static data, starting from 10000000hex -- objects whose size is known and lifetime same as program execution Dynamic Data, allocated by the program as it executes The third part is the stack segment and resides at the top of the virtual address space (starting at address 7fffffffhex ). Like dynamic data, the maximum size of a program’s stack is not known in advance. Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 10 / 26
  • 11. Memory Usage Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 11 / 26
  • 12. CPU Registers SPIM Registers the MIPS/SPIM CPU contains 32 general purpose 32-bit registers (0-31) register n is designed with $n register $0 always contains the value 0 Registers Conventions MIPS has estabilished a set of conventions as to how registers should be used these suggestons are guidelines a program that violated them will not work properly with other sw Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 12 / 26
  • 13. CPU Registers in details $at (1), $k0 (26), $k1 (27): reserved for assembler and OS $a0...$a3 (4-7): to pass the first 4 arguments to routines $v0,$v1 (2,3): to return values from functions $t0...$t9 (8-15,24,25): caller-saved registers used for temporary quantities (not preserved across calls) $s0...$s7 (16-23): callee-saved registers that hold long-lived values (preserved across calls) $sp (29): stack pointer (last location in use) $fp (30): frame pointer $ra (31): return address for a call (written by a jal) $gp (28): global pointer, points to the middle of a 64K block of memory in the heap Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 13 / 26
  • 14. CPU Registers Summary Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 14 / 26
  • 15. SPIM Directives SPIM supports a subset of the assembler directives provided by MIPS assembler. All the directive must be written preceding them with a single dot (.). .align n align the next datum on a 2n byte boudary. .ascii str store the string in memory, not null-terminated .asciiz str store the string in memory, null-terminated .byte b1, ..., bn store the n values in successive bytes of memory .data <addr> the following data items should be stored in the data segment .double d1, ..., dn store the n FP double precision numbers in successive memory locations .extern sym size the datum stored at sym is size byte large and is a global symbol Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 15 / 26
  • 16. SPIM Directives .float f1, ..., fn store the n FP single precision numbers in successive memory locations .globl sym declare that symbol sym is global and can be referenced from other files .half h1, ..., hn store the n 16-bit quantities in successive memory halfwords .kdata <addr> the following data items should be stored in the kernel data segment .ktext <addr> the next items (instructions / words) are put in the kernel text segment .space n allocate n bytes of space in the data segment .text <addr> the next items (instructions / words) are put in the user text segment .word w1, ..., wn store the n 32-bit quantities in successive memory words Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 16 / 26
  • 17. MIPS Instructions Set (1) Three different instructions types: R, I, J Some examples: R : add, addu, and, jr, slt, mfhi, mult I : addi, beq, bne, lw, ori, sb, sw J : j, jal Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 17 / 26
  • 18. MIPS Instructions Set (2) Instructions Families All intructions can be grouped in different families depending on their application domain: Arithmetic and Logical Instructions abs, add, and, div, mult, neg, nor, not, or, sll, xor, sub, ... Constant-Manipulating Instructions lui, li Comparison Instructions slt, seq, sge, sgt, sle, sne, ... Branch Instructions b, beq, bgez, bgtz, bltz, bne, beqz, bge, bgt, ble, blt, ... Jump Instructions j, jal, jr, ... Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 18 / 26
  • 19. MIPS Instructions Set (3) Instructions Families Load Instructions la, lb, lbu, lh, lw, ld, ll, ... Store Instructions sb, sh, sw, swl, sd, sc, ... Data Movement Instructions move, mfhi, mflo, mthi, mtlo, ... Floating-point Instructions Exception and Interrupt Instructions syscall, break, nop, ... Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 19 / 26
  • 20. MIPS Instructions Set (4) Pseudoinstructions The whole instructions set can be divided into two different subsets: Core instructions set Pseudoinstructions set Pseudoinstructions are composed by multiple instructions identified with a name. They are interpreted by MIPS and substituted by the original instructions sequence. Some examples are: Branch Less Then: blt if(R[rs]R[rt]) PC = Label Branch Greater Then: bgt if(R[rs]R[rt]) PC = Label Branch Less Then or Equal: ble if(R[rs] ≤ R[rt]) PC = Label Branch Greater Then or Equal: bge if(R[rs] ≥ R[rt]) PC = Label Load Immediate: li R[rd] = Immediate Move: move R[rd] = R[rs] Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 20 / 26
  • 21. Assembly Program Elements .data # directive foo: .asciiz quot;Hello!nquot; # string null-terminated n: .word 1,4,0 # array res: .word 0 # .word 0 = memory allocation .text # directive .globl main # main becomes global main: lw $a0, foo # $a0,$a1,$t2,... = registers loop: li $a1, 1 # main, loop, store = labels lw $a2, n($t2) # add $a2, $a2, $a1 # beq $a2, $t3, store # j loop # jump to label ‘loop’ store: sw $a2, res # res = identifier ... Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 21 / 26
  • 22. SPIM System Calls SPIM provides a small set of operating-system-like services through the system call (syscall) instruction. To request a service, a program loads the system call code into register $v0 and the arguments into registers $a0...$a3/$f12. System calls that return values put their result in register $v0/$f0. .data str: .asciiz quot;the answer is quot; .text .globl main main: li $v0, 4 # system call code for print_str la $a0, str # address of string to print syscall # print the string li $v0, 1 # system call code for print_int li $a0, 5 # integer to print syscall # print it This code print: the answer is 5 Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 22 / 26
  • 23. SPIM System Call Table Service Syscall Code Args Result $a0 = int $f12 = float print int 1 $f12 = double print float 2 $a0 = string print double 3 $v0 = int print string 4 $f0 = float read int 5 $f0 = double read float 6 $a0 = buffer, $a1 = length read double 7 $a0 = amount $v0 = address read string 8 sbrk 9 exit 10 Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 23 / 26
  • 24. Hello, World! This is a very common simple test program... .data str: .asciiz quot;Hello, World!quot; .text .globl main main: li $v0, 4 # system call code for print_str la $a0, str # address of string to print syscall # print the string Exercise: write an interactive version of “Hello, World!” program Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 24 / 26
  • 25. Hello, name! One interactive version of “Hello, World!” program .data str: .asciiz quot;Hello, quot; iname: .asciiz quot;Name: quot; name: .space 256 .text .globl main main: la $a0, iname # print the prompt li $v0, 4 syscall la $a0, name # read the string li $a1, 256 li $v0, 8 syscall li $v0, 4 # print quot;Hello, quot; la $a0, str syscall li $v0, 4 # print the value inserted la $a0, name syscall li $v0, 10 # exit syscall Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 25 / 26
  • 26. Conclusions Exercises Examples and Exercises Michele Chinosi (Univ. Insubria) SPIM: a MIPS simulator 13.04.2007 26 / 26