Computer Organization and Architecture (AT70.01) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: Freely.

Slides:



Advertisements
Similar presentations
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Advertisements

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
SPIM Tutorial CSE 410 Computer Systems. Introduction SPIM: MIPS simulator –Reads/executes assembly source programs Does not execute binaries Download.
5Z032 Processor Design SPIM, a MIPS simulator Henk Corporaal
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
CS3350B Computer Architecture Winter 2015 Lecture 4
SPIM and MIPS programming
Computer Architecture CSCE 350
MIPS Function Continued
MIPS Assembly Language Programming
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
CS 536 Spring Code generation I Lecture 20.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
SPIM : A MIPS Simulator Archi & Net Lab 이용석
RISC Concepts, MIPS ISA and the Mini–MIPS project
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Computer Organization and Architecture (AT70.01) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: Patterson.
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010 Slides revised 2/2/2014 by Patrick Kelley.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.
Comp Sci vars & expns 1 Ch. 4 Variables and Expressions.
CSE331 W02.1Irwin Fall 2001 PSU Computer Architecture Discussion Lecture # 2 MIPS Programming.
MIPS I/O and Interrupt. .data val1:.float 0.6 val2:.float 0.8 msg_done:.asciiz "done\n".text.globl main main: li.s $f0, mfc1 $a0, $f0 jal calsqrt.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
CWRU EECS 322 March 8, 2000 The SPIM simulator EECS 322: Computer Architecture.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture # 1 SPIM & MIPS Programming. SPIM SPIM is a MIPS32 simulator that reads and executes assembly language program written for SPIM. Platform -Unix,
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Lecture 10: MIPS Simulator Today’s topic –SPIM Simulator Readings –Appendix B 1.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Intro to SPIM Justin Fiore Nathan Parish. Installing SPIM on Windows Download pcspim.zip from the SPIM website:
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
MIPS Lab CMPE 142 – Operating Systems. MIPS Simulator First Time Startup Setting Options.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Lecture 6: Assembly Programs
MIPS Instruction Set Advantages
CS2100 Computer Organisation
Lecture 7: MARS, Computer Arithmetic
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Instructions - Type and Format
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Computer Instructions
COMS 361 Computer Organization
Computer Architecture
MIPS Assembly Language Programming Computer Architecture
9/27: Lecture Topics Memory Data transfer instructions
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Introduction to SPIM Simulator
Presentation transcript:

Computer Organization and Architecture (AT70.01) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: Freely downloadable material adapted and supplemented

Overview of SPIM – the MIPS simulator

Introduction What is SPIM? a simulator that runs assembly programs for MIPS R2000/R3000 RISC computers What does SPIM do? reads MIPS assembly language files and translates to machine language executes the machine language instructions shows contents of registers and memory works as a debugger (supports break-points and single-stepping) provides basic OS-like services, like simple I/O

MIPS & SPIM Resources 1. “Computer Organization & Design: The Hardware/Software Interface”, by Patterson and Hennessy: Chapter 3 and Appendix A Bradley Kjell’s “Programmed Introduction to MIPS Assembly Language” on-line tutorial at A copy is available locally courtesy of Prof. Kjell (see class website) This is an excellent tutorial – make sure to go through it all lesson by lesson!! 3. Other on-line resources 4. Class website 5. Papers in department office – available for you to copy

Learning MIPS & SPIM MIPS assembly is a low-level programming language The best way to learn any programming language is from live code We will get you started by going through a few example programs and explaining the key concepts Further, we have an Examples directory of several simple well- documented assembly programs for you to experiment with We will not try to teach you the syntax line-by-line: pick up what you need from the book and on-line tutorials Tip: Start by copying existing programs and modifying them incrementally making sure you understand the behavior at each step Tip: The best way to understand and remember a construct or keyword is to experiment with it in code, not by reading about it

PCSpim Installation Windows Installation download the file pcspim.zip and save it on your machine. For your convenience a copy is kept locally at the class website unzip the file run the setup.exe program

PCSpim Windows Interface Registers window shows the values of all registers in the MIPS CPU and FPU Text segment window shows assembly instructions & corresponding machine code Data segment window shows the data loaded into the program’s memory and the data of the program’s stack Messages window shows PCSpim messages Separate console window appears for I/O

Using SPIM Loading source file Use File -> Open menu Simulation Simulator -> Settings… : in the Display section check only the first two items Save window positions and General registers in hexadecimal in the Execution section check only Allow pseudo instruction Simulator -> Set Value… : to load PC with address of first instruction enter Address or Register Name as “PC” and enter Value as “0x ” reason: the text area of memory, where programs are stored, starts here Simulator -> Go : run loaded program Click the OK button in the Run Parameters pop-up window if the StartingAddress: value is “0x ” Simulator -> Break : stop execution Simulator -> Clear Registers and Reinitialize : clean-up before new run Important!!

Using SPIM Simulator -> Reload : load file again after editing Simulator -> Single Step or Multiple Step : stepping to debug Simulator -> Breakpoints : set breakpoints Notes: text segment window of SPIM shows assembly and corresponding machine code pseudo-instructions each expand to more than one machine instruction if Load trap file is checked in Simulator -> Settings… then text segment shows additional trap-handling code if Delayed Branches is checked in Simulator -> Settings… then statementx will execute before control jumps to L1 in following code – to avoid insert nop before statementx : nop jal L1 statementx … L1: …

SPIM Example Program: add2numbersProg1.asm ## Program adds 10 and 11.text # text section.globl main # call main by SPIM main: ori $8,$0,0xA # load “10" into register 8 ori $9,$0,0xB # load “11" into register 9 add $10,$8,$9 # add registers 8 and 9, put result # in register 10

MIPS Assembly Code Layout Typical Program Layout.text#code section.globl main#starting point: must be global main: # user program code.data#data section # user program data

MIPS Assembler Directives Top-level Directives:.text indicates that following items are stored in the user text segment, typically instructions.data indicates that following data items are stored in the data segment.globl sym declare that symbol sym is global and can be referenced from other files

SPIM Example Program: add2numbersProg2.asm # Program adds 10 and 20.text # text section.globl main # call main by SPIM main: la $t0, value # load address ‘value’ into $t0 lw $t1, 0($t0) # load word 0(value) into $t1 lw $t2, 4($t0) # load word 4(value) into $t2 add $t3, $t1, $t2 # add two numbers into $t3 sw $t3, 8($t0) # store word $t3 into 8($t0).data # data section value:.word 10, 20, 0 # load data integers. Default data # start address 0x (= value) Parse the machine code for these two instructions!

MIPS Memory Usage as viewed in SPIM reserved 0x x x x7fffeffc 0x7fffffff text segment (instructions) data segment stack segment reserved

MIPS Assembler Directives Common Data Definitions:.word w1, …, wn store n 32-bit quantities in successive memory words.half h1, …, hn store n 16-bit quantities in successive memory halfwords.byte b1, …, bn store n 8-bit quantities in successive memory bytes.ascii str store the string in memory but do not null-terminate it strings are represented in double-quotes “str” special characters, eg. \n, \t, follow C convention.asciiz str store the string in memory and null-terminate it

MIPS Assembler Directives Common Data Definitions:.float f1, …, fn store n floating point single precision numbers in successive memory locations.double d1, …, dn store n floating point double precision numbers in successive memory locations.space n reserves n successive bytes of space.align n align the next datum on a 2 n byte boundary. For example,.align 2 aligns next value on a word boundary..align 0 turns off automatic alignment of.half,.word, etc. till next.data directive

SPIM Example Program: storeWords.asm ## Program shows memory storage and access (big vs. little endian).data here:.word 0xabc89725, 100.byte 0, 1, 2, 3.asciiz "Sample text" there:.space 6.byte 85.align 2.byte 32.text.globl main main: la $t0, here lbu $t1, 0($t0) lbu $t2, 1($t0) lw $t3, 0($t0) sw $t3, 36($t0) sb $t3, 41($t0) Word placement in memory is exactly same in big or little endian – a copy is placed. Byte placement in memory depends on if it is big or little endian. In big-endian bytes in a Word are counted from the byte 0 at the left (most significant) to byte 3 at the right (least significant); in little-endian it is the other way around. Word access (lw, sw) is exactly same in big or little endian – it is a copy from register to a memory word or vice versa. Byte access depends on if it is big or little endian, because bytes are counted 0 to 3 from left to right in big-endian and counted 0 to 3 from right to left in little-endian. SPIM’s memory storage depends on the underlying machine: Intel 80x86 processors are little-endian!

SPIM Example Program: swap2memoryWords.asm ## Program to swap two memory words.data # load data.word 7.word 3.text.globl main main: lui $s0, 0x1001 # load data area start address 0x lw $s1, 0($s0) lw $s2, 4($s0) sw $s2, 0($s0) sw $s1, 4($s0)

SPIM Example Program: branchJump.asm ## Nonsense program to show address calculations for ## branch and jump instructions.text # text section.globl main# call main by SPIM # Nonsense code # Load in SPIM to see the address calculations main: j label add $0, $0, $0 beq $8, $9, label add $0, $0, $0 label: add $0, $0, $0

SPIM Example Program: procCallsProg2.asm.text.globlmain main: la$a0, array addi$a1, $0, 0 addi$sp, $sp, -4 sw$ra, 0($sp) jalswap lw$ra, 0($sp) addi$sp, $sp, 4 jr$ra # equivalent C code: #swap(int v[], int k) #{ #int temp; #temp = v[k]; #v[k] = v[k+1]; #v[k+1] = temp; #} # swap contents of elements $a1 # and $a1 + 1 of the array that # starts at $a0 swap:add$t1, $a1, $a1 add$t1, $t1, $t1 add$t1, $a0, $t1 lw$t0, 0($t1) lw$t2, 4($t1) sw$t2, 0($t1) sw$t0, 4($t1) jr$ra.data array:.word 5, 4, 3, 2, 1 ## Procedure call to swap two array words save return address $ra in stack jump and link to swap restore return address jump to $ra load para- meters for swap

0zero constant 0 1at reserved for assembler 2v0results from callee 3v1returned to caller 4 a0arguments to callee 5a1 from caller: caller saves 6a2 7a3 8t0temporary: caller saves...(callee can clobber) 15t7 MIPS: Software Conventions for Registers 16s0callee saves... (caller can clobber) 23s7 24t8 temporary (cont’d) 25t9 26k0reserved for OS kernel 27k1 28gppointer to global area 29spstack pointer 30fpframe pointer 31rareturn Address (HW): caller saves

SPIM System Calls System Calls (syscall) OS-like services Method load system call code into register $v0 (see following table for codes) load arguments into registers $a0, …, $a3 call system with SPIM instruction syscall after call return value is in register $v0, or $f0 for floating point results

SPIM System Call Codes ServiceCode (put in $v0)ArgumentsResult print_int1$a0=integer print_float2$f12=float print_double3$f12=double print_string4$a0=addr. of string read_int5int in $v0 read_float6float in $f0 read_double7double in $f0 read_string8$a0=buffer, $a1=length sbrk9$a0=amountaddr in $v0 exit10

SPIM Example Program: systemCalls.asm ## Enter two integers in ## console window ## Sum is displayed.text.globl main main: la $t0, value li $v0, 5 syscall sw $v0, 0($t0) li $v0, 5 syscall sw $v0, 4($t0) lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0) li $v0, 4 la $a0, msg1 syscall li $v0, 1 move $a0, $t3 syscall li $v0, 10 syscall.data value:.word 0, 0, 0 msg1:.asciiz “Sum = " system call code for read_int result returned by call argument to print_string call system call code for print_string system call code for print_int argument to print_int call system call code for exit

More SPIM Example Programs In the Examples directory you will find 18 simple well-documented MIPS assembly programs. Run the code in the order below of increasing complexity. 1, 2, 3, 4, 5, 6, and 15 have already been discussed in these slides. 1. add2numbersProg1 2. add2numbersProg2 3. storeWords 4. swap2memoryWords 5. branchJump 6. systemCalls 7. overflow 8. averageOfBytes 9. printLoop 10. sumOfSquaresProg1 11. sumOfSquaresProg2 12. sumOfSquaresProg3 13. procCallsProg1 14. procCallsProg1Modified 15. procCallsProg2 16. addFirst factorialNonRecursive 18. factorialRecursive

Conclusion & More The code presented so far should get you started in writing your own MIPS assembly Remember the only way to master the MIPS assembly language – in fact, any computer language – is to write lots and lots of code For anyone aspiring to understand modern computer architecture it is extremely important to master MIPS assembly as all modern computers (since the mid-80’s) have been inspired by, if not based fully or partly on the MIPS instruction set architecture To help those with high-level programming language (e.g., C) experience, in the remaining slides we show how to synthesize various high-level constructs in assembly…

Synthesizing Control Statements (if, if-else) if ( condition ) { statements } beqz $t0, if_end_label # MIPS code for the # if-statements. if_end_label: if ( condition ) { if-statements } else { else-statements } beqz $t0, if_else_label # MIPS code for the # if-statements. j if_end_label if_else_label: # MIPS code for the # else-statements if_end_label:

Synthesizing Control Statements (while) while ( condition ) { statements } while_start_label: # MIPS code for the condition expression beqz $t0, while_end_label # MIPS code for the while-statements. j while_start_label while_end_label:

Synthesizing Control Statements (do-while) do { statements } while ( condition ); do_start_label: # MIPS code for the do-statements. do_cond_label: # MIPS code for the condition expr: beqz $t0, do_end_label j do_start_label do_end_label:

Synthesizing Control Statements (for) for ( init ; condition ; incr ) { statements } # MIPS code for the init expression. for_start_label: # MIPS code for the condition expression beqz $t0, for_end_label # MIPS code for the for-statements. for_incr_label: # MIPS code for the incr expression. j for_start_label for_end_label:

Synthesizing Control Statements (switch) switch ( expr ) { case const1: statement1 case const2: statement2... case constN: statementN default: default- statement } # MIPS code to compute expr. # Assume that this leaves the # value in $t0 beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2... beq $t0, constN, switch_label_N # If there is a default, then add b switch_default # Otherwise, add following lineinstead: b switch_end_label

Synthesizing Control Statements (switch), cont. switch_label_1: # MIPS code to compute statement1. switch_label_2: # MIPS code to compute statement2.... switch_label_N: # MIPS code to compute statementN. # If there's a default: switch_default: # MIPS code to compute default-statement. switch_end_label:

Array Address Calculation Address calculation in assembler: address of A [n] = address of A [0] + (n* sizeof (element of A)) # $t0 = address of start of A. # $t1 = n. mul $t2, $t1, 4 # compute offset from # the start of the array # assuming sizeof(element)=4 add $t2, $t0, $t2 # add the offset to the # address of A [0]. # now $t2 = &A [n]. sw $t3, ($t2) # A [n] = whatever is in $t3. lw $t3, ($t2) # $t3 = A [n].

Short-Cut Expression Evaluation (and) cond1 && cond2 # MIPS code to compute cond1. # Assume that this leaves the value in $t0. # If $t0 is zero, we're finished # (and the result is FALSE). beqz $t0, and_end # MIPS code to compute cond2. # Assume that this leaves the value in $t0. and_end:

Short-Cut Expression Evaluation (or) cond1 || cond2 # MIPS code to compute cond1. # Assume that this leaves the value in $t0. # If $t0 is not zero, we're finished # (and the result is TRUE). bnez $t0, or_end # MIPS code to compute cond2. # Assume that this leaves the value in $t0. or_end: